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幅图像的结果,与官方文档中给出的结果基本一致