Deshwal
Deshwal

Reputation: 4152

Keras: TypeError: 'float' object is not callable. Unable to call model.fit() or model.fit_generator() on very simple CNN model

I am workimg on MNIST Hand Sign Dataset for classification. I have my (28*28) images pixels in a numpy array as:

X_train.shape, X_val.shape
>>
((2496, 28, 28, 1), (996, 28, 28, 1))

I have used ImageDataGenerator to make batches and train the model. I have been the following instructions:

Batch size =50, epoch=20
All filter (kernel) sizes are 3x3
Initial Conv2D layer with 64 filters
MaxPooling layer following this
Second Conv2D layer with 128 filters
Dense output layer after this

My model is as follows:

datagen = ImageDataGenerator(preprocessing_function=1./255.0)
train_gen = datagen.flow(X_train, y_train, batch_size=50)
val_gen = datagen.flow(X_val,y_val, batch_size=50)

input_ = Input(shape=(28,28,1))
x = Conv2D(64,kernel_size=(3,3))(input_)
x = ELU()(x)
x = MaxPooling2D(pool_size=(3,3))(x)
x = Conv2D(128,kernel_size=(3,3))(input_)
x = ELU()(x)
x = Flatten()(x)
out = Dense(24,activation='softmax')(x)

model = Model(inputs=input_,outputs=out)
model.compile(loss=sparse_categorical_crossentropy,optimizer=Adam(lr=0.001),metrics=['accuracy'])

model.fit(train_gen,epochs=20,) 

I have used Sparse Categorical Cross Entropy because I have some missing class values in my y_labels. There are 24 classes from 0-24 but class=9 missing.

Can someone tell me why is that hapenning??

I think the problem is in train_gen because next(train_gen) gives me the same error.

Lower half of the error is:

/opt/conda/lib/python3.7/site-packages/keras/utils/data_utils.py in get_index(uid, i)
    404         The value at index `i`.
    405     """
--> 406     return _SHARED_SEQUENCES[uid][i]
    407 
    408 

/opt/conda/lib/python3.7/site-packages/keras_preprocessing/image/iterator.py in __getitem__(self, idx)
     63         index_array = self.index_array[self.batch_size * idx:
     64                                        self.batch_size * (idx + 1)]
---> 65         return self._get_batches_of_transformed_samples(index_array)
     66 
     67     def __len__(self):

/opt/conda/lib/python3.7/site-packages/keras_preprocessing/image/numpy_array_iterator.py in _get_batches_of_transformed_samples(self, index_array)
    152             x = self.image_data_generator.apply_transform(
    153                 x.astype(self.dtype), params)
--> 154             x = self.image_data_generator.standardize(x)
    155             batch_x[i] = x
    156 

/opt/conda/lib/python3.7/site-packages/keras_preprocessing/image/image_data_generator.py in standardize(self, x)
    702         """
    703         if self.preprocessing_function:
--> 704             x = self.preprocessing_function(x)
    705         if self.rescale:
    706             x *= self.rescale

TypeError: 'float' object is not callable

Upvotes: 0

Views: 407

Answers (1)

Dr. Snoopy
Dr. Snoopy

Reputation: 56357

The problem is in this part:

datagen = ImageDataGenerator(preprocessing_function=1./255.0)

The preprocessing_function parameter expects a function, not a float value. You probably confused it with the rescale parameter:

datagen = ImageDataGenerator(rescale=1./255.0)

Upvotes: 1

Related Questions