Created
July 9, 2018 08:38
keras mobilenet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import keras | |
from keras.models import load_model | |
from keras.applications import InceptionV3, mobilenet | |
import tensorflow as tf | |
import os | |
import os.path as osp | |
from keras import backend as K | |
from tensorflow.python.framework import graph_util | |
from tensorflow.python.framework import graph_io | |
from tensorflow.python.keras._impl.keras.applications import mobilenet | |
from tensorflow.python.keras._impl.keras.models import load_model | |
input_fld = './' | |
weight_file = 'model.17–1.23.hdf5' | |
num_output = 1 | |
write_graph_def_ascii_flag = True | |
prefix_output_node_names_of_final_network = 'output_node' | |
output_graph_name = 'keras_mobilenetv1.pb' | |
output_fld = input_fld + 'tensorflow_model/' | |
if not os.path.isdir(output_fld): | |
os.mkdir(output_fld) | |
weight_file_path = osp.join(input_fld, weight_file) | |
K.set_learning_phase(0) | |
net_model=keras.applications.mobilenet.MobileNet(input_shape=(192, 192, 3), alpha=1.0, depth_multiplier=1, dropout=1e-3, include_top=False, weights='imagenet', input_tensor=None, pooling=None, classes=1000) | |
pred = [None] * num_output | |
pred_node_names = [None] * num_output | |
for i in range(num_output): | |
pred_node_names[i] = prefix_output_node_names_of_final_network + str(i) | |
pred[i] = tf.identity(net_model.output[i], name=pred_node_names[i]) | |
sess = K.get_session() | |
constant_graph = graph_util.convert_variables_to_constants(sess, sess.graph.as_graph_def(), pred_node_names) | |
graph_io.write_graph(constant_graph, output_fld, output_graph_name, as_text=False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment