上传图像以使用keras进行预测时出现错误消息

nc1teljy  于 2023-04-30  发布在  其他
关注(0)|答案(2)|浏览(89)

上传图片预测时出错

ValueError:block1_conv1层的输入0与图层不兼容:输入形状的预期轴-1具有值3,但接收到的输入具有形状[None,224,224,4]

代码处理/图像预处理

def model_predict(img, model):
    img = img.resize((224, 224))

    # Preprocessing the image
    x = image.img_to_array(img)
    x = np.true_divide(x, 255)
    x = np.expand_dims(x, axis=0)

    # Be careful how your trained model deals with the input
    # otherwise, it won't make correct prediction!
    x = preprocess_input(x, mode='tf')

    preds = model.predict(x)
    return preds

*培训班产出

flow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): Host, Default Version
Compiling model...
Model has 14716227 parameters
Model: "model"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         [(None, 224, 224, 3)]     0         
_________________________________________________________________
block1_conv1 (Conv2D)        (None, 224, 224, 64)      1792      
_________________________________________________________________
block1_conv2 (Conv2D)        (None, 224, 224, 64)      36928     
_________________________________________________________________
block1_pool (MaxPooling2D)   (None, 112, 112, 64)      0         
_________________________________________________________________
block2_conv1 (Conv2D)        (None, 112, 112, 128)     73856     
_________________________________________________________________
block2_conv2 (Conv2D)        (None, 112, 112, 128)     147584    
_________________________________________________________________
block2_pool (MaxPooling2D)   (None, 56, 56, 128)       0         
_________________________________________________________________
block3_conv1 (Conv2D)        (None, 56, 56, 256)       295168    
_________________________________________________________________
block3_conv2 (Conv2D)        (None, 56, 56, 256)       590080    
_________________________________________________________________
block3_conv3 (Conv2D)        (None, 56, 56, 256)       590080    
_________________________________________________________________
block3_pool (MaxPooling2D)   (None, 28, 28, 256)       0         
_________________________________________________________________
block4_conv1 (Conv2D)        (None, 28, 28, 512)       1180160   
_________________________________________________________________
block4_conv2 (Conv2D)        (None, 28, 28, 512)       2359808   
_________________________________________________________________
block4_conv3 (Conv2D)        (None, 28, 28, 512)       2359808   
_________________________________________________________________
block4_pool (MaxPooling2D)   (None, 14, 14, 512)       0         
_________________________________________________________________
block5_conv1 (Conv2D)        (None, 14, 14, 512)       2359808   
_________________________________________________________________
block5_conv2 (Conv2D)        (None, 14, 14, 512)       2359808   
_________________________________________________________________
block5_conv3 (Conv2D)        (None, 14, 14, 512)       2359808   
_________________________________________________________________
block5_pool (MaxPooling2D)   (None, 7, 7, 512)         0         
_________________________________________________________________
tf_op_layer_Mean (TensorFlow [(None, 512)]             0         
_________________________________________________________________
dropout (Dropout)            (None, 512)               0         
_________________________________________________________________
dense (Dense)                (None, 3)                 1539      
======================================
djmepvbi

djmepvbi1#

如果您想使用批处理训练和测试部分,您一定要检查ImageDataGenerator。它不仅可以帮助您创建批次,还可以帮助您进行图像增强(使模型健壮的艺术)。它提供了一个参数rescalingpreprocess_function,您可以轻松地将其设置为rescale=1/255., preprocessing_function=preprocess_input,
您的代码在通道部分有问题。如果您使用的是channels_last,那么它已经在(batch,width,height,3) 3通道数据上进行了训练,当您试图通过数据进行测试时,它会显示错误。
在这种情况下,应删除Alpha通道。请检查ImageData Generator。在所有的flow_from_dataframeflow_from_directoryflow中有一个参数作为默认的color_mode='rgb
否则,您可以将图像打开为

from PIL import Image
img = Image.open(img_path).convert('RGB') # get RGB PNG image
img = np.array(img) # Convert to numpy array
img = np.expand_dims(x, axis=0) # add an extra dimension for batch

然后您可以应用preprocess_function

img = preprocess_input(img, mode='tf')

preds = model.predict(img)
return preds

注意:如果您使用单个图像,则应使用numpyreshapeexpand_dims方法来创建额外的批处理轴,因为Keras总是期望一个批处理。

1wnzp6jl

1wnzp6jl2#

因为你试图用4通道图像来填充模型。请在转换图像到阵列后采取第一个3通道。

Preprocessing the image
x = image.img_to_array(img)
x = img[:, :, :3]
x = np.true_divide(x, 255)
x = np.expand_dims(x, axis=0)

相关问题