具有不同大小图像的 Tensorflow 输入数据集 [英] Tensorflow input dataset with varying size images

查看:53
本文介绍了具有不同大小图像的 Tensorflow 输入数据集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用不同大小的输入图像来训练一个完全卷积的神经网络.我可以通过循环训练图像并在每次迭代时创建一个 numpy 输入来做到这一点,即

I'm trying to train a fully convolutional neural network using input images with different sizes. I can do this by looping over the training images and creating a single numpy input at each iteration i.e.,

for image_input, label in zip(image_data, labels):
    train_input_fn = tf.estimator.inputs.numpy_input_fn(
                                         x= {"x":image_input},
                                         y=label,
                                         batch_size=1, 
                                         num_epochs=None,
                                         shuffle=False)
    fcn_classifier.train(input_fn=input_func_gen, steps=1)

然而,这种方式在每一步之后都会保存和加载模型,浪费了大量资源.我还尝试使用生成器一次创建整个数据集,即

However, in this way the model is saved and loaded after each step wasting huge amount of resources. I have also tried creating the whole dataset at once using generators i.e.,

def input_func_gen():
    dataset = tf.data.Dataset.from_generator(generator=generator, 
                                  output_types=(tf.float32, tf.int32))
    dataset = dataset.batch(1)
    iterator = dataset.make_one_shot_iterator()
    return iterator.get_next()

def generator():
    filenames = ['building-d-mapimage-10-gt.png', 'building-dmapimage- 
                                                   16-gt.png']
    i = 0
    while i < len(filenames):        
        features, labels = loading.read_image_data(filenames[i])
        yield features, labels
        i += 1
        if i >= len(filenames):
            i = 0

然后

 fcn_classifier.train(input_fn=input_func_gen,
                      steps=100)   

但是,通过这种方式,训练变得非常缓慢并且在第一次迭代后内存不足,这表明数据集有问题(在第一种情况下,训练运行必须更快,使用单个输入).生成器中特征的形状也是 (1, image_height, image_width,3) .但是在模型中,我必须将它们重塑为 4-d 张量

However, in this way the training becomes very slow and runs out of memory after first iteration, which indicates that there is something wrong with the dataset (the training runs must faster in the first case were single inputs are used). Also the shape of the features in generator are (1, image_height, image_width,3) . However in the model I have to reshape them to 4-d tensors as

input_shape = tf.shape(input)
input = tf.reshape(input, [1, input_shape[2], input_shape[3], 3])

而不是 tf.reshape(input, [1, input_shape[1], input_shape[2], 3]) ,这表明输入的维度有些奇怪?在第一种情况下,我可以直接使用输入而无需重塑或任何东西?

instead of tf.reshape(input, [1, input_shape[1], input_shape[2], 3]) , which indicates that there is something weird with the dimensions of the input? In the first case I can just use the input directly without need to reshape or anything?

推荐答案

我设法通过将 input_func_gen 更改为以下内容来解决不同大小图像的问题

I manage to solve the problem with varying size images by changing the input_func_gen to following

def input_func_gen():
    load_path = '/path_to_images'
    data_set = 'dataset_to_use'
    image_data, labels = loading.load_image_data_grayscale(load_path,data_set)
    dataset = tf.data.Dataset.from_generator(lambda: 
                              itertools.zip_longest(image_data, labels),
                              output_types=(tf.float32, tf.int32),
                              output_shapes=(tf.TensorShape([1, None, None, 
                                             3]), tf.TensorShape([1, None])))
    dataset = dataset.repeat()
    iterator = dataset.make_one_shot_iterator()
    return iterator.get_next()

这篇关于具有不同大小图像的 Tensorflow 输入数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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