Tensorflow: 动态分割图像成块

我是Tensorflow的新手,需要帮助管理我的图像数据集。由于图像集非常大,读取和训练阶段需要重叠。每个图像的宽度像素远大于高度。我需要将宽图像分割成正方形部分。由于图像的宽度不同,分割的部分数量也会变化。

以下是我的示例代码,我需要帮助使其工作:

def main(unused_argv):   filenames = tf.constant(['im_01.png', 'im_02.png', 'im_03.png', 'im_04.png'])   labels = tf.constant([0, 1, 0, 1])   def parse_fn(file, label):      image = tf.image.decode_png(tf.read_file(file), channels=1)       shape_list = image.get_shape().as_list()       image_width = shape_list[0]       image_height = shape_list[1]       num_pieces = int(image_width/image_height)       Xy_pairs = []       for i in range(num_pieces):          offset_width = i * image_height          sub_image = tf.image.crop_to_bounding_box(image, 0, offset_width, image_height, image_height)          sub_image = tf.image.resize_images(sub_image, [128, 128])          Xy_pairs.append((sub_image, label))       return Dataset.from_tensor_slices(Xy_pairs)dataset = tf.data.Dataset.from_tensor_slices((filenames, labels))dataset = dataset.flat_map(parse_fn)sess = tf.InteractiveSession()it = dataset.make_one_shot_iterator()while True:    print('OUT: ' + it.get_next().eval())

错误看起来像这样 TypeError: unsupported operand type(s) for /: 'NoneType' and 'NoneType',因为Tensorflow在计算数据流图时不知道图像的大小。我几乎可以肯定我需要为image.get_shape()设置一个占位符。我希望社区能帮我解决这个问题。


回答:

正如评论中指出的,您的image形状在图中未定义,因此在评估之前尝试获取值(通过image.get_shape().as_list())会返回(None, None, None)

为了使图中的一切都动态,您可以使用tf.while_loop()来代替当前的for循环提取图像块,如下所示(此代码可能需要一些调整):

Related Posts

Keras Dense层输入未被展平

这是我的测试代码: from keras import…

无法将分类变量输入随机森林

我有10个分类变量和3个数值变量。我在分割后直接将它们…

如何在Keras中对每个输出应用Sigmoid函数?

这是我代码的一部分。 model = Sequenti…

如何选择类概率的最佳阈值?

我的神经网络输出是一个用于多标签分类的预测类概率表: …

在Keras中使用深度学习得到不同的结果

我按照一个教程使用Keras中的深度神经网络进行文本分…

‘MatMul’操作的输入’b’类型为float32,与参数’a’的类型float64不匹配

我写了一个简单的TensorFlow代码,但不断遇到T…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注