验证准确性低且训练准确性高-keras imagedatagenerator flow_from_directory类别分类 [英] Low validation accuracy with good training accuracy - keras imagedatagenerator flow_from_directory categorical classification

查看:121
本文介绍了验证准确性低且训练准确性高-keras imagedatagenerator flow_from_directory类别分类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Keras和ResNet50将Kaggle 10k狗图像分类为120个品种.由于Kaggle(14gb ram)的内存限制,我必须使用ImageDataGenerator,该ImageDataGenerator可以将图像实时馈送到模型中,并且还可以实时进行数据扩充.

I am trying to classify the Kaggle 10k dog images to 120 breeds using Keras and ResNet50. Due to memory constraints at Kaggle (14gb ram) - I have to use the ImageDataGenerator that feeds the images to the model and also allows data augmentation - in real time.

基本的卷积ResNet50模型:

The base convoluted ResNet50 model:

conv_base = ResNet50(weights='imagenet', include_top=False, input_shape=(224,224, 3))

我的模特:

model = models.Sequential()
model.add(conv_base)
model.add(layers.Flatten())
model.add(layers.Dense(256, activation='relu'))
model.add(layers.Dropout(0.5))
model.add(layers.Dense(120, activation='softmax'))

确保只有我最后添加的图层是可训练的-因此,在训练过程和编译模型中不会修改ResNet50原始权重:

Making sure that only my last added layers are trainable - so the ResNet50 original weights will not be modified in the training process and compiling model:

conv_base.trainable = False
model.compile(optimizer=optimizers.Adam(), loss='categorical_crossentropy',metrics=['accuracy'])

Num trainable weights BEFORE freezing the conv base: 216
Num trainable weights AFTER freezing the conv base: 4

以及最终的模型摘要:

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
resnet50 (Model)             (None, 1, 1, 2048)        23587712  
_________________________________________________________________
flatten_1 (Flatten)          (None, 2048)              0         
_________________________________________________________________
dense_1 (Dense)              (None, 256)               524544    
_________________________________________________________________
dropout_1 (Dropout)          (None, 256)               0         
_________________________________________________________________
dense_2 (Dense)              (None, 120)               30840     
=================================================================
Total params: 24,143,096
Trainable params: 555,384
Non-trainable params: 23,587,712
_________________________________________________________________

train和validation目录每个都有120个子目录-每个犬种一个.在这些文件夹中是狗的图像. Keras应该使用这些目录来为每个图像获取正确的标签:因此Keras会自动对"beagle"子目录中的图像进行分类-无需进行一次热编码或类似的操作.

The train and validation directories have each, 120 sub directories - one for each dog breed. In these folders are images of dogs. Keras is supposed to use these directories to get the correct label for each image: so an image from a "beagle" sub dir is classified automatically by Keras - no need for one-hot-encoding or anything like that.

train_dir = '../input/dogs-separated/train_dir/train_dir/'
validation_dir = '../input/dogs-separated/validation_dir/validation_dir/'

train_datagen = ImageDataGenerator(rescale=1./255)
test_datagen = ImageDataGenerator(rescale=1./255)

train_generator = train_datagen.flow_from_directory(
train_dir,target_size=(224, 224),batch_size=20, shuffle=True)
validation_generator = test_datagen.flow_from_directory(
validation_dir,target_size=(224, 224),batch_size=20, shuffle=True)

Found 8185 images belonging to 120 classes.
Found 2037 images belonging to 120 classes.

为了确保这些类正确且顺序正确,我比较了它们的train_generator.class_indices和validation_generator.class_indices-它们是相同的. 训练模型:

Just to make sure these classes are right and in the right order I've compared their train_generator.class_indices and validation_generator.class_indices - and they are the same. Train the model:

history = model.fit_generator(train_generator,
steps_per_epoch=8185 // 20,epochs=10,
validation_data=validation_generator,
validation_steps=2037 // 20)

在下面的图表中请注意,虽然训练精度按预期提高了-验证很快就设置在0.008左右,即1/120 ... RANDOM预测?!

Note in the charts below, that while training accuracy improves as expected - the validation sets quickly around 0.008 which is 1/120...RANDOM prediction ?!

我还用验证替换了火车,反之亦然-并遇到了相同的问题:训练精度提高了,而验证精度却保持在大约0.008 = 1/120.

I've also replaced the train with validation and vice versa - and got the same issue: training accuracy improving while the validation accuracy got stuck on approx 0.008 = 1/120.

任何想法都会受到赞赏.

Any thoughts would be appreciated.

推荐答案

我使用批处理大小进行了测试,发现batch_size = 120(火车中目录的数量以及有效的目录数量)目录)以消除上述问题.现在,我可以愉快地使用数据扩充技术,而不会在内存问题上使我的Kaggle内核崩溃.还是我不知道...

I've played with the batch size and found batch_size = 120 (the number of directories in the train as well as the valid directories) to eliminate the above issue. Now I can happily employ data augmentation techniques without crashing my Kaggle kernel on memory issues. Still I wonder...

Keras ImageDataGenerator如何在分类模式下(深度或广度)从目录中采样图像?

如果深度较深-批量为20个时,它将进入第一个目录,例如100张照片(五次),然后移至下一个目录,以20批为单位,移至下一个目录... 还是广度?

If depth wise - than with a batch size of 20 it will go thru the FIRST directory of say 100 photos (five times), and then move to the next directory, do it in batches of 20, move to the next dir... Or is it breadthwise ?

从宽度上看-最初的20张照片将是前20个目录中的每一张的照片,然后是接下来20个目录中的每张照片? 在与 flow_from _directory 和fit_generator一起使用时,我在文档中找不到Keras ImageDataGenerator如何与批处理一起使用.

Breadthwise - the initial batch of 20 will be ONE photo from each of the first 20 directories and then the next 20 directories ? I couldn't find in the documentation how is Keras ImageDataGenerator working with batches when used with flow_from _directory and fit_generator.

这篇关于验证准确性低且训练准确性高-keras imagedatagenerator flow_from_directory类别分类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆