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()