迁移学习之DenseNet121(121层),DenseNet169(169层),DenseNet201(201层)(图像识别)

本文对比了ResNet、MobileNet、Inception系列及DenseNet在图像分类任务上的表现,发现DenseNet169在准确率与效率间取得了较好的平衡。DenseNet模型因其密集连接特性,虽计算量大但预测结果准确。对于资源有限的情况,ResNet50是不错的选择。代码示例展示了DenseNet121、169、201对狗的图像识别过程。

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

1.实现效果:

在这里插入图片描述
实际图片:
在这里插入图片描述
(1)DenseNet121预测第一个结果为:Sealyham_terrier(㹴犬)——0.21411328
(2)DenseNet169预测的第一个结果为:Sealyham_terrier(㹴犬)——0.43225577
(3)DenseNet201预测第一个结果为:Sealyham_terrier(㹴犬)——0.15977885

2.结果分析:

相比于之前的ResNet50,ResNet101,MobileNet,MobileNetV2,InceptionV3,Xception,Inception_ResNet_V2其中DenseNet169预测结果是最好的,也是正确的,虽然DenseNet121预测准确率不是很高,但是结果是正确的;但是呢!这个DenseNet121,169相比之前的ResNet50要多了很多的层,所以计算量更大,如果是自己也加了数据集进行训练的话,计算量将更大,所以折中的选择是ResNet50.

关于ResNet50和ResNet101:
https://mydreamambitious.blog.csdn.net/article/details/123906833
关于VGG16和VGG19:
https://mydreamambitious.blog.csdn.net/article/details/123906643
关于InceptionV3(159层),Xception(126层),Inception_ResNet_V2(572层):
https://mydreamambitious.blog.csdn.net/article/details/123907490
关于MobileNet(88层)和MobileNetV2(88层):
https://mydreamambitious.blog.csdn.net/article/details/123907955
EfficientNetBX
https://mydreamambitious.blog.csdn.net/article/details/123929264

3.主文件TransorDenseNet.py:

import os
import cv2
import tensorflow
import numpy as np
from PIL import Image
from tensorflow import keras
from tensorflow.keras.preprocessing import image
from tensorflow.keras.preprocessing.image import img_to_array
from tensorflow.keras.applications.inception_v3 import preprocess_input,decode_predictions

def load_DenseNet121():
    model_DenseNet=tensorflow.keras.applications.densenet.DenseNet121(weights='imagenet')

    #图形路径
    curr_path=os.getcwd()
    img_path=curr_path+'\\images\\train\\dog\\1.jpg'
    #将图像转换为网络需要的大小,因为我们这里加载的模型都是固定输入大小224*224
    img=image.load_img(img_path,target_size=(224,224))
    #首先需要转换为向量的形式
    img_out=image.img_to_array(img)
    #扩充维度
    img_out=np.expand_dims(img_out,axis=0)
    #对输入的图像进行处理
    img_out=preprocess_input(img_out)
    # decode the results into a list of tuples (class, description, probability)
    # (one such list for each sample in the batch)
    #上面这段话的意思是输出包括(类别,图像描述,输出概率)
    preds=model_DenseNet.predict(img_out)
    #输出前三个结果的可能性
    print('Predicted: ',decode_predictions(preds,top=3)[0])
    print('Predicted: ',decode_predictions(preds,top=3))


def load_DenseNet169():
    model_DenseNet169 =tensorflow.keras.applications.densenet.DenseNet169(weights='imagenet')

    # 图形路径
    img_path = 'images/train/dog/1.jpg'
    # 将图像转换为网络需要的大小,因为我们这里加载的模型都是固定输入大小224*224
    img = image.load_img(img_path, target_size=(224, 224))
    # 首先需要转换为向量的形式
    img_out = image.img_to_array(img)
    # 扩充维度
    img_out = np.expand_dims(img_out, axis=0)
    # 对输入的图像进行处理
    img_out = preprocess_input(img_out)
    # decode the results into a list of tuples (class, description, probability)
    # (one such list for each sample in the batch)
    # 上面这段话的意思是输出包括(类别,图像描述,输出概率)
    preds = model_DenseNet169.predict(img_out)
    # 输出前三个结果的可能性
    print('Predicted: ', decode_predictions(preds, top=3)[0])
    print('Predicted: ', decode_predictions(preds, top=3))
def load_DenseNet201():
    model_DenseNet201 =tensorflow.keras.applications.densenet.DenseNet201(weights='imagenet')

    # 图形路径
    img_path = 'images/train/dog/1.jpg'
    # 将图像转换为网络需要的大小,因为我们这里加载的模型都是固定输入大小224*224
    img = image.load_img(img_path, target_size=(224, 224))
    # 首先需要转换为向量的形式
    img_out = image.img_to_array(img)
    # 扩充维度
    img_out = np.expand_dims(img_out, axis=0)
    # 对输入的图像进行处理
    img_out = preprocess_input(img_out)
    # decode the results into a list of tuples (class, description, probability)
    # (one such list for each sample in the batch)
    # 上面这段话的意思是输出包括(类别,图像描述,输出概率)
    preds = model_DenseNet201.predict(img_out)
    # 输出前三个结果的可能性
    print('Predicted: ', decode_predictions(preds, top=3)[0])
    print('Predicted: ', decode_predictions(preds, top=3))


if __name__ == '__main__':
    print('Pycharm')
    print('DenseNet121: \\n')
    load_DenseNet121()
    print('DenseNet169: \\n')
    load_DenseNet169()
    print('DenseNet201: \\n')
    load_DenseNet201()

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值