【发布时间】:2020-02-19 23:51:04
【问题描述】:
我有一个包含两列路径和类的数据集。我想用它微调 VGGface。
dataset.head(5):
path class
0 /f3_224x224.jpg red
1 /bc_224x224.jpg orange
2 /1c_224x224.jpg brown
3 /4b_224x224.jpg red
4 /0c_224x224.jpg yellow
我想使用这些路径来预处理图像并提供给 keras。我的预处理功能如下:
from keras.preprocessing.image import img_to_array, load_img
def 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_split
path = 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 = 4
hidden_dim = 512
vgg_model = VGGFace(include_top=False, input_shape=(224, 224, 3))
last_layer = vgg_model.get_layer('pool5').output
x = 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)。
【问题讨论】:
标签: python-3.x tensorflow machine-learning keras computer-vision