这里我们将会把vgg16网络的前8层进行冻结,然后对后面的网络重新进行训练。这么做是因为最初的几层网络捕获的是曲线、边缘这种普遍的特征,这跟我们的问题是相关的。我们想要保证这些权重不变,让网络在学习过程中重点关注这个数据集特有的一些特征,从而对后面的网络进行调整。 fromkeras.models importSequential fromscipy.misc importimreadget_ipython().magic( 'matplotlib inline') importmatplotlib.pyplot asplt importnumpy asnp importkeras fromkeras.layers importDense importpandas aspd fromkeras.applications.vgg16 importVGG16 fromkeras.preprocessing importimage fromkeras.applications.vgg16 importpreprocess_input importnumpy asnp fromkeras.applications.vgg16 importdecode_predictions fromkeras.utils.np_utils importto_categorical fromsklearn.preprocessing importLabelEncoder fromkeras.models importSequential fromkeras.optimizers importSGD fromkeras.layers importInput, Dense, Convolution2D, MaxPooling2D, AveragePooling2D, ZeroPadding2D, Dropout, Flatten, merge, Reshape, Activation fromsklearn.metrics importlog_losstrain=pd.read_csv( "R/Data/Train/train.csv")test=pd.read_csv( "R/Data/test.csv")train_path= "R/Data/Train/Images/train/"test_path= "R/Data/Train/Images/test/"fromscipy.misc importimresizetrain_img=[] fori inrange(len(train)): temp_img=image.load_img(train_path+train[ 'filename'][i],target_size=( 224, 224)) temp_img=image.img_to_array(temp_img) train_img.append(temp_img)train_img=np.array(train_img) train_img=preprocess_input(train_img)test_img=[] fori inrange(len(test)):temp_img=image.load_img(test_path+test[ 'filename'][i],target_size=( 224, 224)) temp_img=image.img_to_array(temp_img) test_img.append(temp_img)test_img=np.array(test_img) test_img=preprocess_input(test_img) fromkeras.models importModel defvgg16_model(img_rows, img_cols, channel=1, num_classes=None):model = VGG16(weights= 'imagenet', include_top= True) model.layers.pop() model.outputs = [model.layers[- 1].output] model.layers[- 1].outbound_nodes = [] x=Dense(num_classes, activation= 'softmax')(model.output) model=Model(model.input,x) #To set the first 8 layers to non-trainable (weights will not be updated)forlayer inmodel.layers[: 8]: layer.trainable = False# Learning rate is changed to 0.001sgd = SGD(lr= 1e-3, decay= 1e-6, momentum= 0.9, nesterov= True) model.compile(optimizer=sgd, loss= 'categorical_crossentropy', metrics=[ 'accuracy']) returnmodeltrain_y=np.asarray(train[ 'label'])le = LabelEncoder()train_y = le.fit_transform(train_y)train_y=to_categorical(train_y)train_y=np.array(train_y) fromsklearn.model_selection importtrain_test_splitX_train, X_valid, Y_train, Y_valid=train_test_split(train_img,train_y,test_size= 0.2, random_state= 42) # Example to fine-tune on 3000 samples from Cifar10img_rows, img_cols = 224, 224# Resolution of inputschannel = 3num_classes = 10batch_size = 16nb_epoch = 10# Load our modelmodel = vgg16_model(img_rows, img_cols, channel, num_classes)model.summary() # Start Fine-tuningmodel.fit(X_train, Y_train,batch_size=batch_size,epochs=nb_epoch,shuffle= True,verbose= 1,validation_data=(X_valid, Y_valid)) # Make predictionspredictions_valid = model.predict(X_valid, batch_size=batch_size, verbose= 1) # Cross-entropy loss scorescore = log_loss(Y_valid, predictions_valid) 相关资源 原文: https://www.analyticsvidhya.com/blog/2017/06/transfer-learning-the-art-of-fine-tuning-a-pre-trained-model/ VGG-16: https://gist.github.com/baraldilorenzo/07d7802847aaad0a35d3 Keras库中的ImageNet预训练模型: https://keras.io/applications/ 手写数字数据集MNIST: 【完】 一则通知 (责任编辑:本港台直播) |