Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def keras_to_coreml():
with custom_object_scope({'smoothL1': smoothL1, 'relu6': relu6}):
ml_model = load_model(MODEL_PATH)
coreml_model = coremltools.converters.keras.convert(ml_model,
input_names='image', image_input_names='image',
is_bgr=False)
coreml_model.save(ML_MODEL_PATH)
def convert():
file = sys.argv[2];
base, extension = os.path.splitext(file)
if extension == '.h5':
import coremltools
coreml_model = coremltools.converters.keras.convert(file)
coreml_model.save(base + '.mlmodel')
elif extension == '.pkl':
import coremltools
import sklearn
sklearn_model = sklearn.externals.joblib.load(file)
coreml_model = coremltools.converters.sklearn.convert(sklearn_model)
coreml_model.save(base + '.mlmodel')
return 1 # error
return 0
elif args.srcModelFormat == 'keras':
try:
if not args.inputNames:
raise TypeError("Neural network 'inputNames' are required for converting Keras models.")
if not args.outputNames:
raise TypeError("Neural network 'outputNames' are required for converting Keras models.")
if args.kerasJsonPath:
model = (args.kerasJsonPath, args.srcModelPath)
else:
model = args.srcModelPath
model = converters.keras.convert(model,
args.inputNames,
args.outputNames,
image_input_names = set(args.imageInputNames) if args.imageInputNames else None,
is_bgr = args.isBGR,
red_bias = args.redBias,
blue_bias = args.blueBias,
green_bias = args.greenBias,
gray_bias = args.grayBias,
image_scale = args.scale,
class_labels = args.classInputPath if args.classInputPath else None,
predicted_feature_name = args.predictedFeatureName,
respect_trainable = args.respectTrainable)
model.save(args.dstModelPath)
except Exception as e:
print('error: coremlconverter: %s.' % str(e))
return 1 # error
def keras_to_coreml():
with custom_object_scope({'smoothL1': smoothL1, 'relu6': relu6, 'DepthwiseConv2D': mobilenet.DepthwiseConv2D}):
ml_model = load_model(MODEL_PATH)
coreml_model = coremltools.converters.keras.convert(ml_model,
input_names='image', image_input_names='image',
is_bgr=False)
coreml_model.save(ML_MODEL_PATH)
import coremltools
DNN_ml_model = coremltools.converters.keras.convert('car_detection_keras_DNN_model.h5')
DNN_ml_model.author = 'Ashis Laha'
DNN_ml_model.description = 'Use for Car Detection'
DNN_ml_model.save('car_detection_keras_DNN.mlmodel')
print(DNN_ml_model)
DNN_ml_model = coremltools.converters.keras.convert('car_detection_keras_CNN_model.h5')
DNN_ml_model.author = 'Ashis Laha'
DNN_ml_model.description = 'Use for Car Detection'
DNN_ml_model.save('car_detection_keras_CNN.mlmodel')
print(DNN_ml_model)
def main(input_model_path):
"""
Convert hdf5 file to CoreML model.
:param input_model_path:
:return:
"""
out_path = re.sub(r"h5$", 'mlmodel', input_model_path)
hack_coremltools()
with CustomObjectScope(custom_objects()):
model = load_model(input_model_path)
# https://github.com/akirasosa/mobile-semantic-segmentation/issues/6#issuecomment-344508193
coreml_model = coremltools.converters.keras.convert(model,
input_names='image',
image_input_names='image',
red_bias=29.24429131 / 64.881128947,
green_bias=29.24429131 / 64.881128947,
blue_bias=29.24429131 / 64.881128947,
image_scale=1. / 64.881128947)
coreml_model.save(out_path)
print('CoreML model is created at %s' % out_path)