bhomass
bhomass

Reputation: 3572

tensorflow slice with negative size

In this tutorial http://learningtensorflow.com/lesson4/ Its calling the tensorflow slice method on some image data raw_image_data = mpimg.imread(filename)

image = tf.placeholder("uint8", [None, None, 3])
slice = tf.slice(image, [1000, 0, 0], [3000, -1, -1])

from the slice definition tf.slice(input_, begin, size, name=None)

I take it means start with pixel 1000,0 R layer (out of rgb), take a slice of size 3000, -1, and minus layer by -1. I am lost on how you can go -1 from 0 for both the second image dimension and the rgb layer.

Any knows?

Upvotes: 0

Views: 1494

Answers (2)

David Wong
David Wong

Reputation: 748

-1 here is equivalent to "*" or "take all in that dimension", i.e. per documentation "if size[i] is -1, all remaining elements in dimension i are included in the slice."

In other words, if

x = [[1,2,3],[4,5,6]]

Then

tf.slice(x, [0, 0], [1, -1])

would return

[1 2 3]

Upvotes: 3

nessuno
nessuno

Reputation: 27042

-1 is a special value in tensor's size definitions.

It doesn't mean -1 but it means everything.

At the line

image = tf.placeholder("uint8", [None, None, 3])

You're defining a placeholder with shape (?, ?, 3).

In the following line:

slice = tf.slice(image, [1000, 0, 0], [3000, -1, -1])

Your're defining the slice operation. This operation means:

Extract a slice from image whose shape is(?, ?, 3).

Starting from position (1000, 0, 0) extract a slice with shape (3000, ?, 3).

The 3000 means that the returned slice will have 3000 elements, extracted from image, after position 1000 in dimension 0.

The ? means that Tensorflow will determine at run time the value (is not possibile due to image undefined shape).

3 is inferred by image. Every element of the extract slice will have a depth of 3.

Upvotes: 3

Related Questions