我有一些按文件夹排列的训练图像,一些验证图像和一些测试图像。我正在使用图像生成器,因为没有。的图像是不够的。我正在使用此代码:
height=150
width=150
channels=3
batch_size=32
seed=1337
# Training generator
train_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory(train_dir,target_size=(height,width),batch_size=batch_size,seed=seed,class_mode='categorical')
# Test generator
test_datagen = ImageDataGenerator(rescale=1./255)
test_generator = test_datagen.flow_from_directory(test_dir,target_size=(height,width),batch_size=batch_size,seed=seed,class_mode='categorical')
并获得输出:
找到属于 5 个类别的 723 个图像。
找到属于 5 个类别的 144 个图像。
这是我的模型架构:
model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=(150, 150, 3)))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(32, (3, 3)))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, (3, 3)))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
# the model so far outputs 3D feature maps (height, width, features)
model.add(Flatten()) # this converts our 3D feature maps to 1D feature vectors
model.add(Dense(64))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(5))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])
这是代码.fit_generator()
:
history = model.fit_generator(
train_generator,
steps_per_epoch=2000 // 64,
epochs=5,
validation_data=test_generator,
validation_steps=800 // 64)
我收到accuracy
的70%
经过5 epochs
,但不幸的是val_acc
和val_loss
遗迹0.00000e+00
,我不太能弄清楚。我还有一个文件夹,里面有 20 张图片,需要预测。我如何.predict()
对它们使用函数?我没有任何.csv
给出标签的文件。单独的文件夹中只给出了训练图像,其名称基本上是图像的类别。
首先修复:
steps_per_epoch = 2000 // 64 validation_steps = 800 // 64
至:
steps_per_epoch = 723 / batch_size validation_steps = 144 / batch_size
然而,这不是这里的问题。我看不出你的代码有问题。我什至在我的数据库上运行它,它运行良好。正如你在这里说,检查的文件夹test_dir
,并train_dir
具有相同的名称。
关于predict_gen
,阅读Keras的文档。输出是每个验证图像的向量。如果你想要字符串标签,你可以使用生成器的类列表。所以像:
pred_Y = np.argmax(model.predict_generator(valid_gen),axis=1) predicted_labels = [valid_gen.classes[pred_y] for pred_y in pred_Y ]
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句