Tensorflow Serving With JPEG or PNG Images

1,819 views
Skip to first unread message

rob...@perchinteractive.com

unread,
Mar 28, 2016, 4:17:25 PM3/28/16
to Discuss
I'm going through the Tensorflow Serving example (https://tensorflow.github.io/serving/serving_basic) and trying to figure out how to modify the code to accept JPEG files instead of MNST data, any ideas how to do this? Any examples available?

Here's how I trained my model in python:

  filenames_and_labels = []


 
for i in xrange(start_image_number, end_image_number):
    file_name
= os.path.join(data_dir, 'image%d.jpg' % i)
    label
= labels[i - 1]
    filenames_and_labels
.append(file_name + "," + str(label))


 
print('Reading filenames for ' + str(len(filenames_and_labels)) + ' files (from ' + str(start_image_number) + ' to ' + str(end_image_number) + ')')


 
for filename_and_label in filenames_and_labels:
    array
= filename_and_label.split(",")
    f
= array[0]
   
# print(array)
   
if not tf.gfile.Exists(f):
     
raise ValueError('Failed to find file: ' + f)


 
# Create a queue that produces the filenames to read.
  filename_and_label_queue
= tf.train.string_input_producer(filenames_and_labels)


  filename_and_label_tensor
= filename_and_label_queue.dequeue()
  filename
, label = tf.decode_csv(filename_and_label_tensor, [[""], [""]], ",")
  file_contents
= tf.read_file(filename)
  image
= tf.image.decode_jpeg(file_contents)


  reshaped_image
= tf.cast(image, tf.float32)
  reshaped_image
.set_shape([ORIGINAL_IMAGE_SIZE, ORIGINAL_IMAGE_SIZE, 3])




  height
= IMAGE_SIZE
  width
= IMAGE_SIZE


 
# Image processing for evaluation.
 
# Crop the central [height, width] of the image.
  resized_image
= tf.image.resize_image_with_crop_or_pad(reshaped_image,
                                                         width
, height)


 
# Subtract off the mean and divide by the variance of the pixels.
  float_image
= tf.image.per_image_whitening(resized_image)


 
# Ensure that the random shuffling has good mixing properties.
  min_fraction_of_examples_in_queue
= 0.4
  min_queue_examples
= int(num_examples_per_epoch *
                           min_fraction_of_examples_in_queue
)


 
# Generate a batch of images and labels by building up a queue of examples.
 
return _generate_image_and_label_batch(float_image, tf.to_int64(tf.string_to_number(label)),
                                         min_queue_examples
, batch_size)

Saif Ahmed

unread,
Mar 28, 2016, 4:50:08 PM3/28/16
to rob...@perchinteractive.com, Discuss
Dear Robert,

I suggest using the ndimage library:
   from scipy import ndimage

You can then achieve this in a single line.  I load jpegs for batches as shown below.  This is for B&W images, you may wish to change the flatten=True parameter if you are using colour images.

  for image in os.listdir(folder):
    image_file = os.path.join(folder, image)
    try:
      image_data = (ndimage.imread(image_file, flatten=True).astype(float) - pixel_depth / 2) / pixel_depth
      if image_data.shape != (image_size, image_size):
        raise Exception('Unexpected image shape: %s' % str(image_data.shape))
      dataset[image_index, :, :] = image_data
      image_index += 1
    except IOError as e:
      logger.warning('Could not read:' +  image_file + ':' + str(e) + '...whatever, we have plenty others')

I'll be posting some code on GitHub in the coming weeks with some good examples on smart-batching, etc, but until then, the above will suffice.

Cheers,
Saif Ahmed


--
You received this message because you are subscribed to the Google Groups "Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to discuss+u...@tensorflow.org.
To post to this group, send email to dis...@tensorflow.org.
To view this discussion on the web visit https://groups.google.com/a/tensorflow.org/d/msgid/discuss/c5e9739d-b3fe-45df-9a99-d7c53623ae7f%40tensorflow.org.

Robert Grzesik

unread,
Mar 28, 2016, 4:53:25 PM3/28/16
to Saif Ahmed, Discuss
Which file are you modifying here? Clearly it's written in python, I thought the server was written in C++.

These are all the files in the example I'm referring to: https://github.com/tensorflow/serving/tree/master/tensorflow_serving/example

Saif Ahmed

unread,
Mar 28, 2016, 5:04:00 PM3/28/16
to Robert Grzesik, Discuss
The problem with sourcing from http://yann.lecun.com/exdb/mnist/ is that it starts with an intermediate format.  I removed intermediates and read straight from JPEG files, which is more reflective of applied usage.  Sorry, cant opine on C++ examples or that side of things.




rob...@perchinteractive.com

unread,
Mar 28, 2016, 5:12:16 PM3/28/16
to Discuss, rob...@perchinteractive.com
This is my first time hearing about serving with Python, it would definitely be much easier to use Python rather than C++. Is there a tutorial anywhere that exaplins how to set up Tensorflow serving using Python? Where did you start to set this up?

Kiril Gorovoy

unread,
Mar 28, 2016, 5:26:46 PM3/28/16
to Discuss, rob...@perchinteractive.com
You can take a look at the inception example for an example using jpeg requests: https://github.com/tensorflow/serving/blob/master/tensorflow_serving/example/inception_inference.cc#L17

Saif Ahmed

unread,
Mar 28, 2016, 5:27:21 PM3/28/16
to Robert Grzesik, Discuss
I'm in the process of setting it up right now, so not sure how/if it all works.  I figured https://github.com/tensorflow/serving/tree/master/tensorflow_serving/example would be a natural place to start given the commit comment "Add end-to-end example and tutorial for serving InceptionV3 model in …"

I started with the Python piece, as it is a natural progression from general TensorFlow usage, but I don't have it serving yet.  However, I did manage to excise out the intermediate ubyte files and seed everything with JPEGs, which is why I chimed in when you asked. 

I would be wonderful if one of the authors (kirilg?) could help reconcile the basic guide https://github.com/tensorflow/serving/blob/master/tensorflow_serving/g3doc/serving_basic.md against the end-to-end example and explain how the components work together.



rmallen...@gmail.com

unread,
Aug 28, 2018, 3:53:33 PM8/28/18
to Discuss, rob...@perchinteractive.com
Here's an example on how to do Serving for mnist, feeding a JPG or PNG image...I don't know if this is exactly what you need, but it might be helpful for you or someone else.



Rodrigo
Reply all
Reply to author
Forward
0 new messages