【Tensorflow】用tersorflow内置函数做图片预处理
来源:互联网 发布:国家统计局2016年数据 编辑:程序博客网 时间:2024/06/11 08:53
本篇作为【Tensorflow】超大规模数据集解决方案的补充,介绍一下tersorflow内置函数对图片的预处理。前面的方法都是用skimage等辅助库来处理图像,因为我们都是在外部处理完所有的图像,然后再输入网络,以Placeholder的形式。但是当我们使用Tensorflow内部的Input pipeline的时候,图片一经读取,就已经转换成了Tensorflow内置的格式,这种格式下,我们无法再用其他辅助工具来处理,此时,就只能使用tensorflow内部的图片处理方法了。
环境Tensorflow1.2,python2.7
我们还是使用上一篇的CoCo2014数据集里拷贝出来的那些图。
首先来看Image Resize:tf.image.resize_images函数,参数就不详细介绍了,重点对比一下不同的插值方法,程序如下
import tensorflow as tfimport matplotlib.pyplot as pltdataset_path='train2014/COCO_train2014_000000000025.jpg'with tf.Session() as sess: image_raw_data_jpg = tf.gfile.FastGFile(dataset_path, 'r').read() img_data_jpg = tf.image.decode_png(image_raw_data_jpg) img_data_jpg = tf.image.convert_image_dtype(img_data_jpg, dtype=tf.float32) resized_image1 = tf.image.resize_images(img_data_jpg, [200, 200]) resized_image2 = tf.image.resize_images(img_data_jpg, [200, 200], 1) resized_image3 = tf.image.resize_images(img_data_jpg, [200, 200], 2) resized_image4 = tf.image.resize_images(img_data_jpg, [200, 200], 3) plt.figure() plt.subplot(221) plt.imshow(resized_image1.eval()) plt.title('Bilinear interpolation') plt.subplot(222) plt.imshow(resized_image2.eval()) plt.title('Nearest neighbor interpolation') plt.subplot(223) plt.imshow(resized_image3.eval()) plt.title('Bicubic interpolation') plt.subplot(224) plt.imshow(resized_image4.eval()) plt.title('Area interpolation') plt.show() sess.close()
Cropping:tf.image.resize_image_with_crop_or_pad,tf.image.central_crop,tf.image.pad_to_bounding_box,tf.image.crop_to_bounding_box实验程序:
import tensorflow as tfimport matplotlib.pyplot as pltdataset_path='train2014/COCO_train2014_000000000025.jpg'with tf.Session() as sess: image_raw_data_jpg = tf.gfile.FastGFile(dataset_path, 'r').read() img_data_jpg = tf.image.decode_png(image_raw_data_jpg) img_data_jpg = tf.image.convert_image_dtype(img_data_jpg, dtype=tf.float32) resized_image1 = tf.image.resize_image_with_crop_or_pad(img_data_jpg, 200, 200) resized_image2 = tf.image.central_crop(img_data_jpg, 0.6) resized_image3 = tf.image.crop_to_bounding_box(img_data_jpg, 0,0,200,200) resized_image4 = tf.image.resize_image_with_crop_or_pad(img_data_jpg,800,800) plt.figure() plt.subplot(221) plt.imshow(resized_image1.eval()) plt.title('crop 200*200') plt.subplot(222) plt.imshow(resized_image2.eval()) plt.title('60% of picture') plt.subplot(223) plt.imshow(resized_image3.eval()) plt.title('from (0,0) crop 200*200(bounding box)') plt.subplot(224) plt.imshow(resized_image4.eval()) plt.title('pad 800*800') plt.show() sess.close()
Flipping, Rotating and Transposing:
tf.image.flip_up_down,tf.image.random_flip_up_down,tf.image.flip_left_right,tf.image.random_flip_left_right,tf.image.transpose_image这里要说一下random_flip_up_down这个函数是以二分之一的几率反转。
import tensorflow as tfimport matplotlib.pyplot as pltdataset_path='train2014/COCO_train2014_000000000025.jpg'with tf.Session() as sess: image_raw_data_jpg = tf.gfile.FastGFile(dataset_path, 'r').read() img_data_jpg = tf.image.decode_png(image_raw_data_jpg) img_data_jpg = tf.image.convert_image_dtype(img_data_jpg, dtype=tf.float32) resized_image1 = tf.image.flip_up_down(img_data_jpg) resized_image2 = tf.image.random_flip_up_down(img_data_jpg) resized_image3 = tf.image.flip_left_right(img_data_jpg) resized_image4 = tf.image.transpose_image(img_data_jpg) plt.figure() plt.subplot(221) plt.imshow(resized_image1.eval()) plt.title('flip_up_down') plt.subplot(222) plt.imshow(resized_image2.eval()) plt.title('random_flip_up_down') plt.subplot(223) plt.imshow(resized_image3.eval()) plt.title('flip_left_right') plt.subplot(224) plt.imshow(resized_image4.eval()) plt.title('transpose') plt.show() sess.close()
Converting Between Colorspaces:tf.image.rgb_to_grayscale,tf.image.grayscale_to_rgb,tf.image.hsv_to_rgb,tf.image.rgb_to_hsv,tf.image.convert_image_dtype颜色空间转换,比较简单,不详述了。
ImageAdjustments:tf.image.adjust_brightness,tf.image.random_brightness,tf.image.adjust_contrast,tf.image.random_contrast,tf.image.adjust_hue,tf.image.random_hue,tf.image.adjust_gamma,tf.image.adjust_saturation,tf.image.random_saturation,tf.image.per_image_standardization调节亮度对比度的一系列操作。需要注意的是tf.image.per_image_standardization这个函数是对单张图像做规范化的,它计算(x - mean) / adjusted_stddev
, 其中 mean
指图像的均值, adjusted_stddev = max(stddev, 1.0/sqrt(image.NumElements()))
,stddev则是标准差。
import tensorflow as tfimport matplotlib.pyplot as pltdataset_path='train2014/COCO_train2014_000000000025.jpg'with tf.Session() as sess: image_raw_data_jpg = tf.gfile.FastGFile(dataset_path, 'r').read() img_data_jpg = tf.image.decode_png(image_raw_data_jpg) img_data_jpg = tf.image.convert_image_dtype(img_data_jpg, dtype=tf.float32) resized_image1 = tf.image.adjust_brightness(img_data_jpg,0.003) resized_image2 = tf.image.adjust_contrast(img_data_jpg,0.3) resized_image3 = tf.image.random_hue(img_data_jpg,0.3) resized_image4 = tf.image.per_image_standardization(img_data_jpg) plt.figure() plt.subplot(221) plt.imshow(resized_image1.eval()) plt.title('adjust_brightness') plt.subplot(222) plt.imshow(resized_image2.eval()) plt.title('adjust_contrast') plt.subplot(223) plt.imshow(resized_image3.eval()) plt.title('random_hue') plt.subplot(224) plt.imshow(resized_image4.eval()) plt.title('standardization') plt.show() sess.close()
Draw Bounding Boxes:tf.image.draw_bounding_boxes
import tensorflow as tfimport matplotlib.pyplot as pltimport numpy as npdataset_path='train2014/COCO_train2014_000000000025.jpg'with tf.Session() as sess: boxes = tf.constant([[[0.1, 0.2, 0.5, 0.9]]],dtype=tf.float32) image_raw_data_jpg = tf.gfile.FastGFile(dataset_path, 'r').read() img_data_jpg = tf.image.decode_png(image_raw_data_jpg) img_data_jpg = tf.image.convert_image_dtype(img_data_jpg, dtype=tf.float32) batch_data_jpg=tf.expand_dims(img_data_jpg, 0) resized_image1 = tf.image.draw_bounding_boxes(batch_data_jpg,boxes) plt.figure() plt.imshow(np.squeeze(resized_image1.eval())) plt.title('draw_bounding boxes') plt.show() sess.close()
Total_variation:tf.image.total_variation。这是对一张图片计算总变差,即像素之间的差异大小。通常设置loss = tf.reduce_sum(tf.image.total_variation(images))加到优化项里面,可以平滑生成的图像。
import tensorflow as tfdataset_path='train2014/COCO_train2014_000000000025.jpg'with tf.Session() as sess: boxes = tf.constant([[[0.1, 0.2, 0.5, 0.9]]],dtype=tf.float32) image_raw_data_jpg = tf.gfile.FastGFile(dataset_path, 'r').read() img_data_jpg = tf.image.decode_png(image_raw_data_jpg) img_data_jpg = tf.image.convert_image_dtype(img_data_jpg, dtype=tf.float32) total_v=tf.image.total_variation(img_data_jpg) print total_v.eval() sess.close()
这张图片的Total_variation即为:
135254.0
- 【Tensorflow】用tersorflow内置函数做图片预处理
- [TensorFlow实战] 图片预处理
- Tensorflow(4) Semantic Segmentation 图片预处理
- Tensorflow图像预处理函数学习笔记
- 用自己的图片数据做tensorflow深度学习
- 制作Tensorflow数据,图片收集预处理方法(一)
- tensorflow图片预处理,随机亮度,旋转,剪切,翻转。
- TensorFlow图像数据预处理
- tensorflow图像数据预处理
- Tensorflow基础:图像预处理
- Tensorflow-图像预处理
- tensorflow下的图片标准化函数per_image_standardization
- tensorflow图片预处理和测试效果(resize,crop,pad等)
- 利用tensorflow训练自己的图片数据(1)——预处理
- 利用tensorflow训练自己的图片数据(1)——预处理
- MySQL优化之表索引、视图、内置函数、预处理、事务处理、存储、触发器
- MySQL——索引 视图 存储过程 触发器 游标 表复制 内置函数 预处理
- TensorFlow学习--tensorflow图像预处理
- Xshell学习--菜鸟篇
- Linux如何挂载网络硬盘
- 基于vue2写的上拉加载
- SVN VISUALSVN 排除不想更新的目录或文件 DEBUG
- Python爬虫学习,实战一糗事百科(2017/7/21更新)
- 【Tensorflow】用tersorflow内置函数做图片预处理
- 每天一个linux命令(1):ls命令
- oc统计数组相同元素的个数,以及数组排序
- 任意进制的数值转换 C++版
- Quartz定时任务corn表达式解析
- 2017年澳洲建筑师协会奖入围作品:办公空间
- 学习笔记
- 使用sharedPreferences实现密码登录和注册用户名重复检查
- NOI 2017 机房酱油记