1

For example, I want to cut an image of (1, 1028, 2052, 3) into 16 parts (4 * 4). Now I use the following codes:

for i in range(4):
for j in range(4):
    part = input_image[0:1,i*257:i*257+257,j*513:j*513+513,0:3]
    input_image_split.append(part)
input_image = np.concatenate(input_image_split, axis=0)

or

input_image = np.reshape(input_image, [4, 257, 2052, 3])
input_image = np.transpose(input_image, [0, 2, 1, 3])
input_image = np.reshape(input_image, [16, 513, 257, 3])
input_image = np.transpose(input_image, [0, 2, 1, 3])

but this will cost me about 10ms per image. This happens the same when i use the way before in tensorflow. Is there any way faster can i split an image or an tensor? (tf.split will cost more time)

wonton
  • 11
  • 2
  • Maybe try this: https://stackoverflow.com/questions/4370745/view-onto-a-numpy-array – Azmisov Mar 07 '19 at 03:43
  • I think `part = input_image[0:1,i*257:i*257+257,j*513:j*513+513,0:3]` won't cost many time but np.concatenate will cost a lot. But i need a [16, H/4, W/4, 3] tensor to put into the tensorflow graph. – wonton Mar 07 '19 at 03:48

1 Answers1

0

You could try this:

for i in range(4):
    for j in range(4):
        part = input_image[0][i*257:i*257+257,j*513:j*513+513,0:3]
        input_image_split.append(part)
merged = np.stack(input_image_split);

You could try experimenting with TensorFlow's tf.split and tf.stack methods. Though, off hand I can't say what is going to be fastest here.

Azmisov
  • 6,493
  • 7
  • 53
  • 70