图像旋转等预处理tensorflow

    图像预处理是一个非常简单,通过提高训练数据的多样性,进而对训练模型的召回率,适应性有着非常大的提升作用。

另外在训练时,需要更多的训练次数,比如说我对每张图片进行了一次旋转,那么训练次数就要提高一倍。也就是说训练集多样性增加,同时训练次数也要增加。

 

代码:

import tensorflow as tf
from scipy import misc
import numpy as np
 
from skimage import transform,data
#随机旋转图片
def random_rotate_image(image_file, num):
    with tf.Graph().as_default():
        tf.set_random_seed(666)
        file_contents = tf.read_file(image_file)
        image = tf.image.decode_image(file_contents, channels=3)
        image_rotate_en_list = []
        def random_rotate_image_func(image):
            #旋转角度范围
            angle = np.random.uniform(low=-30.0, high=30.0)
            #return misc.imrotate(image, angle, 'bicubic')
            return transform.rotate(image, angle, resize=True)
        for i in range(num):
            image_rotate = tf.py_func(random_rotate_image_func, [image], tf.uint8)
            image_rotate_en_list.append(tf.image.encode_png(image_rotate))
        with tf.Session() as sess:
            sess.run(tf.global_variables_initializer())
            sess.run(tf.local_variables_initializer())
            results = sess.run(image_rotate_en_list)
            for idx,re in enumerate(results):
                with open('data2/'+str(idx)+'.png','wb') as f:
                    f.write(re)
 
#随机左右翻转图片
def random_flip_image(image_file, num):
    with tf.Graph().as_default():
        tf.set_random_seed(666)
        file_contents = tf.read_file(image_file)
        image = tf.image.decode_image(file_contents, channels=3)
        image_flip_en_list = []
        for i in range(num):
            image_flip = tf.image.random_flip_left_right(image)
            image_flip_en_list.append(tf.image.encode_png(image_flip))
        with tf.Session() as sess:
            sess.run(tf.global_variables_initializer())
            sess.run(tf.local_variables_initializer())
            results = sess.run(image_flip_en_list)
            for idx,re in enumerate(results):
                with open('data1/'+str(idx)+'.png','wb') as f:
                    f.write(re)
 
#随机变化图片亮度
def random_brightness_image(image_file, num):
    with tf.Graph().as_default():
        tf.set_random_seed(666)
        file_contents = tf.read_file(image_file)
        image = tf.image.decode_image(file_contents, channels=3)
        image_bright_en_list = []
        for i in range(num):
            image_bright = tf.image.random_brightness(image, max_delta=0.8)
            image_bright_en_list.append(tf.image.encode_png(image_bright))
        with tf.Session() as sess:
            sess.run(tf.global_variables_initializer())
            sess.run(tf.local_variables_initializer())
            results = sess.run(image_bright_en_list)
            for idx,re in enumerate(results):
                with open('data/'+str(idx)+'.png','wb') as f:
                    f.write(re)
 
#随机裁剪图片
def random_crop_image(image_file, num):
    with tf.Graph().as_default():
        tf.set_random_seed(666)
        file_contents = tf.read_file(image_file)
        image = tf.image.decode_image(file_contents, channels=3)
        image_crop_en_list = []
        for i in range(num):
            #裁剪后图片分辨率保持160x160,3通道
            image_crop = tf.random_crop(image, [300, 400, 3])
            image_crop_en_list.append(tf.image.encode_png(image_crop))
        with tf.Session() as sess:
            sess.run(tf.global_variables_initializer())
            sess.run(tf.local_variables_initializer())
            results = sess.run(image_crop_en_list)
            for idx,re in enumerate(results):
                with open('data/'+str(idx)+'.png','wb') as f:
                    f.write(re)
 
if __name__ == '__main__':
    #处理图片,进行20次随机处理,并将处理后的图片保存到输入图片相同的路径下
    #random_brightness_image('data/test.jpeg', 20)
	random_crop_image('test.jpeg', 20)
	random_flip_image('test.jpeg', 30)
	random_rotate_image('test.jpeg', 30)

随机旋转不行,查了下,有的说要在python环境中安装Pillow即可,安装了不行,有的说要降低scipy版本,

我不想降低版本。https://docs.scipy.org/doc/scipy-1.1.0/reference/generated/scipy.misc.imrotate.html#scipy.misc.imrotate

中提到:imrotate is deprecated! imrotate is deprecated in SciPy 1.0.0, and will be removed in 1.2.0. Use skimage.transform.rotate instead.

发现不行,搜索tensorflow旋转图片。

https://codeday.me/bug/20180831/241170.html中提到:

Image.Image.rotate

 

这样下面是代码:

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


#随机旋转图片
def random_rotate_image(image_file, num):
    with tf.Graph().as_default():
        tf.set_random_seed(666)
        file_contents = tf.read_file(image_file)
        image = tf.image.decode_image(file_contents, channels=3)
        with tf.Session() as sess:
            sess.run(tf.global_variables_initializer())
            sess.run(tf.local_variables_initializer())
            image = Image.fromarray(image.eval())
            j=0
            for i in range(num):
                angle = np.random.uniform(low=-80.0, high=90.0)
                h=Image.Image.rotate(image, angle)
                # plt.imshow(h)
                # plt.show()
                # 使用matplotlib写入图像
                j=j+1
                h.save('data2/%s.png'%j)
         
if __name__ == '__main__':
    #处理图片,进行20次随机处理,并将处理后的图片保存到输入图片相同的路径下
	random_rotate_image('test.jpeg', 20)

TensorFlow 2 中,可以使用 `tf.data` 模块进行数据预处理。`tf.data` 提供了一系列用于创建高效数据输入管道的工具。下面是一个基本的数据预处理流程: 1. 加载数据:首先,你需要加载你的原始数据。这可以是来自文件、数据库或其他源的数据。根据你的数据格式,你可能需要使用不同的函数来加载数据,如 `tf.data.TextLineDataset`、`tf.data.TFRecordDataset` 等。 2. 数据转换:一旦你加载了数据,你可以使用 TensorFlow 的各种操作来转换数据。例如,你可以使用 `map()` 函数应用任何自定义的数据转换函数,也可以使用 `batch()` 函数对数据进行批处理,使用 `shuffle()` 函数对数据进行随机化等。 3. 预处理函数:如果需要进行特定的数据预处理,你可以编写一个预处理函数,并将其应用到数据上。例如,你可以编写一个函数来对图像进行缩放、裁剪或旋转。 4. 数据增强:如果你的数据集相对较小,你可以使用数据增强技术来增加样本的多样性。例如,在图像处理中,你可以应用随机裁剪、旋转、翻转等操作来生成更多的训练样本。 5. 打包成可用的格式:最后,将预处理后的数据打包成可用于训练或评估模型的格式。你可以使用 `tf.data.Dataset` 对象来表示你的数据集,并将其传递给模型进行训练或评估。 注意,以上只是一个基本的数据预处理流程示例,具体的步骤和操作可能因你的数据类型和需求而有所不同。你可以根据实际情况进行调整和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值