Kaggle competition: Digit Recognizer
def inference(model, states, test_loader, device):
model.to(device)
 tk0 = tqdm(enumerate(test_loader), total=len(test_loader))
probs = []
for i,(features)in tk0:
features = features.to(device)
 avg_preds = []
for state in states:
model.load_state_dict(state['model'])
 model.eval()
with torch.no_grad() :
y_preds = model(features)
 avg_preds.append(y_preds.softmax(1 ).to('cpu' ).numpy())
 avg_preds = np.mean(avg_preds, axis=0)
 probs.append(avg_preds)
 probs = np.concatenate(probs)
 return probs<save_to_csv>
np.random.seed(7)
 %matplotlib inline
model = CNNStacking(CFG['target_size'])
 states = [torch.load(STAGE2_DIR+f'/fold{fold}_best.pth')for fold in CFG['trn_fold']]
test_dataset = StackingDataset(stage1_predictions)
 test_loader = DataLoader(test_dataset, batch_size=CFG['batch_size'], shuffle=False,
num_workers=CFG['num_workers'], pin_memory=True)
 pred_stacking = inference(model, states, test_loader, device)
<load_pretrained>
train_path = '.. //input//train.csv'
test_path = '.. //input//test.csv'
train_df = pd.read_csv(train_path)
 test_df = pd.read_csv(test_path)
 train_df.info()
test_df.info()
with open('.. /input/train-weights-optimization/best_weights.json', 'r')as f:
weights_dict = json.load(f)
 weights_dict<define_variables>
pd.options.display.max_rows = 1000
print(train_df.isnull().sum() )
pred_weights_opt = np.zeros(weights_opt_feats.shape[1:], dtype=np.float)
 for idx, key in enumerate(model_dirs):
pred_weights_opt += weights_opt_feats[idx] * weights_dict[key[:-1]]<define_variables>
print(test_df.isnull().sum() )
BLENDING_WEIGHTS = {
"stacking": 0.5,
"weights_opt": 0.5
}<prepare_output>
X_train = X_train.astype('float32')
 X_test = X_test.astype('float32')
 X_train /= 255
X_test /= 255
predictions = pred_stacking * BLENDING_WEIGHTS['stacking'] + pred_weights_opt * BLENDING_WEIGHTS['weights_opt']
predictions<save_to_csv>
def inception_block(inputs):
tower_one = MaxPooling2D(( 3,3), strides=(1,1), padding='same' )(inputs)
 tower_one = Conv2D(6,(1,1), activation='relu', border_mode='same' )(tower_one)
 tower_two = Conv2D(6,(1,1), activation='relu', border_mode='same' )(inputs)
 tower_two = Conv2D(6,(3,3), activation='relu', border_mode='same' )(tower_two)
 tower_three = Conv2D(6,(1,1), activation='relu', border_mode='same' )(inputs)
 tower_three = Conv2D(6,(5,5), activation='relu', border_mode='same' )(tower_three)
 x = concatenate([tower_one, tower_two, tower_three], axis=3)
 return x
def inception_model(x_train):
inputs = Input(x_train.shape[1:])
 x = inception_block(inputs)
 x = Dropout(0.25 )(x)
 x = Conv2D(32,(3, 3), padding='same' )(x)
 x = Activation('relu' )(x)
 x = BatchNormalization()(x)
 x = Conv2D(32,(3, 3), padding='same' )(x)
 x = Activation('relu' )(x)
 x = BatchNormalization()(x)
 x = MaxPooling2D(pool_size=(2, 2))(x)
 x = Dropout(0.25 )(x)
 x = Flatten()(x)
 x = Dense(64, activation='relu' )(x)
 x = Dropout(0.5 )(x)
 predictions = Dense(10, activation='softmax' )(x)
 model = Model(input=inputs, output=predictions)
 model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
 return model
test['label'] = predictions.argmax(1)
 test[['image_id', 'label']].to_csv(OUTPUT_DIR+'submission.csv', index=False )<define_variables>
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'] )
package_path = '.. /input/pytorch-image-models/pytorch-image-models-master'
sys.path.append(package_path)
 DATA_DIR = '.. /input/cassava-leaf-disease-classification'
MODEL_DIR_0 = '.. /input/gpu-vit-noisearch-amp-aug-fold-0'
MODEL_DIR_1 = '.. /input/gpu-vit-noisearch-amp-aug-fold-1'
MODEL_DIR_2 = '.. /input/gpu-vit-noisearch-amp-aug-fold-2'
MODEL_DIR_3 = '.. /input/gpu-vit-noisearch-amp-aug-fold-3'
MODEL_DIR_4 = '.. /input/gpu-vit-noisearch-amp-aug-fold-4'
MODEL_DIR_01 = '.. /input/gpu-en-b4-ns-noisearch-amp-aug-fold-0'
MODEL_DIR_11 = '.. /input/gpu-en-b4-ns-noisearch-amp-aug-fold-1'
MODEL_DIR_21 = '.. /input/gpu-en-b4-ns-noisearch-amp-aug-fold-2'
MODEL_DIR_31 = '.. /input/gpu-en-b4-ns-noisearch-amp-aug-fold-3'
MODEL_DIR_41 = '.. /input/gpu-en-b4-ns-noisearch-amp-aug-fold-4'
MODEL_DIR_02 = '.. /input/gpu-seresnext-noisearch-amp-aug-fold-0'
MODEL_DIR_12 = '.. /input/gpu-seresnext-noisearch-amp-aug-fold-1'
MODEL_DIR_22 = '.. /input/gpu-seresnext-noisearch-amp-aug-fold-2'
MODEL_DIR_32 = '.. /input/gpu-seresnext-noisearch-amp-aug-fold-3'
MODEL_DIR_42 = '.. /input/gpu-seresnext-noisearch-amp-aug-fold-4'
MODEL_DIR_03 = '.. /input/gpu-en-b4-ns-amp-aug-fold-0'
MODEL_DIR_13 = '.. /input/gpu-en-b4-ns-amp-aug-fold-1'
MODEL_DIR_23 = '.. /input/gpu-en-b4-ns-amp-aug-fold-2'
MODEL_DIR_33 = '.. /input/gpu-en-b4-ns-amp-aug-fold-3'
MODEL_DIR_43 = '.. /input/gpu-en-b4-ns-amp-aug-fold-4' 
<import_modules>
history = model.fit(X_train, Y_train,
batch_size=100,
epochs=100,
validation_split=0.1,
shuffle=True )
HorizontalFlip, VerticalFlip, IAAPerspective, ShiftScaleRotate, CLAHE, RandomRotate90,
Transpose, ShiftScaleRotate, Blur, OpticalDistortion, GridDistortion, HueSaturationValue,
IAAAdditiveGaussianNoise, GaussNoise, MotionBlur, MedianBlur, IAAPiecewiseAffine, RandomResizedCrop,
IAASharpen, IAAEmboss, RandomBrightnessContrast, Flip, OneOf, Compose, Normalize, Cutout, CoarseDropout, ShiftScaleRotate, CenterCrop, Resize 
)
<init_hyperparams>
scores_train = model.evaluate(X_train, Y_train)
 print("
%s: %.2f%%" %(model.metrics_names[1], scores_train[1]*100))
CFG = {
'fold_num': 5,
'seed': 719,
'model_arch': 'vit_base_patch16_384',
'img_size': 384,
'epochs': 10,
'train_bs': 16,
'valid_bs': 16,
'lr': 1e-4,
'num_workers': 4,
'accum_iter': 1,
'verbose_step': 1,
'device': 'cuda:0',
'tta': 3,
'used_epochs': [7,8,9],
'weights': [1,1,1,1,1,1]
}<define_variables>
predictions = model.predict(X_test)
 predictions = np.argmax(predictions, axis = 1)
 predictions
EPOCHS0 = {
0: [9,8,6,5],
1: [5,6,4,8],
2: [8,9,7,6],
3: [8,7,9,6],
4: [9,8,7,4]
}
EPOCHS1 = {
0: [8,9,7,6],
1: [9,4,8,6],
2: [9,7,8,4],
3: [5,8,9,3],
4: [6,7,8,9]
}
EPOCHS2 = {
0: [8,9,6,7],
1: [9,8,5,6],
2: [9,7,5,4],
3: [5,8,9,4],
4: [5,8,9,7]
}
EPOCHS3 = {
0: [8,9,7,6],
1: [2,7,9,5],
2: [5,6,9,7],
3: [8,9,7,6],
4: [4,5,8,7]
} 
<load_from_csv>
result=pd.DataFrame({"ImageId": list(range(1,len(predictions)+1)) ,"Label": predictions})
 result.to_csv("mnist_cnn_only_v1.csv", index=False, header=True )