Tensorflow(3)-image

本文简要介绍一下tensorflow对image的预处理.->官方API

场景:对于训练集,采用SGD训练。每一轮迭代前,对这批图像预处理(随机增强),然后洗牌,开始训练。这个操作,图像预处理可以并行的放在cpu上计算,因此对模型不会带来太多的额外开销。

1)tf.random_crop()
parameters:
image: 3-D [height, width, channels]
size: [height, width]
seed
name

随机一个offset,然后按照size进行裁剪。offset满足均值分布。

2)tf.image.random_flip_left_right()
parameters:
image
seed

1/2的概率水平翻转图片。

3)tf.image.random_brightness()
parameters:
image
max_delta: float, 非负
seed

随机调整图片亮度。随机取一个值[-max_delta, max_delta] 然后加到整个图像上。

4)tf.image.random_contrast()
parameters:
image
lower: float
upper: float
seed

随机调整图片对比度。contrast_factor 随机取自 [lower, upper]
(x-mean) * contrast_factor + mean

Tensorflow实际使用中需要通过session

import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
from scipy.misc import imread

IMAGE_SIZE = 200

img = imread('/cat.jpg')

reshaped_image = tf.cast(img, tf.float32)
distorted_image = tf.random_crop(reshaped_image, [IMAGE_SIZE,IMAGE_SIZE,3])
distorted_image = tf.image.random_flip_left_right(distorted_image)
distorted_image = tf.image.random_brightness(distorted_image,
                                               max_delta=63)
distorted_image = tf.image.random_contrast(distorted_image,
                                             lower=0.2, upper=1.8)

rtval = tf.cast(distorted_image, tf.uint8)

with tf.Session() as sess:

    plt.imshow(sess.run(rtval))
    plt.show()

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值