案例:使用pre_trained模型进行VGG

本文介绍了在TensorFlow和Keras中使用预训练的VGG模型进行图像识别的任务。通过比较VGG与其他流行模型如ResNet、Inception等的性能指标,强调了VGG在特定场景识别上的优势。建议在VGG基础上进行微调以提升预测效果,并提供了加载和预测图像的代码示例。此外,还展示了如何保存和加载模型权重。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

tensorflow2

Goole在提供VGG进行预测时效果会更好一些,所以选择使用VGG来进行测试

在tensorflow,keras.applications存在很多的模型

Xception8879.0%94.5%22.9M81109.48.1
VGG1652871.3%90.1%138.4M1669.54.2
VGG1954971.3%90.0%143.7M1984.84.4
ResNet509874.9%92.1%25.6M10758.24.6
ResNet50V29876.0%93.0%25.6M10345.64.4
ResNet10117176.4%92.8%44.7M20989.65.2
ResNet101V217177.2%93.8%44.7M20572.75.4
ResNet15223276.6%93.1%60.4M311127.46.5
ResNet152V223278.0%94.2%60.4M307107.56.6
InceptionV39277.9%93.7%23.9M18942.26.9
InceptionResNetV221580.3%95.3%55.9M449130.210.0
MobileNet1670.4%89.5%4.3M5522.63.4
MobileNetV21471.3%90.1%3.5M10525.93.8
DenseNet1213375.0%92.3%8.1M24277.15.4
DenseNet1695776.2%93.2%14.3M33896.46.3
DenseNet2018077.3%93.6%20.2M402127.26.7
NASNetMobile2374.4%91.9%5.3M38927.06.7
NASNetLarge34382.5%96.0%88.9M533344.520.0
EfficientNetB02977.1%93.3%5.3M13246.04.9
EfficientNetB13179.1%94.4%7.9M18660.25.6
EfficientNetB23680.1%94.9%9.2M18680.86.5
EfficientNetB34881.6%95.7%12.3M210140.08.8
EfficientNetB47582.9%96.4%19.5M258308.315.1
EfficientNetB511883.6%96.7%30.6M312579.225.3
EfficientNetB616684.0%96.8%43.3M360958.140.4
EfficientNetB725684.3%97.0%66.7M4381578.961.6
EfficientNetV2B02978.7%94.3%7.2M---
EfficientNetV2B13479.8%95.0%8.2M---
EfficientNetV2B24280.5%95.1%10.2M---
EfficientNetV2B35982.0%95.8%14.5M---
EfficientNetV2S8883.9%96.7%21.6M---
EfficientNetV2M22085.3%97.4%54.4M---
EfficientNetV2L47985.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()

运行结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值