迁移学习之EfficientNetBX(图像识别)
文章目录1.实现效果:2.结果分析:3.主文件:TransorEfficientNetBx.py:1.实现效果:实际图像:(1)EfficientNetB0预测的前三个中第一个结果:whippet(惠佩特犬)——0.18034123(2)EfficientNetB1预测的前三个中第一个结果:Ibizan_hound(古老犬)——0.18319356(3)EfficientNetB6预测的前三个中第
1.实现效果:
实际图像:
(1)EfficientNetB0预测的前三个中第一个结果:whippet(惠佩特犬)——0.18034123
(2)EfficientNetB1预测的前三个中第一个结果:Ibizan_hound(古老犬)——0.18319356
(3)EfficientNetB6预测的前三个中第一个结果:Walker_hound(步行猎犬)—— 0.1849944
(4)EfficientNetB7预测的前三个中第一个结果:whippet(惠佩特犬)0.32211745
2.结果分析:
从预测的结果上来看,都预测成了狗的类别,只是预测的具体狗的类型不同,但是从图像上来看,这应该是属于“梗犬”,属于预测的结果是错误的(但是仅凭一张图说明不了什么问题),可以从预测的概率上来看,EfficientNetB7预测的概率值明显大于前面的三个模型,但是同时EfficientNetB7的模型也比之前的三个模型大很多,所以预测的效果也要好很多,但是模型大对于计算量也很大,训练的时候对于硬件的要求也很高,所以选择一种折中的方法比较好。
关于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
关于DenseNet121(121层),DenseNet169(169层),DenseNet201(201层):
https://mydreamambitious.blog.csdn.net/article/details/123908742
3.主文件:TransorEfficientNetBx.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
import tensorflow.keras.applications.efficientnet
from tensorflow.keras.preprocessing.image import img_to_array
from tensorflow.keras.applications.efficientnet import preprocess_input,decode_predictions
def load_EfficientNetB0():
#加载InceptionV3并且保留顶层(也就是全连接层)
model_EfficientNetB0=tensorflow.keras.applications.efficientnet.EfficientNetB0(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_EfficientNetB0.predict(img_out)
#输出前三个结果的可能性
print('Predicted: ',decode_predictions(preds,top=3)[0])
print('Predicted: ',decode_predictions(preds,top=3))
def load_EfficientNetB1():
# 加载Xception并且保留顶层(也就是全连接层)
model_EfficientNetB1 =tensorflow.keras.applications.efficientnet.EfficientNetB1(weights='imagenet')
# 图形路径
img_path = 'images/train/dog/1.jpg'
# 将图像转换为网络需要的大小,因为我们这里加载的模型都是固定输入大小224*224
img = image.load_img(img_path, target_size=(240, 240))
# 首先需要转换为向量的形式
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_EfficientNetB1.predict(img_out)
# 输出前三个结果的可能性
print('Predicted: ', decode_predictions(preds, top=3)[0])
print('Predicted: ', decode_predictions(preds, top=3))
def load_EfficientNetB6():
#加载InceptionV3并且保留顶层(也就是全连接层)
model_EfficientNetB6=tensorflow.keras.applications.efficientnet.EfficientNetB6(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=(528,528))
#首先需要转换为向量的形式
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_EfficientNetB6.predict(img_out)
#输出前三个结果的可能性
print('Predicted: ',decode_predictions(preds,top=3)[0])
print('Predicted: ',decode_predictions(preds,top=3))
def load_EfficientNetB7():
#加载InceptionV3并且保留顶层(也就是全连接层)
model_EfficientNetB7=tensorflow.keras.applications.efficientnet.EfficientNetB7(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=(600,600))
#首先需要转换为向量的形式
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_EfficientNetB7.predict(img_out)
#输出前三个结果的可能性
print('Predicted: ',decode_predictions(preds,top=3)[0])
print('Predicted: ',decode_predictions(preds,top=3))
if __name__ == '__main__':
print('Pycharm')
print('EfficientNetB0: \\n')
load_EfficientNetB0()
print('EfficientNetB1: \\n')
load_EfficientNetB1()
print('EfficientNetB6: \\n')
load_EfficientNetB6()
print('EfficientNetB7: \\n')
load_EfficientNetB7()
更多推荐
所有评论(0)