How to pass base64 encoded image to Tensorflow prediction?

前端 未结 3 1508
死守一世寂寞
死守一世寂寞 2020-12-01 22:13

I have a google-cloud-ml model that I can run prediction by passing a 3 dimensional array of float32...

{ \'instances\' [ { \'input\' : \'[ [ [ 0.0 ], [ 0.5 ],

3条回答
  •  栀梦
    栀梦 (楼主)
    2020-12-01 22:44

    If you just want an efficient way to send images to a model (and not necessarily base-64 encode it), I would suggest uploading your images(s) to Google Cloud Storage and then having your model read off GCS. This way, you are not limited by image size and you can take advantage of multi-part, multithreaded, resumable uploads etc. that the GCS API provides.

    TensorFlow's tf.read_file will directly off GCS. Here's an example of a serving input_fn that will do this. Your request to CMLE would send it an image URL (gs://bucket/some/path/to/image.jpg)

    def read_and_preprocess(filename, augment=False):
        # decode the image file starting from the filename
        # end up with pixel values that are in the -1, 1 range
        image_contents = tf.read_file(filename)
        image = tf.image.decode_jpeg(image_contents, channels=NUM_CHANNELS)
        image = tf.image.convert_image_dtype(image, dtype=tf.float32) # 0-1
        image = tf.expand_dims(image, 0) # resize_bilinear needs batches
        image = tf.image.resize_bilinear(image, [HEIGHT, WIDTH], align_corners=False)
        #image = tf.image.per_image_whitening(image)  # useful if mean not important
        image = tf.subtract(image, 0.5)
        image = tf.multiply(image, 2.0) # -1 to 1
        return image
    
    def serving_input_fn():
        inputs = {'imageurl': tf.placeholder(tf.string, shape=())}
        filename = tf.squeeze(inputs['imageurl']) # make it a scalar
        image = read_and_preprocess(filename)
        # make the outer dimension unknown (and not 1)
        image = tf.placeholder_with_default(image, shape=[None, HEIGHT, WIDTH, NUM_CHANNELS])
    
        features = {'image' : image}
        return tf.estimator.export.ServingInputReceiver(features, inputs)
    

    Your training code will train off actual images, just as in rhaertel80's suggestion above. See https://github.com/GoogleCloudPlatform/training-data-analyst/blob/master/courses/machine_learning/deepdive/08_image/flowersmodel/trainer/task.py#L27 for what the training/evaluation input functions would look like.

提交回复
热议问题