AnandInguva commented on code in PR #23456: URL: https://github.com/apache/beam/pull/23456#discussion_r1001106061
########## sdks/python/apache_beam/examples/inference/tfx_bsl/build_tensorflow_model.py: ########## @@ -0,0 +1,71 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +import tensorflow as tf + + +class TFModelWrapperWithSignature(tf.keras.Model): + def __init__( + self, + model, + preprocess_input=None, + input_dtype=tf.float32, + feature_description=None): + super().__init__() + self.model = model + self.preprocess_input = preprocess_input + self.input_dtype = input_dtype + self.feature_description = feature_description + if not feature_description: + self.feature_description = {'image': tf.io.FixedLenFeature((), tf.string)} + + @tf.function(input_signature=[tf.TensorSpec(shape=[None], dtype=tf.string)]) + def call(self, serialized_examples): + features = tf.io.parse_example( + serialized_examples, features=self.feature_description) + + # using TensorArray as suggested at + # https://github.com/tensorflow/tensorflow/issues/39323#issuecomment-627586602 + batch = len(features['image']) + deserialized_vectors = tf.TensorArray( + self.input_dtype, size=batch, dynamic_size=True) + # issue arise with the indexing at features['image'] + # Vectorized version of tf.io.parse_tensor is not available + # https://github.com/tensorflow/tensorflow/issues/43706 + for i in range(batch): + deserialized_value = tf.io.parse_tensor( + features['image'][i], out_type=self.input_dtype) + + # http://github.com/tensorflow/tensorflow/issues/30409#issuecomment-508962873 + # In Graph mode, return value must get assigned in order to + # update the array + deserialized_vectors = deserialized_vectors.write(i, deserialized_value) + + # deserialized_value = tf.expand_dims(deserialized_vectors, axis=0) + deserialized_tensor = deserialized_vectors.stack() + if self.preprocess_input: + deserialized_tensor = self.preprocess_input(deserialized_tensor) + return self.model(deserialized_tensor, training=False) + + +def save_tf_model(path, model=None, preprocess_input=None): + if not model: + model = tf.keras.applications.MobileNetV2(weights='imagenet') + preprocess_input = tf.keras.applications.mobilenet_v2.preprocess_input + signature_model = TFModelWrapperWithSignature( + model=model, preprocess_input=preprocess_input) + tf.saved_model.save(signature_model, path) Review Comment: I am using this in the integration test where we load the model and save the model using this method and then use that model in the run inference transform -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
