apple / apple/coremltools

Tensorflow channel first model conversion to CoreML

Open
#738 0 comments 0 reactions 0 assignees View on GitHub
question tf1.x
Dominant language
Python
Stars
5.4k
Forks
850
Avg merge
4d 5h
Merged PRs (30d)
10

Description

## ❓Question
I have a Tensorflow model that contains a Conv2D layer in channel first (NCHW) format. When I try to convert it to CoreML using coremltools 4.0b1, I get the error:

`ValueError: Incompatible dim 3 in shapes (is1, 32, 1022, 1022) vs. (1, 1, 1, 32)`

It seems the problem is that the filter op (conv2d/Conv2D) of the Conv2D layer has a shape of [1, 32, 1024, 1024], while the bias op (conv2d/BiasAdd) has a shape of 32, which coremltools interprets as [1, 1, 1, 32], leading to the incompatibility error. Is there any way to tell coremltools that the model is is NCHW and that it should interpret the shape of BiasAdd as [1, 32, 1, 1]? Or is this a problem with the Tensorflow model itself (details on a toy model included below)? Or is NCHW models not supported by coremltools at all?

The problem can be reproduced on a simple toy model like this:

![Screen Shot 2020-06-25 at 8 05 52 AM](https://user-images.githubusercontent.com/67419721/85716710-bda37580-b6ba-11ea-935f-7da1bffababf.png)

I tried coremltools 3.4, and I essentially get the same error:
`ValueError: Incompatible dimension 3 in BiasAdd operation conv2d/BiasAdd`
In coremltools 3.4, the command I used to perform the conversion is:
`model = coremltools.converters.tensorflow.convert('./frozen_model.pb', inputs={'x':[1,16,1024,1024]}, outputs=['conv2d/Relu'], image_input_names=['x'], tf_image_format='NCHW')`

I would much appreciate any help or insights you kindly offer.

## System Information
- coremltools 4.0b1
- Ubuntu 18.04
- Python 3.5.2
- Tensorflow 1.14

## I am attaching here the code I used to set up and freeze the toy model:

[dummy_model_maker.txt](https://github.com/apple/coremltools/files/4831094/dummy_model_maker.txt)

Contributor guide

Open the contributing guide

Research direction

Start with dummy_model_maker.txt and the TensorFlow 1.14 toy model; reproduce the conversion using the shown coremltools 3.4 or 4.0b1 command and NCHW input shape. Determine whether the BiasAdd shape mismatch is caused by the TensorFlow model or unsupported NCHW handling, and document a verified outcome.

Written by the indexing model from the issue text.

Assessment

Tech stack
machine-learning, python
Domain
machine-learning
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.