def get_input_reciever_fn(self): feature = tf.placeholder(dtype=tf.int32, shape=[None, None, None], name="feature_tensor") pos_name = tf.placeholder(dtype=tf.int32, shape=[None, None, None], name = "pos_tensor") receiver_tensors = {self.feature_name: feature, self.pos_name: pos_name} return build_raw_serving_input_receiver_fn(receiver_tensors)
def serving_input_fn(params): user = tf.placeholder(tf.int64, shape=[1]) item = tf.placeholder(tf.int64, shape=[1]) return build_raw_serving_input_receiver_fn({ USER_EMBEDDING_TENSOR_NAME: user, ITEM_EMBEDDING_TENSOR_NAME: item })()
def dummy_serving_receiver_fn(): feature_spec = { 'x': array_ops.placeholder(dtype=dtypes.int64, shape=(2, 1), name='feature_x'), } return export.build_raw_serving_input_receiver_fn(feature_spec)
def test_build_raw_serving_input_receiver_fn_name(self): """Test case for issue #12755.""" f = { "feature": array_ops.placeholder( name="feature", shape=[32], dtype=dtypes.float32) } serving_input_receiver_fn = export.build_raw_serving_input_receiver_fn(f) v = serving_input_receiver_fn() self.assertTrue(isinstance(v, export.ServingInputReceiver))
def main(unused_argv): tf.logging.set_verbosity(tf.logging.INFO) num_training_data = maybe_download_iris_data(IRIS_TRAINING, IRIS_TRAINING_URL) num_test_data = maybe_download_iris_data(IRIS_TEST, IRIS_TEST_URL) # Build 3 layer DNN with 10, 20, 10 units respectively. feature_columns = [ tf.feature_column.numeric_column(key, shape=1) for key in FEATURE_KEYS ] classifier = tf.estimator.DNNClassifier(feature_columns=feature_columns, hidden_units=[10, 20, 10], n_classes=3) # Train. train_input_fn = input_fn(IRIS_TRAINING, num_training_data, batch_size=32, is_training=True) classifier.train(input_fn=train_input_fn, steps=400) # Eval. test_input_fn = input_fn(IRIS_TEST, num_test_data, batch_size=32, is_training=False) scores = classifier.evaluate(input_fn=test_input_fn) print('Accuracy (tensorflow): {0:f}'.format(scores['accuracy'])) # Export the SavedModel file import shutil import tempfile #savemodel_dir = classifier.export_savedmodel(tempfile.mkdtemp(), serving_input_fn = serving_input_fn, as_text = True) from tensorflow.python.estimator.export import export #feature_spec = {'MY_FEATURE': tf.constant(2.0, shape=[1, 1])} # FEATURE_KEYS = ['sepal_length', 'sepal_width', 'petal_length', 'petal_width'] feature_spec = { 'sepal_length': tf.constant(2.0, shape=[1, 1]), 'sepal_width': tf.constant(2.0, shape=[1, 1]), 'petal_length': tf.constant(2.0, shape=[1, 1]), 'petal_width': tf.constant(2.0, shape=[1, 1]) } serving_input_fn = export.build_raw_serving_input_receiver_fn(feature_spec) savemodel_dir = classifier.export_savedmodel(tempfile.mkdtemp(), serving_input_fn, as_text=True) savemodel_dir = savemodel_dir.decode("UTF-8") name = "1" if (os.path.isdir("savedmodel/" + name)): shutil.rmtree("savedmodel/" + name) shutil.move(savemodel_dir, "savedmodel/" + name)
def serving_input_fn(hyperparameters): feature_spec = { INPUT_TENSOR_PRICE: tf.placeholder(tf.float32, shape=[1]), INPUT_TENSOR_INVENTORY: tf.placeholder(tf.float32, shape=[1]) } # These should have () after them? I don't know. Most example for input_fn show it, but my simple example # doesn't work with it. # Used in RAW for parse_input return build_raw_serving_input_receiver_fn(feature_spec)
def get_input_reciever_fn(self): context = tf.placeholder(dtype=tf.string, shape=[None, None, None], name="context_tensor") question = tf.placeholder(dtype=tf.string, shape=[None, None], name="query_tensor") receiver_tensors = { self.context_name: context, self.question_name: question } return build_raw_serving_input_receiver_fn(receiver_tensors)
def test_build_raw_serving_input_receiver_fn_without_shape(self): """Test case for issue #21178.""" f = {"feature_1": array_ops.placeholder(dtypes.float32), "feature_2": array_ops.placeholder(dtypes.int32)} serving_input_receiver_fn = export.build_raw_serving_input_receiver_fn(f) v = serving_input_receiver_fn() self.assertTrue(isinstance(v, export.ServingInputReceiver)) self.assertEqual( tensor_shape.unknown_shape(), v.receiver_tensors["feature_1"].shape) self.assertEqual( tensor_shape.unknown_shape(), v.receiver_tensors["feature_2"].shape)
def test_build_raw_serving_input_receiver_fn_without_shape(self): """Test case for issue #21178.""" f = {"feature_1": array_ops.placeholder(dtypes.float32), "feature_2": array_ops.placeholder(dtypes.int32)} serving_input_receiver_fn = export.build_raw_serving_input_receiver_fn(f) v = serving_input_receiver_fn() self.assertTrue(isinstance(v, export.ServingInputReceiver)) self.assertEqual( tensor_shape.unknown_shape(), v.receiver_tensors["feature_1"].shape) self.assertEqual( tensor_shape.unknown_shape(), v.receiver_tensors["feature_2"].shape)
def main(_): if len(sys.argv) < 1 or sys.argv[-1].startswith('-'): print('Usage: keras_vgg.py export_dir') sys.exit(-1) export_path_base = sys.argv[-1] model = keras.applications.vgg16.VGG16(weights='imagenet') model.compile(optimizer=keras.optimizers.SGD(lr=.01, momentum=.9), loss='binary_crossentropy', metrics=['accuracy']) print(model.summary()) estimator = tf.keras.estimator.model_to_estimator(keras_model=model) feature_spec = {'input_1': model.input} serving_input_fn = export.build_raw_serving_input_receiver_fn(feature_spec) estimator.export_savedmodel(export_path_base, serving_input_fn)
def test_build_raw_serving_input_receiver_fn(self): features = {"feature_1": constant_op.constant(["hello"]), "feature_2": constant_op.constant([42])} serving_input_receiver_fn = export.build_raw_serving_input_receiver_fn( features) with ops.Graph().as_default(): serving_input_receiver = serving_input_receiver_fn() self.assertEqual(set(["feature_1", "feature_2"]), set(serving_input_receiver.features.keys())) self.assertEqual(set(["feature_1", "feature_2"]), set(serving_input_receiver.receiver_tensors.keys())) self.assertEqual( dtypes.string, serving_input_receiver.receiver_tensors["feature_1"].dtype) self.assertEqual( dtypes.int32, serving_input_receiver.receiver_tensors["feature_2"].dtype)
def export(self): assert self.config.checkpoint_path is not None model_dir = str(self.config.checkpoint_path) def model_fn(features, labels, mode): sentence = features['sentence'] model = Model(sentence, labels, self.params, mode) return tf.estimator.EstimatorSpec(mode, {'label': model.prediction}) estimator = tf.estimator.Estimator(model_fn, model_dir) sentence = tf.placeholder(tf.string, [None], 'sentence') serving_input_receiver_fn = build_raw_serving_input_receiver_fn( {'sentence': sentence}) estimator.export_saved_model( str(self.config.checkpoint_path / 'exported'), serving_input_receiver_fn) log.info("Export complete")
def main(unused_argv): tf.logging.set_verbosity(tf.logging.INFO) num_training_data = maybe_download_iris_data( IRIS_TRAINING, IRIS_TRAINING_URL) num_test_data = maybe_download_iris_data(IRIS_TEST, IRIS_TEST_URL) # Build 3 layer DNN with 10, 20, 10 units respectively. feature_columns = [ tf.feature_column.numeric_column(key, shape=1) for key in FEATURE_KEYS] classifier = tf.estimator.DNNClassifier( feature_columns=feature_columns, hidden_units=[10, 20, 10], n_classes=3) # Train. train_input_fn = input_fn(IRIS_TRAINING, num_training_data, batch_size=32, is_training=True) classifier.train(input_fn=train_input_fn, steps=400) # Eval. test_input_fn = input_fn(IRIS_TEST, num_test_data, batch_size=32, is_training=False) scores = classifier.evaluate(input_fn=test_input_fn) print('Accuracy (tensorflow): {0:f}'.format(scores['accuracy'])) # Export the SavedModel file import shutil import tempfile #savemodel_dir = classifier.export_savedmodel(tempfile.mkdtemp(), serving_input_fn = serving_input_fn, as_text = True) from tensorflow.python.estimator.export import export #feature_spec = {'MY_FEATURE': tf.constant(2.0, shape=[1, 1])} # FEATURE_KEYS = ['sepal_length', 'sepal_width', 'petal_length', 'petal_width'] feature_spec = {'sepal_length': tf.constant(2.0, shape=[1, 1]), 'sepal_width': tf.constant(2.0, shape=[1, 1]), 'petal_length': tf.constant(2.0, shape=[1, 1]), 'petal_width': tf.constant(2.0, shape=[1, 1])} serving_input_fn = export.build_raw_serving_input_receiver_fn(feature_spec) savemodel_dir = classifier.export_savedmodel(tempfile.mkdtemp(), serving_input_fn, as_text = True) savemodel_dir = savemodel_dir.decode("UTF-8") name = "1" if(os.path.isdir("savedmodel/" + name)): shutil.rmtree("savedmodel/" + name) shutil.move(savemodel_dir, "savedmodel/" + name)
fc1 = tf.layers.dense(fc1, 1024) # Apply Dropout (if is_training is False, dropout is not applied) fc1 = tf.layers.dropout(fc1, rate=dropout, training=is_training) # Output layer, class prediction out = tf.layers.dense(fc1, n_classes) return out from tensorflow.python.estimator.export import export with tf.Session() as sess: # Build the Estimator feature_spec = {'images': tf.constant(mnist.train.images)} serving_input_fn = export.build_raw_serving_input_receiver_fn(feature_spec) # Train the Model # Evaluate the Model # Define the input function for evaluating input_fn = tf.estimator.inputs.numpy_input_fn( x={'images': mnist.test.images}, y=mnist.test.labels, batch_size=batch_size, shuffle=False) # Define a scope for reusing the variables # TF Estimator input is a dict, in case of multiple inputs x = mnist.test.images is_training = False n_classes = 10
def serving_input_fn(params): tensor = tf.placeholder(tf.float32, shape=[1, 7]) return build_raw_serving_input_receiver_fn({INPUT_TENSOR_NAME: tensor})()
def serving_input_fn(params): inputs = tf.placeholder(tf.int32, shape=[None, 7]) tensors = {'inputs': inputs} return build_raw_serving_input_receiver_fn(tensors)()
def dummy_serving_receiver_fn(): feature_spec = {'x': array_ops.placeholder( dtype=dtypes.int64, shape=(2, 1), name='feature_x'),} return export.build_raw_serving_input_receiver_fn(feature_spec)
def serving_input_fn(params): inputs = tf.placeholder(tf.int32, shape=[None, 7]) tensors = {'inputs': inputs} return build_raw_serving_input_receiver_fn(tensors)()
def serving_input_fn(params): tensor = tf.placeholder(tf.float32, shape=[1, 7]) return build_raw_serving_input_receiver_fn({INPUT_TENSOR_NAME: tensor})()
def serving_input_fn(params): inputs = tf.convert_to_tensor(X_test) return build_raw_serving_input_receiver_fn({INPUT_TENSOR_NAME: inputs})()