Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<choose_model_class>
Path.ls = lambda x: list(x.iterdir())
 path = Path('/kaggle/input/digit-recognizer/')
 def get_data(path,fn='train.csv'):
df = pd.read_csv(path/fn)
 if 'label' not in df.columns:
vals = np.ones_like(df.iloc[:,0].values)*-1
df.insert(0,'label',vals)
 X = df.iloc[:,1:].values
y = df.iloc[:,0].values
return X,y
class Dataset:
def __init__(self,X,y):
self.X, self.y = X,y
def __len__(self):
return len(self.X)
 def __getitem__(self,idx):
return torch.tensor(self.X[idx],dtype=torch.float),torch.tensor(self.y[idx],dtype=torch.long)
 def get_dls(train_ds,test_ds,bs=64):
return(DataLoader(train_ds,batch_size = bs, shuffle=True, drop_last=True),
DataLoader(test_ds, batch_size = bs*2, shuffle=False))
def init_cnn(m, uniform=False):
f = init.kaiming_uniform_ if uniform else init.kaiming_normal_
for l in m:
if isinstance(l, nn.Sequential):
f(l[0].weight, a=0.1)
 if l[0].bias is not None:
l[0].bias.data.zero_()
class Lambda(nn.Module):

 def __init__(self,func):
super().__init__()
self.func = func
def forward(self,x):
return self.func(x)
 def flatten(x):
return x.view(x.shape[0],-1)
 def mnist_resize(x):
return x.view(-1,1,28,28)
 def get_model(layers,pct_sparsity=0.,use_gpu=True):

 model = layers;
if use_gpu:
return model.cuda()
return model
def get_optimizer(model,lr=0.1):

 return optim.SGD(model.parameters() , lr=lr, momentum=0.9)
 def accuracy(preds, y):
return(torch.argmax(preds, dim=1)== y ).float().mean()
def average_metrics(dataloader,model,metrics=[],use_gpu=True):

 with torch.no_grad() :
count = 0
tot_metrics = [0.for _ in metrics]
for xb,yb in dataloader:
if use_gpu:
xb, yb = xb.cuda() , yb.cuda()
bs=len(xb)
 for idx, metric in enumerate(metrics):
tot_metrics[idx] += metric(model(xb),yb)* bs
count += bs
avg_metrics = list()
for metric in tot_metrics:
avg_metrics.append(metric/count)
 return avg_metrics
def fit_one_cycle(epochs,sched_func,use_gpu=True):

 n_epochs = 0;
for epoch in range(epochs):
n_epochs = epoch
iters = len(train_dl)
 model.train() ;
for xb,yb in train_dl:
if use_gpu:
xb, yb = xb.cuda() , yb.cuda()
preds = model(xb)
 loss = loss_func(preds,yb)
 loss.backward()
sched_params(opt, n_epochs, epochs,sched_func)
 opt.step()
opt.zero_grad()
n_epochs += 1./iters
print(f"Epoch {epoch} completed")
 def sched_params(opt, n_epochs, epoch,sched_func):
for pg in opt.param_groups:
pg['lr'] = sched_func(n_epochs/epoch)
 def annealer(f):
def _inner(start, end): return partial(f, start, end)
 return _inner
@annealer
def sched_cos(start, end, pos): return start +(1+math.cos(math.pi*(1-pos)))*(end-start)/2
def combine_scheds(pcts, scheds):
assert sum(pcts)== 1
pcts = tensor([0]+ list(pcts))
assert torch.all(pcts>=0)
 pcts = torch.cumsum(pcts,0)
 def _inner(pos):
idx =(pos>=pcts ).nonzero().max()
actual_pos =(pos-pcts[idx])/(pcts[idx+1]-pcts[idx])
 return scheds[idx](actual_pos)
 return _inner
def normalize(x,m,s):
return(x-m)/s
def normalize_to(data, train):
mean, std = train.mean() , train.std()
return normalize(data, mean, std), normalize(train, mean, std)
 def get_normalized_data() :
X_train, y_train = get_data(path, 'train.csv')
 X_test, y_test = get_data(path,'test.csv')
 X_test, X_train = normalize_to(X_test, X_train)
 return X_train, y_train, X_test, y_test
def get_stats(x):
return f'mean :{x.mean() } , std : {x.std() }';
def count_parameters(model):
return sum(p.numel() for p in model.parameters() if p.requires_grad)
 def get_preds(model,dl):
model.eval()
preds = []
for xb,yb in dl:
xb,yb = xb.cuda() , yb.cuda()
pred= model(xb)
 preds += pred.cpu().detach()
return preds
ensemble_outputs = []
with strategy.scope() :
X = L.Input(shape=input_shape)
 for i, w in enumerate(bin_weights):
base_name = w.split('-bin')[0]
model = build_classifier(base_name, n_classes = 1, input_shape=input_shape, weights = None, name_suffix='-M{}'.format(i+1))
model.load_weights('weights/' + w)
 model_output = model(X)
 ensemble_outputs.append(model_output)
 Y = L.Average()(ensemble_outputs)
 binary_ensemble = tf.keras.Model(inputs=X, outputs=Y, name='Binary_Classification_Ensemble')
 binary_ensemble.compile(optimizer='adam', loss='binary_crossentropy', metrics=[])
 del model, ensemble_outputs, model_output<predict_on_test>
class GeneralReLU(nn.Module):

 def __init__(self, leak=None, sub=None, maxv=None):
super().__init__()
self.leak, self.sub, self.maxv = leak, sub, maxv;
def forward(self, x):
x = F.leaky_relu(x, self.leak)if self.leak is not None else F.relu(x)
 if self.sub is not None: x.sub_(self.sub);
if self.maxv is not None: x.clamp_max_(self.maxv)
 return x
start_preds = time()
binary_predictions = binary_ensemble.predict(test_dataset_bin)
 del binary_ensemble
K.clear_session()
gc.collect()
print('Elapsed time(binary predictions){}'.format(time_passed(start_preds)) )<choose_model_class>
def conv_layer(f_in, f_out, ks, s, p):
return nn.Sequential(nn.Conv2d(f_in, f_out, kernel_size=ks, stride=s, padding=p,bias=False),
nn.BatchNorm2d(f_out),
GeneralReLU(sub=0.5))
ensemble_outputs = []
with strategy.scope() :
X = L.Input(shape=input_shape)
 for i, w in enumerate(seg_weights):
backbone_name = w.split('-unetpp')[0]
model = xnet(backbone_name, num_classes = 4, input_shape=input_shape, weights = None)
 model._name = '{}-M{}'.format(model.name, i+1)
 model.load_weights('weights/' + w)
 model_output = model(X)
 ensemble_outputs.append(model_output)
 Y = L.Average()(ensemble_outputs)
 seg_ensemble = tf.keras.Model(inputs=X, outputs=Y, name='Mask_Segmentation_Ensemble')
 seg_ensemble.compile(optimizer='adam', loss='binary_crossentropy', metrics=[])
 del model, ensemble_outputs, model_output
!rm -r weights<define_variables>
class ResBlock(nn.Module):
def __init__(self, nf):
super().__init__()
self.nf = nf
self.conv1 = conv_layer(nf,nf,3,1,1)
 self.conv2 = conv_layer(nf,nf,3,1,1)
 def forward(self, X):
return X + self.conv2(self.conv1(X))
THRESHOLD = 0.80
masked_indexes = np.where(binary_predictions>=THRESHOLD)[0]
unmasked_indexes = np.where(binary_predictions<THRESHOLD)[0]
seg_ids = list(np.array(test_ids)[masked_indexes])
 no_seg_ids = list(np.array(test_ids)[unmasked_indexes])
 print(len(seg_ids), len(no_seg_ids), len(seg_ids)+ len(no_seg_ids), len(test_ids))<create_dataframe>
class DenseBlock(nn.Module):
def __init__(self, ni, nf):
super().__init__()
self.ni, self.nf = ni, nf
self.conv1 = conv_layer(ni, nf,3,1,1)
 self.conv2 = conv_layer(nf, nf,3,1,1)
 def forward(self, X):
return torch.cat([X,self.conv2(self.conv1(X)) ],dim=1 )
fnames_seg = [get_test_path(i)for i in seg_ids]
batch_size = 8
test_dataset_seg = get_test_dataset(fnames_seg, target_size=target_size, batch_size=batch_size)
 num_batches = tf.data.experimental.cardinality(test_dataset_seg);
print('num of batches', num_batches.numpy() )<predict_on_test>
layers = nn.Sequential(Lambda(mnist_resize),
conv_layer(1,8,5,1,2),
nn.Dropout2d(p=0.05),
ResBlock(8),
nn.Dropout2d(p=0.05),
nn.MaxPool2d(3,2,1),
DenseBlock(8,8),
nn.Dropout2d(p=0.05),
nn.MaxPool2d(3,2,1),
DenseBlock(16,16),
nn.Dropout2d(p=0.05),
nn.AdaptiveAvgPool2d(1),
Lambda(flatten),
nn.Linear(32,10),
nn.BatchNorm1d(10)
 )
n_batches = 1
sample_preds = seg_ensemble.predict(test_dataset_seg.take(n_batches))
examples = retrieve_examples(test_dataset_seg, batch_size*n_batches)
 idx = -1
mask_rgb = [(230, 184, 0),(0, 128, 0),(102, 0, 204),(204, 0, 102)]<predict_on_test>
X_train, y_train, X_test, y_test = get_normalized_data()
train_dl, valid_dl = get_dls(Dataset(X_train,y_train), Dataset(X_test,y_test))
model = get_model(layers=layers)
 opt = get_optimizer(model)
 loss_func = nn.CrossEntropyLoss()
init_cnn(model )
thresh_upper = [0.7,0.7,0.7,0.7]
thresh_lower = [0.4,0.5,0.4,0.5]
min_area = [180, 260, 200, 500]
empty_mask = np.zeros(target_size, int)
 rles_dict = {}
for img_prefix in no_seg_ids:
for c in range(N_CLASSES):
row_name = '{}.jpg_{}'.format(img_prefix, c+1)
 rles_dict[row_name] = ''
start_preds = time()
for item in test_dataset_seg:
mask_predictions = seg_ensemble.predict(item[0])
 for k, p in enumerate(mask_predictions):
for ch in range(N_CLASSES):
ch_probs = p[..., ch]
ch_pred =(ch_probs > thresh_upper[ch])
 if ch_pred.sum() < min_area[ch]:
ch_pred = empty_mask.copy()
else:
ch_pred =(ch_probs > thresh_lower[ch])
 mask_predictions[k,:,:,ch] = ch_pred
img_ids = item[1]
ids = [l.decode('utf-8')for l in img_ids.numpy() ]
rles = [create_rles(p, IMAGE_SIZE)for p in mask_predictions]
for i in range(len(ids)) :
rle = rles[i]
img_prefix = ids[i]
for c in range(N_CLASSES):
row_name = '{}.jpg_{}'.format(img_prefix, c+1)
 rles_dict[row_name] = rle[c].numpy().decode('utf-8')
 print('Elapsed time(mask predictions){}'.format(time_passed(start_preds)) )<save_to_csv>
count_parameters(model )
df = pd.DataFrame.from_dict(rles_dict, orient='index')
 df.reset_index(level=0, inplace=True)
 df.columns = ['ImageId_ClassId', 'EncodedPixels']
df.to_csv('submission.csv', index=False )<load_from_csv>
one_cycle_sched= combine_scheds([0.3,0.7], [sched_cos(1e-3,1e-1), sched_cos(0.1,1e-6)])
 fit_one_cycle(30,one_cycle_sched )
train_data=pd.read_csv('/kaggle/input/forest-cover-type-prediction/train.csv')
 train_data.head()<load_from_csv>
preds = get_preds(model,valid_dl)
 res = []
for t in preds:
r = t.argmax().item()
res.append(r )
<count_values><EOS> 
submission = pd.read_csv(path/'sample_submission.csv')
 submission['Label'] = res
submission.to_csv('subs.csv',index=False )