Explore Courses Blog Tutorials Interview Questions
0 votes
in Machine Learning by (19k points)

I am trying to perform the usual classification on the MNIST database but with randomly cropped digits. Images are cropped the following way : removed randomly first/last and/or row/column.

I would like to use a Convolutional Neural Network using Keras (and Tensorflow backend) to perform convolution and then the usual classification.

Inputs are of variable size and i can't manage to get it to work.

Here is how I cropped digits

import numpy as np

from keras.utils import to_categorical

from sklearn.datasets import load_digits

digits = load_digits()

X = digits.images

X = np.expand_dims(X, axis=3)

X_crop = list()

for index in range(len(X)):

    X_crop.append(X[index, np.random.randint(0,2):np.random.randint(7,9), np.random.randint(0,2):np.random.randint(7,9), :])

X_crop = np.array(X_crop)

y = to_categorical(

from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X_crop, y, train_size=0.8, test_size=0.2)

And here is the architecture of the model I want to use

from keras.layers import Dense, Dropout

from keras.layers.convolutional import Conv2D

from keras.models import Sequential

model = Sequential()



                 input_shape=(None, None, 1), 


model.add(Dense(128, activation='relu'))


model.add(Dense(10, activation='softmax'))

model.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy'])

model.summary(), y_train, epochs=100, batch_size=16, validation_data=(X_test, y_test))

1 Answer

0 votes
by (33.1k points)
edited by

You should also use the train_on_batch method.

For example:

from six import next

batches_generator = get_cropped_batches_generator(X, batch_size=16)

losses = list()

for epoch_nb in range(nb_of_epochs):

    epoch_losses = list()

    for batch_nb in range(nb_of_batches):

        # cropped_x has a different shape for different batches (in general)

        cropped_x, cropped_y = next(batches_generator) 

        current_loss = model.train_on_batch(cropped_x, cropped_y)


    losses.append(epoch_losses.sum() / (1.0 * len(epoch_losses))

final_loss = losses.sum() / (1.0 * len(losses))

Hope this answer helps you!

Keras and Machine Learning are interrelated. To know more about Keras, study the Machine Learning Algorithms.

Watch this video to know more about Keras:

Browse Categories