Kaggle competition: Digit Recognizer
normal_configs = []
tta_configs = []
normal_model_dirs = []
tta_model_dirs = []
for model_dir in model_dirs:
assert len(glob.glob(f'{model_dir}/*.yml')) ==1
config_path = glob.glob(f'{model_dir}/*.yml')[0]
with open(config_path)as f:
config = yaml.load(f)
 if 'valid_augmentation' in config['tag'].keys() :
tta_model_dirs.append(model_dir)
 tta_configs.append(config)
 else:
normal_model_dirs.append(model_dir)
 normal_configs.append(config )<compute_test_metric>
import numpy as np
import pandas as pd
import random
def get_score(y_true, y_pred):
return accuracy_score(y_true, y_pred)
 def remove_glob(pathname, recursive=True):
for p in glob.glob(pathname, recursive=recursive):
if os.path.isfile(p):
os.remove(p)
 @contextmanager
def timer(name):
t0 = time.time()
LOGGER.info(f'[{name}] start')
 yield
LOGGER.info(f'[{name}] done in {time.time() - t0:.0f} s.')
 def seed_torch(seed=42):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True 
<define_variables>
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D
from keras import backend as K
TRAIN_PATH = '.. /input/cassava-leaf-disease-classification/train_images'
TEST_PATH = '.. /input/cassava-leaf-disease-classification/test_images'<normalization>
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
class TestDataset(Dataset):
def __init__(self, df, transform=None):
self.df = df
self.file_names = df['image_id'].values
self.transform = transform
def __len__(self):
return len(self.df)
 def __getitem__(self, idx):
file_name = self.file_names[idx]
file_path = f'{TEST_PATH}/{file_name}'
image = cv2.imread(file_path)
 image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
 if self.transform:
augmented = self.transform(image=image)
 image = augmented['image']
return image
class TTADataset(Dataset):
def __init__(self, df, image_path, ttas):
self.df = df
self.file_names = df['image_id'].values
self.labels = df['label'].values
self.image_path = image_path
self.ttas = ttas
def __len__(self)-> int:
return len(self.df)
 def __getitem__(self, idx):
file_name = self.file_names[idx]
file_path = f'{self.image_path}/{file_name}'
image = cv2.imread(file_path)
 image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
 imglist=[tta(image=image)['image'] for tta in self.ttas]
image=torch.stack(imglist)
 label = torch.tensor(self.labels[idx] ).long()
return image, label<normalization>
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv")
def _get_augmentations(aug_list, cfg):
process = []
for aug in aug_list:
if aug == 'Resize':
process.append(Resize(cfg['size'], cfg['size']))
elif aug == 'RandomResizedCrop':
process.append(RandomResizedCrop(cfg['size'], cfg['size']))
elif aug == 'CenterCrop':
process.append(CenterCrop(CFG['size'], CFG['size']))
elif aug == 'Transpose':
process.append(Transpose(p=0.5))
elif aug == 'HorizontalFlip':
process.append(HorizontalFlip(p=0.5))
elif aug == 'VerticalFlip':
process.append(VerticalFlip(p=0.5))
elif aug == 'ShiftScaleRotate':
process.append(ShiftScaleRotate(p=0.5))
elif aug == 'Normalize':
process.append(Normalize(
 mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225], 
))
else:
raise ValueError(f'{aug} is not suitable')
 process.append(ToTensorV2())
 return process
def get_transforms(*, aug_list, cfg):
return Compose(
 _get_augmentations(aug_list, cfg)
 )<choose_model_class>
y_train = train["label"]
X_train = train.drop(labels = ["label"],axis = 1 )
class CustomModel(nn.Module):
def __init__(self, model_name, target_size, pretrained=False):
super().__init__()
self.model = timm.create_model(model_name, pretrained=pretrained)
 if hasattr(self.model, 'classifier'):
n_features = self.model.classifier.in_features
self.model.classifier = nn.Linear(n_features, target_size)
 elif hasattr(self.model, 'fc'):
n_features = self.model.fc.in_features
self.model.fc = nn.Linear(n_features, target_size)
 elif hasattr(self.model, 'head'):
n_features = self.model.head.in_features
self.model.head = nn.Linear(n_features, target_size)
 def forward(self, x):
x = self.model(x)
 return x<categorify>
def prep_data(X_train, y_train, test):
X_train = X_train.astype('float32')/ 255
test = test.astype('float32')/255
X_train = X_train.values.reshape(-1,28,28,1)
 test = test.values.reshape(-1,28,28,1)
 y_train = keras.utils.np_utils.to_categorical(y_train)
 classes = y_train.shape[1]
X_train, X_test, y_train, y_test = train_test_split(X_train, y_train, test_size = 0.2, random_state = int(time.time()))
return X_train, y_train, X_test, y_test, classes, test
def inference_tta(model, states, tta_loader, device):
model.to(device)
 tk0 = tqdm(enumerate(tta_loader), total=len(tta_loader))
probs = []
for i,(images, _)in tk0:
images = images.to(device)
 batch_size, n_crops, c, h, w = images.size()
images = images.view(-1, c, h, w)
 avg_preds = []
for state in states:
model.load_state_dict(state['model'])
 model.eval()
with torch.no_grad() :
y_preds = model(images ).softmax(1)
 y_preds = y_preds.view(batch_size, n_crops,-1)
 avg_preds.append(y_preds.to('cpu' ).numpy())
 avg_preds = np.mean(avg_preds, axis=0)
 probs.append(avg_preds)
 del images, _, y_preds, avg_preds
torch.cuda.empty_cache()
probs = np.concatenate(probs)
 return probs<create_dataframe>
X_train, y_train, X_test, y_test, out_neurons, test = prep_data(X_train, y_train, test )
def main_tta(config, model_dir):
INFO = config['info']
TAG = config['tag']
CFG = config['cfg']
CFG['train'] = False
CFG['inference'] = True
inference_batch_size = 8
seed_torch(seed=CFG['seed'])
 model = CustomModel(TAG['model_name'], CFG['target_size'], pretrained=False)
 states = [torch.load(path)for path in glob.glob(f'{model_dir}/*.pth')]
ttas = get_ttas(CFG)
 tta_dataset = TTADataset(test, TEST_PATH, ttas=ttas)
 tta_loader = DataLoader(tta_dataset, batch_size=inference_batch_size, shuffle=False,
num_workers=2, pin_memory=True)
 predictions = inference_tta(model, states, tta_loader, device)
 return predictions<categorify>
model = Sequential([
Conv2D(32, kernel_size =(3, 3), padding = 'same', activation = 'relu', input_shape =(28,28,1)) ,
Conv2D(32, kernel_size =(3, 3), activation = 'relu', padding = 'same'),
MaxPool2D(pool_size =(2, 2)) ,
Dropout(0.25),
Conv2D(64, kernel_size =(3, 3), activation = 'relu', padding = 'same'),
Conv2D(64, kernel_size =(3, 3), activation = 'relu', padding = 'same'),
MaxPool2D(pool_size =(2, 2)) ,
Dropout(0.25),
Conv2D(128, kernel_size =(3, 3), activation = 'relu', padding = 'same'),
Conv2D(128, kernel_size =(3, 3), activation = 'relu', padding = 'same'),
MaxPool2D(pool_size =(2, 2)) ,
Dropout(0.25),
Conv2D(128, kernel_size =(3, 3), activation = 'relu', padding = 'same'),
Conv2D(128, kernel_size =(3, 3), activation = 'relu', padding = 'same'),
MaxPool2D(pool_size =(2, 2)) ,
Dropout(0.25),
Flatten() ,
Dense(512, activation = 'relu'),
Dropout(0.5),
Dense(256, activation = 'relu'),
Dropout(0.5),
Dense(out_neurons, activation = 'softmax')
 ])
 model.summary()
sgd = optimizers.SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
 model.compile(loss = 'categorical_crossentropy', optimizer = sgd , metrics = ['accuracy'] )
data_num = len(test)
 model_num = len(model_dirs)
 target_num = CFG['target_size']
channel_num = 4
stage1_predictions = np.zeros(( model_num, data_num, channel_num, target_num), dtype=np.float)
 for config, model_dir in zip(tta_configs, tta_model_dirs):
stage1_predictions[model_dirs.index(model_dir)] = main_tta(config, model_dir)
 weights_opt_feats = stage1_predictions.mean(axis=2)
 stage1_predictions = stage1_predictions.transpose(1, 2, 0, 3 )<categorify>
model.fit(X_train, y_train,
batch_size = 512,
epochs = 180,
validation_data =(X_test, y_test),
verbose = 0);
class StackingDataset(Dataset):
def __init__(self, X: np.ndarray, y: Optional[np.ndarray] = None):
self.X = X
self.y = y
def __len__(self):
return self.X.shape[0]
def __getitem__(self, idx):
if self.y is None:
return torch.tensor(self.X[idx], dtype=torch.float)
 else:
return(
 torch.tensor(self.X[idx], dtype=torch.float),
torch.tensor(self.y[idx], dtype=torch.long),
)<choose_model_class>
result = model.evaluate(X_test, y_test, verbose = 0)
 print('Accuracy: ', result[1])
 print('Error: %.2f%%' %(100- result[1]*100))
y_pred = model.predict(test, verbose=0 )
class CNNStacking(nn.Module):
def __init__(self, n_labels):
super(CNNStacking, self ).__init__()
self.sq = nn.Sequential(
 nn.Conv2d(in_channels=4, out_channels=8, kernel_size=(3, 1), bias=False),
nn.ReLU() ,
nn.Conv2d(in_channels=8, out_channels=16, kernel_size=(3, 1), bias=False),
nn.ReLU() ,
nn.Flatten() ,
nn.Linear(in_features=16* n_labels, out_features=4 * n_labels),
nn.ReLU() ,
nn.Linear(in_features=4 * n_labels, out_features=n_labels), 
)
 def forward(self, x):
return self.sq(x )<predict_on_test>
solution = np.argmax(y_pred,axis = 1)
 solution = pd.Series(solution, name="Label" ).astype(int)
 submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),solution],axis = 1)
 submission.to_csv("mnist_with_cnn.csv",index=False )