Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<choose_model_class>
import numpy as np
import pandas as pd
import tensorflow as tf
from skimage import io, transform
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.metrics import accuracy_score
def squeeze_weights(m):
m.weight.data = m.weight.data.sum(dim=1)[:,None]
m.in_channels = 1
model.conv1.apply(squeeze_weights)
 num_classes = 340
model.fc = nn.Linear(512, out_features=num_classes, bias=True )<find_best_params>
from keras.models import Sequential
from keras.layers import Conv2D, MaxPool2D, Dropout, Flatten, Dense, BatchNormalization
from keras.utils import to_categorical
from keras.preprocessing.image import ImageDataGenerator
from keras.optimizers import RMSprop
from keras.callbacks import ReduceLROnPlateau
from keras.datasets import mnist
%%time
model(torch.randn(12,1,224,224)).size()<train_model>
train_data = pd.read_csv(".. /input/train.csv")
 test_data = pd.read_csv(".. /input/test.csv")
 print(train_data.shape,
test_data.shape )
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
 model = model.to(device )<choose_model_class>
( x_train1, y_train1),(x_test1, y_test1)= mnist.load_data()
x_train1 = np.concatenate(( x_test1, x_train1))
y_train1 = np.concatenate(( y_test1, y_train1))
x_train1 = x_train1.reshape(( x_train1.shape[0], 28, 28, 1))
print(x_train1.shape, y_train1.shape )
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters() , lr=0.002, amsgrad = True)
 scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, milestones=[5000, 12000, 18000], gamma=0.5 )<train_model>
x = np.array(train_data.drop(['label'], axis = 1))
y = np.array(train_data['label'])
 test_data = np.array(test_data)
 x = x.reshape(( x.shape[0], 28, 28, 1))
test_data = test_data.reshape(test_data.shape[0], 28, 28, 1)
 x = np.concatenate(( x, x_train1))
y = np.concatenate(( y, y_train1))
x = x/255
test_data = test_data/255
y = to_categorical(y, num_classes = 10)
 print(x.shape, y.shape )
%%time
epochs = 1
lsize = len(train_dataloader)
 print(f"size of train : {lsize}")
 itr =1
p_itr = 1000
model.train()
tloss, score =0,0
for epoch in range(epochs):
for X, y in train_dataloader:
X, y = X.to(device), y.to(device)
 output = model(X)
 loss = criterion(output, y)
 tloss += loss.item()
optimizer.zero_grad()
loss.backward()
optimizer.step()
score += mapk(output, y)[0].item()
scheduler.step()
if itr %p_itr ==0 :
print('Iteration {} -> Train Loss: {:.4f}, MAP@3: {:.3f}'.format(itr, tloss/p_itr, score/p_itr))
tloss, score = 0, 0
itr +=1
if itr >= 6000:
break 
<save_model>
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.10, shuffle = True)
 print(x_train.shape, y_train.shape, x_test.shape, y_test.shape )
filename_pth = 'checkpoint_resnet18.pth'
torch.save(model.state_dict() , filename_pth )<load_from_csv>
model = Sequential()
model.add(Conv2D(filters = 32, kernel_size =(3,3), activation ='relu', input_shape =(28,28,1)))
 model.add(Conv2D(filters = 32, kernel_size =(3,3), activation ='relu'))
model.add(MaxPool2D(( 2,2)))
 model.add(BatchNormalization())
 model.add(Conv2D(filters = 64, kernel_size =(3,3), activation ='relu'))
model.add(Conv2D(filters = 64, kernel_size =(3,3), activation ='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(filters = 64, kernel_size =(3,3), activation ='relu'))
model.add(Conv2D(filters = 64, kernel_size =(3,3), activation ='relu'))
model.add(BatchNormalization())
 model.add(Flatten())
 model.add(Dense(128, activation = "relu"))
model.add(Dropout(0.30))
model.add(Dense(10, activation = "softmax"))
optimizer = RMSprop(lr = 0.01, rho = 0.9, epsilon = 1e-08, decay = 0.0)
 model.compile(optimizer = optimizer, loss = 'categorical_crossentropy', metrics = ['accuracy'])
 model.summary()
testset = DoodleDataset('test_simplified.csv', '.. /input/quickdraw-doodle-recognition/', mode='test', nrows=None, size=SIZE)
 testloader = DataLoader(testset, batch_size=128, shuffle=False, num_workers=0 )<train_model>
datagen = ImageDataGenerator(
 rotation_range = 10,
zoom_range = 0.1,
width_shift_range = 0.1,
height_shift_range = 0.1,)
 train_batch = datagen.flow(x, y, batch_size = 64)
 val_batch = datagen.flow(x_test, y_test, batch_size = 64 )
model.eval()
model = model.to(device)
 labels = np.empty(( 0,3))
for x in tqdm.tqdm(testloader):
x = x.to(device)
 output = model(x)
 _, pred = output.topk(3, 1, True, True)
 labels = np.concatenate([labels, pred.cpu() ], axis = 0 )<load_from_csv>
learning_rate_reduction = ReduceLROnPlateau(monitor = 'val_loss',
patience = 3,
verbose = 1,
factor = 0.1,
min_lr = 0.00001 )
%%time
submission = pd.read_csv('.. /input/quickdraw-doodle-recognition/test_simplified.csv', index_col='key_id')
 submission.drop(['countrycode', 'drawing'], axis=1, inplace=True)
 submission['word'] = ''
for i, label in enumerate(labels):
submission.word.iloc[i] = " ".join([decode_labels(l)for l in label] )<save_to_csv>
history = model.fit_generator(generator = train_batch,
epochs = 100,
steps_per_epoch = len(train_batch),
validation_data = val_batch,
validation_steps = len(val_batch),
verbose = 1,
callbacks = [learning_rate_reduction] )
submission.to_csv('submission.csv' )<train_model>
res = model.predict_classes(test_data, batch_size = 64)
 result = pd.Series(res, name = 'Label')
 submission = pd.concat([pd.Series(range(1, 28001), name = 'ImageId'), result], axis = 1)
 submission.to_csv('Submission.csv', index = False )
print("done" )<set_options>
res = model.evaluate(x, y, batch_size = 1024)
 print(res[1]*100 )