Kaggle competition: Digit Recognizer
model_dirs = []
for stage1 in CFG['stage1_models']:
num = str(stage1 ).rjust(2, '0')
 output_dir_ = glob.glob(f'.. /input/{num}*/')
 assert len(output_dir_)== 1, output_dir_
model_dirs.append(output_dir_[0])
 model_dirs<load_pretrained>
sample_submission = pd.read_csv(".. /input/digit-recognizer/sample_submission.csv")
 test = pd.read_csv(".. /input/digit-recognizer/test.csv")
 train = pd.read_csv(".. /input/digit-recognizer/train.csv" )
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>
X_train = train / 255.0
X_test = test / 255.0
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>
X_train =(X_train.iloc[:,1:].values ).astype('float32')
 y_train = train['label'].astype('float32')
 X_test = X_test.values.astype('float32' )
TRAIN_PATH = '.. /input/cassava-leaf-disease-classification/train_images'
TEST_PATH = '.. /input/cassava-leaf-disease-classification/test_images'<normalization>
mean_px = X_train.mean().astype(np.float32)
 std_px = X_train.std().astype(np.float32)
 def standardize(x):
return(x - mean_px)/std_px
[mean_px, std_px]
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>
print(y_train)
 y_train= to_categorical(y_train)
 num_classes = y_train.shape[1]
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>
seed = 43
np.random.seed(seed)
 seed
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>
gen = image.ImageDataGenerator()
X = X_train
y = y_train
X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.10, random_state=42)
 batches = gen.flow(X_train, y_train, batch_size=64)
 val_batches=gen.flow(X_val, y_val, batch_size=64)
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>
gen =ImageDataGenerator(rotation_range=8, width_shift_range=0.08, shear_range=0.3,
height_shift_range=0.08, zoom_range=0.08)
 batches = gen.flow(X_train, y_train, batch_size=64)
 val_batches = gen.flow(X_val, y_val, batch_size=64)
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<define_variables>
def get_bn_model() :
model = Sequential([
Lambda(standardize, input_shape=(28,28,1)) ,
Conv2D(32,(3,3), activation='relu'),
BatchNormalization() ,
Conv2D(32,(3,3), activation='relu'),
MaxPooling2D() ,
BatchNormalization() ,
Conv2D(64,(3,3), activation='relu'),
BatchNormalization() ,
Conv2D(64,(3,3), activation='relu'),
MaxPooling2D() ,
Flatten() ,
BatchNormalization() ,
Dense(512, activation='relu'),
BatchNormalization() ,
Dense(10, activation='softmax')
 ])
 model.compile(Adam() , loss='categorical_crossentropy', metrics=['accuracy'])
 return model
model= get_bn_model()
model.optimizer.learning_rate=0.01
history=model.fit_generator(generator=batches, steps_per_epoch=batches.n, epochs=1,
validation_data=val_batches, validation_steps=val_batches.n)
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)
 stage1_predictions = stage1_predictions.transpose(1, 2, 0, 3 )<categorify>
model.optimizer.learning_rate=0.01
gen = image.ImageDataGenerator()
batches = gen.flow(X, y, batch_size=64)
 history=model.fit_generator(generator=batches, steps_per_epoch=batches.n, epochs=3 )
<choose_model_class><EOS> 
predictions = model.predict_classes(X_test, verbose=0)
 submissions=pd.DataFrame({"ImageId": list(range(1,len(predictions)+1)) ,
"Label": predictions})
 submissions.to_csv("DR.csv", index=False, header=True)