Add metadata to tensorflow serving api call

Viewed 242

Is it possible to add metadata to a tensorflow serving servable, such that this metadata is also populated in the response from the servable?

If I have a servable with the file structure:

my_servable/ 
           1541778457/ 
                     variables/ 
                     saved_model.pb 

For example:

```
outputs {
  key: "classes"
  value {
    dtype: DT_STRING
    tensor_shape {
      dim {
        size: 8
      }
    }
    string_val: "a"
    string_val: "b"
    string_val: "c"
    string_val: "d"
    string_val: "e"
    string_val: "f"
    string_val: "g"
    string_val: "h"
  }
}
outputs {
  key: "scores"
  value {
    dtype: DT_FLOAT
    tensor_shape {
      dim {
        size: 1
      }
      dim {
        size: 8
      }
    }
    float_val: 1.212528104588273e-06
    float_val: 5.094948463124638e-08
    float_val: 0.0009737954242154956
    float_val: 0.9988483190536499
    float_val: 3.245145592245535e-07
    float_val: 0.00010837535955943167
    float_val: 4.101086960872635e-05
    float_val: 2.676981057447847e-05
  }
}
model_spec {
  name: "my_model"
  version {
    value: 1541778457
  }
  signature_name: "prediction"
}

If I have something like a git hash or unique identifier for the code that generated this servable like f6ca434910504532a0d50dfd12f22d4c, is it possible to get this data in the client request?

Ideally something like:

```
outputs {
  key: "classes"
  value {
    dtype: DT_STRING
    tensor_shape {
      dim {
        size: 8
      }
    }
    string_val: "a"
    string_val: "b"
    string_val: "c"
    string_val: "d"
    string_val: "e"
    string_val: "f"
    string_val: "g"
    string_val: "h"
  }
}
outputs {
  key: "scores"
  value {
    dtype: DT_FLOAT
    tensor_shape {
      dim {
        size: 1
      }
      dim {
        size: 8
      }
    }
    float_val: 1.212528104588273e-06
    float_val: 5.094948463124638e-08
    float_val: 0.0009737954242154956
    float_val: 0.9988483190536499
    float_val: 3.245145592245535e-07
    float_val: 0.00010837535955943167
    float_val: 4.101086960872635e-05
    float_val: 2.676981057447847e-05
  }
}
model_spec {
  name: "my_model"
  version {
    value: 1541778457
  }
  hash {
    value: f6ca434910504532a0d50dfd12f22d4c
 }
  signature_name: "prediction"
}

I tried changing the directory from 1541778457 to the hash, but this gave:

W tensorflow_serving/sources/storage_path/file_system_storage_path_source.cc:268] No versions of servable default found under base path

1 Answers

I suppose you could approach this problem in a couple of ways. If you want the idea to change the folder name to work, remember that folder name in this case describe your model version which I think must be an integer. I would therefore assume that you would need to convert your hash to either binary or decimal and then convert it back when you receive it.

A better solution in my opinion would be if you were able to change your model and add a variable containing your hash. And add it to the models signature_def. In python that would look something like:

// create your field
hash = tf.placeholder("f6ca434910504532a0d50dfd12f22d4c",tf.string, name="HASH")

// build tensor
hash_info = tf.saved_model.utils.build_tensor_info(hash)

// add hash_info in your output in signature_def

// then you should be able to receive that data in your request
Related