Kaggle competition: Tabular Playground Series - Jan 2021
def load_state(model_path):
model = CustomResNext(CFG.model_name, pretrained=False)
 try:
model.load_state_dict(torch.load(model_path)['model'], strict=True)
 state_dict = torch.load(model_path)['model']
except:
state_dict = torch.load(model_path)['model']
state_dict = {k[7:] if k.startswith('module.')else k: state_dict[k] for k in state_dict.keys() }
return state_dict
def inference(model, states, test_loader, device):
tta_probs = []
for e in range(CFG.tta):
model.to(device)
 tk0 = tqdm(enumerate(test_loader), total=len(test_loader))
probs = []
for i,(images)in tk0:
images = images.to(device)
 avg_preds = []
for state in states:
model.load_state_dict(state)
 model.eval()
with torch.no_grad() :
y_preds = model(images)
 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)
 tta_probs.append(probs)
 tta_probs = np.mean(tta_probs, axis=0)
 return tta_probs<load_pretrained>
mpl.rcParams['agg.path.chunksize'] = 10000
model = CustomResNext(CFG.model_name, pretrained=False)
 states = [load_state(MODEL_DIR+f'{CFG.model_name}_fold{fold}.pth')for fold in CFG.trn_fold]
test_dataset = TestDataset(test, transform=get_transforms(data='valid'))
test_loader = DataLoader(test_dataset, batch_size=CFG.batch_size, shuffle=False,
num_workers=CFG.num_workers, pin_memory=True)
 predictions = inference(model, states, test_loader, device )<feature_engineering>
train_data = pd.read_csv('/kaggle/input/tabular-playground-series-jan-2021/train.csv')
 test_data = pd.read_csv('/kaggle/input/tabular-playground-series-jan-2021/test.csv')
 print("successfully loaded!" )
submission = test[["image_id"]]
submission["label"] =(
 np.mean(tst_preds_all_folds, axis=0)* 0.7
+ predictions * 0.3
).argmax(1 )<save_to_csv>
outlier = train_data.loc[train_data.target < 1.0]
print(outlier )
submission.to_csv("submission.csv", index=False )<save_to_csv>
train_data.drop([170514], inplace = True )
submission.to_csv("submission.csv", index=False )<define_variables>
y_train = train_data["target"]
train_data.drop(columns = ["target"], inplace = True )
class CFG:
img_size = 512
num_classes = 5
num_workers = 4
batch_size = 64
epochs = 1
OUTPUT_DIR = './'
ROOT_DIR = '.. /input/cassava-leaf-disease-classification/'
TRAIN_PATH = '.. /input/cassava-leaf-disease-classification/train_images'
TEST_PATH = '.. /input/cassava-leaf-disease-classification/test_images'
MODEL_DIR = '.. /input/cassava-models'<normalization>
params = { 'n_estimators' : [1500, 2000, 2500],
'learning_rate' : [0.01, 0.02]
}
xgb = XGBRegressor(
 objective = 'reg:squarederror',
subsample = 0.8,
colsample_bytree = 0.8,
learning_rate = 0.01,
tree_method = 'gpu_hist')
 grid_search = GridSearchCV(xgb,
param_grid = params,
scoring = 'neg_root_mean_squared_error',
n_jobs = -1,
verbose = 10)
 grid_search.fit(train_data, y_train)
 print('
Best estimator:')
 print(grid_search.best_estimator_)
 print('
Best score:')
 print(grid_search.best_score_)
 print('
Best hyperparameters:')
 print(grid_search.best_params_ )
def get_augmentation(data):
if data=='test':
return A.Compose([
A.Resize(CFG.img_size, CFG.img_size),
A.Normalize(
 mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225],
max_pixel_value=255.0,
p=1.0),
ToTensorV2()
])
 test_aug = A.Compose([
A.Resize(CFG.img_size, CFG.img_size),
A.Transpose(p=0.5),
A.HorizontalFlip(p=0.5),
A.VerticalFlip(p=0.5),
A.HueSaturationValue(
 hue_shift_limit=0.2,
sat_shift_limit=0.2,
val_shift_limit=0.2,
p=0.5 
),
A.RandomBrightnessContrast(
 brightness_limit=(-0.1,0.1),
contrast_limit=(-0.1, 0.1),
p=0.5 
),
A.Normalize(
 mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225],
max_pixel_value=255.0,
p=1.0 
),
ToTensorV2()
], p=1.)<categorify>
test_data_backup = test_data.copy()
train_data.drop(columns = ["id"], inplace = True)
 test_data.drop(columns = ["id"], inplace = True )
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 = Image.open(file_path ).convert('RGB')
 image = np.array(image)
 if self.transform:
augmented = self.transform(image=image)
 image = augmented['image']
return image<choose_model_class>
clf = XGBRegressor(
 objective = 'reg:squarederror',
subsample = 0.8,
learning_rate = 0.02,
max_depth = 7,
n_estimators = 2000,
tree_method = 'gpu_hist')
 clf.fit(train_data, y_train)
 y_pred_xgb = clf.predict(test_data)
 print(y_pred_xgb )
<choose_model_class><EOS> 
solution = pd.DataFrame({"id":test_data_backup.id, "target":y_pred_xgb})
 solution.to_csv("solution.csv", index = False)
 print("saved successful!" )