tensorflow学习之从图像处理看tensor和numpy数据间转换

两盒软妹~` 提交于 2020-01-11 01:50:32

参考文章:https://blog.csdn.net/xiaosongshine/article/details/84955891

在这里插入图片描述
在这里插入图片描述

问题一:tensor与numpy数据转换

在用tensorflow过程中,经常会接触到numpy,在编写过程不会感觉到太大差别,但在输出网络中,输出的结果仍然是tensor,用这些结果去执行numpy数据来执行的操作比如matplotlib时就会出现一些奇奇怪怪的错误,比如:

import os
import matplotlib.pyplot as plt
import matplotlib
#matplotlib.use("Qt5Agg")
import tensorflow as tf
import cv2

file_name = 'F:\\gaohu.bmp'
image_raw = tf.gfile.FastGFile(file_name,'rb').read()
image_data = tf.image.decode_bmp(image_raw)

##压缩维度,变二维
image_data = tf.squeeze(image_data)
image_data = tf.image.convert_image_dtype(image_data,dtype=tf.float32)

with tf.Session():
    #image_data = image_data.eval()
    plt.imshow(image_data)
    plt.show()
TypeError: Image data cannot be converted to float

原因是此时没有image_data没有经过eval()转换成numpy,还是tensor类型,而plt只能处理numpy数据

问题一解决:

  •   Numpy2Tensor
    

虽然TensorFlow网络在输入Numpy数据时会自动转换为Tensor来处理,但是我们自己也可以去显式的转换:

data_tensor = tf.convert_to_tensor(data_numpy)
  •   Tensor2Numpy
    

网络输出的结果仍为Tensor,当我们要用这些结果去执行只能由Numpy数据来执行的操作时就会出现莫名其妙的错误。解决方法:

with tf.Session():
    data_numpy = data_tensor.eval()
sess = tf.Session()
data_numpy = sess.run(data_tensor)

问题二:matplotlib

问题描述

import os
import matplotlib.pyplot as plt
import matplotlib
import tensorflow as tf
import cv2

sess = tf.Session()

file_name = 'F:\\gaohu.bmp'
image_raw = tf.gfile.FastGFile(file_name,'rb').read()
image_data = tf.image.decode_bmp(image_raw)
image_data_0 = tf.image.decode_bmp(image_raw)


#保留维度,三维
image_data_0 = tf.image.convert_image_dtype(image_data_0,dtype=tf.float32)


#图像处理
adjust_image_data_contrast = tf.image.adjust_contrast(image_data_0,5)

#截断操作
adjusted_contrast= tf.clip_by_value(adjust_image_data_contrast,0.0,1.0)

with tf.Session():
    adjusted_contrast1 = adjusted_contrast.eval()
    print(adjusted_contrast1)
    plt.imshow(adjusted_contrast1)
    plt.show()
 
TypeError: Invalid dimensions for image data
  • matplotlib.pyplot.imshow()需要数据是二维的数组或者第三维深度是3或4的三维数组,当第三维深度为1时,就会报错。

    解决

  • 当第三维深度是1的时候,使用np.squeeze()压缩数据成为二维数组

问题三:tensorflow中的InvalidArgumentError: input must be at least 3-D

问题描述


import os
import matplotlib.pyplot as plt
import matplotlib
import tensorflow as tf
import cv2

file_name = 'F:\\mianqiang.bmp'
image_raw = tf.gfile.FastGFile(file_name,'rb').read()
image_data = tf.image.decode_bmp(image_raw)
image_data_0 = tf.image.decode_bmp(image_raw)

#压缩维度,变二维
image_data = tf.squeeze(image_data)
image_data = tf.image.convert_image_dtype(image_data,dtype=tf.float32)

#图像处理
adjust_image_data_contrast = tf.image.adjust_contrast(image_data,5)

adjusted_contrast= tf.clip_by_value(adjust_image_data_contrast,0.0,1.0)

with tf.Session():
    adjusted_contrast1 = adjusted_contrast.eval()
    plt.imshow(adjusted_contrast1)
    plt.show()

InvalidArgumentError: input must be at least 3-D, got shape[1944,2592]
  • 如果被降维后的tensor经过tf.image.adjust_contrast()处理,那么被处理过后的tensor用eval方法的话,就会出现维度不对的报错,提示必须是最少3维。
  • 但是如果是经过tf.image.adjust_brightness()之类的函数处理的话,就不会有这样的情况,暂时还没找到原因。
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!