def init_graph(self): """ Get a model from the published tensorflowjs models. If it's not present, download it. """ self.download_tfjs_model() self.graph = load_graph_model(str(self.model_path)) logger.info("TensorflowJS model %s loaded", self.model_name)
def convert(model_cfg): model_file_path = os.path.join(model_cfg['tfjs_dir'], model_cfg['filename']) if not os.path.exists(model_file_path): print('Cannot find tfjs model path %s, downloading tfjs model...' % model_file_path) tfjsdownload.download_tfjs_model(model_cfg) # 'graph_model_to_saved_model' doesn't store the signature for the model! # tfjs.api.graph_model_to_saved_model(model_cfg['tfjs_dir'], model_cfg['tf_dir'], ['serve']) # So we do it manually below. # This link was a great help to do this: # https://www.programcreek.com/python/example/104885/tensorflow.python.saved_model.signature_def_utils.build_signature_def #graph = tfjs.api.load_graph_model(model_cfg['tfjs_dir']) graph = tfjs_api.load_graph_model(model_cfg['tfjs_dir']) builder = tf.compat.v1.saved_model.Builder(model_cfg['tf_dir']) with tf.compat.v1.Session(graph=graph) as sess: input_tensor_names = tfjs.util.get_input_tensors(graph) output_tensor_names = tfjs.util.get_output_tensors(graph) signature_inputs = __tensor_info_def(sess, input_tensor_names) signature_outputs = __tensor_info_def(sess, output_tensor_names) method_name = tf.compat.v1.saved_model.signature_constants.PREDICT_METHOD_NAME signature_def = tf.compat.v1.saved_model.build_signature_def(inputs=signature_inputs, outputs=signature_outputs, method_name=method_name) signature_map = {tf.compat.v1.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: signature_def} builder.add_meta_graph_and_variables(sess=sess, tags=['serve'], signature_def_map=signature_map) return builder.save()
def __init__(self, model_path, segment_logits_index, part_heatmaps_index, heatmaps_index): self.model_path = model_path self.segment_logits_index = segment_logits_index self.part_heatmaps_index = part_heatmaps_index self.heatmaps_index = heatmaps_index self.graph = tfjs_api.load_graph_model(self.model_path)
def test_load_graph_model_with_fused_depthwise_prelu(self): """load_graph_model should split fused depthwise conv2d with prelu""" model_dir = testutils.get_path_to(testutils.DEPTHWISE_PRELU_PATH) graph = api.load_graph_model(model_dir) loaded_model = testutils.graph_to_model(graph) original_model_name = testutils.get_path_to( testutils.DEPTHWISE_PRELU_FILE) original_model = testutils.graph_to_model(original_model_name) # run both models and compare results x = _load_image(testutils.get_path_to('./data/human1.jpg')) y_from_loaded = _argmax(loaded_model(x)) y_from_original = _argmax(original_model(x)) # sanity check - should be reckognised as a human (class 1) self.assertEqual(y_from_original[0], 1) # same class self.assertEqual(y_from_loaded[0], y_from_original[0]) # same confidence self.assertAlmostEqual(y_from_loaded[1], y_from_original[1], 4)
def test_load_graph_model_with_simple_model(self): """load_graph_model should load simple model""" model_dir = testutils.get_path_to(testutils.SIMPLE_MODEL_PATH_NAME) graph = api.load_graph_model(model_dir) self.assertIsInstance(graph, tf.Graph) loaded_model = testutils.graph_to_model(graph) original_model_name = testutils.get_path_to( testutils.SIMPLE_MODEL_FILE_NAME) original_model = testutils.graph_to_model(original_model_name) # run both models and compare results x_ = 4 x = tf.constant([[x_]], dtype=tf.float32) y_from_loaded_model = as_scalar(loaded_model(x)) y_from_original_model = as_scalar(original_model(x)) # sanity check; fails if model is different from the one we expected: # we want a model that predicts y = 5*x self.assertAlmostEqual(y_from_original_model, x_*5, places=1) # actual test self.assertAlmostEqual(y_from_loaded_model, y_from_original_model, places=4)
def test_load_graph_model_with_prelu(self): """load_graph_model should convert prelu operations""" model_dir = testutils.get_path_to(testutils.PRELU_MODEL_PATH) graph = api.load_graph_model(model_dir) loaded_model = testutils.graph_to_model(graph) original_model_name = testutils.get_path_to(testutils.PRELU_MODEL_FILE) original_model = testutils.graph_to_model(original_model_name) # run both models and compare results cx, cy, cz, r = -0.12, 0.2, 0.1, 0.314158 px, py, pz = -0.4, 0.5, 0.4 x = tf.constant([[cx, cy, cz, r, px, py, pz]], dtype=tf.float32) y_from_loaded_model = as_scalar(loaded_model(x)) y_from_original_model = as_scalar(original_model(x)) # sanity check; fails if model is different from the one we expected: # we want a model that predicts whether a point (px,py,pz) is inside # a sphere at (cx,cy,cz) of radius r self.assertAlmostEqual(y_from_original_model, 1, places=1) # actual test self.assertAlmostEqual(y_from_loaded_model, y_from_original_model, places=4)
multiplier=multiplier, stride=output_stride)) model_path = ('bodypix_mobilenet_float_{multiplier:03d}' + '_model-stride{stride}').format(multiplier=int(100 * multiplier), stride=output_stride) elif model_type == "resnet50": print("Model: resnet50 (stride={stride})".format(stride=output_stride)) model_path = 'bodypix_resnet50_float_model-stride{stride}'.format( stride=output_stride) else: print('Unknown model type. Use "mobilenet" or "resnet50".') sys.exit(1) # Load the tensorflow model print("Loading model...") graph = tfjs_api.load_graph_model(model_path) print("done.") # Setup the tensorflow session sess = tf.compat.v1.Session(graph=graph) input_tensor_names = tfjs_util.get_input_tensors(graph) output_tensor_names = tfjs_util.get_output_tensors(graph) input_tensor = graph.get_tensor_by_name(input_tensor_names[0]) # Initialize layers layers = reload_layers(config) static_image = None for extension in ["jpg", "jpeg", "png"]: if config['real_video_device'].lower().endswith(extension):
def init_graph(self): self.download_tfjs_model() self.graph = load_graph_model(str(self.model_path))
def main(): global placeholder global text_placeholder global second_text global threshold global blurring_kernel_size global output_stride global chosen_background global chosen_heatmaps global chosen_type_output global model global write_to_device global show_preview # ResNet on CPU won't be fast fake_device_exists = False try: fake_camera = open_fake_device() fake_device_exists = True except Exception as e: st.error(f'Could not find fake device. {e}') st.info('Use `sudo modprobe v4l2loopback exclusive_caps=1`') model = st.sidebar.selectbox("Model", ["mobilenet", "resnet"]) # Lower stride --> Higher res but slower output_stride = st.sidebar.selectbox("Output Stride", [8, 16], 1) if model == "mobilenet": model_path = f"models/bodypix_mobilenet_float_050_model-stride{output_stride}/" elif model == "resnet": model_path = f"models/bodypix_resnet50_float_model-stride{output_stride}" potential_backgrounds = [im for im in glob.glob("images/*")] nb_frames = st.sidebar.slider("Run Model every N frames", 1, 30, 3, 1) threshold = st.sidebar.slider("Sigmoid threshold", 0.0, 1.0, 0.7, 0.05) # https://github.com/streamlit/streamlit/issues/745 Can't use odd numbers in slider. 😿 blurring_kernel_size = st.sidebar.slider("Kernel Size for blurring", 2, 100, 30, 2) blurring_kernel_size += 1 chosen_type_output = st.sidebar.selectbox("Choose type of mask", ["Segmentation", "Heatmaps"]) # TODO --> Choose parts we want # chosen_heatmaps = st.sidebar.multiselect( # "Body parts heatmaps", # [i for i, x in enumerate(constants.PART_CHANNELS)], # format_func=lambda x: constants.PART_CHANNELS[x], # ) chosen_background = st.sidebar.selectbox( "Background", ["", "blur"] + potential_backgrounds, format_func=lambda x: x.split("/")[-1] ) show_preview = st.checkbox("Show preview ?", False) write_to_device = st.checkbox("Write to Device ?", False) placeholder = st.empty() text_placeholder = st.sidebar.empty() second_text = st.sidebar.empty() graph = tfjs_api.load_graph_model(model_path) sess = tf.compat.v1.Session(graph=graph, config=tf.compat.v1.ConfigProto(intra_op_parallelism_threads=1)) if show_preview or write_to_device: vc = cv2.VideoCapture(0) if not vc.isOpened(): raise Exception("Cannot capture video") capture_display_video(vc, sess, graph, nb_frames, fake_camera) sess.close()
def load_model_resnet(model_path): graph = tfjs.load_graph_model(model_path) sess = tf.compat.v1.Session(graph=graph) return sess, graph