0 votes
1 view
in Machine Learning by (13.4k 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(digits.target)

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()

model.add(Conv2D(filters=10, 

                 kernel_size=(3,3), 

                 input_shape=(None, None, 1), 

                 data_format='channels_last'))

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

model.add(Dropout(0.2))

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

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

model.summary()

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

1 Answer

0 votes
by (32.8k 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)

        epoch_losses.append(current_loss)

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

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

Hope this answer helps you!

Watch this video to know more about Keras:

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...