简体   繁体   English

Tensorflow-如何将图像分成两半?

[英]Tensorflow - How do I split an image in half?

I want to split an image in to two pieces so that I can process the first piece on GPU #1 and the second piece on GPU #2. 我想将图像分成两部分,以便可以在GPU#1上处理第一部分,在GPU#2上处理第二部分。 Here's the problem, though: I can't seem to split the image in half 不过,这是问题所在:我似乎无法将图像分成两半

with tf.device(my_gpu):

    # Load onto GPU
    filename_queue = tf.train.string_input_producer([image_path], capacity=1)
    reader = tf.WholeFileReader()
    key, value = reader.read(filename_queue)

    # Get image as float32
    image = tf.image.decode_png(value, channels=3)
    image_float32 = tf.image.convert_image_dtype(image, tf.float32)

Now comes the tricky part. 现在是棘手的部分。 How do I split the image in half? 如何将图像分成两半? Here is pseudo code of what I want to do 这是我想做的伪代码

x,y,z = image.shape
half = x / 2

a = half - 3
b = half + 3

first_half = image[:b, :, :]
second_half = image[a:, :, :]

batch1 = tf.stack([first_half])
batch2 = tf.stack([second half])

I've tried to get the image shape using image_float32.get_shape().as_list() , which returns [None,None,3] . 我试图使用image_float32.get_shape().as_list()获得图像形状,该图像返回[None,None,3] I've also tried x=tf.shape(image_float32)[0] , but that returns 我也尝试过x=tf.shape(image_float32)[0] ,但是返回

TypeError: int() argument must be a string or a number, not 'Tensor'

I know about tf.split , but I don't know how to split the image in the way I want in my pseudo code. 我知道tf.split ,但是我不知道如何用伪代码分割图像。 Any ideas? 有任何想法吗?

you can use tf.slice 你可以使用tf.slice

first_half = tf.slice(image, [0, 0, 0], [a, y, z])
second_half = tf.slice(image, [a, 0, 0], [b, y, z])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM