inpainting_gmcnn
inpainting_gmcnn copied to clipboard
trained model conversion to coreml
Hi, is it possible to convert your trained pytorch or tensorflow model into coreml without implementing custom layer?
I guess so. The custom modules are only used in training, and the generator for testing is formed by standard modules in tensorflow, like convolutional layers.
@shepnerd , do you have .pb file of this tensorflow model? Also i think i would need .meta file (i might be wrong here) inorder to convert checkpoints into .pb file
@shepnerd Ive tried to generate .meta file by retraining places_2 model. but now i get the following error
And I am using the following code to convert checkpoint files into .pb file
Also if you may please guide me about, what the output_node_name is thn it would be helpfull
Successfully converted tensorflow model into Coreml. Thanks @shepnerd
@farazBhatti I'm trying to go through this process and converting this model to CoreML, can you please share how did you create the .meta file and how did you convert it to CoreML? Thanks
@developeder , u would have to first freeze tf model Freeze tensorflow model and save .pb fille after loading trained model during testing add following line so that loaded tf model gets frozed code for freezing tf model
output_node_names =["mul_4"]
frozen_graph_def = tf.graph_util.convert_variables_to_constants(sess,sess.graph_def,output_node_names)
with open('output_graph_500.pb', 'wb') as f:
f.write(frozen_graph_def.SerializeToString())
print('Model Saved')
mesg here again after freezing model
@farazBhatti Thank you for the quick response! Now I have the .pb file, what next?
use this code to now convert frozen tf model to coreml. note: dimensions of tf input for my model were 500 * 500, change these values accordingly to yours.
import os
import tensorflow as tf
import tfcoreml
frozen_model_file = os.path.abspath("output_graph_500.pb")
input_tensor_shapes = {"input/placeholder_1": [1, 500, 500, 1],"input/placeholder": [1, 500, 500, 3]}
# Output CoreML model path
coreml_model_file = 'inpaint_500*500.mlmodel'
output_tensor_names = ['mul_4:0']
def convert():
# Read the pb model
with tf.gfile.GFile(frozen_model_file, "rb") as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
# Then, we import the graph_def into a new Graph
tf.import_graph_def(graph_def, name="")
# Convert
tfcoreml.convert(
tf_model_path=frozen_model_file,
mlmodel_path=coreml_model_file,
input_name_shape_dict=input_tensor_shapes,
output_feature_names=output_tensor_names)
convert()
print('DONE')