我是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
循环提取图像块,如下所示(此代码可能需要一些调整):