In [3]:
# These are all the modules we'll be using later. Make sure you can import them
# before proceeding further.
from __future__ import print_function
import matplotlib.pyplot as plt
import numpy as np
import os
import sys
from scipy import ndimage
# Config the matplotlib backend as plotting inline in IPython
%matplotlib inline
In [ ]:
In [4]:
image_size = 28 # Pixel width and height.
pixel_depth = 255.0 # Number of levels per pixel.
folders = [
r'.\\notMNIST_small\\A\\',
r'.\\notMNIST_small\\B\\',
r'.\\notMNIST_small\\C\\',
r'.\\notMNIST_small\\D\\',
r'.\\notMNIST_small\\E\\',
r'.\\notMNIST_small\\F\\',
r'.\\notMNIST_small\\G\\',
r'.\\notMNIST_small\\H\\',
r'.\\notMNIST_small\\I\\',
r'.\\notMNIST_small\\J\\'
]
def load_letter(folder, min_num_images):
"""Load the data for a single letter label."""
image_files = os.listdir(folder)
dataset = np.ndarray(shape=(len(image_files), image_size, image_size),
dtype=np.float32)
print(folder)
num_images = 0
for image in image_files:
image_file = os.path.join(folder, image)
try:
image_data = (ndimage.imread(image_file).astype(float) -
pixel_depth / 2) / pixel_depth
if image_data.shape != (image_size, image_size):
raise Exception('Unexpected image shape: %s' % str(image_data.shape))
dataset[num_images, :, :] = image_data
num_images = num_images + 1
except IOError as e:
print('Could not read:', image_file, ':', e, '- it\'s ok, skipping.')
dataset = dataset[0:num_images, :, :]
if num_images < min_num_images:
raise Exception('Many fewer images than expected: %d < %d' %
(num_images, min_num_images))
print('Full dataset tensor:', dataset.shape)
print('Mean:', np.mean(dataset))
print('Standard deviation:', np.std(dataset))
return dataset
X_datasets = list()
Y_datasets = list()
for idx in range(len(folders)):
folder = folders[idx]
X_datasets.append(load_letter(folder, 1800))
labels = np.zeros((X_datasets[-1].shape[0],len(folders)))
labels[:,idx] = 1
Y_datasets.append(labels)
In [ ]:
In [ ]:
In [ ]:
In [5]:
from sklearn.model_selection import train_test_split
X_datasets2 = np.concatenate(X_datasets)
Y_datasets2 = np.concatenate(Y_datasets)
print("Total samples number:",X_datasets2.shape)
X_trains,X_tests,Y_trains,Y_tests = train_test_split(X_datasets2,Y_datasets2,test_size=0.25)
print("Samples for tests:",Y_tests.shape[0])
print("Samples for trains:",Y_trains.shape[0])
plt.imshow(X_tests[0],cmap='gray')
Out[5]:
In [ ]:
In [ ]:
In [ ]:
In [15]:
# Create first network with Keras
from keras.models import Sequential
from keras.layers import Dense,Activation,Reshape
from keras.callbacks import EarlyStopping,ModelCheckpoint
import numpy
callbacks = [
EarlyStopping(monitor='val_loss', min_delta=0.00001, verbose=1),
# EarlyStopping(monitor='val_loss', patience=2, verbose=0),
ModelCheckpoint(filepath='./weights.net', verbose=1, save_best_only=True),
]
# fix random seed for reproducibility
seed = 7
numpy.random.seed(seed)
input_dim = X_trains[0].shape[0]*X_trains[0].shape[1]
print((X_trains[0].shape[0],X_trains[0].shape[1]))
print(Y_trains[0].shape[0])
# create model
model = Sequential()
model.add(Reshape((input_dim,), input_shape=(X_trains[0].shape[0],X_trains[0].shape[1])))
model.add(Dense(input_dim, input_shape = (input_dim,), init='uniform', activation='relu'))
model.add(Dense(int(input_dim), init='uniform', activation='relu'))
model.add(Dense(int(input_dim/2),init='uniform',activation='sigmoid'))
model.add(Dense(Y_trains[0].shape[0],init='uniform', name="output"))
model.add(Activation('softmax', name="softmax"))
model.summary()
# Compile model
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
# Fit the model
model.fit(X_trains,
Y_trains,
epochs=50,
batch_size=10,
verbose=2,
validation_split=0.25,
callbacks=callbacks)
# calculate predictions
results = model.evaluate(X_tests, Y_tests, batch_size=32, verbose=1, sample_weight=None)
# round predictions
print(results)
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [14]:
clazzez = ['A','B','C','D','E','F','G','H','I','J']
results = model.predict(X_tests)
errors = list()
for idx in range(len(results)):
res = results[idx]
cla_pre = clazzez[np.argmax(res)]
cla_tar = clazzez[np.argmax(Y_tests[idx])]
if cla_pre==cla_tar:
# print(cla_pre,cla_tar)
errors.append(idx)
# print(errors)
problems = 10
fig, axes = plt.subplots(problems, figsize=(10,10))
fig.tight_layout()
for idx in range(problems):
err = errors[idx]
cla_pre = clazzez[np.argmax(results[err])]
cla_tar = clazzez[np.argmax(Y_tests[err])]
axes[idx].imshow(X_tests[err],cmap='gray')
axes[idx].set_title("cla_pre=%s cla_tar=%s " % (cla_pre,cla_tar), fontsize=10)
axes[idx].set_xticks([])
axes[idx].set_yticks([])
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]: