How to delete a model details from custom model config file used to load multiple models in tensorflow serving?

Viewed 516

How to delete a model details from custom model config file, which is used to load multiple models in tensorflow serving?

I have impemented the code to add the tensorflow models in runtime to the tensorflow serving config file using above thread : TensorFlow Serving: Update model_config (add additional models) at runtime

I have no clue how to delete any model from this config file using python as this config file has .conf extension and its in string format.

This config file looks like this :

model_config_list: {
  config: {
    name:  "first",
    base_path:  "/models/first",
    model_platform: "tensorflow",
    }
  },
  config: {
    name:  "second",
    base_path:  "/models/second",
    model_platform: "tensorflow",
  }
}

Now how to delete the model named "first" from the above file during runtime using python code?

input :

model_config_list: {
  config: {
    name:  "first",
    base_path:  "/models/first",
    model_platform: "tensorflow",
    }
  }
  config: {
    name:  "second",
    base_path:  "/models/second",
    model_platform: "tensorflow",
  }
}

some python code using grpc or google.protobuf, tensorflow_serving.apis, tensorflow_serving.config, to delete model named "first"

output :

model_config_list: {
  config: {
    name:  "second",
    base_path:  "/models/second",
    model_platform: "tensorflow",
  }
}
1 Answers

I have found out the answer for my question. The file models.conf is in protobuf data format but its type is string. To edit the it, use google python package. first, i converted this file into json format and then removed the particular model details and then again converted back to protobuf format.

1. read models.conf file

with open('models.conf', 'r+') as f:
    config_ini = f.read()

2.convert it to the json format

from tensorflow_serving.config import model_server_config_pb2
from google.protobuf import text_format, json_format
import json
name='first'

model_server_config = model_server_config_pb2.ModelServerConfig()
model_server_config = text_format.Parse(text=config_ini, message=model_server_config)
proto_to_json = json_format.MessageToJson(model_server_config)
proto_to_json = json.loads(proto_to_json)

3.delete the required model:

proto_to_json['modelConfigList']['config'] = [dic for dic in proto_to_json['modelConfigList']['config'] if name!= dic['name']]

4.convert json back to protobuf string:

json_to_proto = json_format.Parse(json.dumps(proto_to_json), message=model_server_config ,ignore_unknown_fields=False)

Please refer this documentation: https://developers.google.com/protocol-buffers/docs/reference/python/

Related