张量流中的剪切图像 [英] Shearing image in tensorflow

查看:71
本文介绍了张量流中的剪切图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用tf.keras建立我的网络.由于我的数据在tfrecords文件中,因此我正在tensor_wise级别进行所有扩充.然后,我需要进行剪切 zca 进行增强,但是在张量流中找不到合适的实现.而且我不能使用同时执行我需要的操作的DataImageGenerator,因为正如我说的那样,我的数据不适合内存,并且是tfrecord格式.因此,我所有的扩充过程都应该是向北移动的.

I am using tf.keras to build my network. And I am doing all the augmentation in tensor_wise level since my data in tfrecords file. Then I needed to do shearing and zca for augmentation but couldn't find a proper implementation in tensor flow. And I can't use the DataImageGenerator that did both operation I needed because as I said my data doesn't fit in memory and it is in tfrecord format. So all my augmentations process should be tesnorwise.

@fchollet 此处提出了一种使用ImgaeDataGenerator数据集.

@fchollet here suggested a way to use ImgaeDataGenerator with large dataset.

我的第一个questino是 如果我使用@fchollet方式(基本上是使用大数据的X-sample来运行ImageDataGenerator),然后使用train_on_batch来训练网络,则如何将验证数据馈送到网络.

My first questino is if I use @fchollet way, which is basically using X-sample of the large data to run the ImageDataGenerator then using train_on_batch to train the network , how I can feed my validation data to the network.

我的第二个问题是剪切 zca 操作是否有任何张量实施.有些人喜欢此处建议使用tf.contrib.image.transform,但不知道如何使用.如果有人对如何实现有想法,我将不胜感激.

My Second question is there any tensor-wise implementation for shear and zca operations. Some people like here suggested using tf.contrib.image.transform but couldn't understand how. If some one have the idea on how to do it, I will appreciate that.

更新:

这是我尝试通过ski_image构造转换矩阵的尝试

This is my trial to construct the transformation matrix through ski_image

from skimage import io
from skimage import transform as trans
import tensor flow as tf 

def augment()
  afine_tf = trans.AffineTransform(shear=0.2)
  transform = tf.contrib.image.matrices_to_flat_transforms(tf.linalg.inv(afine_tf.params))
  transform= tf.cast(transform, tf.float32)
  image = tf.contrib.image.transform(image, transform)  # Image here is a tensor 
  return image


dataset_train = tf.data.TFRecordDataset(training_files, num_parallel_reads=calls)

dataset_train = dataset_train.apply(tf.contrib.data.shuffle_and_repeat(buffer_size=1000+ 4 * batch_size))
dataset_train = dataset_train.map(decode_train, num_parallel_calls= calls)  
dataset_train = dataset_train.map(augment,num_parallel_calls=calls )  
dataset_train = dataset_train.batch(batch_size)    
dataset_train = dataset_train.prefetch(tf.contrib.data.AUTOTUNE)

推荐答案

我将回答第二个问题.

I will answer the second question.

今天,我的一个老问题被用户评论,但是当我添加有关如何使用tf.contrib.image.transform的更多详细信息时,这些评论已被删除.我想是你,对吧?

Today one of my old questions was commented by a user, but the comments have been deleted when I was adding more details on how to use tf.contrib.image.transform. I guess it's you, right?

因此,我已经编辑了问题并添加了示例,请检查它

So, I have edited my question and added an example, check it here.

TL; DR:

def transformImg(imgIn,forward_transform):
    t = tf.contrib.image.matrices_to_flat_transforms(tf.linalg.inv(forward_transform))
    # please notice that forward_transform must be a float matrix,
    # e.g. [[2.0,0,0],[0,1.0,0],[0,0,1]] will work
    # but [[2,0,0],[0,1,0],[0,0,1]] will not
    imgOut = tf.contrib.image.transform(imgIn, t, interpolation="BILINEAR",name=None)
    return imgOut
def shear_transform_example(filename,shear_lambda):
    image_string = tf.read_file(filename)
    image_decoded = tf.image.decode_jpeg(image_string, channels=3)
    img = transformImg(image_decoded, [[1.0,shear_lambda,0],[0,1.0,0],[0,0,1.0]])
    # Notice that this is a shear transformation parallel to the x axis
    # If you want a y axis version, use this:
    # img = transformImg(image_decoded, [[1.0,0,0],[shear_lambda,1.0,0],[0,0,1.0]])
    return img
img = shear_transform_example("white_square.jpg",0.1)

这篇关于张量流中的剪切图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆