我有一个训练好的模型,该模型已被训练用于识别不同的文档,我的数据集来自 http://www.cs.cmu.edu/~aharley/rvl-cdip/。
以下是我构建模型的方式
import numpy as npfrom keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_imgimport picklefrom keras.optimizers import SGDfrom keras.models import Sequential, save_modelfrom keras.layers import Dense, Dropout, Flatten, Activationfrom keras.layers.convolutional import Conv2D, MaxPooling2D# Set image informationchannels = 1height = 1000width = 754model = Sequential()# Add a Conv2D layer with 32 nodes to the modelmodel.add(Conv2D(32, (3, 3), input_shape=(1000, 754, 3)))# Add the reLU activation function to the modelmodel.add(Activation('relu'))model.add(MaxPooling2D(pool_size=(2, 2)))model.add(Conv2D(32, (3, 3)))model.add(Activation('relu'))model.add(MaxPooling2D(pool_size=(2, 2)))model.add(Conv2D(32, (3, 3)))model.add(Activation('relu'))model.add(MaxPooling2D(pool_size=(2, 2)))model.add(Flatten()) # this converts our 3D feature maps to 1D feature vectorsmodel.add(Dense(64))model.add(Activation('relu'))model.add(Dropout(0.5))model.add(Dense(1))model.add(Activation('relu'))model.compile(loss='categorical_crossentropy', # sparse_categorical_crossentropy # Adam(lr=.0001) SGD variation with learning rate optimizer='adam', metrics=['accuracy'])# Image data generator to import iamges from data folderdatagen = ImageDataGenerator()# Flowing images from folders sorting by labels, and generates batches of imagestrain_it = datagen.flow_from_directory( "data/train/", batch_size=16, target_size=(height, width), shuffle=True, class_mode='categorical')test_it = datagen.flow_from_directory( "data/test/", batch_size=16, target_size=(height, width), shuffle=True, class_mode='categorical')val_it = datagen.flow_from_directory( "data/validate/", batch_size=16, target_size=(height, width), shuffle=True, class_mode='categorical')history = model.fit( train_it, epochs=2, batch_size=16, validation_data=val_it, shuffle=True, steps_per_epoch=2000 // 16, validation_steps=800 // 16)save_model(model, "./ComplexDocumentModel")model.save("my_model", save_format='h5')
如最后一行所示,我将模型保存为h5格式。
现在我尝试使用该训练好的模型来预测单个图像,以查看它属于哪个类别,下面是脚本。
from keras.models import load_modelimport cv2import numpy as npimport kerasfrom keras.preprocessing import imagemodel = load_model('my_model')# First trydef prepare(file): img_array = cv2.imread(file, cv2.IMREAD_GRAYSCALE) new_array = cv2.resize(img_array, (1000, 754)) return new_array.reshape(3, 1000, 754, 1)# Second tryimg = image.load_img( "/home/user1/Desktop/Office/image-process/test/0000113760.tif")img = image.img_to_array(img)img = np.expand_dims(img, axis=-1)prediction = model.predict( [prepare("/home/user1/Desktop/Office/image-process/test/0000113760.tif")])print(prediction)
我尝试了两种方式来预测图像,但都出现了错误
ValueError: Input 0 of layer sequential is incompatible with the layer: expected axis -1 of input shape to have value 3 but received input with shape (None, 762, 3, 1)
我还尝试使用PIL打开图像并转换为NumPy数组,这是我在谷歌上找到的方法。不幸的是,我找到的其他答案、博客或视频教程都没有帮助我。
回答:
您正在尝试将灰度图像输入到一个期望具有3个通道的网络中。您可以将最后一个通道堆叠3次以获得兼容的形状,但预测结果可能会较差:
def prepare(file): img_array = cv2.imread(file, cv2.IMREAD_GRAYSCALE) new_array = cv2.resize(img_array, (1000, 754)) # shape is (1000,754) # converting to RGB array_color = cv2.cvtColor(new_array, cv2.COLOR_GRAY2RGB) # shape is (1000,754,3) array_with_batch_dim = np.expand_dims(array_color, axis=0) # shape is (1,1000,754,3) return array_with_batch_dim
另一种解决方案是不在读取图像时将其转换为灰度,省略标志 cv2.IMREAD_GRAYSCALE
。OpenCV的默认行为是加载具有3个通道的图像。
def prepare(file): img_array = cv2.imread(file) new_array = cv2.resize(img_array, (1000, 754)) # shape is (1000,754, 3) # converting to RGB array_with_batch_dim = np.expand_dims(new_array, axis=0) # shape is (1,1000,754,3) return array_with_batch_dim
注意: 根据您的预处理,您可能需要在将图像输入网络之前通过除以255将其归一化到0到1之间。