浅谈Tensorflow加载Vgg预训练模型的几个注意事项


Posted in Python onMay 26, 2020

写这个博客的关键Bug: Value passed to parameter 'input' has DataType uint8 not in list of allowed values: float16, bfloat16, float32, float64。本博客将围绕 加载图片 和 保存图片到本地 来详细解释和解决上述的Bug及其引出来的一系列Bug。

加载图片

首先,造成上述Bug的代码如下所示

image_path = "data/test.jpg" # 本地的测试图片
 
image_raw = tf.gfile.GFile(image_path, 'rb').read()
# 一定要tf.float(),否则会报错
image_decoded = tf.image.decode_jpeg(image_raw)
 
# 扩展图片的维度,从三维变成四维,符合Vgg19的输入接口
image_expand_dim = tf.expand_dims(image_decoded, 0)
 
# 定义Vgg19模型
vgg19 = VGG19(data_path)
net = vgg19.feed_forward(image_expand_dim, 'vgg19')
print(net)

上述代码是加载Vgg19预训练模型,并传入图片得到所有层的特征图,具体的代码实现和原理讲解可参考我的另一篇博客:Tensorflow加载Vgg预训练模型。那么,为什么代码会出现: Value passed to parameter 'input' has DataType uint8 not in list of allowed values: float16, bfloat16, float32, float64,这个Bug呢?

这句英文翻译过来是指:传递的值类型是uint8,但是接受的参数类型必须是float的那几种。故原因就是传入值的数据类型错了,那么如何解决这个Bug呢,很简单

image_path = "data/test.jpg" # 本地的测试图片
 
image_raw = tf.gfile.GFile(image_path, 'rb').read()
# 一定要tf.float(),否则会报错
image_decoded = tf.to_float(tf.image.decode_jpeg(image_raw))
 
# 扩展图片的维度,从三维变成四维,符合Vgg19的输入接口
image_expand_dim = tf.expand_dims(image_decoded, 0)
 
# 定义Vgg19模型
vgg19 = VGG19(data_path)
net = vgg19.feed_forward(image_expand_dim, 'vgg19')
print(net)

这两个代码块唯一的变动就是:image_decoded结果在输出前加了一个tf.float(),将其转换为float类型。

在tensorflow API中,tf.image.decode_jpeg()默认读取的图片数据格式为unit8,而不是float。uint8数据的范围在(0, 255)中,正好符合图片的像素范围(0, 255)。但是,保存在本地的Vgg19预训练模型的数据接口为float,所以才造成了本文开头的Bug。

这里还要提一点,若是使用PIL的方法来加载图片,则不会出现上述的Bug,因为通过PIL得到的图片格式是float,而不是uint8,故不需要转换。

很多同学可能会疑惑,若是强行改变了原图片的数据格式,从uint8类型转变成float,会不会导致数据改变或者出错?故我做了下面这个实验:

image_path = "data/3.jpg"
image_raw = tf.gfile.GFile(image_path, 'rb').read()
image_unit8 = tf.image.decode_jpeg(image_raw)
image_float = tf.to_float(image_unit8)
 
with tf.Session() as sess:
 image_unit8_, image_float_ = sess.run([image_unit8, image_float])
 
print("image_unit8_", image_unit8_)
print("image_float_ ", image_float_ )

代码结果如下:

image_unit8_
 [180, 192, 204],
 [183, 195, 207],
 [186, 198, 210],
 ...,
 [191, 205, 218],
 [191, 205, 218],
 [190, 204, 217]],
 
 image_float_ 
 [180., 192., 204.],
 [183., 195., 207.],
 [186., 198., 210.],
 ...,
 [191., 205., 218.],
 [191., 205., 218.],
 [190., 204., 217.]],

可以看到,数据根本没有变化,只是后面多加了个小数点,变得只有类型,而没有强制改变值,故同学们不需要过度担心。

保存图片到本地

在加载图片的时候,为了使用保存在本地的预训练Vgg19模型,我们需要将读取的图片由uint8格式转换成float格式。那若是我们想将已经转换为float格式的图片再保存到本地,该怎么做呢?

首先,我们根据上述的文字的意思读取图片,并且将其转换为float格式,在将读取的图片再次保存到本地之前,我们首先可视化一下转换格式后的图片,代码如下:

import tensorflow as tf
from matplotlib import pyplot as plt
image_path = "data/boat.jpg"
 
image_raw = tf.gfile.GFile(image_path, 'rb').read()
image_decoded = tf.image.decode_jpeg(image_raw)
image_decoded = tf.to_float(image_decoded)
 
with tf.Session() as sess:
 image_decoded_ = sess.run(image_decoded)
 plt.imshow(image_decoded_)
 plt.show()

生成的图片如下图所示:

浅谈Tensorflow加载Vgg预训练模型的几个注意事项

左边是原图,右边是转换为float格式的图片,可见将图片转换为float格式,虽然数值没有造成太大影响,但是若想将图片保存到本地就会出现问题。

说了这么多,只为了说一点,在保存图片到本地之前,需要将其格式从float转回uint8,否则会造成一系列错误:图片显示异常,API报错等。正确的保存代码如下:

save_path = "data/boat_copy.jpg"
image_uint = tf.cast(image_decoded, tf.uint8)
with tf.Session() as sess:
 with open(save_path, 'wb') as img:
 image_saved = sess.run(tf.image.encode_jpeg(image_uint))
 img.write(image_saved)

其中只有一句话最关键,即 tf.cast(image_decoded, tf.uint8)。

以上这篇浅谈Tensorflow加载Vgg预训练模型的几个注意事项就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python中使用异常处理来判断运行的操作系统平台方法
Jan 22 Python
Python装饰器入门学习教程(九步学习)
Jan 28 Python
Python实现的快速排序算法详解
Aug 01 Python
pygame实现俄罗斯方块游戏
Jun 26 Python
python+pyqt5实现24点小游戏
Jan 24 Python
Python常见的pandas用法demo示例
Mar 16 Python
tensorflow 环境变量设置方式
Feb 06 Python
python中导入 train_test_split提示错误的解决
Jun 19 Python
浅谈Python 命令行参数argparse写入图片路径操作
Jul 12 Python
Python利用命名空间解析XML文档
Aug 10 Python
python爬取微博评论的实例讲解
Jan 15 Python
解决pytorch 模型复制的一些问题
Mar 03 Python
Tensorflow加载Vgg预训练模型操作
May 26 #Python
PyQt5如何将.ui文件转换为.py文件的实例代码
May 26 #Python
TensorFlow实现模型断点训练,checkpoint模型载入方式
May 26 #Python
python 日志模块 日志等级设置失效的解决方案
May 26 #Python
python3.7+selenium模拟淘宝登录功能的实现
May 26 #Python
TensorFlow固化模型的实现操作
May 26 #Python
Python 如何批量更新已安装的库
May 26 #Python
You might like
ecshop 订单确认中显示省市地址信息的方法
2010/03/15 PHP
网站用php实现paypal整合方法
2010/11/28 PHP
PHP设计模式之命令模式示例详解
2020/12/20 PHP
javascript 关闭IE6、IE7
2009/06/01 Javascript
什么是json和jsonp,jQuery json实例详详细说明
2012/12/11 Javascript
Jquery多选框互相内容交换的实例代码
2013/07/04 Javascript
javascript判断chrome浏览器的方法
2014/03/26 Javascript
触屏中的JavaScript事件分析
2015/02/06 Javascript
js拆分字符串并将分割的数据放到数组中的方法
2015/05/06 Javascript
jquery+css3实现会动的小圆圈效果
2016/01/27 Javascript
浅谈sass在vue注意的地方
2017/08/10 Javascript
详解使用Vue Router导航钩子与Vuex来实现后退状态保存
2017/09/11 Javascript
vue 根据数组中某一项的值进行排序的方法
2018/08/30 Javascript
node.js命令行教程图文详解
2019/05/27 Javascript
vue+element 模态框表格形式的可编辑表单实现
2019/06/07 Javascript
Vue实现商品飞入购物车效果(电商项目)
2019/11/26 Javascript
JavaScript实现随机点名器
2020/03/25 Javascript
微信小程序12行js代码自己写个滑块功能(推荐)
2020/07/15 Javascript
Python数组条件过滤filter函数使用示例
2014/07/22 Python
Python基于identicon库创建类似Github上用的头像功能
2017/09/25 Python
python 自动批量打开网页的示例
2019/02/21 Python
django 自定义filter 判断if var in list的例子
2019/08/20 Python
关于tensorflow的几种参数初始化方法小结
2020/01/04 Python
Python定时器线程池原理详解
2020/02/26 Python
Pytorch对Himmelblau函数的优化详解
2020/02/29 Python
python GUI库图形界面开发之PyQt5树形结构控件QTreeWidget详细使用方法与实例
2020/03/02 Python
html5的自定义data-*属性与jquery的data()方法的使用
2014/07/02 HTML / CSS
奥地利网上书店:Weltbild
2017/07/14 全球购物
巴西手表购物网站:eclock
2019/03/19 全球购物
歌唱比赛主持词
2014/03/18 职场文书
学前班评语大全
2014/05/04 职场文书
活动总结结尾怎么写
2014/08/30 职场文书
2015年班组长工作总结
2015/04/10 职场文书
python中__slots__节约内存的具体做法
2021/07/04 Python
Python中三种花式打印的示例详解
2022/03/19 Python
sentinel支持的redis高可用集群配置详解
2022/04/01 Redis