参考书

《TensorFlow:实战Google深度学习框架》(第2版)

以下TensorFlow程序完成了从图像片段截取,到图像大小调整再到图像翻转及色彩调整的整个图像预处理过程。

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# coding=utf-8 """
@author: Li Tian
@contact: 694317828@qq.com
@software: pycharm
@file: figure_deal_test2.py
@time: 2019/1/28 11:39
@desc: 图像预处理完整样例
""" import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt # 给定一张图像,随机调整图像的色彩。因为调整亮度,对比度,饱和度和色相的顺序会影响最后得到的结果。
# 所以可以定义多种不同的顺序。具体使用哪一种顺序可以在训练数据预处理时随机地选择一种。
# 这样可以进一步降低无关因素对模型的影响。
def distort_color(image, color_ordering=0):
if color_ordering == 0:
image = tf.image.random_brightness(image, max_delta=32. / 255.)
image = tf.image.random_saturation(image, lower=0.5, upper=1.5)
image = tf.image.random_hue(image, max_delta=0.2)
image = tf.image.random_contrast(image, lower=0.5, upper=1.5) elif color_ordering == 1:
image = tf.image.random_saturation(image, lower=0.5, upper=1.5)
image = tf.image.random_brightness(image, max_delta=32. / 255.)
image = tf.image.random_contrast(image, lower=0.5, upper=1.5)
image = tf.image.random_hue(image, max_delta=0.2) elif color_ordering == 2:
# 还可以定义其他的排列,但是在这里就不再一一列出了。
# ...
pass return tf.clip_by_value(image, 0.0, 1.0) # 给定一张解码后的图像、目标图像的尺寸以及图像上的标注框,此函数可以对给出的图像进行预处理。
# 这个函数的输入图像是图像识别问题中原始的训练图像,而输出则是深井网络模型的输入层。注意这里
# 只是处理模型的训练数据,对于预测的数据,一般不需要使用随机变换的步骤。。
def preprocess_for_train(image, height, width, bbox):
# 如果没有提供标注框,则认为整个图像就是需要关注的部分。
if bbox is None:
bbox = tf.constant([0.0, 0.0, 1.0, 1.0], dtype=tf.float32, shape=[1, 1, 4]) # 转换图像张量的类型。
if image.dtype != tf.float32:
image = tf.image.convert_image_dtype(image, dtype=tf.float32) # 随机截取图像,减小需要关注的物体大小对图像识别算法的影响。
bbox_begin, bbox_size, _ = tf.image.sample_distorted_bounding_box(tf.shape(image), bounding_boxes=bbox)
distorted_image = tf.slice(image, bbox_begin, bbox_size) # 将随机截取的图像调整为神经网络层输入层的大小。大小调整的算法是随机选择的。
distorted_image = tf.image.resize_images(distorted_image, [height, width], method=np.random.randint(4)) # 随机左右翻转图像。
distorted_image = tf.image.random_flip_left_right(distorted_image) # 使用一种随机的顺序调整图像色彩。
distorted_image = distort_color(distorted_image, np.random.randint(2)) return distorted_image image_raw_data = tf.gfile.FastGFile('F:/Python3Space/figuredata_deal/krystal.jpg', "rb").read()
with tf.Session() as sess:
img_data = tf.image.decode_jpeg(image_raw_data)
boxes = tf.constant([[[0.05, 0.05, 0.9, 0.7], [0.35, 0.47, 0.5, 0.56]]]) # 开始绘图
plt.rcParams['font.sans-serif'] = ['SimHei'] # 步骤一(替换sans-serif字体)
plt.rcParams['axes.unicode_minus'] = False # 步骤二(解决坐标轴负数的负号显示问题)
fig1 = plt.figure(1, (16, 9), dpi=100) # 运行6次获得6种不同的图像。
for i in range(6):
# 将图像的尺寸调整为299*299.
ax = plt.subplot(2, 3, i+1)
ax.set_title('运行第' + str(i+1) + '次的图像')
result = preprocess_for_train(img_data, 299, 299, boxes)
plt.imshow(result.eval()) fig1.subplots_adjust(wspace=0.1)
# plt.tight_layout() plt.savefig('F:/Python3Space/figuredata_deal/图像预处理完整样例.jpg', bbox_inches='tight')
 

运行结果:

TensorFlow图像预处理完整样例的更多相关文章

  1. 吴裕雄 python 神经网络——TensorFlow 图像预处理完整样例

    import numpy as np import tensorflow as tf import matplotlib.pyplot as plt def distort_color(image, ...

  2. 吴裕雄--天生自然 pythonTensorFlow图形数据处理:图像预处理完整样例

    import numpy as np import tensorflow as tf import matplotlib.pyplot as plt #随机调整图片的色彩,定义两种顺序. def di ...

  3. TensorFlow入门之MNIST样例代码分析

    这几天想系统的学习一下TensorFlow,为之后的工作打下一些基础.看了下<TensorFlow:实战Google深度学习框架>这本书,目前个人觉得这本书还是对初学者挺友好的,作者站在初 ...

  4. TensorFlow图像预处理-函数

    更多的基本的API请参看TensorFlow中文社区:http://www.tensorfly.cn/tfdoc/api_docs/python/array_ops.html 下面是实验的代码,可以参 ...

  5. TensorFlow 图像预处理(一) 图像编解码,图像尺寸调整

    from: https://blog.csdn.net/chaipp0607/article/details/73029923 TensorFlow提供了几类图像处理函数,下面介绍图像的编码与解码,图 ...

  6. 『TensorFlow』第九弹_图像预处理_不爱红妆爱武装

    部分代码单独测试: 这里实践了图像大小调整的代码,值得注意的是格式问题: 输入输出图像时一定要使用uint8编码, 但是数据处理过程中TF会自动把编码方式调整为float32,所以输入时没问题,输出时 ...

  7. 通过Canvas及File API缩放并上传图片完整演示样例

    创建一个只管的用户界面,并同意你控制图片的大小.上传到server端的数据,并不须要处理enctype为 multi-part/form-data 的情况.只一个简单的POST表单处理程序就能够了. ...

  8. Nginx完整配置配置样例【官方版】

    我们主要参考nginx官方给出的完整配置的样例: https://www.nginx.com/resources/wiki/start/topics/examples/full/# 完整摘录如下: n ...

  9. Android清理设备内存具体完整演示样例(二)

    版权声明: https://blog.csdn.net/lfdfhl/article/details/27672913 MainActivity例如以下: package cc.c; import j ...

随机推荐

  1. cocoaPods 安装和应用

    一.安装 下载安装CocoaPods需要Ruby环境 1. 检测gem版本 $ gem -v 如果gem版本小于2.6.x,则需要更新gem 2. 更新gem(gem版本高于2.6.x可跳过此步) 检 ...

  2. 获取DOM父元素和子元素

    利用javascript可以遍历DOM树,这篇文章介绍用获取一个DOM元素的所有父节点和获取一个DOM元素的所以子孙节点. 1.获取所有父节点.用递归的方法,用parentNode属性. <!D ...

  3. Cannot instantiate the type AppiumDriver,AppiumDriver升级引发的问题

    转自:http://blog.csdn.net/zhubaitian/article/details/39717889 1. 问题描述和起因 在使用Appium1.7.0及其以下版本的时候,我们可以直 ...

  4. python实现斐波那契查找

    通过在网上找教程解释和看书,总结出一套比较简单易懂的代码实现. 斐波那契查找和二分查找一样,针对的是有序序列,在此前提下: # 先创建一个Fibonacci函数 fib = lambda n: n i ...

  5. VUE 之 组件

    组件是为了解决页面布局的. 什么是单页面? 答:整个页面的切换都是在这个页面上进行变化的,没有页面的刷新. 1.全局组件 1.1全局组件流程:    1.创建全局组件======>创建一个Vue ...

  6. Marking as slave lost.

    Spark on Yarn提交任务时报ClosedChannelException解决方案_服务器应用_Linux公社-Linux系统门户网站 http://www.linuxidc.com/Linu ...

  7. jquery和CSS3带倒影的3D万花筒旋转动画特效效果演示

    <!DOCTYPE html> <html> <head> <title></title> <meta charset='utf-8' ...

  8. ie9浏览器中h标签的嵌套问题

    ie9中 h1-h6 标签中不能够嵌套h1-h6标签,否则往下看吧. 举个栗子: 我们要实现h1下的两个div实现左右分离,很简单吧? 看看html结构及css吧 <!DOCTYPE html& ...

  9. js 中整理(一)

    一. 冒泡与冒泡阻止   var arr={5,0,-56,900,12,9000,-123}; var flag=false; //大的排序次数(arr.length-1) for(var i=0; ...

  10. UVA10480 Sabotage —— 最小割最大流

    题目链接:https://vjudge.net/problem/UVA-10480 题解: 实际就是求最小割集. 1.什么是网络流图的“割”?答:一个边的集合,使得网络流图删除这些边之后,点被分成两部 ...