Skip to content

Commit

Permalink
export the model into protobuf
Browse files Browse the repository at this point in the history
  • Loading branch information
vbezgachev committed Jun 17, 2017
1 parent f90c18a commit c6c5f23
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 44 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ checkpoints/
data/
__pycache__/
svnh_test_images/
gan-export/
export/
.vscode/
*.pkl
4 changes: 4 additions & 0 deletions HowTo.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ Successfully exported model to inception-export
```

## Test the functioning
- Start the gRPC server
```
bazel-bin/tensorflow_serving/model_servers/tensorflow_model_server --port=9000 --model_name=inception --model_base_path=inception-export &> inception_log &
```
- Download test image
```
apt-get update
Expand Down
15 changes: 9 additions & 6 deletions gan.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ class GAN:
:param beta1: The beta1 parameter for Adam.
"""
def __init__(self, real_size, z_size, learning_rate, num_classes=10,
alpha=0.2, beta1=0.5):
alpha=0.2, beta1=0.5, drop_rate=0.5):
tf.reset_default_graph()

self.learning_rate = tf.Variable(learning_rate, trainable=False)
inputs = self.model_inputs(real_size, z_size)
self.input_real, self.input_z, self.y, self.label_mask = inputs
self.drop_rate = tf.placeholder_with_default(.5, (), "drop_rate")
self.drop_rate = tf.placeholder_with_default(drop_rate, (), "drop_rate")

loss_results = self.model_loss(self.input_real, self.input_z,
real_size[2], self.y, num_classes,
Expand All @@ -26,7 +26,9 @@ def __init__(self, real_size, z_size, learning_rate, num_classes=10,
drop_rate=self.drop_rate)

self.d_loss, self.g_loss, self.correct, \
self.masked_correct, self.samples, self.pred_class = loss_results
self.masked_correct, self.samples, self.pred_class, \
self.discriminator_class_logits, self.discriminator_out = \
loss_results

self.d_opt, self.g_opt, self.shrink_lr = self.model_opt(self.d_loss,
self.g_loss,
Expand Down Expand Up @@ -65,7 +67,7 @@ def model_loss(self, input_real, input_z, output_dim, y, num_classes,
g_model = self.generator(input_z, output_dim, alpha=alpha, size_mult=g_size_mult)
d_on_data = self.discriminator(input_real, alpha=alpha, drop_rate=drop_rate,
size_mult=d_size_mult)
_, class_logits_on_data, gan_logits_on_data, data_features = d_on_data
out, class_logits_on_data, gan_logits_on_data, data_features = d_on_data
d_on_samples = self.discriminator(g_model, reuse=True, alpha=alpha, drop_rate=drop_rate,
size_mult=d_size_mult)
_, _, gan_logits_on_samples, sample_features = d_on_samples
Expand Down Expand Up @@ -111,7 +113,8 @@ def model_loss(self, input_real, input_z, output_dim, y, num_classes,
correct = tf.reduce_sum(tf.to_float(eq), name='correct_pred_sum')
masked_correct = tf.reduce_sum(label_mask * tf.to_float(eq))

return d_loss, g_loss, correct, masked_correct, g_model, pred_class
return d_loss, g_loss, correct, masked_correct, g_model, pred_class,\
class_logits_on_data, out


def model_opt(self, d_loss, g_loss, learning_rate, beta1):
Expand Down Expand Up @@ -237,6 +240,6 @@ def discriminator(self, x, reuse=False, alpha=0.2, drop_rate=0., num_classes=10,
max_val = tf.squeeze(max_val)
gan_logits = tf.log(tf.reduce_sum(tf.exp(stable_class_logits), 1)) + max_val

out = tf.nn.softmax(class_logits)
out = tf.nn.softmax(class_logits, name='discriminator_out')

return out, class_logits, gan_logits, features
90 changes: 52 additions & 38 deletions svnh_semi_supervised_model_saved.py
Original file line number Diff line number Diff line change
@@ -1,61 +1,75 @@
import os
import shutil

import tensorflow as tf
from tensorflow.python.framework import graph_util

import utils
from gan import GAN



tf.app.flags.DEFINE_string('checkpoint_dir', '/tmp/train',
"""Directory where to read training checkpoints.""")
tf.app.flags.DEFINE_string('output_dir', '/tmp/export',
"""Directory where to export the model.""")


def preprocess_image(image_buffer):
"""Preprocess JPEG encoded bytes to 3D float Tensor."""
image = tf.image.decode_jpeg(image_buffer, channels=3)
image = utils.scale(image)

return image
#def preprocess_image(image_buffer):
# """Preprocess JPEG encoded bytes to 3D float Tensor."""
# image = tf.image.decode_jpeg(image_buffer, channels=3)
# image = utils.scale(image)
#
# return image


def main(_):

#tf.saved_model.utils.build_tensor_info()
# Create GAN model
real_size = (32, 32, 3)
z_size = 100
learning_rate = 0.0003
net = GAN(real_size, z_size, learning_rate, drop_rate=0.)

loaded_graph = tf.Graph()
# Create saver to restore from checkpoints
saver = tf.train.Saver()

with tf.Session(graph=loaded_graph) as sess:
saver = tf.train.import_meta_graph('./checkpoints/generator.ckpt.meta')
with tf.Session() as sess:
# Restore the model from last checkpoints
saver.restore(sess, tf.train.latest_checkpoint('./checkpoints'))

# Input transformation.
serialized_tf_example = tf.placeholder(tf.string, name='tf_example')
feature_configs = {
'image/encoded': tf.FixedLenFeature(
shape=[32, 32, 3], dtype=tf.string),
}
tf_example = tf.parse_example(serialized_tf_example, feature_configs)
jpegs = tf_example['image/encoded']
images = tf.map_fn(preprocess_image, jpegs, dtype=tf.float32)

# Create GAN model
real_size = (32, 32, 3)
z_size = 100
learning_rate = 0.0003
net = GAN(real_size, z_size, learning_rate)

# Restore variables from the last checkpoint
pred_class_tensor = loaded_graph.get_tensor_by_name("pred_class:0")
inputs_real_tensor = loaded_graph.get_tensor_by_name("input_real:0")
drop_rate_tensor = loaded_graph.get_tensor_by_name("drop_rate:0")

# Build the signature_def_map.
classification_inputs = tf.saved_model.utils.build_tensor_info(
serialized_tf_example)
classification_outputs_classes = tf.saved_model.utils.build_tensor_info(
net.pred_class)
classification_outputs_scores = tf.saved_model.utils.build_tensor_info(
values)
# (re-)create export directory
export_path = 'gan-export'
if os.path.exists(export_path):
shutil.rmtree(export_path)

# create model builder
builder = tf.saved_model.builder.SavedModelBuilder(export_path)

# create tensors info
predict_tensor_inputs_info = tf.saved_model.utils.build_tensor_info(net.input_real)
predict_tensor_scores_info = tf.saved_model.utils.build_tensor_info(
net.discriminator_out)

# build prediction signature
prediction_signature = (
tf.saved_model.signature_def_utils.build_signature_def(
inputs={'images': predict_tensor_inputs_info},
outputs={'scores': predict_tensor_scores_info},
method_name=tf.saved_model.signature_constants.PREDICT_METHOD_NAME
)
)

# save the model
legacy_init_op = tf.group(tf.tables_initializer(), name='legacy_init_op')
builder.add_meta_graph_and_variables(
sess, [tf.saved_model.tag_constants.SERVING],
signature_def_map={
'predict_images': prediction_signature
},
legacy_init_op=legacy_init_op)

builder.save()


if __name__ == '__main__':
Expand Down

0 comments on commit c6c5f23

Please sign in to comment.