简体   繁体   English

Keras model.fit_generator()给出0.0%的验证精度

[英]Keras model.fit_generator() gives 0.0% validation accuracy

I have a few training images arranged folder wise, few validation images and a few test images. 我有一些按文件夹排列的训练图像,一些验证图像和一些测试图像。 I'm using image generator because the no. 我正在使用图片生成器,因为没有。 of images are not sufficient. 的图像数量不足。 I'm using this code: 我正在使用此代码:

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')

and getting an output: 并获得输出:

Found 723 images belonging to 5 classes. 找到属于5个类别的723张图像。

Found 144 images belonging to 5 classes. 找到144个属于5个类别的图像。

And this is my model architecture: 这是我的模型架构:

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'])

This is the code for .fit_generator() : 这是.fit_generator()的代码:

history = model.fit_generator(
        train_generator,
        steps_per_epoch=2000 // 64,
        epochs=5,
        validation_data=test_generator,
        validation_steps=800 // 64)

I'm getting an accuracy of 70% after 5 epochs , but unfortunately val_acc and val_loss remains 0.00000e+00 and I'm not quite able to figure it out. 5 epochs ,我的accuracy达到了70% ,但不幸的是val_accval_loss仍然为0.00000e+00 ,我不太清楚。 Also I have a folder with 20 images, to be predicted. 另外,我有一个包含20张图像的文件夹,可以预测。 How do I use .predict() function on them? 如何在它们上使用.predict()函数? I don't have any .csv file where labels are given. 我没有提供标签的任何.csv文件。 Only the training images are given in separate folders, whose name are basically the class of the images. 仅训练图像在单独的文件夹中给出,其名称基本上是图像的类别。

First of all fix: 首先修复:

steps_per_epoch = 2000 // 64 validation_steps = 800 // 64

to: 至:

steps_per_epoch = 723 / batch_size validation_steps = 144 / batch_size

However, this isn't the issue here. 但是,这不是这里的问题。 I don't see a problem in your code. 我看不到您的代码中的问题。 I even ran it on my database and it worked fine. 我什至在我的数据库上运行它,并且运行良好。 As you've been told here, check that the folders in test_dir and train_dir have the same names. 正如您在此处train_dir ,请检查test_dirtrain_dir中的文件夹名称是否相同。

About predict_gen , read the documentation of Keras. 关于predict_gen ,请阅读predict_gen文档 The output is a vector for each validation image. 输出是每个验证图像的向量。 If you want the string labels you can use the classes list of the generator. 如果需要字符串标签,可以使用生成器的类列表。 So something like: 所以像这样:

pred_Y = np.argmax(model.predict_generator(valid_gen),axis=1) predicted_labels = [valid_gen.classes[pred_y] for pred_y in pred_Y ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM