我有一个包含两列的数据库,分别是路径(path)和类别(class)。我想用它来微调VGGface模型。
dataset.head(5): path class0 /f3_224x224.jpg red1 /bc_224x224.jpg orange2 /1c_224x224.jpg brown3 /4b_224x224.jpg red4 /0c_224x224.jpg yellow
我想使用这些路径来预处理图像并输入到Keras模型中。我的预处理函数如下:
from keras.preprocessing.image import img_to_array, load_imgdef prep_image(photo): img = image.load_img(path + photo, target_size=(224, 224)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = utils.preprocess_input(x, version=1) return x
我用以下代码准备我的数据集:
from sklearn.model_selection import train_test_splitpath = list(dataset.columns.values)path.remove('class')X = dataset[path]y = dataset['class']X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)
我用以下代码训练我的模型:
nb_class = 4hidden_dim = 512vgg_model = VGGFace(include_top=False, input_shape=(224, 224, 3))last_layer = vgg_model.get_layer('pool5').outputx = Flatten(name='flatten')(last_layer)x = Dense(hidden_dim, activation='relu', name='fc6')(x)x = Dense(hidden_dim, activation='relu', name='fc7')(x)out = Dense(nb_class, activation='softmax', name='fc8')(x)custom_vgg_model = Model(vgg_model.input, out)custom_vgg_model.compile( optimizer="adam", loss="categorical_crossentropy" )custom_vgg_model.fit(X_train, y_train, epochs=50, batch_size=16)test_loss, test_acc = model.evaluate(X_test, y_test)
然而,我遇到了值错误,因为我无法弄清楚如何预处理图像并将数组输入模型。我该如何转换X_train/test数据框架中的路径,并用prep_image函数的输出替换它们?
ValueError: Error when checking input: expected input_2 to have 4 dimensions, but got array with shape (50297, 1)
所以形状应该是(50297, 224, 224, 3)。
回答:
X_train和X_test基本上只是路径名称。您在数据准备步骤中只需修改代码,添加最后两行即可。
from sklearn.model_selection import train_test_splitpath = list(dataset.columns.values)path.remove('class')X = dataset[path]y = dataset['class']X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)X_train = np.array([prep_image(path)[0] for path in X_train])X_test = np.array([prep_image(path)[0] for path in X_test])