In [1]:
from utils import *
In [2]:
path = 'data/mnist/'
# path = 'data/mnist/sample' # sample path
In [3]:
train = pd.read_csv(path + 'train.csv')
test = pd.read_csv(path + 'test.csv')
In [4]:
train = train.values
test = test.values
# 打乱
train = np.random.permutation(train)
In [5]:
valid = train[:2000, :]
valid_data = valid[:, 1:]
valid_label = onehot(valid[:, 0])
In [6]:
train = train[2000:, :]
In [7]:
train_data = train[:, 1:]
train_label = onehot(train[:, 0])
In [8]:
train_data.shape, train_label.shape, valid_label.shape, valid_data.shape
Out[8]:
In [9]:
def reshape(data, target_size): return np.reshape(data, target_size)
In [10]:
train_data = reshape(train_data, [40000, 1, 28, 28])
valid_data = reshape(valid_data, [2000, 1, 28, 28])
train_data.shape, train_label.shape, valid_label.shape, valid_data.shape
Out[10]:
In [11]:
mean_px = train_data.mean().astype(np.float32)
std_px = train_data.std().astype(np.float32)
In [12]:
def norm_input(x): return (x-mean_px)/std_px
In [13]:
BATCH_SIZE = 64
In [14]:
def get_lin_model():
model = Sequential([
Lambda(norm_input, input_shape=(1,28,28)),
Flatten(),
Dense(10, activation='softmax')
])
model.compile(Adam(), loss='categorical_crossentropy', metrics=['accuracy'])
return model
In [15]:
lm = get_lin_model()
In [16]:
gen = image.ImageDataGenerator()
batches = gen.flow(train_data, train_label, batch_size=BATCH_SIZE)
val_batches = gen.flow(valid_data, valid_label, batch_size=BATCH_SIZE)
In [17]:
lm.fit_generator(
batches,
samples_per_epoch=len(train_data) / 20,
nb_epoch=1,
validation_data=val_batches,
nb_val_samples=len(valid_data) / 20
)
Out[17]:
In [18]:
lm.fit_generator(
batches,
samples_per_epoch=len(train_data) / 20,
nb_epoch=1,
validation_data=val_batches,
nb_val_samples=len(valid_data) / 20
)
Out[18]:
In [19]:
def get_fc_model():
model = Sequential([
Lambda(norm_input, input_shape=(1,28,28)),
Flatten(),
Dense(512, activation='relu'),
Dense(10, activation='softmax')
])
model.compile(Adam(lr=.01), loss='categorical_crossentropy', metrics=['accuracy'])
return model
In [20]:
fc = get_fc_model()
In [21]:
fc.fit_generator(
batches,
samples_per_epoch=batches.n,
nb_epoch=1,
validation_data=val_batches,
nb_val_samples=val_batches.n
)
Out[21]:
In [22]:
def get_model():
model = Sequential([
Lambda(norm_input, input_shape=(1,28,28)),
Convolution2D(32,3,3, activation='relu'),
Convolution2D(32,3,3, activation='relu'),
MaxPooling2D(),
Convolution2D(64,3,3, activation='relu'),
Convolution2D(64,3,3, activation='relu'),
MaxPooling2D(),
Flatten(),
Dense(512, activation='relu'),
Dense(10, activation='softmax')
])
model.compile(Adam(), loss='categorical_crossentropy', metrics=['accuracy'])
return model
In [23]:
model = get_model()
In [24]:
model.fit_generator(
batches,
samples_per_epoch=batches.n,
nb_epoch=1,
validation_data=val_batches,
nb_val_samples=val_batches.n
)
Out[24]:
In [25]:
model = get_model()
In [17]:
gen = image.ImageDataGenerator(rotation_range=8, width_shift_range=0.08, shear_range=0.3,
height_shift_range=0.08, zoom_range=0.08)
batches = gen.flow(train_data, train_label, batch_size=BATCH_SIZE)
val_batches = gen.flow(valid_data, valid_label, batch_size=BATCH_SIZE)
In [27]:
model.fit_generator(
batches,
samples_per_epoch=batches.n,
nb_epoch=1,
validation_data=val_batches,
nb_val_samples=val_batches.n
)
Out[27]:
In [18]:
def get_model_bn_do():
model = Sequential([
Lambda(norm_input, input_shape=(1,28,28)),
Convolution2D(32,3,3, activation='relu'),
BatchNormalization(axis=1),
Convolution2D(32,3,3, activation='relu'),
MaxPooling2D(),
BatchNormalization(axis=1),
Convolution2D(64,3,3, activation='relu'),
BatchNormalization(axis=1),
Convolution2D(64,3,3, activation='relu'),
MaxPooling2D(),
Flatten(),
# BatchNormalization(),
Dense(512, activation='relu'),
BatchNormalization(),
Dropout(0.5),
Dense(10, activation='softmax')
])
model.compile(Adam(), loss='categorical_crossentropy', metrics=['accuracy'])
return model
In [19]:
model = get_model_bn_do()
In [20]:
model.optimizer.lr=0.01
In [21]:
model.fit_generator(
batches,
samples_per_epoch=batches.n,
nb_epoch=4,
validation_data=val_batches,
nb_val_samples=val_batches.n
)
Out[21]:
In [22]:
model.fit_generator(
batches,
samples_per_epoch=batches.n,
nb_epoch=1,
validation_data=val_batches,
nb_val_samples=val_batches.n
)
Out[22]:
In [23]:
model.fit_generator(
batches,
samples_per_epoch=batches.n,
nb_epoch=1,
validation_data=val_batches,
nb_val_samples=val_batches.n
)
Out[23]:
In [24]:
model.fit_generator(
batches,
samples_per_epoch=batches.n,
nb_epoch=1,
validation_data=val_batches,
nb_val_samples=val_batches.n
)
Out[24]:
In [25]:
model.fit_generator(
batches,
samples_per_epoch=batches.n,
nb_epoch=1,
validation_data=val_batches,
nb_val_samples=val_batches.n
)
Out[25]:
In [26]:
model.fit_generator(
batches,
samples_per_epoch=batches.n,
nb_epoch=2,
validation_data=val_batches,
nb_val_samples=val_batches.n
)
Out[26]:
In [27]:
model.fit_generator(
batches,
samples_per_epoch=batches.n,
nb_epoch=2,
validation_data=val_batches,
nb_val_samples=val_batches.n
)
Out[27]:
In [28]:
model.fit_generator(
batches,
samples_per_epoch=batches.n,
nb_epoch=2,
validation_data=val_batches,
nb_val_samples=val_batches.n
)
Out[28]:
In [29]:
model.fit_generator(
batches,
samples_per_epoch=batches.n,
nb_epoch=1,
validation_data=val_batches,
nb_val_samples=val_batches.n
)
Out[29]:
In [30]:
model.fit_generator(
batches,
samples_per_epoch=batches.n,
nb_epoch=3,
validation_data=val_batches,
nb_val_samples=val_batches.n
)
Out[30]:
In [31]:
model.fit_generator(
batches,
samples_per_epoch=batches.n,
nb_epoch=3,
validation_data=val_batches,
nb_val_samples=val_batches.n
)
Out[31]:
In [32]:
model.fit_generator(
batches,
samples_per_epoch=batches.n,
nb_epoch=3,
validation_data=val_batches,
nb_val_samples=val_batches.n
)
Out[32]:
In [36]:
model.fit_generator(
batches,
samples_per_epoch=batches.n,
nb_epoch=3,
validation_data=val_batches,
nb_val_samples=val_batches.n
)
Out[36]:
In [39]:
model.fit_generator(
batches,
samples_per_epoch=batches.n,
nb_epoch=3,
validation_data=val_batches,
nb_val_samples=val_batches.n
)
Out[39]:
In [40]:
model.optimizer.lr=0.001
In [42]:
model.fit_generator(
batches,
samples_per_epoch=batches.n,
nb_epoch=4,
validation_data=val_batches,
nb_val_samples=val_batches.n
)
Out[42]:
In [43]:
model.optimizer.lr=0.0001
In [44]:
model.fit_generator(
batches,
samples_per_epoch=batches.n,
nb_epoch=1,
validation_data=val_batches,
nb_val_samples=val_batches.n
)
Out[44]:
In [49]:
model.fit_generator(
batches,
samples_per_epoch=batches.n,
nb_epoch=2,
validation_data=val_batches,
nb_val_samples=val_batches.n
)
Out[49]:
In [67]:
model.optimizer.lr=0.001
In [74]:
model.fit_generator(
batches,
samples_per_epoch=batches.n,
#samples_per_epoch=2000,
nb_epoch=10,
validation_data=val_batches,
nb_val_samples=val_batches.n
)
Out[74]:
In [75]:
preds = model.predict(reshape(test, [28000, 1, 28, 28]))
In [76]:
pre = np.argmax(preds, 1)
In [77]:
pre[:5]
Out[77]:
In [78]:
conv_y_submission = pd.DataFrame({
'ImageId': range(1, 28001),
'Label': pre
})
In [79]:
conv_y_submission.to_csv('./conv_submission9.csv', index=False)
In [80]:
# 保存权重
path = 'models/mnist/'
model.save_weights(path + 'final1.h5')
In [81]:
# 载入权重
model.load_weights(path + 'final1.h5')
preds = model.predict(reshape(test, [28000, 1, 28, 28]))
pre = np.argmax(preds, 1)
In [82]:
pre[:5]
Out[82]:
In [ ]: