Kaggle competition: Digit Recognizer
model = NeuralNetwork()
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adamax(model.parameters() , lr=0.0001, betas=(0.9, 0.999), eps=1e-08, weight_decay=0.8)
 epochs = 40
steps = 0
train_losses, val_losses, val_accs = [], [], []
early_stop_count = 0<train_on_grid>
PATH = '.. /input/digit-recognizer'
df_train = pd.read_csv(os.path.join(PATH, 'train.csv'))
train_y = df_train['label'].values
train_x = df_train.drop(['label'], axis=1 ).values
df_test = pd.read_csv(os.path.join(PATH, 'test.csv'))
test_x = df_test.values
print(train_x.shape)
 print(train_y.shape)
 print(test_x.shape )
if torch.cuda.is_available() :
model = model.cuda()
criterion = criterion.cuda()
print("GPU Training")
 else:
print("CPU Training" )<train_model>
IMG_SIZE = 32
print("---- Starting Model Training ----
")
 for e in range(epochs):
if early_stop_count >= 10:
print("
Validation Accuracy not improved for {} epochs.val_acc: {}.".format(early_stop_count, sorted(val_accs)[-1]))
break
print("
-- Epoch {}/{} -- ".format(e+1, epochs))
running_loss = 0
steps = 0
for images, labels in train_loader:
if torch.cuda.is_available() :
images = images.to(device)
 labels = labels.to(device)
 steps += 1
optimizer.zero_grad()
log_ps = model(images)
 loss = criterion(log_ps, labels)
 loss.backward()
optimizer.step()
running_loss += loss.item()
if steps % len(train_loader)== 0:
val_loss = 0
val_accuracy = 0
train_accuracy = 0
with torch.no_grad() :
model.eval()
for images, labels in val_loader:
if torch.cuda.is_available() :
images = images.to(device)
 labels = labels.to(device)
 log_ps = model(images)
 val_loss += criterion(log_ps, labels)
 ps = torch.exp(log_ps)
 top_p, top_class = ps.topk(1, dim=1)
 equals = top_class == labels.view(*top_class.shape)
 val_accuracy += torch.mean(equals.type(torch.FloatTensor))
for images, labels in train_loader:
if torch.cuda.is_available() :
images = images.to(device)
 labels = labels.to(device)
 log_ps = model(images)
 ps = torch.exp(log_ps)
 top_p, top_class = ps.topk(1, dim=1)
 equals = top_class == labels.view(*top_class.shape)
 train_accuracy += torch.mean(equals.type(torch.FloatTensor))
model.train()
train_losses.append(running_loss/len(train_loader))
val_losses.append(val_loss/len(val_loader))
val_accs.append(val_accuracy/len(val_loader))
if val_accs[-1] == sorted(val_accs)[-1]:
best_model_params = model.state_dict()
early_stop_count = 0
print("+")
 print("Training Loss: {:.4f} ".format(train_losses[-1]),
"Training Acc: {:.4f}".format(train_accuracy/len(train_loader)) ,
"Valid Loss: {:.4f} ".format(val_losses[-1]),
"Valid Accuracy: {:.4f}".format(val_accs[-1]))
early_stop_count += 1
if e ==(epochs-1):
print("
Top Validation Accuracy: {}".format(sorted(val_accs)[-1]))<load_from_disk>
def resize(img_array):
tmp = np.empty(( img_array.shape[0], IMG_SIZE, IMG_SIZE))
for i in range(len(img_array)) :
img = img_array[i].reshape(28, 28 ).astype('uint8')
 img = cv2.resize(img,(IMG_SIZE, IMG_SIZE))
img = img.astype('float32')/255
tmp[i] = img
return tmp
train_x_resize = resize(train_x)
 test_x_resize = resize(test_x )
model.load_state_dict(best_model_params )<categorify>
train_y_final = to_categorical(train_y, num_classes=10)
 print(train_y_final.shape )
final_test_np = final_test.values/255
test_labels = np.zeros(final_test_np.shape)
 test_tn = torch.from_numpy(final_test_np)
 test_labels = torch.from_numpy(test_labels )<categorify>
vgg19 = VGG19(weights = 'imagenet',
include_top = False,
input_shape=(IMG_SIZE, IMG_SIZE, 3)
)
 model = Sequential()
model.add(vgg19)
 model.add(Flatten())
 model.add(Dense(10, activation='softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='sgd',
metrics=['accuracy'])
 model.summary()
class test_mnist(Dataset):
def __init__(self, feat_tens):
self.data = feat_tens.reshape(len(feat_tens), 1, 28, 28)
 def __len__(self):
return len(self.data)
 def __getitem__(self, index):
image = self.data[index]
return image
submission_dataset = test_mnist(test_tn)
 submission_loader = torch.utils.data.DataLoader(submission_dataset, batch_size = 256, shuffle = False )<prepare_output>
x_train, x_test, y_train, y_test = train_test_split(train_x_final, train_y_final, test_size=0.2, random_state=2019)
 print(x_train.shape)
 print(x_test.shape)
 print(y_train.shape)
 print(y_test.shape )
submission = [['ImageId', 'Label']]
with torch.no_grad() :
model.eval()
image_id = 1
for images in submission_loader:
if torch.cuda.is_available() :
images = images.to(device)
 log_ps = model(images)
 ps = torch.exp(log_ps)
 top_p, top_class = ps.topk(1, dim=1)
 for prediction in top_class:
submission.append([image_id, prediction.item() ])
 image_id += 1
submission_df = pd.DataFrame(submission)
 submission_df.columns = submission_df.iloc[0]
submission_df = submission_df.drop(0, axis=0)
 submission_df.to_csv("submission.csv", index=False )<load_from_csv>
es = EarlyStopping(monitor='val_acc', verbose=1, patience=5)
 mc = ModelCheckpoint(filepath='mnist-vgg19.h5', verbose=1, monitor='val_acc')
 cb = [es, mc]
train_set = pd.read_csv('.. /input/digit-recognizer/train.csv')
 test_set = pd.read_csv('.. /input/digit-recognizer/test.csv' )<drop_column>
history = model.fit(x_train, y_train,
epochs=100,
batch_size=128,
validation_data=(x_test, y_test),
callbacks=cb )
X = train_set.drop('label', axis=1)
 labels = train_set['label']<prepare_x_and_y>
preds = model.predict(test_x_final, batch_size=128 )
y = pd.get_dummies(labels )<feature_engineering>
results = np.argmax(preds, axis=-1)
 results.shape
<split><EOS> 
sub = pd.read_csv(os.path.join(PATH, 'sample_submission.csv'))
sub.head()
df = pd.DataFrame({'ImageId': sub['ImageId'], 'Label': results})
 df.to_csv('submission.csv', index=False)
 os.listdir('./' )