什麼是遷移學習?什麼時候使用遷移學習?
用Keras實現遷移學習
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()
- 影像處理,計算機視覺人臉識別影像修復
- 程式設計入門教程書籍零基礎,深度學習爬蟲
- 用流行的Python影像處理庫、機器學習庫和深度學習庫解決影像處理問題。
- 使用TensorFlow和Keras實現高階深度學習和神經網路模型
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/30239065/viewspace-2746516/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 深度學習趣談:什麼是遷移學習?(附帶Tensorflow程式碼實現)深度學習遷移學習
- 遷移學習系列---基於例項方法的遷移學習遷移學習
- 遷移學習(Transfer Learning)遷移學習
- 遷移學習時間序列分類遷移學習
- 深度學習之遷移學習介紹與使用深度學習遷移學習
- flask資料庫遷移是什麼意思Flask資料庫
- 深度學習+深度強化學習+遷移學習【研修】深度學習強化學習遷移學習
- 使用Mobilenet和Keras進行遷移學習!Keras遷移學習
- 遷移學習基本原理遷移學習
- [譯] TensorFlow 教程 #08 – 遷移學習遷移學習
- 基於VGG的遷移學習遷移學習
- 伺服器遷移中心SMC是什麼?SMC是做什麼用的伺服器
- 機器學習:詳解遷移學習(Transfer learning)機器學習遷移學習
- 深度學習之PyTorch實戰(4)——遷移學習深度學習PyTorch遷移學習
- Python遷移學習:機器學習演算法Python遷移學習機器學習演算法
- 跨雲業務遷移時需要注意什麼?
- 站在巨人的肩膀上:遷移學習遷移學習
- 遷移學習中的BN問題遷移學習
- Laravel 學習之資料庫遷移Laravel資料庫
- 遷移學習(ADDA)《Adversarial Discriminative Domain Adaptation》遷移學習AIAPT
- 機器什麼時候能夠學習?
- 教程:使用遷移學習來解決影像問題!遷移學習
- [譯] 從 Cron 到 Airflow 的遷移中我們學到了什麼AI
- 深度學習不得不會的遷移學習Transfer Learning深度學習遷移學習
- 《PyTorch》Part5 PyTorch之遷移學習PyTorch遷移學習
- 圖解BERT(NLP中的遷移學習)圖解遷移學習
- 再談遷移學習:微調網路遷移學習
- 數字資訊化之遷移學習遷移學習
- 再無需從頭訓練遷移學習模型!亞馬遜開源遷移學習資料庫 Xfer遷移學習模型亞馬遜資料庫
- 一文讀懂遷移學習(附學習工具包)遷移學習
- 【學習】NPM是什麼NPM
- 深度學習是什麼深度學習
- android中的深度學習——快速風格遷移Android深度學習
- 視覺語言模型的高效遷移學習視覺模型遷移學習
- 遷移學習(IIMT)——《Improve Unsupervised Domain Adaptation with Mixup Training》遷移學習AIAPT
- 遷移學習(DANN)《Domain-Adversarial Training of Neural Networks》遷移學習AI
- 企業資訊系統在遷移過程中,資料遷移要注意什麼?
- Homestead 怎麼遷移?