import numpy as np
import tensorflow as tf
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)
else:
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) 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, min_object_covered=0.4)
bbox_begin, bbox_size, _ = tf.image.sample_distorted_bounding_box(tf.shape(image), bounding_boxes=bbox, min_object_covered=0.4)
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:\\TensorFlowGoogle\\201806-github\\datasets\\cat.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]]])
for i in range(9):
result = preprocess_for_train(img_data, 299, 299, boxes)
plt.imshow(result.eval())
plt.show()

吴裕雄--天生自然 pythonTensorFlow图形数据处理:图像预处理完整样例的更多相关文章

  1. 吴裕雄--天生自然 pythonTensorFlow图形数据处理:数据集高层操作

    import tempfile import tensorflow as tf # 1. 列举输入文件. # 输入数据生成的训练和测试数据. train_files = tf.train.match_ ...

  2. 吴裕雄--天生自然 pythonTensorFlow图形数据处理:将MNIST手写图片数据写入TFRecord文件

    import numpy as np import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_dat ...

  3. 吴裕雄--天生自然 pythonTensorFlow图形数据处理:循环神经网络预测正弦函数

    import numpy as np import tensorflow as tf import matplotlib.pyplot as plt # 定义RNN的参数. HIDDEN_SIZE = ...

  4. 吴裕雄--天生自然 pythonTensorFlow图形数据处理:数据集基本使用方法

    import tempfile import tensorflow as tf # 1. 从数组创建数据集. input_data = [1, 2, 3, 5, 8] dataset = tf.dat ...

  5. 吴裕雄--天生自然 pythonTensorFlow图形数据处理:输入数据处理框架

    import tensorflow as tf # 1. 创建文件列表,通过文件列表创建输入文件队列 files = tf.train.match_filenames_once("F:\\o ...

  6. 吴裕雄--天生自然 pythonTensorFlow图形数据处理:输入文件队列

    import tensorflow as tf # 1. 生成文件存储样例数据. def _int64_feature(value): return tf.train.Feature(int64_li ...

  7. 吴裕雄--天生自然 pythonTensorFlow图形数据处理:多线程队列操作

    import tensorflow as tf #1. 定义队列及其操作. queue = tf.FIFOQueue(100,"float") enqueue_op = queue ...

  8. 吴裕雄--天生自然 pythonTensorFlow图形数据处理:队列操作

    import tensorflow as tf #1. 创建队列,并操作里面的元素. q = tf.FIFOQueue(2, "int32") init = q.enqueue_m ...

  9. 吴裕雄--天生自然 pythonTensorFlow图形数据处理:TensorFlow图像处理函数

    import numpy as np import tensorflow as tf import matplotlib.pyplot as plt #读取图片 image_raw_data = tf ...

随机推荐

  1. 7 ~ express ~ body-parser 模块的使用

    一,安装 : npm install body-parser 二,加载 : var bodyParser = require('body-parser') 三,配置 : https://github. ...

  2. Pytorch_torch.nn.MSELoss

    Pytorch_torch.nn.MSELoss 均方损失函数作用主要是求预测实例与真实实例之间的loss loss(xi,yi)=(xi−yi)2 函数需要输入两个tensor,类型统一设置为flo ...

  3. SVM手撕公式

    卓越源于坚持,努力须有方向. 如上图所示,有一堆训练数据的正负样本,标记为:,假设有一个超平面H:,可以把这些样本正确无误地分割开来,同时存在两个平行于H的超平面H1和H2: 使离H最近的正负样本刚好 ...

  4. hiho1482出勤记录II(string类字符串中查找字符串,库函数的应用)

    string类中有很多好用的函数,这里介绍在string类字符串中查找字符串的函数. string类字符串中查找字符串一般可以用: 1.s.find(s1)函数,从前往后查找与目标字符串匹配的第一个位 ...

  5. 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:第一个Spring程序

    1. 创建项目 在 MyEclipse 中创建 Web 项目 springDemo01,将 Spring 框架所需的 JAR 包复制到项目的 lib 目录中,并将添加到类路径下,添加后的项目如图 2. ...

  6. 前端框架vue学习笔记

    占坑

  7. CTF -攻防世界-web高手区-ics-06

    打开网址 根据题意点开报表中心(因为其他的点开都一样,不信你试试) 会看见id =1 想到burp爆破id 所以打开burp抓包(不会抓包的百度 或者看我web新手区,有一题就有抓包 我说的很详细) ...

  8. Linux下yum出现no module named pycurl 解决办法

    1.1 no module named pycurl 解决办法 下载curl:http://curl.haxx.se/download/curl-7.21.3.tar.gz .tar.gz ./con ...

  9. 7206VXR系列板卡简介(转)

    原文链接:https://www.it610.com/article/3116409.htm作者:hjw2011 ATM板卡介绍 板卡PA-A6-OC3MM,PA-A6-OC3SMI,PA-A6-OC ...

  10. SQL基础教程(第2版)第3章 聚合与排序:3-1 对表进行聚合查询

    3-1 对表进行聚合查询 ● 使用聚合函数对表中的列进行计算合计值或者平均值等的汇总操作.● 通常,聚合函数会对NULL以外的对象进行汇总.但是只有COUNT函数例外,使用COUNT(*)可以查出包含 ...