[英]Loss always go to nan when transfer learning by ResNet50 in keras
I am using transfer learning to train a image classifier by ResNet50
model in Keras
and by loading the pre-trained weights, but the loss
go to nan
initially and instantly and the acc
stay in random level. 我使用的转移学习培训由图像分类ResNet50
模型Keras
并通过加载预训练的权重,但loss
去nan
最初瞬间和acc
留在随机水平。
Actually, I don't know what is wrong because I have used this model to train a classifier successfully, though it was not with high acc
but it works well. 实际上,我不知道出什么问题了,因为我使用此模型成功地训练了分类器,尽管它不是具有较高的acc
但效果很好。 This time it failed. 这次失败了。
I tuned the lr
but nothing happened. 我调了lr
但是什么也没发生。 Someone said the data may have problems, so I changed the data and only to find with different images the same model will show different results(that is to say, some data/images works well and another data/images will results loss:nan
instantly). 有人说数据可能有问题,所以我更改了数据,结果发现同一模型使用不同的图像会显示不同的结果(也就是说,某些数据/图像效果很好,而另一数据/图像会导致loss:nan
立即loss:nan
)。 How could that be? 怎么可能 I am really confused and can't figure out what is wrong with my images. 我真的很困惑,无法弄清楚我的图像出了什么问题。
Dataset: 8 classes and each class contains about 300 images. 数据集:8个类别,每个类别包含约300张图像。
Here is the code for all: 这是所有代码:
import keras
import h5py
import numpy as np
import matplotlib.pyplot as plt
from keras.applications import ResNet50
from keras.models import Sequential
from keras.layers import Dense, Flatten, GlobalAveragePooling2D
from keras.applications.resnet50 import preprocess_input
from keras.preprocessing.image import ImageDataGenerator
data_generator = ImageDataGenerator(preprocessing_function= preprocess_input,
rescale = 1./255)
train_generator = data_generator.flow_from_directory("image/train",
target_size = (100, 100),
batch_size = 32,
class_mode = "categorical")
dev_generator = data_generator.flow_from_directory("image/dev",
target_size = (100, 100),
batch_size = 32,
class_mode = "categorical")
num_classes = 8
model = Sequential()
model.add(ResNet50(include_top = False, pooling = "avg", weights= "resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5"))
model.add(Dense(num_classes, activation = "softmax"))
model.layers[0].trainable = False
model.compile(optimizer= "adam", loss= "categorical_crossentropy", metrics=["accuracy"])
model.fit_generator(train_generator, steps_per_epoch= 1, epochs = 1)
and the running output is: 运行的输出是:
Epoch 1/1
1/1 [==============================] - 6s 6s/step - loss: nan - acc: 0.0938
First Correct “image/dev”
to "image/dev"
首先将“image/dev”
更正为"image/dev"
I think your error lies on this line: 我认为您的错误就在于此行:
data_generator = ImageDataGenerator(preprocessing_function= preprocess_input, rescale = 1./255)
you double scale your data when you are using both the preprocess_input
function and rescale = 1./255
. 当您同时使用preprocess_input
函数和rescale = 1./255
时,您可以对数据进行两倍rescale = 1./255
。 Try Removing rescaling ... 尝试移除重新缩放...
data_generator = ImageDataGenerator(preprocessing_function= preprocess_input)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.