1

For a softmax regression program using tensorflow in python, I want to get my 1000 jpeg image files as a 2D tensor x of: [image index, pixel index]. The "image index" is the image and the pixel index is a specific image pixel for that image. The model equation is:

y = tf.nn.softmax(tf.matmul(x, W) + b)
where:
x = tf.placeholder(tf.float32, [None, image_size])
W = tf.Variable(tf.zeros([image_size, classes]))
b = tf.Variable(tf.zeros([classes]))

image size = height*width of image (constant for all images).

What is the best way in tensorflow to get my image files in that form?

2 Answers 2

1

When I do image processing I like to use either OpenCV (cv2.imread(...)) or Scipy (scipy.ndimage.imread(...)) to read the image files. I also think tensorflow might have its own image reader you can use. These two function return the image as a numpy array. You can specify in the arguments if you want grayscale or color. Now you need to preprocess the images. You may need to convert the datatype (OpenCV uses 8 bit integers rather than float32) and normalize the data. You can also resize at this point if the images are not all the same size.

You can then flatten these numpy arrays to get a flat representation of the images. Just call the flatten() function of the np.ndarray. After you have loaded and flattened the images you want for your batch, string them together in a numpy array np.array([img1, img2, ..., imgN]) and this array will be of shape [images, pixels]. You can then feed this to you x placeholder.

Sign up to request clarification or add additional context in comments.

Comments

0

I would prefer to preprocess every images if it is for training, but for using Tensorflow on-line with a live image stream, I would try the following method that dynamically changes the data in the memory:

any_shape = [the most natural shape according to the data you already have...]
x_unshaped = tf.placeholder(tf.float32, any_shape)
x = tf.reshape(x_unshaped, [-1, image_size])

If your data is already properly ordered in memory, you could try tf.Tensor.set_shape():

The tf.Tensor.set_shape() method updates the static shape of a Tensor object, and it is typically used to provide additional shape information when this cannot be inferred directly. It does not change the dynamic shape of the tensor.

Source: https://www.tensorflow.org/versions/r0.9/api_docs/python/framework.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.