Giter Club home page Giter Club logo

apple / coremltools Goto Github PK

View Code? Open in Web Editor NEW
4.1K 117.0 593.0 57.62 MB

Core ML tools contain supporting tools for Core ML model conversion, editing, and validation.

Home Page: https://coremltools.readme.io

License: BSD 3-Clause "New" or "Revised" License

C++ 16.48% Objective-C++ 0.36% Objective-C 0.05% Python 81.62% CMake 0.24% C 0.91% Shell 0.23% Mustache 0.03% Makefile 0.06% CSS 0.01% Batchfile 0.01%
coremltools tensorflow pytorch coreml machine-learning model-converter model-conversion

coremltools's Introduction

Build Status PyPI Release Python Versions

Core ML Tools logo

Use Core ML Tools (coremltools) to convert machine learning models from third-party libraries to the Core ML format. This Python package contains the supporting tools for converting models from training libraries such as the following:

With coremltools, you can:

  • Convert trained models to the Core ML format.
  • Read, write, and optimize Core ML models.
  • Verify conversion/creation (on macOS) by making predictions using Core ML.

After conversion, you can integrate the Core ML models with your app using Xcode.

Installation

To install, run the following command in your terminal:

pip install -U coremltools

Core ML

Core ML is an Apple framework to integrate machine learning models into your app. Core ML provides a unified representation for all models. Your app uses Core ML APIs and user data to make predictions, and to fine-tune models, all on the user’s device. Core ML optimizes on-device performance by leveraging the CPU, GPU, and Neural Engine while minimizing its memory footprint and power consumption. Running a model strictly on the user’s device removes any need for a network connection, which helps keep the user’s data private and your app responsive.

Resources

To install coremltools, see Installing Core ML Tools. For more information, see the following:

coremltools's People

Contributors

1duo avatar arjunsharda avatar aseemw avatar bhushan23 avatar dawerg avatar freedomtan avatar fukatani avatar ghop02 avatar gustavla avatar jakesabathia2 avatar junpeiz avatar keith-wyss avatar mrfarhadi avatar msiracusa avatar mstronach avatar necross avatar nikalra avatar pcuenca avatar rxwei avatar sberardi-apple avatar seibert avatar shantanuchhabra avatar slin07 avatar srikris avatar tobyroseman avatar tonybove-apple avatar vladx avatar y-ich avatar yifanshensz avatar znation avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

coremltools's Issues

AttributeError: 'MLModel' object has no attribute 'SerializeToString'

I am getting error while converting caffe model

import coremltools
coreml_model = coremltools.converters.caffe.convert(('pose_iter_440000.caffemodel', 'pose_deploy_linevec.prototxt'))
coremltools.utils.save_spec(coreml_model, 'my_model.mlmodel')
coremltools.utils.save_spec(coreml_model, 'my_model.mlmodel')
  File "/Users/***/xCode/NeuralNetworks/openpose-1.2.0/models/pose/coco/mlvirtualenv/pythonenv/lib/python2.7/site-packages/coremltools/models/utils.py", line 54, in save_spec
    s = spec.SerializeToString()
AttributeError: 'MLModel' object has no attribute 'SerializeToString'

did I miss to install smth or?

Problem with output layer in neasted model

When I tried to convert yolo v2 model (https://github.com/experiencor/basic-yolo-keras) with mobilenet backend, I got error:

File "/usr/local/lib/python2.7/dist-packages/coremltools/converters/keras/_topology2.py", line 174, in make_output_layers self.model.output_layers] IndexError: list index out of range

I have debuged code of coreml tool convertor, and I have found that backend model translated to 3 nested level:

  1. Layers of frontend model and backend model layer.
    image

  2. Backend model layer
    image

  3. Layers of backend model
    image

The problem is related to the fact that output layer of 2-nd model points itself. I change the output layer of 2nd level model on the last layer of 3 level model.
yolo.feature_extractor.feature_extractor.output_layers = [yolo.feature_extractor.feature_extractor.layers[-1].layers[-1]]

Now I can convert model without problem.

Invalid dst shape

In my model, I'm attempting to mimick a tf.image.resize_nearest_neighbor operation in TensorFlow (which reshapes a layer from (512, 6, 8) to (512, 5, 8), where shape is (C,H,W)) with the builder.add_reshape(..., target_shape=(1, 512, 5, 8)) function in CoreMLTools.

I get the error RuntimeError: Error compiling model: "compiler error: Invalid dst shape8 x 6 x 512 x 1->1->8 x 5 x 512 x 0 x 1 x ". when the line model = MLModel(builder.spec) is executing.

Any idea what this error message means?

`ValueError: Keras layer '<class 'keras_resnet.layers._batch_normalization.BatchNormalization'>' not supported.` when trying to convert to coreml

I learned a model with keras-retinanet which seems to work pretty well, now I would like to convert it to a coreml model to use it in my iOS app:

import coremltools
coreml_model = coremltools.converters.keras.convert(model)
coreml_model.save('my_model.mlmodel')

Unfortunately I get the following error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-7-ba230c07a72c> in <module>()
      1 import coremltools
----> 2 coreml_model = coremltools.converters.keras.convert(model)
      3 # Saving the Core ML model to a file.
      4 coreml_model.save('my_model.mlmodel')

/home/jonas/projects/keras/keras-env/local/lib/python2.7/site-packages/coremltools/converters/keras/_keras_converter.pyc in convert(model, input_names, output_names, image_input_names, is_bgr, red_bias, green_bias, blue_bias, gray_bias, image_scale, class_labels, predicted_feature_name, model_precision, predicted_probabilities_output, add_custom_layers, custom_conversion_functions)
    743                       predicted_probabilities_output,
    744                       add_custom_layers,
--> 745                       custom_conversion_functions=custom_conversion_functions)
    746 
    747     return _MLModel(spec)

/home/jonas/projects/keras/keras-env/local/lib/python2.7/site-packages/coremltools/converters/keras/_keras_converter.pyc in convertToSpec(model, input_names, output_names, image_input_names, is_bgr, red_bias, green_bias, blue_bias, gray_bias, image_scale, class_labels, predicted_feature_name, model_precision, predicted_probabilities_output, add_custom_layers, custom_conversion_functions, custom_objects)
    541                                            add_custom_layers=add_custom_layers,
    542                                            custom_conversion_functions=custom_conversion_functions,
--> 543                                            custom_objects=custom_objects)
    544     else:
    545         raise RuntimeError(

/home/jonas/projects/keras/keras-env/local/lib/python2.7/site-packages/coremltools/converters/keras/_keras2_converter.pyc in _convert(model, input_names, output_names, image_input_names, is_bgr, red_bias, green_bias, blue_bias, gray_bias, image_scale, class_labels, predicted_feature_name, predicted_probabilities_output, add_custom_layers, custom_conversion_functions, custom_objects)
    185 
    186     # Check valid versions
--> 187     _check_unsupported_layers(model, add_custom_layers)
    188 
    189     # Build network graph to represent Keras model

/home/jonas/projects/keras/keras-env/local/lib/python2.7/site-packages/coremltools/converters/keras/_keras2_converter.pyc in _check_unsupported_layers(model, add_custom_layers)
     98         else:
     99             if type(layer) not in _KERAS_LAYER_REGISTRY:
--> 100                 raise ValueError("Keras layer '%s' not supported. " % str(type(layer)))
    101             if isinstance(layer, _keras.layers.wrappers.TimeDistributed):
    102                 if type(layer.layer) not in _KERAS_LAYER_REGISTRY:

ValueError: Keras layer '<class 'keras_resnet.layers._batch_normalization.BatchNormalization'>' not supported. 

I'm confused by that batchnormalization as i didn't find that string anywhere in this repo...

Is there a way to convert it to coreml and/or use it in an Android app? I searched for tutorials but didn't find any unfortunately.

Help converting Torch7 RGB Model into individual channel models?

I am using the https://github.com/prisma-ai/torch2coreml implementation to convert Torch7 models (trained with fast-neural-style) to mlmodels to use with CoreML. This works fine, but on devices that run CoreML Prediction on the CPU, it takes too much RAM and crashes.

I tried editing the convert-fast-neural-style.py python script, but when I run it I get the error:
RuntimeError: Need input of dimension 4 and input.size[1] == 3 but got input to be of shape: [1 x 1 x 1280 x 1280] at /pytorch/torch/lib/THNN/generic/SpatialConvolutionMM.c:47

Is it possible to convert the Torch7 RGB model into individual grayscale channel models? If so, can anyone offer suggestions or help?

If someone can help modify the script to create a separate model for each RGB channel I would be happy to compensate for their time and share the solution here.

Incorrect input shape

I have keras model like that:

inputlayer = Input(shape=(126,12))

model = BatchNormalization()(inputlayer)
model = Conv1D(16, 25, activation='relu')(model)
model = Flatten()(model)
model = Dense(output_size, activation='sigmoid')(model)

model = Model(inputs=inputlayer, outputs=model)

Which I convert to coreml:

coreml_model = coremltools.converters.keras.convert(model,
                                                    class_labels=classes)
coreml_model.save('speech_model.mlmodel')

So, I expect to see MultiArray (Double 126x12), but I see MultiArray (Double 12)

enter image description here

Could you help to say what I'm doing wrong?

Preprocessing for mean standardization.

I use Keras and use featurewise_center and featurewise_std_normalization. Then, I have mean and std for each channels.

But coreml provides only redBias, greenBias, blueBias and imageScale, so that I can not scale for each channels respectively.

Could you provide options something like redScale, greenScale and blueScale?

Error while converting a Keras model

Hi

I'm trying to do transfer learning for the inceptionv3 model in keras, when i try to convert the model to mlmodel I'm getting this error ->
File "/Users/alcaljos/anaconda/envs/captioning-image/lib/python2.7/site-packages/coremltools/converters/keras/_keras_converter.py", line 724, in convert
return _MLModel(spec)
File "/Users/alcaljos/anaconda/envs/captioning-image/lib/python2.7/site-packages/coremltools/models/model.py", line 153, in init
self.proxy = _get_proxy_from_spec(filename)
File "/Users/alcaljos/anaconda/envs/captioning-image/lib/python2.7/site-packages/coremltools/models/model.py", line 77, in _get_proxy_from_spec
return _MLModelProxy(filename)
RuntimeError: Error compiling model: "compiler error: Invalid data blob: 'conv2d_1_output' shape (C,H,W = 32,0,0) for output of layer: 'conv2d_1'.

Here is the transfer learning model ->
https://gist.github.com/josete89/04e6a687281a217da40fa7800bc807c2

Regards

The size of the output layer 'chairConfidence' in the neural network does not match the number of classes in the classifier.

Hi All,
I have created CNN successfully, when i tried implement that model into the iOS project i got the following error

The size of the output layer 'chairConfidence' in the neural network does not match the number of classes in the classifier.

code is

from keras.models import Sequential
from keras.layers import Conv2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dense
import coremltools
print("Installed")
# Initialising the CNN
classifier = Sequential()
# Step 1 - Convolution
classifier.add(Conv2D(32, (3, 3), input_shape = (64, 64, 3), activation = 'relu'))
# Step 2 - Pooling
classifier.add(MaxPooling2D(pool_size = (2, 2)))
# Adding a second convolutional layer
classifier.add(Conv2D(32, (3, 3), activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (2, 2)))
# Step 3 - Flattening
classifier.add(Flatten())
# Step 4 - Full connection
classifier.add(Dense(units = 128, activation = 'relu'))
classifier.add(Dense(units = 1, activation = 'sigmoid'))
# Compiling the CNN
classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
# Part 2 - Fitting the CNN to the images
from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(rescale = 1./255,
                                   shear_range = 0.2,
                                   zoom_range = 0.2,
                                   horizontal_flip = True)
test_datagen = ImageDataGenerator(rescale = 1./255)
training_set = train_datagen.flow_from_directory('dataset/training_set',
                                                 target_size = (64, 64),
                                                 batch_size = 2,
                                                 class_mode = 'binary')
test_set = test_datagen.flow_from_directory('dataset/test_set',
                                            target_size = (64, 64),
                                            batch_size = 2,
                                            class_mode = 'binary')
classifier.fit_generator(training_set,
                         steps_per_epoch = 42,
                         epochs = 5,
                         validation_data = test_set,
                         validation_steps = 8)
# Part 3 - Making new predictions
import numpy as np
from keras.preprocessing import image
test_image = image.load_img('dataset/single_prediction/1.jpeg', target_size = (64, 64))
test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis = 0)
result = classifier.predict(test_image)
training_set.class_indices
print(result[0][0])
if result[0][0] == 1:
    prediction = 'plastic'
else:
    prediction = 'metal'
print(prediction)
coreml_model = coremltools.converters.keras.convert(classifier, input_names=['image'], output_names=['chairConfidence'], class_labels=['metal','plastic'], image_input_names=['image'])
coreml_model.save('my_model.mlmodel')

Keras predict it properly, iOS app i got the error

Bug in shape inference logic involving sequences

Sequence != 1 case is not handled properly in the shape inference logic which is called by the graph visualization util. For instance, broadcasting for sequence axis has not been coded in for 'add', 'multiply' ops.

Fatal Python error: PyThreadState_Get: no current thread

Got this Error while trying to convert a Keras Model.

Keras Version: 2.0.6
Tensorflow Version: 1.1.0

Python 2.7 and the packages are installed within a virtual environment.

My search so far led me some stack overflow answers which suggest that it might be triggered by a wrongly linked .dylib/.so package. but I'm not an expert on this field.

Do you have any ideas how to resolve that problem?

That is the Output of my Conversion:

0 : lstm_1_input, <keras.engine.topology.InputLayer object at 0x122c77f50>
1 : lstm_1, <keras.layers.recurrent.LSTM object at 0x122d14f10>
2 : bidirectional_1, <keras.layers.wrappers.Bidirectional object at 0x122ecfb10>
3 : lstm_3, <keras.layers.recurrent.LSTM object at 0x123699a90>
4 : bidirectional_2, <keras.layers.wrappers.Bidirectional object at 0x123a44d90>
5 : lstm_5, <keras.layers.recurrent.LSTM object at 0x1241a4990>
6 : time_distributed_1, <keras.layers.wrappers.TimeDistributed object at 0x12450b8d0>
7 : time_distributed_1__activation__, <keras.layers.core.Activation object at 0x1255e6590>
Fatal Python error: PyThreadState_Get: no current thread

Layers Roadmap

Hello Coremltools Team,

is there a roadmap which layers you want to implement next? The GaussianDropout of Keras 2.0.4 is currently not incorporated but very important to us. And we want to integrate it to iOS with CoreML.

Kindest regards and thanks for your awesome converter!

Strange problem with Image Process in converter caffe model to coreml

As I try to convert a caffe model to coreml model, I use the parameter "isbgr" = True, but In objective c project I use a cvpixelbuffer which format is kCVPixelFormatType_32ARGB I got the same result with caffe model; But the "predictionFromData:" interface which coreml system auto generate suggest I use kCVPixelFormatType_32BGRA predict Image, but I got a wrong result; is this a bug? Do you have some reasonable explanation;?

Unsupported option input_shape

I'm trying to convert my YOLO keras model to CoreML. Those are the last 2 layers

model.add(Activation('linear'))
model.add(Reshape((GRID_H, GRID_W, BOX, 4 + 1 + CLASS)))

Now when I want to convert this model with coremltools it complains

...
31 : conv2d_8, <keras.layers.convolutional.Conv2D object at 0x1149f8a50>
32 : activation_1, <keras.layers.core.Activation object at 0x114b87b90>
33 : reshape_1, <keras.layers.core.Reshape object at 0x114be7f10>
Traceback (most recent call last):
  File "./tinyyolo.py", line 160, in <module>
    coreml_model = coremltools.converters.keras.convert(model, image_input_names='yolo_in', output_names='reshape_1')
  File "/Users/era/git/coremltools/coremltools/converters/keras/_keras_converter.py", line 505, in convert
    predicted_probabilities_output = predicted_probabilities_output)
  File "/Users/era/git/coremltools/coremltools/converters/keras/_keras2_converter.py", line 278, in _convert
    converter_func(builder, layer, input_names, output_names, keras_layer)
  File "/Users/era/git/coremltools/coremltools/converters/keras/_layers2.py", line 858, in convert_reshape
    _utils.raise_error_unsupported_categorical_option('input_shape', str(input_shape), 'reshape', layer)
  File "/Users/era/git/coremltools/coremltools/converters/keras/_utils.py", line 12, in raise_error_unsupported_categorical_option
    layer_type, layer_name))
RuntimeError: Unsupported option input_shape=(None, 13, 13, 160) in layer reshape(reshape_1)

Installed versions:

Keras (2.1.1)
coremltools (0.6.3)

Complete layer stack

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
yolo_in (Conv2D)             (None, 416, 416, 16)      432       
_________________________________________________________________
batch_normalization_1 (Batch (None, 416, 416, 16)      64        
_________________________________________________________________
leaky_re_lu_1 (LeakyReLU)    (None, 416, 416, 16)      0         
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 208, 208, 16)      0         
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 208, 208, 32)      4608      
_________________________________________________________________
batch_normalization_2 (Batch (None, 208, 208, 32)      128       
_________________________________________________________________
leaky_re_lu_2 (LeakyReLU)    (None, 208, 208, 32)      0         
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 104, 104, 32)      0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 104, 104, 64)      18432     
_________________________________________________________________
batch_normalization_3 (Batch (None, 104, 104, 64)      256       
_________________________________________________________________
leaky_re_lu_3 (LeakyReLU)    (None, 104, 104, 64)      0         
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 52, 52, 64)        0         
_________________________________________________________________
conv2d_3 (Conv2D)            (None, 52, 52, 128)       73728     
_________________________________________________________________
batch_normalization_4 (Batch (None, 52, 52, 128)       512       
_________________________________________________________________
leaky_re_lu_4 (LeakyReLU)    (None, 52, 52, 128)       0         
_________________________________________________________________
max_pooling2d_4 (MaxPooling2 (None, 26, 26, 128)       0         
_________________________________________________________________
conv2d_4 (Conv2D)            (None, 26, 26, 256)       294912    
_________________________________________________________________
batch_normalization_5 (Batch (None, 26, 26, 256)       1024      
_________________________________________________________________
leaky_re_lu_5 (LeakyReLU)    (None, 26, 26, 256)       0         
_________________________________________________________________
max_pooling2d_5 (MaxPooling2 (None, 13, 13, 256)       0         
_________________________________________________________________
conv2d_5 (Conv2D)            (None, 13, 13, 512)       1179648   
_________________________________________________________________
batch_normalization_6 (Batch (None, 13, 13, 512)       2048      
_________________________________________________________________
leaky_re_lu_6 (LeakyReLU)    (None, 13, 13, 512)       0         
_________________________________________________________________
max_pooling2d_6 (MaxPooling2 (None, 13, 13, 512)       0         
_________________________________________________________________
conv2d_6 (Conv2D)            (None, 13, 13, 1024)      4718592   
_________________________________________________________________
batch_normalization_7 (Batch (None, 13, 13, 1024)      4096      
_________________________________________________________________
leaky_re_lu_7 (LeakyReLU)    (None, 13, 13, 1024)      0         
_________________________________________________________________
conv2d_7 (Conv2D)            (None, 13, 13, 1024)      9437184   
_________________________________________________________________
batch_normalization_8 (Batch (None, 13, 13, 1024)      4096      
_________________________________________________________________
leaky_re_lu_8 (LeakyReLU)    (None, 13, 13, 1024)      0         
_________________________________________________________________
conv2d_8 (Conv2D)            (None, 13, 13, 160)       164000    
_________________________________________________________________
activation_1 (Activation)    (None, 13, 13, 160)       0         
_________________________________________________________________
reshape_1 (Reshape)          (None, 13, 13, 5, 32)     0         
=================================================================
Total params: 15,903,760
Trainable params: 15,897,648
Non-trainable params: 6,112

Error converting while converting .mlmodel

I tried converting my trained model(.h5) to coreml, but it fails giving bad marshal data error.
coremltools (0.8)
h5py (2.7.1)
Keras (1.2.2)
tensorflow (1.4.1)
tensorflow-tensorboard (0.4.0)

Error Description:-
Traceback (most recent call last):
File "python.py", line 2, in
coreml_model = coremltools.converters.keras.convert('/home/depam/Downloads/Archive/keyboard.h5')
File "/coreml/local/lib/python2.7/site-packages/coremltools/converters/keras/_keras_converter.py", line 745, in convert
custom_conversion_functions=custom_conversion_functions)
File "/coreml/local/lib/python2.7/site-packages/coremltools/converters/keras/_keras_converter.py", line 525, in convertToSpec
custom_objects=custom_objects)
File "/coreml/local/lib/python2.7/site-packages/coremltools/converters/keras/_keras_converter.py", line 161, in _convert
model = _keras.models.load_model(model, custom_objects = custom_objects)
File "/coreml/local/lib/python2.7/site-packages/keras/models.py", line 142, in load_model
model = model_from_config(model_config, custom_objects=custom_objects)
File "/coreml/local/lib/python2.7/site-packages/keras/models.py", line 193, in model_from_config
return layer_from_config(config, custom_objects=custom_objects)
File "/coreml/local/lib/python2.7/site-packages/keras/utils/layer_utils.py", line 40, in layer_from_config
custom_objects=custom_objects)
File "/coreml/local/lib/python2.7/site-packages/keras/engine/topology.py", line 2582, in from_config
process_layer(layer_data)
File "/coreml/local/lib/python2.7/site-packages/keras/engine/topology.py", line 2560, in process_layer
custom_objects=custom_objects)
File "/coreml/local/lib/python2.7/site-packages/keras/utils/layer_utils.py", line 40, in layer_from_config
custom_objects=custom_objects)
File "/coreml/local/lib/python2.7/site-packages/keras/layers/core.py", line 682, in from_config
function = func_load(config['function'], globs=globs)
File "/coreml/local/lib/python2.7/site-packages/keras/utils/generic_utils.py", line 183, in func_load
code = marshal.loads(code.encode('raw_unicode_escape'))
ValueError: bad marshal data (unknown type code)

Keras converter gives incorrect output shapes

Converting the following Keras model (taken from test_keras2.py:test_permute) results in an incorrect output shape in the .mlmodel file.

    keras_model = Sequential()
    keras_model.add(Permute((3, 2, 1), input_shape=(10, 64,3)))

    input_names = ['input']
    output_names = ['output']
    coreml_model = keras.convert(model, input_names, output_names)

   print coreml_model.get_spec()

Results in:

specificationVersion: 1
description {
input {
name: "input"
type {
multiArrayType {
shape: 3
shape: 10
shape: 64
dataType: DOUBLE
}
}
}
output {
name: "output"
type {
multiArrayType {
shape: 10
shape: 3
shape: 64
dataType: DOUBLE
}
}
}
}
neuralNetwork {
layers {
name: "permute_1"
input: "input"
output: "output"
permute {
axis: 0
axis: 1
axis: 3
axis: 2
}
}
}

The parameters of the permute layer specify that the last two dimensions should be swapped, but the output description has the channel and height of the input swapped (corresponding to a permutation layer (0, 2, 1, 3).

Strange behaviour when attempting to evaluate batches of sequences

I have the following model defined in keras which I convert to CoreML:

SEQUENCE_LENGTH = 8000
NUM_CATS = 1025

inputs = Input(shape=(SEQUENCE_LENGTH,))
L0 = Reshape((SEQUENCE_LENGTH,1))(inputs)
L1 = Conv1D(filters=4,
             kernel_size=4,
             strides=4, 
             padding='same', 
             activation='relu')(L0)
L2  = Dense(32, activation='relu')(L1)
L3 = Dense(NUM_CATS, activation='softmax')(L2)

model = Model(inputs=inputs, outputs=L3)

If I run a prediction with one sequence it appears to work correctly:

inputx = np.array(X[0])
print(inputx.shape)
t0 = time.time()
print("CoreML model output shape: ", coreml_model.predict({'input1': inputx})['output1'].shape)
print("Time taken: ", time.time()-t0)

(8000,)
CoreML model output shape:  (2000, 1025)
Time taken:  0.049160003662109375

However if I run it on multiple sequences the output shape is incorrect:

inputx = np.array([X[0:20]])
print(inputx.shape)
t0 = time.time()
print("CoreML model output shape: ", coreml_model.predict({'input1': inputx})['output1'].shape)
print("Time taken: ", time.time()-t0)

(1, 20, 8000)
CoreML model output shape:  (2000, 1025)
Time taken:  0.5843930244445801

The output shape should be (20, 2000, 1025) not (2000, 1025). The time taken to process has increased by 20x so something is happening. As things stand without the ability to process in batches evaluating this particular model on CPU is faster than GPU.

Memory allocation error during Keras conversion

I have run into an issue with conversion between Keras and CoreML. Here's the output:

python(823,0x7fff9a083340) malloc: *** mach_vm_map(size=18446744072360034304) failed (error code=3)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
Neural Network compiler 11: 420 , name = lstm_1, Traceback (most recent call last):
File "", line 1, in
File "coremltools/converters/keras/_keras_converter.py", line 747, in convert
return _MLModel(spec)
File "coremltools/models/model.py", line 153, in init
self.proxy = _get_proxy_from_spec(filename)
File "coremltools/models/model.py", line 77, in _get_proxy_from_spec
return _MLModelProxy(filename)
RuntimeError: Error compiling model: "compiler error: Memory allocation error: ?>18446744072360034304".

I have tried converting the same model both from the current release, version 7.0, as well as the current master built from source. Anyone have any thoughts? I should also note that the converter script takes around 2.5 hours before getting to this point. It seems to consume a sizable amount of memory as well, around 15-20GB

Keras -> 2.0.6
tensorflow -> 1.2.1

the model is as follows:

# Instantiate model
model = Sequential()

model.add(Conv2D(64, (3, 3), activation='relu', data_format = 'channels_last', input_shape = (224, 224, 3)))
model.add(MaxPooling2D((2, 2), strides=(1, 1)))

model.add(Conv2D(128, (4,4), activation='relu'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))

model.add(Conv2D(256, (4,4), activation='relu'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))

model.add(Flatten())
model.add(Dropout(0.5))
model.add(Reshape((1, 719104)))

model.add(LSTM(256, return_sequences=True, return_state=False, dropout=0.5))

model.add(Dense(num_classes, activation='sigmoid'))

EDIT:

I was attempting to run this on Mac OS previously, but I decided to have a go on an Ubuntu system. I got a single line output that just said "killed" which seems to mean the kernel killed the process. probably due to excessive resource usage. Sounds like a memory leak

It seems to hang once it hits the LSTM layer and that's when the memory usage spikes to 22GB. It stays there for around 2 hours then throws the error.

screen shot 2018-01-30 at 6 07 45 am

screen shot 2018-01-30 at 6 09 24 am

Probabilities Do Not Match for Scikit Learn's GradientBoostingClassifier

from sklearn.ensemble import GradientBoostingClassifier
from sklearn.datasets import load_boston
import numpy as np
import pandas as pd

from coremltools.converters import sklearn as skl_converter

args = {'max_leaf_nodes': None, 'min_samples_leaf': 1, 'min_samples_split': 2,
        'min_weight_fraction_leaf': 0.5, 'max_features': 1, 'max_depth': 1}

scikit_model = GradientBoostingClassifier(random_state = 1, **args)

scikit_data = load_boston()
X = scikit_data.data.astype('f').astype('d')

num_classes = 3
t = scikit_data.target
target = np.digitize(t, np.histogram(t, bins = num_classes - 1)[1]) - 1
scikit_model.fit(X, target)

coreml_model = skl_converter.convert(scikit_model, scikit_data.feature_names)

df = pd.DataFrame(X, columns=scikit_data.feature_names)

indexs_for_wrong_predictions = []
for i, row in df.iterrows():
    predicted = coreml_model.predict(dict(row))
    print(predicted['classProbability'], scikit_model.predict_proba([X[i]]))

Note the sets of probabilities on each line don't match. They are typically off by several percentage points.

This issue is causing a unit test failure

Keras imported model not working properly in CoreML

I am creating a out-of-the-box ResNet50 (or any other model you like from keras) model as follows:

from keras.layers.core import Flatten
initial_model = keras.applications.ResNet50(include_top=True, weights='imagenet')
initial_model.save("original.h5")
convert = coremltools.converters.keras.convert(
    "original.h5", 
    input_names=['img'], 
    image_input_names=['img'], 
    class_labels=list(map(str, range(1000)))
)
convert.save("orig.mlmodel")

However when imported into my basic "point-and-classify" dummy app I get terrible classification results with the winning class always having probability 1 (weirdly enough for 95% of the frames it's class with index 641).

Swift code for the implementation of this is fairly straightforward:

import UIKit
import AVKit
import Vision

class XYZ: UIViewController, AVCaptureVideoDataOutputSampleBufferDelegate{
  override func viewDidLoad() {
    super.viewDidLoad()
    
    let captureSession = AVCaptureSession()
    
    guard let captureDevice = AVCaptureDevice.default(for: .video) else { return }
    guard let input = try? AVCaptureDeviceInput(device: captureDevice) else { return }
    captureSession.addInput(input)
    captureSession.startRunning()
    
    let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
    view.layer.addSublayer(previewLayer)
    previewLayer.frame = view.frame
    
    let dataOutput = AVCaptureVideoDataOutput()
    dataOutput.setSampleBufferDelegate(self, queue: DispatchQueue(label: "videoQueue"))
    captureSession.addOutput(dataOutput)
    
  }
  
  func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
    guard let pixelBuffer: CVPixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else { return }
    
    guard let model = try? VNCoreMLModel(for: orig().model) else { return }
    let request = VNCoreMLRequest(model: model) { (finishedReq, err) in
      guard let results = finishedReq.results as? [VNClassificationObservation] else {
        return
      }
      guard let firstObservation = results.first else { return }
      print(firstObservation.identifier, firstObservation.confidence)      
    }
    
    try? VNImageRequestHandler(cvPixelBuffer: pixelBuffer, options: [:]).perform([request])
  }
  
}

How come performances are that far from officially apple-supplied mlmodels?

Problem with input of layer

I have model layer with several output:

def MobileNetEncoder(input_shape,
                     alpha=1.0,
                     depth_multiplier=1): 
    img_input = Input(shape=input_shape)
    b00 = _conv_block(img_input, 32, alpha, strides=(2, 2), block_id=0)
    ...
    b13 = _depthwise_conv_block(b12, 1024, alpha, depth_multiplier, block_id=13)
    model = Model(img_input,[b00, b13])
    return model

Then I 've made model with this layer:

    img_input = Input(shape=input_shape)

    height, width, channels = input_shape

    encoder =  MobileNetEncoder(input_shape)
    encoder =  MobileNetEncoder(input_shape)
    encoder_output =encoder(img_input)
    output = Conv2D(30,
                    (1, 1), strides=(1, 1),
                    padding='same',
                    name='conv_23',
                    kernel_initializer='lecun_normal')(encoder_output[-1])
    model = Model(img_input, yolo_model)

I looked in debug mode that 'conv_23' layer was connected to 'depthwise_conv_block_13' of MobileNetEncoder. But after conversation model to coreml, I saw that 'conv_23' layer was connected to 'conv_0' of MobileNetEncoder.

layers {
  name: "conv_23"
  input: "model_1_conv_0_relu_output"
  output: "conv_23"
  convolution {
    outputChannels: 30
    kernelChannels: 1024
    nGroups: 1
    kernelSize: 1
    kernelSize: 1
    stride: 1
    stride: 1
    dilationFactor: 1
    dilationFactor: 1
    same {
    }
    hasBias: true
    weights {
    }
    bias {
    }
  }
}

What should I do to fix this problem?

Error converting Caffe model

Im getting the following error when trying to convert a simple Caffe model:
RuntimeError: Caffe model error in layer 'hidden' of type 'Inner Product': 'bias_term' flag and blob size for bias incompatible.

I'm using DIGITS on Nvidia's Docker image 18.01 to train a Caffe model following the instructions found here. After downloading and unpacking the model I run the following in ipython:

import coremltools
model = coremltools.converters.caffe.convert('snapshot_iter_1890.caffemodel')

which produces the error above. Adding the deploy.prototxt as a parameter does not help.

Input shapes (height and width dimensions) must be equal for layer

In my convolutional network, when I attempt to concat (function builder.add_elementwise) two convolutional layers (function builder.add_convolutional); one being a deconv layer and the other a conv layer, I get the error Input shapes (height and width dimensions) must be equal for layer: 'upcnv7_concat'.

Is there anyway to check the shapes of each layer within the NeuralNetworkBuilder object?

Error converting while converting .mlmodel

I'm trying to convert my trained model(.h5) to coreml, but it fails.

coremltools (0.8)
h5py (2.7.1)
Keras (2.1.3)
tensorflow (1.5.0)
tensorflow-tensorboard (1.5.1)

Error Description:
/home/depam/Desktop/mlvirtualenv/pythonenv/local/lib/python2.7/site-packages/keras/engine/topology.py:1269: UserWarning: Update your InputLayer call to the Keras 2 API: InputLayer(dtype="float32", batch_input_shape=[None, 416..., sparse=False, name="input_1")
return cls(**config)
/home/depam/Desktop/mlvirtualenv/pythonenv/local/lib/python2.7/site-packages/keras/engine/topology.py:1269: UserWarning: Update your Conv2D call to the Keras 2 API: Conv2D(kernel_initializer="glorot_uniform", kernel_constraint=None, activity_regularizer=None, trainable=True, padding="same", strides=[1, 1], filters=32, use_bias=False, name="convolution2d_1", bias_regularizer=None, bias_constraint=None, data_format="channels_last", kernel_regularizer={u'l2': 0...., activation="linear", kernel_size=(3, 3))
return cls(**config)
Traceback (most recent call last):
File "python.py", line 2, in
coreml_model = coremltools.converters.keras.convert('/home/depam/Downloads/Archive/keyboard.h5')
File "/home/depam/Desktop/mlvirtualenv/pythonenv/local/lib/python2.7/site-packages/coremltools/converters/keras/_keras_converter.py", line 745, in convert
custom_conversion_functions=custom_conversion_functions)
File "/home/depam/Desktop/mlvirtualenv/pythonenv/local/lib/python2.7/site-packages/coremltools/converters/keras/_keras_converter.py", line 543, in convertToSpec
custom_objects=custom_objects)
File "/home/depam/Desktop/mlvirtualenv/pythonenv/local/lib/python2.7/site-packages/coremltools/converters/keras/_keras2_converter.py", line 182, in _convert
model = _keras.models.load_model(model, custom_objects = custom_objects)
File "/home/depam/Desktop/mlvirtualenv/pythonenv/local/lib/python2.7/site-packages/keras/models.py", line 243, in load_model
model = model_from_config(model_config, custom_objects=custom_objects)
File "/home/depam/Desktop/mlvirtualenv/pythonenv/local/lib/python2.7/site-packages/keras/models.py", line 317, in model_from_config
return layer_module.deserialize(config, custom_objects=custom_objects)
File "/home/depam/Desktop/mlvirtualenv/pythonenv/local/lib/python2.7/site-packages/keras/layers/init.py", line 55, in deserialize
printable_module_name='layer')
File "/home/depam/Desktop/mlvirtualenv/pythonenv/local/lib/python2.7/site-packages/keras/utils/generic_utils.py", line 143, in deserialize_keras_object
list(custom_objects.items())))
File "/home/depam/Desktop/mlvirtualenv/pythonenv/local/lib/python2.7/site-packages/keras/engine/topology.py", line 2507, in from_config
process_layer(layer_data)
File "/home/depam/Desktop/mlvirtualenv/pythonenv/local/lib/python2.7/site-packages/keras/engine/topology.py", line 2493, in process_layer
custom_objects=custom_objects)
File "/home/depam/Desktop/mlvirtualenv/pythonenv/local/lib/python2.7/site-packages/keras/layers/init.py", line 55, in deserialize
printable_module_name='layer')
File "/home/depam/Desktop/mlvirtualenv/pythonenv/local/lib/python2.7/site-packages/keras/utils/generic_utils.py", line 145, in deserialize_keras_object
return cls.from_config(config['config'])
File "/home/depam/Desktop/mlvirtualenv/pythonenv/local/lib/python2.7/site-packages/keras/engine/topology.py", line 1269, in from_config
return cls(**config)
File "/home/depam/Desktop/mlvirtualenv/pythonenv/local/lib/python2.7/site-packages/keras/legacy/interfaces.py", line 91, in wrapper
return func(*args, **kwargs)
File "/home/depam/Desktop/mlvirtualenv/pythonenv/local/lib/python2.7/site-packages/keras/layers/convolutional.py", line 462, in init
**kwargs)
File "/home/depam/Desktop/mlvirtualenv/pythonenv/local/lib/python2.7/site-packages/keras/layers/convolutional.py", line 116, in init
self.kernel_regularizer = regularizers.get(kernel_regularizer)
File "/home/depam/Desktop/mlvirtualenv/pythonenv/local/lib/python2.7/site-packages/keras/regularizers.py", line 80, in get
return deserialize(identifier)
File "/home/depam/Desktop/mlvirtualenv/pythonenv/local/lib/python2.7/site-packages/keras/regularizers.py", line 73, in deserialize
printable_module_name='regularizer')
File "/home/depam/Desktop/mlvirtualenv/pythonenv/local/lib/python2.7/site-packages/keras/utils/generic_utils.py", line 126, in deserialize_keras_object
raise ValueError('Improper config format: ' + str(config))
ValueError: Improper config format: {u'l2': 0.0005000000237487257, u'name': u'L1L2Regularizer', u'l1': 0.0}

Model prediction does not work in Python 3

With Python 3, the prediction code path using PyBind11 doesn't quite work. This is required to have full functionality with Python 3

Here is a simle repro step (this works in Python 2.7)
``
from sklearn import datasets
from sklearn.ensemble import RandomForestClassifier
import coremltools
import numpy

Train a scikit-learn model for Iris data

iris = datasets.load_iris()
clf = RandomForestClassifier()
clf.fit(iris.data, iris.target_names[iris.target])

Convert the model

model = coremltools.converters.sklearn.convert(clf)

Make predictions

model.predict({'input': np.zeros(4)})
``

Try 11.2 coreML custom layer converter

today , I have try the new method in iOS 11.2, I find only support keras. so I find a keras model, and follow the step in offical document, but I am fail in first step;
image
is there any one can help ?

Error converting Keras model

I'm trying to convert saved YOLOv2 model(.h5) to coreml, but it fails.

coremltools (0.8)
h5py (2.7.1)
Keras (2.1.3)
tensorflow (1.4.1)
tensorflow-tensorboard (0.4.0)

I cannot find ..libcoremlpython, I have no idea why the error occurs there.

Traceback (most recent call last):
  File "convert_to_coreml.py", line 20, in <module>
    image_scale=1/255.)
  File "/Users/WeiJay/anaconda/envs/py2/lib/python2.7/site-packages/coremltools/converters/keras/_keras_converter.py", line 747, in convert
    return _MLModel(spec)
  File "/Users/WeiJay/anaconda/envs/py2/lib/python2.7/site-packages/coremltools/models/model.py", line 153, in __init__
    self.__proxy__ = _get_proxy_from_spec(filename)
  File "/Users/WeiJay/anaconda/envs/py2/lib/python2.7/site-packages/coremltools/models/model.py", line 77, in _get_proxy_from_spec
    return _MLModelProxy(filename)
RuntimeError: Error compiling model: "compiler error:  Error saving network to file.".

Also, my model seems fine in model.summary().

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         (None, 416, 416, 3)       0         
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 416, 416, 16)      432       
_________________________________________________________________
batch_normalization_1 (Batch (None, 416, 416, 16)      64        
_________________________________________________________________
leaky_re_lu_1 (LeakyReLU)    (None, 416, 416, 16)      0         
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 208, 208, 16)      0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 208, 208, 32)      4608      
_________________________________________________________________
batch_normalization_2 (Batch (None, 208, 208, 32)      128       
_________________________________________________________________
leaky_re_lu_2 (LeakyReLU)    (None, 208, 208, 32)      0         
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 104, 104, 32)      0         
_________________________________________________________________
conv2d_3 (Conv2D)            (None, 104, 104, 64)      18432     
_________________________________________________________________
batch_normalization_3 (Batch (None, 104, 104, 64)      256       
_________________________________________________________________
leaky_re_lu_3 (LeakyReLU)    (None, 104, 104, 64)      0         
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 52, 52, 64)        0         
_________________________________________________________________
conv2d_4 (Conv2D)            (None, 52, 52, 128)       73728     
_________________________________________________________________
batch_normalization_4 (Batch (None, 52, 52, 128)       512       
_________________________________________________________________
leaky_re_lu_4 (LeakyReLU)    (None, 52, 52, 128)       0         
_________________________________________________________________
max_pooling2d_4 (MaxPooling2 (None, 26, 26, 128)       0         
_________________________________________________________________
conv2d_5 (Conv2D)            (None, 26, 26, 256)       294912    
_________________________________________________________________
batch_normalization_5 (Batch (None, 26, 26, 256)       1024      
_________________________________________________________________
leaky_re_lu_5 (LeakyReLU)    (None, 26, 26, 256)       0         
_________________________________________________________________
max_pooling2d_5 (MaxPooling2 (None, 13, 13, 256)       0         
_________________________________________________________________
conv2d_6 (Conv2D)            (None, 13, 13, 512)       1179648   
_________________________________________________________________
batch_normalization_6 (Batch (None, 13, 13, 512)       2048      
_________________________________________________________________
leaky_re_lu_6 (LeakyReLU)    (None, 13, 13, 512)       0         
_________________________________________________________________
max_pooling2d_6 (MaxPooling2 (None, 13, 13, 512)       0         
_________________________________________________________________
conv2d_7 (Conv2D)            (None, 13, 13, 1024)      4718592   
_________________________________________________________________
batch_normalization_7 (Batch (None, 13, 13, 1024)      4096      
_________________________________________________________________
leaky_re_lu_7 (LeakyReLU)    (None, 13, 13, 1024)      0         
_________________________________________________________________
conv2d_8 (Conv2D)            (None, 13, 13, 512)       4718592   
_________________________________________________________________
batch_normalization_8 (Batch (None, 13, 13, 512)       2048      
_________________________________________________________________
leaky_re_lu_8 (LeakyReLU)    (None, 13, 13, 512)       0         
_________________________________________________________________
conv2d_9 (Conv2D)            (None, 13, 13, 425)       218025    
=================================================================
Total params: 11,237,145
Trainable params: 11,232,057
Non-trainable params: 5,088
_________________________________________________________________
0 : input_1, <keras.engine.topology.InputLayer object at 0x11d8fd590>
1 : conv2d_1, <keras.layers.convolutional.Conv2D object at 0x11d8fd910>
2 : batch_normalization_1, <keras.layers.normalization.BatchNormalization object at 0x11d8fd9d0>
/Users/WeiJay/anaconda/envs/py2/lib/python2.7/site-packages/coremltools/converters/keras/_layers2.py:499: RuntimeWarning: invalid value encountered in sqrt
  f = 1.0 / _np.sqrt(std + keras_layer.epsilon)
3 : leaky_re_lu_1, <keras.layers.advanced_activations.LeakyReLU object at 0x11d8fdb90>
4 : max_pooling2d_1, <keras.layers.pooling.MaxPooling2D object at 0x11d8fd810>
5 : conv2d_2, <keras.layers.convolutional.Conv2D object at 0x11d8fd850>
6 : batch_normalization_2, <keras.layers.normalization.BatchNormalization object at 0x11d8fda50>
7 : leaky_re_lu_2, <keras.layers.advanced_activations.LeakyReLU object at 0x11d8fdc10>
8 : max_pooling2d_2, <keras.layers.pooling.MaxPooling2D object at 0x11d8fddd0>
9 : conv2d_3, <keras.layers.convolutional.Conv2D object at 0x11d9383d0>
10 : batch_normalization_3, <keras.layers.normalization.BatchNormalization object at 0x11d9385d0>
11 : leaky_re_lu_3, <keras.layers.advanced_activations.LeakyReLU object at 0x11d938750>
12 : max_pooling2d_3, <keras.layers.pooling.MaxPooling2D object at 0x11d938650>
13 : conv2d_4, <keras.layers.convolutional.Conv2D object at 0x11d9387d0>
14 : batch_normalization_4, <keras.layers.normalization.BatchNormalization object at 0x11d938810>
15 : leaky_re_lu_4, <keras.layers.advanced_activations.LeakyReLU object at 0x11d938950>
16 : max_pooling2d_4, <keras.layers.pooling.MaxPooling2D object at 0x11d938a90>
17 : conv2d_5, <keras.layers.convolutional.Conv2D object at 0x11d938b10>
18 : batch_normalization_5, <keras.layers.normalization.BatchNormalization object at 0x11d938b50>
19 : leaky_re_lu_5, <keras.layers.advanced_activations.LeakyReLU object at 0x11d938c90>
20 : max_pooling2d_5, <keras.layers.pooling.MaxPooling2D object at 0x11d938dd0>
21 : conv2d_6, <keras.layers.convolutional.Conv2D object at 0x11d938e50>
22 : batch_normalization_6, <keras.layers.normalization.BatchNormalization object at 0x11d938e90>
23 : leaky_re_lu_6, <keras.layers.advanced_activations.LeakyReLU object at 0x11d938fd0>
24 : max_pooling2d_6, <keras.layers.pooling.MaxPooling2D object at 0x11d94d150>
25 : conv2d_7, <keras.layers.convolutional.Conv2D object at 0x11d94d1d0>
26 : batch_normalization_7, <keras.layers.normalization.BatchNormalization object at 0x11d94d210>
27 : leaky_re_lu_7, <keras.layers.advanced_activations.LeakyReLU object at 0x11d94d350>
28 : conv2d_8, <keras.layers.convolutional.Conv2D object at 0x11d94d490>
29 : batch_normalization_8, <keras.layers.normalization.BatchNormalization object at 0x11d94d4d0>
30 : leaky_re_lu_8, <keras.layers.advanced_activations.LeakyReLU object at 0x11d94d610>
31 : conv2d_9, <keras.layers.convolutional.Conv2D object at 0x11d94d750>
Input name(s) and shape(s): 
image : (C,H,W) = (3, 416, 416) 
Neural Network compiler 0: 100 , name = conv2d_1, output shape : (C,H,W) = (16, 416, 416) 
Neural Network compiler 1: 160 , name = batch_normalization_1, output shape : (C,H,W) = (16, 416, 416) 
Neural Network compiler 2: 130 , name = leaky_re_lu_1, output shape : (C,H,W) = (16, 416, 416) 
Neural Network compiler 3: 120 , name = max_pooling2d_1, output shape : (C,H,W) = (16, 208, 208) 
Neural Network compiler 4: 100 , name = conv2d_2, output shape : (C,H,W) = (32, 208, 208) 
Neural Network compiler 5: 160 , name = batch_normalization_2, output shape : (C,H,W) = (32, 208, 208) 
Neural Network compiler 6: 130 , name = leaky_re_lu_2, output shape : (C,H,W) = (32, 208, 208) 
Neural Network compiler 7: 120 , name = max_pooling2d_2, output shape : (C,H,W) = (32, 104, 104) 
Neural Network compiler 8: 100 , name = conv2d_3, output shape : (C,H,W) = (64, 104, 104) 
Neural Network compiler 9: 160 , name = batch_normalization_3, output shape : (C,H,W) = (64, 104, 104) 
Neural Network compiler 10: 130 , name = leaky_re_lu_3, output shape : (C,H,W) = (64, 104, 104) 
Neural Network compiler 11: 120 , name = max_pooling2d_3, output shape : (C,H,W) = (64, 52, 52) 
Neural Network compiler 12: 100 , name = conv2d_4, output shape : (C,H,W) = (128, 52, 52) 
Neural Network compiler 13: 160 , name = batch_normalization_4, output shape : (C,H,W) = (128, 52, 52) 
Neural Network compiler 14: 130 , name = leaky_re_lu_4, output shape : (C,H,W) = (128, 52, 52) 
Neural Network compiler 15: 120 , name = max_pooling2d_4, output shape : (C,H,W) = (128, 26, 26) 
Neural Network compiler 16: 100 , name = conv2d_5, output shape : (C,H,W) = (256, 26, 26) 
Neural Network compiler 17: 160 , name = batch_normalization_5, output shape : (C,H,W) = (256, 26, 26) 
Neural Network compiler 18: 130 , name = leaky_re_lu_5, output shape : (C,H,W) = (256, 26, 26) 
Neural Network compiler 19: 120 , name = max_pooling2d_5, output shape : (C,H,W) = (256, 13, 13) 
Neural Network compiler 20: 100 , name = conv2d_6, output shape : (C,H,W) = (512, 13, 13) 
Neural Network compiler 21: 160 , name = batch_normalization_6, output shape : (C,H,W) = (512, 13, 13) 
Neural Network compiler 22: 130 , name = leaky_re_lu_6, output shape : (C,H,W) = (512, 13, 13) 
Neural Network compiler 23: 120 , name = max_pooling2d_6, output shape : (C,H,W) = (512, 13, 13) 
Neural Network compiler 24: 100 , name = conv2d_7, output shape : (C,H,W) = (1024, 13, 13) 
Neural Network compiler 25: 160 , name = batch_normalization_7, output shape : (C,H,W) = (1024, 13, 13) 
Neural Network compiler 26: 130 , name = leaky_re_lu_7, output shape : (C,H,W) = (1024, 13, 13) 
Neural Network compiler 27: 100 , name = conv2d_8, output shape : (C,H,W) = (512, 13, 13) 
Neural Network compiler 28: 160 , name = batch_normalization_8, output shape : (C,H,W) = (512, 13, 13) 
Neural Network compiler 29: 130 , name = leaky_re_lu_8, output shape : (C,H,W) = (512, 13, 13) 
Neural Network compiler 30: 100 , name = conv2d_9, output shape : (C,H,W) = (425, 13, 13) 

"ValueError: No outputs can be identified" when converting Keras Tensorflow model with coremltools

The latest changes ( 5b9ca1a ), to _topology2.py, has fixed the index out of range issue but it seems that there is still a problem with detecting the input and output layers/names.

The conversion script:

import sys
sys.path.insert(0, r'/Users/corneel/GitHub/KerasTests2')
import coremltools
from keras.models import load_model
model = load_model('final_model.h5')
print("Loaded")

coreml_model = coremltools.converters.keras.convert(model,
    input_names="input_1",
    image_input_names="input_1",
    output_names="sequential_1",
    add_custom_layers=True)

coreml_model.save('final3.mlmodel')

Traceback (most recent call last):

File "Convert.py", line 15, in <module> add_custom_layers=True)
File "/Users/corneel/GitHub/KerasTests2/coremltools/converters/keras/_keras_converter.py" line 745, in convert custom_conversion_functions=custom_conversion_functions)
File "/Users/corneel/GitHub/KerasTests2/coremltools/converters/keras/_keras_converter.py", line 543, in convertToSpec custom_objects=custom_objects)
File "/Users/corneel/GitHub/KerasTests2/coremltools/converters/keras/_keras2_converter.py", line 191, in _convert graph.build()
File "/Users/corneel/GitHub/KerasTests2/coremltools/converters/keras/_topology2.py", line 704, in build self.make_output_layers()
File "/Users/corneel/GitHub/KerasTests2/coremltools/converters/keras/_topology2.py", line 185, in make_output_layers raise ValueError("No outputs can be identified")
ValueError: No outputs can be identified

The failure is as a result of this piece of code in _topology2.py

I was following the guidance in the article: "https://blog.keras.io/building-powerful-image-classification-models-using-very-little-data.html" by @fchollet (author of Keras). The model is a VGG-like convnet , seeded with a pretrained network (imagenet). As far as I can see my network behaves perfectly, the extra images I loaded is detected (predicted) and any report I run on this network, completes without any problem. I was also careful to use the versions of software you prescribed (pip_freeze.txt (coremltools does not show there because I use a compiled version)).

I attached a visual display of the network, as well as the json and yaml reports. GitHub_Issue_No-outputs-can-be-identified.zip

Here is the Keras summary of the model
Keras_model_summary.txt

This model was created using the following script as provided by @fchollet:

from keras import applications
from keras.preprocessing.image import ImageDataGenerator
from keras import optimizers
from keras.models import Sequential
from keras.models import Model
from keras.layers import Dropout, Flatten, Dense

# path to the model weights files.
weights_path = '../keras/examples/vgg16_weights.h5'
top_model_weights_path = 'bottleneck_fc_model.h5'
# dimensions of our images.
img_width, img_height = 150, 150

train_data_dir = 'data/train'
validation_data_dir = 'data/validation'
nb_train_samples = 2000
nb_validation_samples = 800
epochs = 50
batch_size = 16

# build the VGG16 network
base_model = applications.VGG16(weights='imagenet', include_top=False, input_shape=(150,150,3))
print('Model loaded.')

# build a classifier model to put on top of the convolutional model
top_model = Sequential()
top_model.add(Flatten(input_shape=base_model.output_shape[1:]))
top_model.add(Dense(256, activation='relu'))
top_model.add(Dropout(0.5))
top_model.add(Dense(1, activation='sigmoid'))

# note that it is necessary to start with a fully-trained
# classifier, including the top classifier,
# in order to successfully do fine-tuning
top_model.load_weights(top_model_weights_path)

# add the model on top of the convolutional base
# model.add(top_model)
model = Model(inputs=base_model.input, outputs=top_model(base_model.output))

# set the first 25 layers (up to the last conv block)
# to non-trainable (weights will not be updated)
for layer in model.layers[:15]:
    layer.trainable = False

# compile the model with a SGD/momentum optimizer
# and a very slow learning rate.
model.compile(loss='binary_crossentropy',
              optimizer=optimizers.SGD(lr=1e-4, momentum=0.9),
              metrics=['accuracy'])

# prepare data augmentation configuration
train_datagen = ImageDataGenerator(
    rescale=1. / 255,
    shear_range=0.2,
    zoom_range=0.2,
    horizontal_flip=True)

test_datagen = ImageDataGenerator(rescale=1. / 255)

train_generator = train_datagen.flow_from_directory(
    train_data_dir,
    target_size=(img_height, img_width),
    batch_size=batch_size,
    class_mode='binary')

validation_generator = test_datagen.flow_from_directory(
    validation_data_dir,
    target_size=(img_height, img_width),
    batch_size=batch_size,
    class_mode='binary')

model.summary()

# fine-tune the model
model.fit_generator(
    train_generator,
    steps_per_epoch=nb_train_samples // batch_size,
    epochs=epochs,
    validation_data=validation_generator,
    validation_steps=nb_validation_samples // batch_size,
    verbose=2)

model.predict_function

model.save

In the script the output is distinctly identified:

model = Model(inputs=base_model.input, outputs=top_model(base_model.output))

Why would the convertor not recognize the output? (Sequential) layer?

Systematic delta with xgboost regressor

from coremltools.models.utils import evaluate_regressor
from coremltools.converters import xgboost as xgb_converter

from sklearn.datasets import load_boston
import xgboost
import pandas as pd

arg = {'colsample_bytree': 1, 'colsample_bylevel': 1, 'scale_pos_weight': 1,
       'learning_rate': 0.1, 'max_delta_step': 0.1, 'min_child_weight': 1,
       'n_estimators': 1, 'objective': 'reg:linear', 'max_depth': 1}

scikit_data = load_boston()
X = scikit_data.data.astype('f').astype('d')
feature_names, output_name = scikit_data.feature_names, 'target'

dtrain = xgboost.DMatrix(scikit_data.data, label = scikit_data.target,
                            feature_names = feature_names)
xgb_model = xgboost.train(arg, dtrain)

spec = xgb_converter.convert(xgb_model,feature_names, output_name, force_32bit_float = False)

df = pd.DataFrame(X, columns=feature_names)
df['prediction'] = xgb_model.predict(dtrain)

metrics = evaluate_regressor(spec, df, target = 'target', verbose = True)

Every single error is off by -0.1000

This is causing a unit test failure

homebrew python crashes coremltools

When using prediction on a python with homebrew installed, coremltools crashes.

Fatal Python error: PyThreadState_Get: no current thread
Abort trap: 6

Keras mobilenet can't be imported (missing Relu6 / DepthwiseConv2D)

I have a custom trained MobileNet network from Keras and I bump into an issue about CoreML Tools not recognizing Relu6 as an activation function.

my keras model is something like:

import keras
from keras.layers.core import Flatten
initial_model = keras.applications.mobilenet.MobileNet(input_shape=(size, size, 3), include_top=False, weights='imagenet', classes=2)
last = initial_model.output
x = Flatten()(last)
preds = Dense(2, activation='softmax')(x)

model = Model(initial_model.input, preds)

And after training I try to convert it with:

from keras.utils.generic_utils import CustomObjectScope
with CustomObjectScope({'relu6': keras.applications.mobilenet.relu6,'DepthwiseConv2D': keras.applications.mobilenet.DepthwiseConv2D}):
      convert = coremltools.converters.keras.convert("model.h5", input_names=['img'], image_input_names=['img'], class_labels=['class1', 'class2'])

But this raises:

RuntimeError: Unsupported option activation=relu6 in layer Dense(conv1_relu)

Because coreML Tools doesn't know what DepthwiseConv2D and Relu6 are.

problem running convert example

run the example in https://apple.github.io/coremltools/ for model conversion

"
import coremltools

Convert a caffe model to a classifier in Core ML

coreml_model = coremltools.converters.caffe.convert(('bvlc_alexnet.caffemodel', 'deploy.prototxt'),
predicted_feature_name='class_labels.txt'
)

Now save the model

coreml_model.save('BVLCObjectClassifier.mlmodel')
"

i got
"Traceback (most recent call last):
File "convert.py", line 5, in
predicted_feature_name='class_labels.txt'
File "/root/.local/lib/python2.7/site-packages/coremltools-0.8-py2.7.egg/coremltools/converters/caffe/_caffe_converter.py", line 191, in convert
predicted_feature_name)
File "/root/.local/lib/python2.7/site-packages/coremltools-0.8-py2.7.egg/coremltools/converters/caffe/_caffe_converter.py", line 255, in _export
predicted_feature_name)
MemoryError: std::bad_alloc
"

why example fails ?? thanks

coremltools.converters.keras.convert error

I've trying to convert a keras model to coreml format but I get the following error, how should I fix it?

File ".../venv/lib/python2.7/site-packages/coremltools/converters/keras/_keras_converter.py", line 722, in convert
custom_conversion_functions=custom_conversion_functions)
File ".../venv/lib/python2.7/site-packages/coremltools/converters/keras/_keras_converter.py", line 527, in convertToSpec
custom_conversion_functions=custom_conversion_functions)
File ".../venv/lib/python2.7/site-packages/coremltools/converters/keras/_keras2_converter.py", line 173, in _convert
graph.build()
File ".../venv/lib/python2.7/site-packages/coremltools/converters/keras/_topology2.py", line 714, in build
self.make_output_layers()
File ".../venv/lib/python2.7/site-packages/coremltools/converters/keras/_topology2.py", line 174, in make_output_layers
self.model.output_layers]
IndexError: list index out of range

sklearn.convert supports GradientBoostingRegressor only with default value of loss function (loss=‘ls’)

If you create model in scikit-learn (v 0.18.1) with GradientBoostingRegressor(..., loss=‘huber’) then converters.sklearn.convert failed with error «AttributeError: 'QuantileEstimator' object has no attribute ‘mean’». But GradientBoostingRegressor(..., loss=‘ls’) will be ok for converting. As I understand 'ls' does not use QuantileEstimator class (in scikit-learn). Thats why converter works. But in https://apple.github.io/coremltools/generated/coremltools.converters.sklearn.convert.html there are no any comments about this limitation. Gradient Boosting Classifier and Regressor marked as supported.

Python 3 Does Not Support Caffe Model Conversion

Trying to convert any Caffe model in Python 3 will result in the following error:

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-2-0841eccd1027> in <module>()
----> 1 caffe_converter.convert('/Users/tobyroseman/Downloads/oxford102.caffemodel')

/Users/tobyroseman/anaconda3/lib/python3.6/site-packages/coremltools/converters/caffe/_caffe_converter.py in convert(model, image_input_names, is_bgr, red_bias, blue_bias, green_bias, gray_bias, image_scale, class_labels, predicted_feature_name, model_precision)
    181             blue_bias,
    182             green_bias, gray_bias, image_scale, class_labels,
--> 183             predicted_feature_name)
    184     model = MLModel(model_path)
    185 

/Users/tobyroseman/anaconda3/lib/python3.6/site-packages/coremltools/converters/caffe/_caffe_converter.py in _export(filename, model, image_input_names, is_bgr, red_bias, blue_bias, green_bias, gray_bias, image_scale, class_labels, predicted_feature_name)
    194            image_scale=1.0,
    195            class_labels=None, predicted_feature_name=None):
--> 196     from ... import libcaffeconverter
    197 
    198     if isinstance(model, basestring):

ImportError: dynamic module does not define module export function (PyInit_libcaffeconverter)

When I was converting Keras CNN MNIST to Core ML, it show error:Fatal Python error: PyThreadState_Get: no current thread

I was trying to convert a Keras CNN MNIST model to a Core ML by using the code:
`import coremltools

output_labels = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
scale = 1/255.
coreml_model = coremltools.converters.keras.convert('MNIST.h5',
input_names='image',
image_input_names='image',
output_names='output',
class_labels=output_labels,
image_scale=scale)

coreml_model.author = 'Jiayu Li'
coreml_model.license = 'MIT'
coreml_model.short_description = 'Model to classify hand written digit'

coreml_model.input_description['image'] = 'Grayscale image of hand written digit'
coreml_model.output_description['output'] = 'Predicted digit'

coreml_model.save('MNIST.mlmodel')`

And here is the complete error I got:
`/usr/local/Cellar/python/2.7.13_1/Frameworks/Python.framework/Versions/2.7/bin/python2.7 /Users/jiayuli/work/STEM_project_2018/convert.py
2018-01-21 10:07:16.587908: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
2018-01-21 10:07:16.587928: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
2018-01-21 10:07:16.587933: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
2018-01-21 10:07:16.587939: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
/usr/local/lib/python2.7/site-packages/keras/models.py:281: UserWarning: Error in loading the saved optimizer state. As a result, your model is starting with a freshly initialized optimizer.
warnings.warn('Error in loading the saved optimizer '
0 : conv2d_1_input, <keras.engine.topology.InputLayer object at 0x10615f2d0>
1 : conv2d_1, <keras.layers.convolutional.Conv2D object at 0x10615f110>
2 : conv2d_1__activation__, <keras.layers.core.Activation object at 0x11474de50>
3 : conv2d_2, <keras.layers.convolutional.Conv2D object at 0x113124190>
4 : conv2d_2__activation__, <keras.layers.core.Activation object at 0x1147768d0>
5 : max_pooling2d_1, <keras.layers.pooling.MaxPooling2D object at 0x113124c90>
6 : flatten_1, <keras.layers.core.Flatten object at 0x113163710>
7 : dense_1, <keras.layers.core.Dense object at 0x11317b910>
8 : dense_1__activation__, <keras.layers.core.Activation object at 0x1147acdd0>
9 : dense_2, <keras.layers.core.Dense object at 0x113211bd0>
10 : dense_2__activation__, <keras.layers.core.Activation object at 0x1147c9a10>
Fatal Python error: PyThreadState_Get: no current thread

Process finished with exit code 134 (interrupted by signal 6: SIGABRT)`
If you need, here is my Keras MNIST code:
https://github.com/jiayulinfs/mnist-keras/blob/master/mnist-keras-lenet.py

xgboost convertor throws exception

Using the most recent version of xgboost, running the code below:

import xgboost
import coremltools

xgb_model = xgboost.XGBRegressor()
xgb_model.fit([[0,0],[1,1]], [0,2])

coremltools.converters.xgboost.convert(xgb_model)

Produces: TypeError: 'str' object is not callable

This is causing several unit test failures.

Using keras converter on model with Lambda followed by Reshape creates models that won't compile

I created a simple python script that demonstrates the problem. It is here: https://github.com/lauriebyrum/CoreML-Custom-Layers/blob/master/convert_lambda.py

Without this change lauriebyrum/CoreML-Custom-Layers@ba5dc0b, the model can be converted to coreml and the model will compile. WIth that change, the converter succeeds, but the compiler rejects it:
/Applications/Xcode.app/Contents/Developer/usr/bin/coremlc compile NeuralMcNeuralNet.mlmodel .
Input name(s) and shape(s):
image : (C,H,W) = (3, 256, 256)
Neural Network compiler 0: 100 , name = conv2d_1, output shape : (C,H,W) = (6, 256, 256)
Neural Network compiler 1: 500 , name = lambda_1, output shape : (C,H,W) = (-1, -1, -1)
Neural Network compiler 2: 300 , name = reshape_1, output shape : (C,H,W) = (6, 256, 256)
Neural Network compiler 3: 120 , name = global_average_pooling2d_1, output shape : (C,H,W) = (6, 1, 1)
Neural Network compiler 4: 140 , name = dense_1, output shape : (C,H,W) = (10, 1, 1)
Neural Network compiler 5: 175 , name = dense_1__activation__, output shape : (C,H,W) = (10, 1, 1)
coremlc: Error: compiler error: Invalid dst shape-1 x -1 x -1 x -1->-1->256 x 256 x 6 x 0 x 1 x

I have looked for a way to fix the converter, but have not found one. Help?

Meanwhile, I'll try rolling the Reshape into the Lambda, but we generally want to allow our developers to develop in keras and not have to change the model to get it into coreml.

Thanks in advance for any advice.

Model converted from Keras to .mlmodel doesn't work properly

Hi. I am not sure that its right place. So, I'm sorry to trouble you
I have convolutional model written on Keras.
Input : 1000 floats
output : 2 floats(probability for each of two classes)
This model works fine in Python 2.7 and in Android
But converting this model with coremltools for iOS leading to totally unexpected behaviour of model.
My steps :
1)Save model from to .mlmodel file(code below)
import coremltools coreml_model = coremltools.converters.keras.convert(autoencoder, input_names="input_1", output_names="dense_3/Softmax") coreml_model.save('validation_model.mlmodel')
2)Import in Xcode(importing is just "drag and drop" of model)
3)Set input as (1,1000,1)
4)Uses generated code predicts result.
Expected output : 2 floats(probability of belonging to each of the 2 classes)
Real output : 1000 floats

Okey, even if it normal, output is always the same(1000 values is always the same for different data). Even if data in python were classified to first class, output on iOS shows that this data belongs to second class

SKLearn convert gives error on converting the model

from coremltools.converters import sklearn

coreml_model = sklearn.convert(clf)
where clf is a RandomForestClassifier gives the following type error:

_feature_management.pyc in process_or_validate_features(features, num_dimensions, feature_type_map)
185 if num_dimensions is not None and not isinstance(num_dimensions, int):
186 raise TypeError("num_dimensions must be None or an integer, not '%s'"
--> 187 % str(type(num_dimensions)))
188
189

TypeError: num_dimensions must be None or an integer, not '<type 'long'>'

Converting a saved Keras model not working as expected

Started with Keras some days ago and wanted to test coremltools.
Opened the saved model and attempted to convert with

coreml_model = coremltools.converters.keras.convert(model)

then i got:

'0 : conv2d_1_input, <keras.engine.topology.InputLayer object at 0x109f00150>
1 : conv2d_1, <keras.layers.convolutional.Conv2D object at 0x109f00190>
2 : conv2d_1__activation__, <keras.layers.core.Activation object at 0x11274bc50>
3 : conv2d_2, <keras.layers.convolutional.Conv2D object at 0x109f00490>
4 : conv2d_2__activation__, <keras.layers.core.Activation object at 0x11274bbd0>
5 : max_pooling2d_1, <keras.layers.pooling.MaxPooling2D object at 0x109f00750>
6 : flatten_1, <keras.layers.core.Flatten object at 0x109f82550>
7 : dense_1, <keras.layers.core.Dense object at 0x109f586d0>
8 : dense_1__activation__, <keras.layers.core.Activation object at 0x11274bc10>
9 : dense_2, <keras.layers.core.Dense object at 0x10a02aa10>
10 : dense_2__activation__, <keras.layers.core.Activation object at 0x11274bd10>
Fatal Python error: PyThreadState_Get: no current thread
Abort trap: 6'

What have i done or what is going on, that this is not working ?

Tryed on Python 2.7.14 (installed with Homebrew) on macOS 10.13

convert failed if using the same layer in many parts

I made this model in keras.

inputs = [Input(shape=(2,3,4)) for _ in range(3)]
convLayer = Conv2D(12, kernel_size=(2,2), padding='valid')
conved = [convLayer(_i) for _i in inputs]
flat_layer = [Flatten()(c) for c in conved]
model = Model(inputs=inputs, outputs=flat_layer)

There's no problem in keras when learning and predicting.
But when I converted it to mlmodel, this error occurred.

KeyError: u'flatten_1'

or

RuntimeError: Error compiling model: "compiler error:  Invalid dst shape1 x 1 x 1344 x 1->1->1 x 1 x 84 x 0 x 4 x ".

(In the latter, shape's number is just an example. )

I'm sorry, I'm not sure the conditions which error occur. When I add another layer, not related to convolution layers I think, one of these error occurred.

I think the best way to do this is using TimeDistributed layer. However, I cannot use it with convolutional2D because of this issue #69 .
I have to reshape to 4 dimension to use conv2D in TimeDistributed...

How can I use one conv2D layer in many parts?

When I prepare conv2D every data, there's no problem. (But it's not model I want...)

inputs = [Input(shape=(2,3,4)) for _ in range(3)]
conved = [ Conv2D(12, kernel_size=(2,2), padding='valid')(_i) for _i in inputs]
flat_layer = [Flatten()(c) for c in conved]
model = Model(inputs=discriminator_in, outputs=flat_layer)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.