Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_glm_regressor(self):
X, y = make_regression(n_features=4, random_state=0)
lr = LinearRegression()
lr.fit(X, y)
lr_coreml = coremltools.converters.sklearn.convert(lr)
lr_onnx = convert(lr_coreml.get_spec())
self.assertTrue(lr_onnx is not None)
dump_data_and_model(X.astype(numpy.float32), lr, lr_onnx, basename="CmlLinearRegression-Dec4")
svr = LinearSVR()
svr.fit(X, y)
svr_coreml = coremltools.converters.sklearn.convert(svr)
svr_onnx = convert(svr_coreml.get_spec())
self.assertTrue(svr_onnx is not None)
dump_data_and_model(X.astype(numpy.float32), svr, svr_onnx, basename="CmlLinearSvr-Dec4")
def test_flatten(self):
N, C, H, W, D = 2, 3, 1, 2, 2
x = create_tensor(N, C, H, W)
keras_model = Sequential()
keras_model.add(Flatten(input_shape=(H, W, C)))
keras_model.add(Dense(D))
keras_model.compile(optimizer='adagrad', loss='mse')
try:
coreml_model = coremltools.converters.keras.convert(keras_model)
except (ImportError, AttributeError):
warnings.warn("Issue in coremltools.")
return
onnx_model = onnxmltools.convert_coreml(coreml_model)
self.assertIsNotNone(onnx_model)
if not self._no_available_inference_engine():
y_reference = keras_model.predict(np.transpose(x, [0, 2, 3, 1]))
y_produced = evaluate_deep_model(onnx_model, x).reshape(N, D)
self.assertTrue(np.allclose(y_reference, y_produced))
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 convert(model_path, prototxt_path, label_path, red=-123, green=-117, blue=-104,
scale=1.0, bgr=False, output_model_name='sku_cls_model_noise.mlmodel'):
"""
Args: red, grenn, blue should be nagative: like -123, -117, -104
output_model_name: default, sku_cls_model_noise.mlmodel
label_path: Classes are sorted by incrementing。
liby_0, 0
liby_1, 1
liby_2, 2
liby_3, 3
liby_4, 4
Returns: auto-saved coreml model in source path.
"""
coreml_model = coremltools.converters.caffe.convert((
model_path, prototxt_path),
image_input_names='data',
is_bgr=bgr,
image_scale=scale,
red_bias= red*scale,
green_bias=green*scale,
blue_bias=blue*scale,
class_labels=label_path,
)
coreml_model.save(os.path.join(os.path.dirname(model_path), output_model_name))
print("finished converte {0} to {1}".format(os.path.basename(model_path), output_model_name))
return
def export_coreml_location_model(model):
cm = coremltools.converters.keras.convert(
model, input_names=['accel_gyro_stream'], output_names=['touch_predictions'])
cm.author = 'Tomas Reimers & Greg Foster'
cm.license = 'MIT'
cm.short_description = ''
cm.input_description['accel_gyro_stream'] = 'An array of time indexed sensor data'
cm.output_description['touch_predictions'] = 'Was the screen touched or not?'
cm.save('location_model.mlmodel')
def export_coreml_touch_model(model):
cm = coremltools.converters.keras.convert(
model, input_names=['touch_windows'], output_names=['touch_predictions'])
cm.author = 'Tomas Reimers & Greg Foster'
cm.license = 'MIT'
cm.short_description = ''
cm.input_description['touch_windows'] = 'An array of arrays of time indexed sensor data'
cm.output_description['touch_predictions'] = 'Where was the screen touched?'
cm.save('touch_model.mlmodel')
warped = img_matlab[int(boundingboxes[0][1]):int(boundingboxes[0][3]),
int(boundingboxes[0][0]):int(boundingboxes[0][2])]
print(int(boundingboxes[0][1]), int(boundingboxes[0][3]), int(boundingboxes[0][0]), int(boundingboxes[0][2]))
return warped
### Step 1: tensorflow to caffemodel
tf_model_dir = '/home/logview/workspace/projects/FaceAll/20170512-110547'
convertTf2Caffe(tf_model_dir, 'InceptionResnet_Model', EMBEDDING_SIZE)
### Step 2: caffemodel to CoreML
### use parameter (image_input_names='data') ==> input CVPixelBufferRef in iOS
### (without image_input_names='data') ==> input MLMultiArray in iOS
if(EMBEDDING_SIZE==512):
coreml_model = coremltools.converters.caffe.convert(('InceptionResnet_Model/inception_resnet_v1_conv1x1.caffemodel', 'InceptionResnet_Model/resnetInception-512.prototxt'), is_bgr=False)
coreml_model.save('InceptionResnet_Model/InceptionResnet.mlmodel')
else:
coreml_model = coremltools.converters.caffe.convert(('InceptionResnet_Model/inception_resnet_v1_conv1x1.caffemodel', 'InceptionResnet_Model/resnetInception-128.prototxt'), is_bgr=False)
coreml_model.save('InceptionResnet_Model/InceptionResnet.mlmodel')
### Step 3: calculate embedding from tensorflow model
imgPath = '4550.jpg'
img = cv2.imread(imgPath) # BGR
crop = mtcnnDetect(img) # RGB
calcTFVector(crop, tf_model_dir)
### Step 4: calculate embedding from caffe model
imgPath = '4550.jpg'
img = cv2.imread(imgPath)