首页 > IT职业 > IT生活 > 什么是迁移学习?什么时候使用迁移学习?
from keras.applications import VGG16 from keras.preprocessing.image import ImageDataGenerator from keras import models, layers, optimizers from keras.layers.normalization import BatchNormalization from keras.preprocessing.image import load_img # train only the top FC layers of VGG16, use weights learnt with ImageNet for the convolution layers vgg_model = VGG16(weights='imagenet', include_top=False, input_shape=(224,224, 3)) # the directory flower_photos is assumed to be on the current path train_dir = './flower_photos/train' validation_dir = './flower_photos/valid' n_train = 500*3 n_val = 50*3 datagen = ImageDataGenerator(rescale=1./255) batch_size = 25 train_features = np.zeros(shape=(n_train, 7, 7, 512)) train_labels = np.zeros(shape=(n_train,3)) train_generator = datagen.flow_from_directory(train_dir, target_size=(224,224), batch_size=batch_size, class_mode='categorical', shuffle=True) i = 0 for inputs_batch, labels_batch in train_generator: features_batch = vgg_model.predict(inputs_batch) train_features[i * batch_size : (i + 1) * batch_size] = features_batch train_labels[i * batch_size : (i + 1) * batch_size] = labels_batch i += 1 if i * batch_size >= n_train: break train_features = np.reshape(train_features, (n_train, 7 * 7 * 512)) validation_features = np.zeros(shape=(n_val, 7, 7, 512)) validation_labels = np.zeros(shape=(n_val,3)) validation_generator = datagen.flow_from_directory(validation_dir, target_size=(224, 224), batch_size=batch_size, class_mode='categorical', shuffle=False) i = 0 for inputs_batch, labels_batch in validation_generator: features_batch = vgg_model.predict(inputs_batch) validation_features[i * batch_size : (i + 1) * batch_size] =features_batch validation_labels[i * batch_size : (i + 1) * batch_size] = labels_batch i += 1 if i * batch_size >= n_val: break validation_features = np.reshape(validation_features, (n_val, 7 * 7 * 512))
# now learn the FC layer parameters by training with the images we have
model = models.Sequential()
model.add(layers.Dense(512, activation='relu', input_dim=7 * 7 * 512))
model.add(BatchNormalization())
model.add(layers.Dropout(0.5))
model.add(layers.Dense(3, activation='softmax'))
model.compile(optimizer=optimizers.Adam(lr=1e-5),
loss='categorical_crossentropy', metrics=['acc'])
history = model.fit(train_features, train_labels, epochs=20,batch_size=batch_size,
validation_data=(validation_features,validation_labels))
filenames = validation_generator.filenames
ground_truth = validation_generator.classes
label2index = validation_generator.class_indices
# Getting the mapping from class index to class label
idx2label = dict((v,k) for k,v in label2index.items())
predictions = model.predict_classes(validation_features)
prob = model.predict(validation_features)
errors = np.where(predictions != ground_truth)[0]
print("No of errors = {}/{}".format(len(errors),n_val))
# No of errors = 13/150
pylab.figure(figsize=(20,12))
for i in range(len(errors)):
pred_class = np.argmax(prob[errors[i]])
pred_label = idx2label[pred_class]
original =load_img('{}/{}'.format(validation_dir,filenames[errors[i]]))
pylab.subplot(3,5,i+1), pylab.imshow(original), pylab.axis('off')
pylab.title('Original
label:{}\nPrediction:{}\nconfidence:{:.3f}'.format(
filenames[errors[i]].split('\\')[0], pred_label,
prob[errors[i]][pred_class]), size=15)
pylab.show()
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/30239065/viewspace-2746516/ ,如需转载,请注明出处,否则将追究法律责任。