Nan因分类问题而失去(转移学习)

时间:2020-04-02 08:24:09

标签: nan loss transfer-learning cnn

我是CNN的新手。我试图在CIFAR10数据集上训练模型。我使用了转移学习的概念,其中我的基本模型是Inception-V3,输出层有10个节点。因此,我使用softmax来执行分类决策。我的代码就是这个,与此有关的问题是它给了我很多损失。我无法确定与代码有关的问题。我考虑过标准化。但是ImageDataGenerator()函数已经在执行所需的任务。任何帮助将非常感激。

from keras.applications.inception_v3 import InceptionV3
from keras.preprocessing import image
from keras.models import Model
from keras.layers import Dense, GlobalAveragePooling2D
from keras import backend as K
from keras.datasets import cifar10
from keras.utils import to_categorical
from keras.preprocessing.image import ImageDataGenerator

# create the base pre-trained model
base_model = InceptionV3(weights='imagenet', include_top=False)
(x_train, y_train), (x_test, y_test) = cifar10.load_data()

y_train = to_categorical(y_train)
y_test=to_categorical(y_test)

# add a global spatial average pooling layer
x = base_model.output
x = GlobalAveragePooling2D()(x)
# let's add a fully-connected layer
x = Dense(1024, activation='relu')(x)
# and a logistic layer -- let's say we have 200 classes
predictions = Dense(10, activation='softmax')(x)

# this is the model we will train
model = Model(inputs=base_model.input, outputs=predictions)

# first: train only the top layers (which were randomly initialized)
# i.e. freeze all convolutional InceptionV3 layers
for layer in base_model.layers:
    layer.trainable = False

# compile the model (should be done *after* setting layers to non-trainable)
model.compile(optimizer='rmsprop', loss='categorical_crossentropy')

datagen=ImageDataGenerator(featurewise_center=True,
                           featurewise_std_normalization=True,
                           rotation_range=20,
                           width_shift_range=0.2,
                           height_shift_range=0.2,
                           horizontal_flip=True)
datagen.fit(x_train)
# train the model on the new data for a few epochs
model.fit_generator(datagen.flow(x_train,y_train,batch_size=128), steps_per_epoch=len(x_train)/128,epochs=10)

# at this point, the top layers are well trained and we can start fine-tuning
# convolutional layers from inception V3. We will freeze the bottom N layers
# and train the remaining top layers.

# let's visualize layer names and layer indices to see how many layers
# we should freeze:
for i, layer in enumerate(base_model.layers):
   print(i, layer.name)

# we chose to train the top 2 inception blocks, i.e. we will freeze
# the first 249 layers and unfreeze the rest:
for layer in model.layers[:249]:
   layer.trainable = False
for layer in model.layers[249:]:
   layer.trainable = True

# we need to recompile the model for these modifications to take effect
# we use SGD with a low learning rate
from keras.optimizers import SGD
model.compile(optimizer=SGD(lr=0.0001, momentum=0.9), loss='categorical_crossentropy')

# we train our model again (this time fine-tuning the top 2 inception blocks
# alongside the top Dense layers
model.fit_generator(datagen.flow(x_train,y_train,batch_size=128), steps_per_epoch=len(x_train)/128,epochs=10)`

0 个答案:

没有答案