Error when importing Keras CNN

Hello,

I implemented a simple CNN with Keras.

import keras
import numpy as np
import tvm
from tvm import relay

input_shape = (1, 32, 32, 3)
# input_shape = (1, 3, 32, 32)
num_classes = 10
dtype = 'float32'

model = keras.Sequential([
    keras.layers.Conv2D(32, 5, padding='same', input_shape=input_shape[1:]),
    keras.layers.Activation('relu'),
    keras.layers.MaxPooling2D((2, 2), (2, 2), padding='same'),
    keras.layers.Conv2D(64, 5, padding='same'),
    keras.layers.Activation('relu'),
    keras.layers.MaxPooling2D((2, 2), (2, 2), padding='same'),
    keras.layers.Flatten(),
    keras.layers.Dense(1024),
    keras.layers.Activation('relu'),
    keras.layers.Dropout(0.4),
    keras.layers.Dense(num_classes)
])

shape_dict = {'conv2d_1_input': input_shape}
image = np.ones(input_shape, np.float32)
image = tvm.nd.array(image.astype(dtype))

mod, params = relay.frontend.from_keras(model, shape_dict)

When I try to import it in TVM I get the following error:

TVMError: 
Error(s) have occurred. We have annotated the program with them:

In `main`: 
v0.0.3
fn (%conv2d_1_input: Tensor[(1, 32, 32, 3), float32], %v_param_1: Tensor[(32, 3, 5, 5), float32], %v_param_2: Tensor[(32,), float32], %v_param_3: Tensor[(64, 32, 5, 5), float32], %v_param_4: Tensor[(64,), float32], %v_param_5: Tensor[(1024, 4096), float32], %v_param_6: Tensor[(1024,), float32], %v_param_7: Tensor[(10, 1024), float32], %v_param_8: Tensor[(10,), float32]) {
  %0 = nn.conv2d(%conv2d_1_input, %v_param_1, padding=[2, 2], channels=32, kernel_size=[5, 5])an internal invariant was violated while typechecking your program [12:06:07] /.../tvm/src/relay/pass/type_solver.cc:119: Check failed: resolved.defined(): Unable to unify parent types: TensorType([32, 32, 5, 5], float32) and TensorType([32, 3, 5, 5], float32)
; ;
  %1 = nn.bias_add(%0, %v_param_2);
  %2 = nn.relu(%1);
  %3 = nn.max_pool2d(%2, pool_size=[2, 2], strides=[2, 2], padding=[0, 0, 0, 0]);
  %4 = nn.conv2d(%3, %v_param_3, padding=[2, 2], channels=64, kernel_size=[5, 5]);
  %5 = nn.bias_add(%4, %v_param_4);
  %6 = nn.relu(%5);
  %7 = nn.max_pool2d(%6, pool_size=[2, 2], strides=[2, 2], padding=[0, 0, 0, 0]);
  %8 = transpose(%7, axes=[0, 2, 3, 1]);
  %9 = nn.batch_flatten(%8);
  %10 = nn.dense(%9, %v_param_5, units=1024);
  %11 = nn.bias_add(%10, %v_param_6);
  %12 = nn.relu(%11);
  %13 = nn.dense(%12, %v_param_7, units=10);
  nn.bias_add(%13, %v_param_8)
}

Does anyone have an idea what’s causing this error or how to solve it?

Hi arne,
Did you ever fix this issue, I’ve got the exactly same one and am unsure how to fix it?
Thanks,
Pete

No, unfortunately not.

I’ve found a solution that seems to have worked for me, not sure if it will work for you.
Although keras has input has (height/width, height/width, channels), tvm requires the input to be (number of images, channels, height/width, height/width). So your input shape dict may have to look like {‘input_1’: (1, 3, 32, 32)} when using relay.front.from_keras.

Hope this helps.

(Another difference between our code is my input shape when building the keras model doesn’t include the number of images so is just (32, 32, 3))