tensorflow2
Goole在提供VGG进行预测时效果会更好一些,所以选择使用VGG来进行测试
在tensorflow,keras.applications存在很多的模型
Xception | 88 | 79.0% | 94.5% | 22.9M | 81 | 109.4 | 8.1 |
VGG16 | 528 | 71.3% | 90.1% | 138.4M | 16 | 69.5 | 4.2 |
VGG19 | 549 | 71.3% | 90.0% | 143.7M | 19 | 84.8 | 4.4 |
ResNet50 | 98 | 74.9% | 92.1% | 25.6M | 107 | 58.2 | 4.6 |
ResNet50V2 | 98 | 76.0% | 93.0% | 25.6M | 103 | 45.6 | 4.4 |
ResNet101 | 171 | 76.4% | 92.8% | 44.7M | 209 | 89.6 | 5.2 |
ResNet101V2 | 171 | 77.2% | 93.8% | 44.7M | 205 | 72.7 | 5.4 |
ResNet152 | 232 | 76.6% | 93.1% | 60.4M | 311 | 127.4 | 6.5 |
ResNet152V2 | 232 | 78.0% | 94.2% | 60.4M | 307 | 107.5 | 6.6 |
InceptionV3 | 92 | 77.9% | 93.7% | 23.9M | 189 | 42.2 | 6.9 |
InceptionResNetV2 | 215 | 80.3% | 95.3% | 55.9M | 449 | 130.2 | 10.0 |
MobileNet | 16 | 70.4% | 89.5% | 4.3M | 55 | 22.6 | 3.4 |
MobileNetV2 | 14 | 71.3% | 90.1% | 3.5M | 105 | 25.9 | 3.8 |
DenseNet121 | 33 | 75.0% | 92.3% | 8.1M | 242 | 77.1 | 5.4 |
DenseNet169 | 57 | 76.2% | 93.2% | 14.3M | 338 | 96.4 | 6.3 |
DenseNet201 | 80 | 77.3% | 93.6% | 20.2M | 402 | 127.2 | 6.7 |
NASNetMobile | 23 | 74.4% | 91.9% | 5.3M | 389 | 27.0 | 6.7 |
NASNetLarge | 343 | 82.5% | 96.0% | 88.9M | 533 | 344.5 | 20.0 |
EfficientNetB0 | 29 | 77.1% | 93.3% | 5.3M | 132 | 46.0 | 4.9 |
EfficientNetB1 | 31 | 79.1% | 94.4% | 7.9M | 186 | 60.2 | 5.6 |
EfficientNetB2 | 36 | 80.1% | 94.9% | 9.2M | 186 | 80.8 | 6.5 |
EfficientNetB3 | 48 | 81.6% | 95.7% | 12.3M | 210 | 140.0 | 8.8 |
EfficientNetB4 | 75 | 82.9% | 96.4% | 19.5M | 258 | 308.3 | 15.1 |
EfficientNetB5 | 118 | 83.6% | 96.7% | 30.6M | 312 | 579.2 | 25.3 |
EfficientNetB6 | 166 | 84.0% | 96.8% | 43.3M | 360 | 958.1 | 40.4 |
EfficientNetB7 | 256 | 84.3% | 97.0% | 66.7M | 438 | 1578.9 | 61.6 |
EfficientNetV2B0 | 29 | 78.7% | 94.3% | 7.2M | - | - | - |
EfficientNetV2B1 | 34 | 79.8% | 95.0% | 8.2M | - | - | - |
EfficientNetV2B2 | 42 | 80.5% | 95.1% | 10.2M | - | - | - |
EfficientNetV2B3 | 59 | 82.0% | 95.8% | 14.5M | - | - | - |
EfficientNetV2S | 88 | 83.9% | 96.7% | 21.6M | - | - | - |
EfficientNetV2M | 220 | 85.3% | 97.4% | 54.4M | - | - | - |
EfficientNetV2L | 479 | 85.7% | 97.5% | 119.0M | - | - | - |
预测条件:Google在用VGG训练ImageNet比赛当中的1000个类别才能预测
- 特定场景的识别任务, 必须训练自己的模型进行预测。
- 可以在VGG的基础之上进行训练,节约训练时间,效果也会得到改善
- 迁移学习
模型获取
from tensorflow.keras.applications import VGG16
model = VGG16()
print(model.summary())
模型打印为:
下载保存模型:
from tensorflow.keras.applications import VGG16
model = VGG16()
model.save_weights("./.keras/model.h5")
/
预测的图片:(图片资源可以自己寻找进行训练预测)
完整代码如下:
from tensorflow.keras.applications import VGG16
from tensorflow.keras.preprocessing.image import load_img, img_to_array
from tensorflow.keras.applications.vgg16 import decode_predictions
from tensorflow.keras.applications.vgg16 import preprocess_input
def predict():
model = VGG16()
# model.save_weights("./.keras/model.h5")
# 加载图片并输入到模型中
# (224, 224)是VGG的输入要求
image = load_img("./images/tiger.png", target_size=(224, 224))
image = img_to_array(image)
# 输入到卷积中,需要四维结构
image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))
# print(image.shape)
# 预测之前做图片的数据处理,归一化处理等等
image = preprocess_input(image)
y_predictions = model.predict(image)
# print(y_predictions)
# 进行结果解码
label = decode_predictions(y_predictions)
# 预测的结果输出为
print("预测的类别为:%s 概率为:%f" % (label[0][0][1], label[0][0][2]))
if __name__ == "__main__":
predict()
运行结果: