Tensorflow 幾個最新版本的更新大力推崇 Keras 相當於官宣啊
相信Keras的可用性給大家做深度學習帶來方便 暢快的同時
逐漸會變成主流的開發組件
0、官方資料
Imagenet的圖像識別任務目睹了近幾年人工神經網絡的進展
其中有很多里程碑式的進展 keras在應用模塊(keras.applications)中提供了帶有預訓練權值的深度學習模型,這些模型可以用來進行預測、特徵提取和微調(fine-tuning)。
中文官方資料:https://keras.io/zh/applications/
1、模型下載 (不推薦)
使用預訓練模型可以事先下載好模型
https://github.com/fchollet/deep-learning-models/releases/
下載好的模型要放在本地目錄:
Linux下是放在“~/.keras/models/”中
Win下則放在Python的“settings/.keras/models/”中
之後就可以脫機使用
2、使用預訓練模型
爲什麼不推薦直接下載的模型呢
因爲我發現下載的文件並不是keras最新版(當前2.2.4)裏指定模型文件
也就是說下載的模型不一定完全能用
所以建議有條件聯網的同志直接聯網使用模型
初次使用會自動下載h5文件到上述目錄中
不過無論怎麼說 使用模型的代碼都是一樣的
from keras.applications.resnet50 import ResNet50
from keras.preprocessing import image
from keras.applications.resnet50 import preprocess_input, decode_predictions
import numpy as np
model = ResNet50(weights='imagenet')
img_path = 'demo.jpg'
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
preds = model.predict(x)
print('Predicted:', decode_predictions(preds, top=5)[0])
上述示例使用 ResNet50 識別 demo.jpg 圖像中的內容
其他使用模型的案例見中文文檔
值得說明的是:
每種網絡有自己的圖像預處理方法 雖然很簡單但是會不同
因此 preprocess_input 函數需要根據網絡不同而區分
但是decode_predictions都是一樣的 不需要針對每個網絡使用不同的函數
3、試驗結果
預訓練模型識別準確率
名稱 | top5準確率 | top1準確率 | |
---|---|---|---|
1 | 'DenseNet201' | 0.972857142857143 | 0.854835164835165 |
2 | 'InceptionResNetV2' | 0.985846153846154 | 0.893714285714286 |
3 | 'InceptionV3' | 0.983450549450549 | 0.884351648351648 |
4 | 'MobileNet' | 0.970417582417583 | 0.850483516483517 |
5 | 'MobileNetV2' | 0.962175824175824 | 0.823494505494506 |
6 | 'NASNetMobile' | 0.952021978021978 | 0.809714285714286 |
7 | 'ResNet50' | 0.945604395604396 | 0.791252747252747 |
8 | 'VGG16' | 0.912219780219780 | 0.729208791208791 |
9 | 'VGG19' | 0.912967032967033 | 0.731472527472527 |
10 | 'Xception' | 0.981516483516484 | 0.881626373626374 |
上表是各個模型識別 Imagenet 中45500幅圖像的結果,與官方文檔中給出的結果基本一致