Umberto
Umberto

Reputation: 1421

Keras gives an error when building a dummy model

I am building the tiny yolo cnn model in Keras. I get a strange error. Anyone can help?

Imports

import numpy as np
import matplotlib.pyplot as plt
import cv2
import glob
#from moviepy.editor import VideoFileClip
from IPython.display import HTML
%matplotlib inline

from tensorflow.keras import backend as K

import tensorflow.keras as keras# broken for keras >= 2.0, use 1.2.2
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Convolution2D, MaxPooling2D
from tensorflow.keras.layers import LeakyReLU
from tensorflow.keras.layers import Flatten, Dense, Activation, Reshape

and the model code is the following

K.set_image_data_format('channels_first')
model = Sequential()
model.add(Convolution2D(16, 3, 3,input_shape=(3,448,448),padding='same'))#,strides=(1,1)))
model.add(LeakyReLU(alpha=0.1))
model.add(MaxPooling2D(pool_size=(2, 2))) # Added the padding option
model.add(Convolution2D(32,3,3 ,padding='same'))
model.add(LeakyReLU(alpha=0.1))
model.add(MaxPooling2D(pool_size=(2, 2),padding='valid'))
model.add(Convolution2D(64,3,3 ,padding='same'))
model.add(LeakyReLU(alpha=0.1))
model.add(MaxPooling2D(pool_size=(2, 2),padding='valid'))
model.add(Convolution2D(128,3,3 ,padding='same'))
model.add(LeakyReLU(alpha=0.1))
model.add(MaxPooling2D(pool_size=(2, 2),padding='valid'))
model.add(Convolution2D(256,3,3 ,padding='same'))
model.add(LeakyReLU(alpha=0.1))
model.add(MaxPooling2D(pool_size=(2, 2),padding='valid'))
model.add(Convolution2D(512,3,3 ,padding='same'))
model.add(LeakyReLU(alpha=0.1))
model.add(MaxPooling2D(pool_size=(2, 2),padding='valid'))
model.add(Convolution2D(1024,3,3 ,padding='same'))
model.add(LeakyReLU(alpha=0.1))
model.add(Convolution2D(1024,3,3 ,padding='same'))
model.add(LeakyReLU(alpha=0.1))
model.add(Convolution2D(1024,3,3 ,padding='same'))
model.add(LeakyReLU(alpha=0.1))
model.add(Flatten())
model.add(Dense(256))
model.add(Dense(4096))
model.add(LeakyReLU(alpha=0.1))
model.add(Dense(1470))

The problem is the following. If I run it I get the following error message

---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
~/Python-Environments/tfenv/lib/python3.7/site-packages/tensorflow/python/framework/ops.py in _create_c_op(graph, node_def, inputs, control_inputs)
   1658   try:
-> 1659     c_op = c_api.TF_FinishOperation(op_desc)
   1660   except errors.InvalidArgumentError as e:

InvalidArgumentError: Negative dimension size caused by subtracting 2 from 1 for 'max_pooling2d_37/MaxPool' (op: 'MaxPool') with input shapes: [?,128,1,1].

and I don't find the reason. Can anyone reproduce it? Keras version is

'2.2.4-tf'

Thanks anyone. Best, Umberto

Upvotes: 0

Views: 148

Answers (1)

meowongac
meowongac

Reputation: 720

Seems you forgot to put brackets in kernel size in your Conv2D layers. Making them have strides bigger than 1. Causing the tensors become too small for a (2, 2) pooling.

Upvotes: 2

Related Questions