Skip to content

Instantly share code, notes, and snippets.

@sugartom
Created June 28, 2018 06:11
Show Gist options
  • Save sugartom/70b58505bf5f28d1cf5d05904f6c0af2 to your computer and use it in GitHub Desktop.
Save sugartom/70b58505bf5f28d1cf5d05904f6c0af2 to your computer and use it in GitHub Desktop.
from darkflow.net.build import TFNet
import cv2
import tensorflow as tf
import os
import sys
from tensorflow.python.saved_model import builder as saved_model_builder
from tensorflow.python.saved_model import signature_constants
from tensorflow.python.saved_model import signature_def_utils
from tensorflow.python.saved_model import tag_constants
from tensorflow.python.saved_model import utils
from tensorflow.python.util import compat
tf.app.flags.DEFINE_integer('model_version', 1, 'version number of the model.')
FLAGS = tf.app.flags.FLAGS
options = { "model": "/path/to/yolo.cfg",
"load": "/path/to/yolo.weights",
"threshold": 0.25,
"gpu": 0.8}
tfnet = TFNet(options)
imgcv = cv2.imread("/path/to/dog.jpg")
result = tfnet.return_predict(imgcv)
for res in result:
print("%s (confidence: %s)" % (res['label'], str(res['confidence'])))
print(" topleft(%s, %s), botright(%s, %s)" %
(res['topleft']['x'], res['topleft']['y'],
res['bottomright']['x'], res['bottomright']['y']))
export_path_base = "darkflow_yolo"
export_path = os.path.join(
compat.as_bytes(export_path_base),
compat.as_bytes(str(FLAGS.model_version)))
print 'Exporting trained model to', export_path
builder = saved_model_builder.SavedModelBuilder(export_path)
tensor_info_x = tf.saved_model.utils.build_tensor_info(tfnet.inp)
tensor_info_y = tf.saved_model.utils.build_tensor_info(tfnet.out)
prediction_signature = tf.saved_model.signature_def_utils.build_signature_def(
inputs={'input': tensor_info_x},
outputs={'output': tensor_info_y},
method_name=tf.saved_model.signature_constants.PREDICT_METHOD_NAME)
legacy_init_op = tf.group(tf.tables_initializer(), name='legacy_init_op')
builder.add_meta_graph_and_variables(
tfnet.sess, [tf.saved_model.tag_constants.SERVING],
signature_def_map={
'predict_images':
prediction_signature,
},
legacy_init_op=legacy_init_op)
builder.save()
print('Done exporting!')
@gauravgola96
Copy link

gauravgola96 commented Feb 4, 2019

Here tfnet.out gives a tensor.

tensor_info_y = tf.saved_model.utils.build_tensor_info(tfnet.out)

can you help with output layer such that i will get dictionary/list format same as in any classification model.
I want the return to be same as from return_predict().
please help!

@Papaass
Copy link

Papaass commented Oct 10, 2019

Hello @gauravgola96
Have you found the response about you question ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment