如何预处理并将数据输入到Keras模型?

我有一个包含两列的数据库,分别是路径(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])

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注