Kaggle competition: Digit Classification DL Workshop
<SOS> metric: categorizationaccuracy Kaggle data source: digit-classification-dl-workshop<categorify>
%matplotlib inline
try:
except ImportError as e:
print('scikit-image is too new, ',e)
y_train = to_categorical(y_train)
 y_val = to_categorical(y_val)
 print(y_train[0] )<choose_model_class>
use_cuda = True
device = torch.device("cuda" if use_cuda else "cpu")
 torch.manual_seed(42 )
model = Sequential()
model.add(Conv2D(filters = 16, kernel_size =(3, 3), activation='relu',
input_shape =(28, 28, 1)))
 model.add(BatchNormalization())
 model.add(Conv2D(filters = 16, kernel_size =(3, 3), activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPool2D(strides=(2,2)))
 model.add(Dropout(0.25))
model.add(Conv2D(filters = 32, kernel_size =(3, 3), activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(filters = 32, kernel_size =(3, 3), activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPool2D(strides=(2,2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(512, activation='relu'))
model.add(Dropout(0.25))
model.add(Dense(1024, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))<choose_model_class>
class AffMNISTDataset(Dataset):
def __init__(self, data_name):

 with np.load(os.path.join(DATA_ROOT_PATH, '{}.npz'.format(data_name)))as npz_data:
self.img_vec = npz_data['img']
self.idx_vec = npz_data['idx']
print('image shape', self.img_vec.shape)
 print('idx shape', self.idx_vec.shape)
 label_path = os.path.join(DATA_ROOT_PATH, '{}_labels.csv'.format(data_name))
if os.path.exists(label_path):
label_df = pd.read_csv(label_path)
 self.lab_dict = dict(zip(label_df['idx'], label_df['label']))
else:
self.lab_dict = {x:x for x in self.idx_vec}
def __len__(self):
return len(self.img_vec)
 def __getitem__(self, idx):
out_label = self.lab_dict[self.idx_vec[idx]]
out_vec = np.array([out_label], dtype='int')
 img = self.img_vec[idx].astype('float32')
 return img, int(out_label )
datagen = ImageDataGenerator(zoom_range = 0.1,
height_shift_range = 0.1,
width_shift_range = 0.1,
rotation_range = 10 )<choose_model_class>
class SimpleMLP(nn.Module):
def __init__(self):
super(SimpleMLP, self ).__init__()
self.fc1 = nn.Linear(40*40, 512)
 self.fc2 = nn.Linear(512, 10)
 def forward(self, x):
x = x.view(-1, 40*40)
 x = F.relu(self.fc1(x))
x = self.fc2(x)
 return F.log_softmax(x, dim=1 )
model.compile(loss='categorical_crossentropy', optimizer = Adam(lr=1e-4), metrics=["accuracy"] )<choose_model_class>
train_ds = AffMNISTDataset('train')
 train_loader = DataLoader(train_ds, batch_size=1024,
shuffle=True, num_workers=4 )
annealer = LearningRateScheduler(lambda x: 1e-3 * 0.9 ** x )<train_model>
model = SimpleMLP().to(device)
 model.train()
hist = model.fit_generator(datagen.flow(x_train, y_train, batch_size=16),
steps_per_epoch=500,
epochs=20,
verbose=2,
validation_data=(x_val[:400,:], y_val[:400,:]),
callbacks=[annealer] )<compute_test_metric>
optimizer = optim.SGD(model.parameters() ,
lr=1e-3 )
final_loss, final_acc = model.evaluate(x_val, y_val, verbose=0)
 print("Final loss: {0:.4f}, final accuracy: {1:.4f}".format(final_loss, final_acc))<compute_test_metric>
log_interval = 100
for epoch in range(1):
for batch_idx,(data, target)in enumerate(train_loader):
data, target = data.to(device), target.to(device)
 optimizer.zero_grad()
output = model(data)
 loss = F.nll_loss(output, target)
 loss.backward()
optimizer.step()
if batch_idx % log_interval == 0:
print('Train Epoch: {} [{}/{}({:.0f}%)]\tLoss: {:.6f}'.format(
 epoch, batch_idx * len(data), len(train_loader.dataset),
100.* batch_idx / len(train_loader), loss.item()))
y_hat = model.predict(x_val)
 y_pred = np.argmax(y_hat, axis=1)
 y_true = np.argmax(y_val, axis=1)
 cm = confusion_matrix(y_true, y_pred)
 print(cm )<data_type_conversions>
test_ds = AffMNISTDataset('test')
 test_loader = DataLoader(test_ds, batch_size=1024,
shuffle=True, num_workers=4 )
mnist_testset = np.loadtxt(test_file, skiprows=1, dtype='int', delimiter=',')
 x_test = mnist_testset.astype("float32")
 x_test = x_test.reshape(-1, 28, 28, 1)/255 .<predict_on_test>
target_out = []
pred_out = []
for batch_idx,(data, target_idx)in enumerate(test_loader):
data = data.to(device)
 target_idx = target_idx.to('cpu' ).numpy()
output = model(data)
 pred = output.to('cpu' ).data.max(1)[1].numpy()
target_out += [target_idx]
pred_out += [pred]
y_hat = model.predict(x_test, batch_size=64 )<prepare_output>
pred_df = pd.DataFrame({'idx': np.concatenate(target_out, 0),
'label': np.concatenate(pred_out, 0)})
 pred_df.to_csv('mlp_predictions.csv', index=False)
 pred_df.sample(5 )