gpt4 book ai didi

python - Tensorflow:动态地将图像分割成 block

转载 作者:行者123 更新时间:2023-11-30 08:31:38 25 4
gpt4 key购买 nike

我是 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() code>) 返回 (None, None, None)

要使图表中的所有内容都动态化,您可以使用 tf.while_loop()而不是当前的 for 循环来提取补丁,如下所示(此代码可能需要一些调整):

import tensorflow as tf

filenames = tf.constant(['im_01.png', 'im_02.png', 'im_03.png', 'im_04.png'])
labels = tf.constant([0, 1, 0, 1])
patch_size = [128, 128]

def parse_fn(file, label):
image = tf.image.decode_png(tf.read_file(file), channels=1)

shape= tf.shape(image)
image_width = shape[1]
image_height = shape[0]
num_pieces = tf.div(image_width, image_height)

def tf_while_condition(index, outputs):
# We loop over the number of pieces:
return tf.less(index, num_pieces)

def tf_while_body(index, outputs):
# We get the image patch for the given index:
offset_width = index * 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, patch_size)
sub_image = tf.expand_dims(sub_image, 0)
# We add it to the output patches (may be a cleaner way to do so):
outputs = tf.concat([outputs[:index], sub_image, outputs[index + 1:]], axis=0)
# We increment our index and return the values:
index = tf.add(index, 1)
return index, outputs

# We build our patches tensor which will be filled with the loop:
patches = tf.zeros((num_pieces, *patch_size, shape[2]), dtype=tf.float32)
_, patches = tf.while_loop(tf_while_condition, tf_while_body, [0, patches])

# We tile the label to have one per patch:
patches_labels = tf.tile(tf.expand_dims(label, 0), [num_pieces])

return tf.data.Dataset.from_tensor_slices((patches, patches_labels))

dataset = tf.data.Dataset.from_tensor_slices((filenames, labels))
dataset = dataset.flat_map(parse_fn)

sess = tf.InteractiveSession()
it = dataset.make_one_shot_iterator()
op = it.get_next()
while True:
res = sess.run(op)
print(res)

关于python - Tensorflow:动态地将图像分割成 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50176515/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com