Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<compute_train_metric>
%matplotlib inline
randomforest = RandomForestClassifier(n_estimators=200, max_depth=5)
 model1 = randomforest.fit(X_train, y_train)
 pred_valid = model1.predict(X_valid)
 pred_valid_prob = model1.predict_proba(X_valid)
 print(classification_report(y_valid, pred_valid))
print(model1.score(X_valid, y_valid))
print(roc_auc_score(y_valid,pred_valid_prob[:,1]))<save_to_csv>
train_df = pd.read_csv('.. /input/train.csv')
 test_df = pd.read_csv('.. /input/test.csv' )
model = randomforest.fit(X, y)
 pred = model.predict(X_test)
 result = pd.DataFrame(pred, index=X_test.index, columns=["Survived"])
 result.to_csv("submission.csv")
 result.head()<import_modules>
x_train = train_df.drop('label', axis=1 ).values
y_train = train_df['label'].values
x_test = test_df.values
import torch.nn as nn
import torch
from torch.utils.data import Dataset, DataLoader, ConcatDataset
import torchvision
from torchvision import transforms, utils
import glob
import os<define_variables>
img_height, img_width = 28, 28
x_train = x_train.reshape(x_train.shape[0], img_height, img_width, 1)
 x_test = x_test.reshape(x_test.shape[0], img_height, img_width, 1 )
en_dict = {}
path = '.. /input/quickdraw-doodle-recognition/train_simplified'
filenames = glob.glob(os.path.join(path, '*.csv'))
filenames[:5]<categorify>
num_classes = 10
y_train = np_utils.to_categorical(y_train, num_classes )
def encode_labels() :
counter = 0
for fn in filenames:
en_dict[fn[:-4].split('/')[-1].replace(' ', '_')] = counter
counter += 1<categorify>
x_test = x_test / 255.0
x_train = x_train / 255.0
encode_labels()<categorify>
model = Sequential()
model.add(Conv2D(
 filters=32,
kernel_size=(5,5),
input_shape=(img_height, img_width, 1),
padding="Same",
activation="relu" 
))
model.add(Conv2D(
 filters=32,
kernel_size=(5,5),
input_shape=(img_height, img_width, 1),
padding="Same",
activation="relu" 
))
model.add(MaxPooling2D(pool_size=(2,2)))
 model.add(Dropout(0.25))
model.add(Conv2D(
 filters=64,
kernel_size=(3,3),
input_shape=(img_height, img_width, 1),
padding="Same",
activation="relu" 
))
model.add(Conv2D(
 filters=64,
kernel_size=(3,3),
input_shape=(img_height, img_width, 1),
padding="Same",
activation="relu" 
))
model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(256, activation="relu"))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation="softmax"))
dec_dict = {v: k for k , v in en_dict.items() }
def decode_labels(label):
return dec_dict[label]<categorify>
model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=["accuracy"])
 print(model.summary() )
decode_labels(181 )<categorify>
checkpoint = ModelCheckpoint(
 'best_model.hdf5',
monitor='val_acc',
save_best_only=True,
verbose=1
)
def get_label(nfile):
return en_dict[nfile[:-4].split('/')[-1].replace(' ', '_')]<define_variables>
lr_reduction = ReduceLROnPlateau(
 monitor='val_acc',
patience=3,
factor=0.5,
min_lr=0.00001,
verbose=1
)
get_label(".. /input/quickdraw-doodle-recognition/train_simplified/The Eiffel Tower.csv" )<load_from_csv>
batch_size = 96
epochs = 40
validation_size = 0.3
class DoodleDataset(Dataset):
def __init__(self, csv_file, root_dir, mode='train', nrows=1000, skiprows=None, size=256, transform=None):
self.root_dir = root_dir
file = os.path.join(root_dir, csv_file)
 self.size=size
self.mode = mode
self.doodle = pd.read_csv(file, usecols=['drawing'], nrows=nrows, skiprows=skiprows)
 self.transform = transform
if self.mode == 'train':
self.label = get_label(csv_file)
 @staticmethod
def _draw(raw_strokes, size=256, lw=6, time_color=True):
BASE_SIZE = 256
img = np.zeros(( BASE_SIZE, BASE_SIZE),np.uint8)
 for t, stroke in enumerate(raw_strokes):
for i in range(len(stroke[0])- 1):
color = 255 - min(t,10)* 13 if time_color else 255
_ = cv2.line(img,(stroke[0][i], stroke[1][i]),(stroke[0][i+1], stroke[1][i+1]), color, lw)
 if size != BASE_SIZE:
return cv2.resize(img,(size, size))
else:
return img
def __len__(self):
return len(self.doodle)
 def __getitem__(self, index):
raw_strokes = ast.literal_eval(self.doodle.drawing[index])
 sample = self._draw(raw_strokes, size=self.size, lw=2, time_color=True)
 if self.transform:
sample = self.transform(sample)
 if self.mode == 'train':
return(sample[None]/255 ).astype('float32'), self.label
else:
return(sample[None]/255 ).astype('float32')
<create_dataframe>
history = model.fit(
 x=x_train,
y=y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_split=validation_size,
callbacks=[checkpoint, lr_reduction]
)
SIZE = 224
select_nrows= 10000
doodles = ConcatDataset([DoodleDataset(fn.split('/')[-1], path, mode='train', nrows=select_nrows, skiprows=None, size=SIZE, transform=None)for fn in filenames])
<load_pretrained>
model.load_weights('best_model.hdf5' )
print(len(doodles))
train_dataloader = DataLoader(doodles, batch_size=128, shuffle=True, num_workers=0 )<compute_test_metric>
probabilities = model.predict(x_test )
def validation(get_loader,lossfn, scorefn):
model.eval()
loss, score = 0,0
vlen = len(get_loader)
 for X , y in get_loader:
X = X.to(device)
 y = y.to(device)
 output = model(X)
 loss += lossfn(output, y ).item()
score += scorefn(output, y)[0].item()
model.train()
return loss/vlen, score/vlen<compute_test_metric>
predictions = np.argmax(probabilities, axis=1 )
def accuracy(output, target, topk=(3,)) :
with torch.no_grad() :
maxk = max(topk)
 batch_size = target.size(0)
 _, pred = output.topk(maxk, 1, True, True)
 pred = pred.t()
correct = pred.eq(target.view(1,-1 ).expand_as(pred))
res = []
for k in topk:
correct_k = correct[:k].view(-1 ).float().sum(0,keepdim=True)
 res.append(correct_k.mul_(100.0/batch_size))
return res<categorify>
submission = pd.DataFrame(data={
'ImageId': list(range(1, predictions.shape[0]+1)) ,
'Label': predictions
} )
def mapk(output, target, k=3):

 with torch.no_grad() :
batch_size = target.size(0)
 _, pred = output.topk(k, 1, True, True)
 pred = pred.t()
correct = pred.eq(target.reshape(1, -1 ).expand_as(pred))
for i in range(k):
correct[i] = correct[i]*(k-i)
 score = correct[:k].reshape(-1 ).float().sum(0, keepdim=True)
 score.mul_(1.0 /(k * batch_size))
return score<define_search_space>
submission.to_csv('submission.csv', index=False )
<choose_model_class><EOS> 
submission.to_csv('submission.csv', index=False )