Kaggle competition: What's Cooking? (Kernels Only)
<SOS> metric: CategorizationAccuracy Kaggle data source: whats-cooking?-(kernels-only)<categorify>
Image(".. /input/ratatouille/ratatouille.jpg" )
class AdvancedHairAugmentation:

 def __init__(self, hairs: int = 10, hairs_folder: str = ""):
self.hairs = hairs
self.hairs_folder = hairs_folder
def __call__(self, img):

 n_hairs = random.randint(0, self.hairs)
 if not n_hairs:
return img
height, width, _ = img.shape
hair_images = [im for im in os.listdir(self.hairs_folder)if 'png' in im]
for _ in range(n_hairs):
hair = cv2.imread(os.path.join(self.hairs_folder, random.choice(hair_images)))
 hair = cv2.flip(hair, random.choice([-1, 0, 1]))
hair = cv2.rotate(hair, random.choice([0, 1, 2]))
h_height, h_width, _ = hair.shape
roi_ho = random.randint(0, img.shape[0] - hair.shape[0])
 roi_wo = random.randint(0, img.shape[1] - hair.shape[1])
 roi = img[roi_ho:roi_ho + h_height, roi_wo:roi_wo + h_width]
img2gray = cv2.cvtColor(hair, cv2.COLOR_BGR2GRAY)
 ret, mask = cv2.threshold(img2gray, 10, 255, cv2.THRESH_BINARY)
 mask_inv = cv2.bitwise_not(mask)
 img_bg = cv2.bitwise_and(roi, roi, mask=mask_inv)
 hair_fg = cv2.bitwise_and(hair, hair, mask=mask)
 dst = cv2.add(img_bg, hair_fg)
 img[roi_ho:roi_ho + h_height, roi_wo:roi_wo + h_width] = dst
return img
def __repr__(self):
return f'{self.__class__.__name__}(hairs={self.hairs}, hairs_folder="{self.hairs_folder}")'<create_dataframe>
%matplotlib inline
plt.rcParams["figure.figsize"] =(15,10 )
dataset = SIIMDataset(df=train_df, imgfolder=train_img_path, train=True, transforms=train_transforms, meta_features=meta_features)
 loader = DataLoader(dataset=dataset, batch_size=16, shuffle=True)
 data = iter(loader)
 images = data.next()
show_images(torchvision.utils.make_grid(images[0][0]))
del dataset, loader<choose_model_class>
data = pd.read_json(".. /input/whats-cooking-kernels-only/train.json")
 test = pd.read_json(".. /input/whats-cooking-kernels-only/test.json")
 data.head()
class SIIMNet(nn.Module):
def __init__(self, base1, base2, n_meta_features: int):
super(SIIMNet, self ).__init__()
self.base1 = base1
self.base1._fc = nn.Linear(in_features=1536, out_features=500, bias=True)
 self.base2 = base2
self.base2._fc = nn.Linear(in_features=1280, out_features=500, bias=True)
 self.fc1 = nn.Linear(1000, 500)
 self.bn1 = nn.BatchNorm1d(500)
 self.meta_net = nn.Sequential(nn.Linear(n_meta_features, 500),
nn.BatchNorm1d(500),
nn.ReLU() ,
nn.Dropout(p=0.2),
nn.Linear(500, 250),
nn.BatchNorm1d(250),
nn.ReLU() ,
nn.Dropout(p=0.2))
self.out = nn.Linear(500+250, 1)
 def forward(self, inputs):
img, meta = inputs
cnn1_ = self.base1(img)
 cnn2_ = self.base2(img)
 cnn_ = torch.cat(( cnn1_, cnn2_), 1)
 cnn_ = self.fc1(cnn_)
 cnn_ = self.bn1(cnn_)
 meta_ = self.meta_net(meta)
 features = torch.cat(( cnn_, meta_), dim=1)
 output = self.out(features)
 return output<choose_model_class>
data.isnull().values.any()
data.dropna(axis=0, how='any',inplace = True)
 data.isnull().values.any()
data.isnull().sum()
epochs = 10
model_path = 'model.pth'
es_patience = 3
TTA = 3
skf = StratifiedKFold(n_splits=5 )<train_model>
data.ingredients =data.ingredients.str.join(' ')
 test.ingredients =test.ingredients.str.join(' ' )
def train_fn(model, train_loader, opt, criterion):
batch = 1
epoch_loss = 0
correct = 0
for x, y in train_loader:
if(batch % 40 == 0):
print("=", end="")
 x[0] = x[0].to(device)
 x[1] = x[1].to(device)
 y = y.to(device)
 opt.zero_grad()
z = model(x)
 loss = criterion(z, y.unsqueeze(1))
loss.backward()
opt.step()
pred = torch.round(torch.sigmoid(z))
correct +=(pred.cpu() == y.cpu().unsqueeze(1)).sum().item()
epoch_loss += loss.item()
batch += 1
return correct, epoch_loss<categorify>
vect = HashingVectorizer()
features = vect.fit_transform(data.ingredients)
 testfeatures = vect.transform(test.ingredients )
def validate_fn(model, val_loader, val_idx):
batch = 1
val_preds = torch.zeros(( len(val_idx), 1), device=device, dtype=torch.float32)
 with torch.no_grad() :
for j,(x_val, y_val)in enumerate(val_loader):
if(batch % 20 == 0):
print("=", end="")
 x_val[0] = x_val[0].to(device)
 x_val[1] = x_val[1].to(device)
 y_val = y_val.to(device)
 z_val = model(x_val)
 val_pred = torch.sigmoid(z_val)
 val_preds[j*val_loader.batch_size : j*val_loader.batch_size + x_val[0].shape[0]] = val_pred
batch += 1
val_acc = accuracy_score(train_df.iloc[val_idx]['target'].values, torch.round(val_preds.cpu()))
val_roc = roc_auc_score(train_df.iloc[val_idx]['target'].values, val_preds.cpu())
 return val_acc, val_roc<prepare_x_and_y>
labels = data.cuisine
def engine() :
oof = np.zeros(( len(train_df), 1))
preds = torch.zeros(( len(test_df), 1), dtype=torch.float32, device=device)
 test_ds = SIIMDataset(
 df = test_df,
imgfolder = test_img_path,
train = False,
transforms = train_transforms,
meta_features = meta_features 
)
 for fold,(train_idx, val_idx)in enumerate(skf.split(X=np.zeros(len(train_df)) , y=train_df['target'], groups=train_df['patient_id'].tolist()), start=1):
print("
")
 print("="*15, "FOLD ", fold, "="*15)
 best_val = None
patience = es_patience
base1 = EfficientNet.from_pretrained('efficientnet-b3')
 base2 = EfficientNet.from_pretrained('efficientnet-b1')
 model = SIIMNet(base1=base1, base2=base2, n_meta_features=len(meta_features))
model.to(device)
 opt = torch.optim.Adam(model.parameters() , lr=0.001)
 scheduler = ReduceLROnPlateau(optimizer=opt, mode='max', patience=1, factor=0.2, verbose=True)
 criterion = nn.BCEWithLogitsLoss()
train_ds = SIIMDataset(
 df = train_df.iloc[train_idx].reset_index(drop=True),
imgfolder = train_img_path,
train = True,
transforms = train_transforms,
meta_features = meta_features 
)
 sampler = get_sampler(train_df, train_idx)
 train_loader = DataLoader(dataset=train_ds, batch_size=32, sampler=sampler, num_workers=2)
 val_ds = SIIMDataset(
 df = train_df.iloc[val_idx].reset_index(drop=True),
imgfolder = train_img_path,
train = True,
transforms = test_transforms,
meta_features = meta_features 
)
 val_loader = DataLoader(dataset=val_ds, batch_size=16, shuffle=False, num_workers=2)
 test_loader = DataLoader(dataset=test_ds, batch_size=16, shuffle=False, num_workers=2)
 for epoch in range(epochs):
start_time = time.time()
print("Training:")
 model.train()
correct, epoch_loss = train_fn(model, train_loader, opt, criterion)
 train_acc = correct / len(train_idx)
 print("
Validating:")
 model.eval()
val_acc, val_roc = validate_fn(model, val_loader, val_idx)
 scheduler.step(val_roc)
 print('
Epoch {:03}: | Loss: {:.3f} | Train acc: {:.3f} | Val acc: {:.3f} | Val roc_auc: {:.3f} | Training time: {}'.format(
 epoch + 1,
epoch_loss,
train_acc,
val_acc,
val_roc,
str(datetime.timedelta(seconds=time.time() - start_time)) [:7]))
if not best_val:
best_val = val_roc
torch.save(model, model_path)
 continue
if val_roc >= best_val:
best_val = val_roc
patience = es_patience
torch.save(model, model_path)
 else:
patience -= 1
if patience == 0:
print('Early stopping.Best Val roc_auc: {:.3f}'.format(best_val))
break
model = torch.load(model_path)
 val_preds = torch.zeros(( len(val_idx), 1), device=device, dtype=torch.float32)
 batch = 1
print("Validating model for FOLD {}:".format(fold))
model.eval()
with torch.no_grad() :
for j,(x_val, y_val)in enumerate(val_loader):
if(batch % 20 == 0):
print("=", end="")
 x_val[0] = x_val[0].to(device)
 x_val[1] = x_val[1].to(device)
 y_val = torch.tensor(y_val, device=device, dtype=torch.float32)
 z_val = model(x_val)
 val_pred = torch.sigmoid(z_val)
 val_preds[j*val_loader.batch_size : j*val_loader.batch_size + x_val[0].shape[0]] = val_pred
batch += 1
oof[val_idx] = val_preds.cpu().numpy()
batch = 1
print("
Testing:")
 for _ in range(TTA):
for i, x_test in enumerate(test_loader):
if(batch % 60 == 0):
print("=", end="")
 x_test[0] = x_test[0].to(device)
 x_test[1] = x_test[1].to(device)
 z_test = model(x_test)
 z_test = torch.sigmoid(z_test)
 preds[i*test_loader.batch_size : i*test_loader.batch_size + x_test[0].shape[0]] += z_test
batch += 1
preds /= TTA
del train_ds, val_ds, train_loader, val_loader, x_val, y_val
gc.collect()
preds /= skf.n_splits
return preds, oof<find_best_params>
X_train, X_test, y_train, y_test = train_test_split(features, labels, test_size=0.2 )
preds, oof = engine()<compute_test_metric>
start = time.time()
log_reg = LogisticRegression(C=12)
 log_reg.fit(X_train,y_train)
 print("Accuracy: ",log_reg.score(X_test, y_test))
print("Time: " , time.time() - start )
print('OOF: {:.3f}'.format(roc_auc_score(train_df['target'], oof)) )<save_to_csv>
start = time.time()
linear_svm = LinearSVC(random_state=0, max_iter = 1500)
 linear_svm.fit(X_train, y_train)
 print("Accuracy: ",linear_svm.score(X_test, y_test))
print("Time: " , time.time() - start )
pd.Series(oof.reshape(-1,)).to_csv('oof.csv', index=False )<save_to_csv>
start = time.time()
rbf_svm = SVC(kernel='rbf', gamma=0.8, C=12)
 rbf_svm.fit(X_train, y_train)
 print("Accuracy: ",rbf_svm.score(X_test, y_test))
print("Time: " , time.time() - start )
<define_variables><EOS> 
prediction = rbf_svm.predict(testfeatures)
 sub = pd.DataFrame({'id':test.id,'cuisine':prediction})
 output = sub[['id','cuisine']]
output.to_csv("sample_submission.csv",index = False )