概述
文章目录
- 1.实现的效果:
- 2.结果分析:
- 3.主文件TransorMobileNet.py:
1.实现的效果:
实际图片:
(1)MobileNet预测的第一个结果为:pug(哈巴狗)
(2)MobileNetV2预测的第一个结果为:West_Highland_white_terrier(西部高地白梗犬)
2.结果分析:
(1)虽然两种模型都预测为了狗,可是结果差别也较大,相比于之前的ResNet50,ResNet101,Inception_ResNet_V2差的较多(当然简单的测试说明不了什么问题,但是也能看出差别)。
(2)说明ResNet残差网络对于图像的预测效果比较好,也不能说明更深层的网络提取的特征一定比稍微浅层的好,就像之前ResNet50和ResNet101的比较结果,ResNet50输出的概率值就比ResNet101要好10个百分点左右(当然,一个简单的测试还是说明不了什么问题)。
(3)建议读者可以在以后选择图片的预测模型时,可以使用ResNet50,这个模型有50层,计算量也相对较少一些,预测的结果也比较好。
关于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
关于DenseNet121(121层),DenseNet169(169层),DenseNet201(201层):
https://mydreamambitious.blog.csdn.net/article/details/123908742
EfficientNetBX
https://mydreamambitious.blog.csdn.net/article/details/123929264
3.主文件TransorMobileNet.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_MobileNet():
#加载InceptionV3并且保留顶层(也就是全连接层)
model_MobileNet=tensorflow.keras.applications.mobilenet.MobileNet(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_MobileNet.predict(img_out)
#输出前三个结果的可能性
print('Predicted: ',decode_predictions(preds,top=3)[0])
print('Predicted: ',decode_predictions(preds,top=3))
def load_MobileNetV2():
# 加载Xception并且保留顶层(也就是全连接层)
model_MobileNetV2 =tensorflow.keras.applications.mobilenet_v2.MobileNetV2(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_MobileNetV2.predict(img_out)
# 输出前三个结果的可能性
print('Predicted: ', decode_predictions(preds, top=3)[0])
print('Predicted: ', decode_predictions(preds, top=3))
if __name__ == '__main__':
print('Pycharm')
print('MobileNet: \n')
load_MobileNet()
print('MobileNetV2: \n')
load_MobileNetV2()
最后
以上就是调皮鞋子为你收集整理的迁移学习之MobileNet(88层)和MobileNetV2(88层)的全部内容,希望文章能够帮你解决迁移学习之MobileNet(88层)和MobileNetV2(88层)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复