Type error when trying to fit a VGG like model

A_Matar

When I try to fit the following model:

model = Sequential([
    Lambda(vgg_preprocess, input_shape=(3,244,244)),
    Conv2D(64,3,3, activation='relu'),
    BatchNormalization(axis=1),
    Conv2D(64,3,3, activation='relu'),
    MaxPooling2D(),
    BatchNormalization(axis=1),
    Conv2D(128,3,3, activation='relu'),
    BatchNormalization(axis=1),
    Conv2D(128,3,3, activation='relu'),
    MaxPooling2D(),
    BatchNormalization(axis=1),
    Conv2D(256,3,3, activation='relu'),
    BatchNormalization(axis=1),
    Conv2D(256,3,3, activation='relu'),
    MaxPooling2D(),
    Flatten(),
    BatchNormalization(),
    Dense(1024, activation='relu'),
    BatchNormalization(),
    Dropout(0.5),
    Dense(1024, activation='relu'),
    BatchNormalization(),
    Dense(10, activation='softmax')
])
model.compile(Adam(), loss='categorical_crossentropy', metrics=['accuracy'])

I get this error:

TypeError: Cannot convert Type TensorType(float32, 4D) (of Variable AbstractConv2d_gradInputs{convdim=2, border_mode='valid', subsample=(1, 1), filter_flip=True, imshp=(None, 256, 56, 56), kshp=(256, 256, 3, 3), filter_dilation=(1, 1)}.0) into Type TensorType(float64, 4D). You can try to manually convert AbstractConv2d_gradInputs{convdim=2, border_mode='valid', subsample=(1, 1), filter_flip=True, imshp=(None, 256, 56, 56), kshp=(256, 256, 3, 3), filter_dilation=(1, 1)}.0 into a TensorType(float64, 4D).

This is how I do the fitting:

 model.fit_generator(train_batches, train_batches.n, nb_epoch=1, validation_data=test_batches, nb_val_samples=test_batches.n)

And here is vgg_preprocess function:

vgg_mean = np.array([123.68, 116.779, 103.939]).reshape((3,1,1))

def vgg_preprocess(x):
    x = x - vgg_mean  #Subtract the mean of each channel
    return x[:, ::-1] #Inverse the channel order to suit that of VGG RGB->BGR

What does it mean, how to fix it?

Marcin Możejko

The problem lies in a fact that vgg_mean.dtype = 'float64, whereas the standard floating point decision in most of DL packages is float32.

Setting:

vgg_mean = np.array(vgg_mean, dtype='float32')

Should fix your problem.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Error: All inputs to the layer should be tensors- when trying to use VGG19 model

index error while trying to fit model keras

Error when trying to fit Hierarchical GAMs (Model GS or S) using mgcv

Error when converting caffe model (VGG_Face2) to coreml

Error when trying to Scaffold a model

ValueError when trying to execute model.fit() -: Failed to convert a NumPy array to a Tensor (Unsupported object type numpy.ndarray)

I am trying to fit an CuDNNLSTM model and I am getting an error

'TypeError: 'CompositeModel' object is not callable' when trying to fit a composite model

Problems with Predict() function when trying to fit Multiple Linear Regression Model

I'm getting an error when trying to fit a sklearn model. TypeError: Only size-1 arrays can be converted to Python scalars

Type error when trying to create a doc2vec model in gensim

KerasClassifier error when trying to run a LST Model

Error when trying to create tables from model

Error when trying to export randomForest model to PMML

Error in model.fit() when using custom loss function

Colab TPU Error when calling model.fit() : UnimplementedError

Haskell: Type Error Trying when using generics

Error when trying to get object type in python

"Incomplete type"-error when trying to open ifstream

Type Error when trying to move Turtle in Python

Error using reflection, when trying to get the type

X is a variable but used like a type when trying to cast

Model fits on a single GPU but script crashes when trying to fit on multiple GPUs

Getting the "ValueError: Shapes (64, 4) and (64, 10) are incompatible" when trying to fit my model

How to fix failed assertion `output channels should be divisible by group' when trying to fit the model in Keras?

Trying to create a Like Button on model

AssertionError while trying to fit model python

Type Error when trying to extract type from unions

InvalidArgumentError:Error at the time of fit model