Kaggle competition: Aerial Cactus Identification
<SOS> metric: AUC Kaggle data source: aerial-cactus-identification<sort_values>
%matplotlib inline
train_on_gpu = True
train['GarageYrBlt'].value_counts().sort_values(ascending=False ).head()<feature_engineering>
!pip install albumentations > /dev/null 2>&1
!pip install pretrainedmodels > /dev/null 2>&1
!pip install kekas > /dev/null 2>&1
!pip install adabound > /dev/null 2>&1
test.loc[1132,'GarageYrBlt'] = 2007<count_missing_values>
import albumentations
from albumentations import torch as AT
import pretrainedmodels
import adabound
from kekas import Keker, DataOwner, DataKek
from kekas.transformations import Transformer, to_torch, normalize
from kekas.metrics import accuracy
from kekas.modules import Flatten, AdaptiveConcatPool2d
from kekas.callbacks import Callback, Callbacks, DebuggerCallback
from kekas.utils import DotDict
train['GarageFinish'].isna().sum() ,test['GarageFinish'].isna().sum()<rename_columns>
labels = pd.read_csv('.. /input/train.csv')
 fig = plt.figure(figsize=(25, 8))
train_imgs = os.listdir(".. /input/train/train")
 for idx, img in enumerate(np.random.choice(train_imgs, 20)) :
ax = fig.add_subplot(4, 20//4, idx+1, xticks=[], yticks=[])
 im = Image.open(".. /input/train/train/" + img)
 plt.imshow(im)
 lab = labels.loc[labels['id'] == img, 'has_cactus'].values[0]
ax.set_title(f'Label: {lab}' )
train['GarageFinish'].fillna("No_Garage",inplace=True)
 test['GarageFinish'].fillna("No_Garage",inplace=True )<groupby>
test_img = os.listdir('.. /input/test/test')
 test_df = pd.DataFrame(test_img, columns=['id'])
 test_df['has_cactus'] = -1
test_df['data_type'] = 'test'
labels['has_cactus'] = labels['has_cactus'].astype(int)
 labels['data_type'] = 'train'
labels.head()
train.groupby('GarageFinish')['SalePrice'].agg(['count','min','max','mean'] ).sort_values(by='count' )<count_missing_values>
labels.loc[labels['data_type'] == 'train', 'has_cactus'].value_counts()
train['GarageCars'].isna().sum() ,test['GarageCars'].isna().sum()<correct_missing_values>
train, valid = train_test_split(labels, stratify=labels.has_cactus, test_size=0.2 )
test['GarageCars'].fillna(0,inplace=True )<count_missing_values>
def reader_fn(i, row):
image = cv2.imread(f".. /input/{row['data_type']}/{row['data_type']}/{row['id']}")[:,:,::-1]
label = torch.Tensor([row["has_cactus"]])
 return {"image": image, "label": label}
train['GarageArea'].isna().sum() ,test['GarageArea'].isna().sum()<correct_missing_values>
def augs(p=0.5):
return albumentations.Compose([
albumentations.HorizontalFlip() ,
albumentations.VerticalFlip() ,
albumentations.RandomBrightness() ,
], p=p )
test['GarageArea'].fillna(0,inplace=True )<count_missing_values>
def get_transforms(dataset_key, size, p):
PRE_TFMS = Transformer(dataset_key, lambda x: cv2.resize(x,(size, size)))
 AUGS = Transformer(dataset_key, lambda x: augs()(image=x)["image"])
 NRM_TFMS = transforms.Compose([
Transformer(dataset_key, to_torch()),
Transformer(dataset_key, normalize())
 ])
 train_tfms = transforms.Compose([PRE_TFMS, AUGS, NRM_TFMS])
 val_tfms = transforms.Compose([PRE_TFMS, NRM_TFMS])
 return train_tfms, val_tfms
train['GarageQual'].isna().sum() ,test['GarageQual'].isna().sum()<rename_columns>
train_tfms, val_tfms = get_transforms("image", 32, 0.5 )
train['GarageQual'].fillna("No_Garage",inplace=True)
 test['GarageQual'].fillna("No_Garage",inplace=True )<sort_values>
train_dk = DataKek(df=train, reader_fn=reader_fn, transforms=train_tfms)
 val_dk = DataKek(df=valid, reader_fn=reader_fn, transforms=val_tfms)
 batch_size = 64
workers = 0
train_dl = DataLoader(train_dk, batch_size=batch_size, num_workers=workers, shuffle=True, drop_last=True)
 val_dl = DataLoader(val_dk, batch_size=batch_size, num_workers=workers, shuffle=False )
train['GarageQual'].value_counts().sort_values(ascending=False )<sort_values>
test_dk = DataKek(df=test_df, reader_fn=reader_fn, transforms=val_tfms)
 test_dl = DataLoader(test_dk, batch_size=batch_size, num_workers=workers, shuffle=False )
test['GarageQual'].value_counts().sort_values(ascending=False )<groupby>
class Net(nn.Module):
def __init__(
 self,
num_classes: int,
p: float = 0.2,
pooling_size: int = 2,
last_conv_size: int = 1664,
arch: str = "densenet169",
pretrained: str = "imagenet")-> None:

 super().__init__()
net = pretrainedmodels.__dict__[arch](pretrained=pretrained)
 modules = list(net.children())[:-1]
modules += [nn.Sequential(
 Flatten() ,
nn.BatchNorm1d(1664),
nn.Dropout(p),
nn.Linear(1664, num_classes)
)]
self.net = nn.Sequential(*modules)
 def forward(self, x):
logits = self.net(x)
 return logits
train.groupby('GarageQual')['SalePrice'].agg(['count','min','max','mean'] ).sort_values(by='count' )<count_missing_values>
dataowner = DataOwner(train_dl, val_dl, None)
 model = Net(num_classes=1)
 criterion = nn.BCEWithLogitsLoss()
train['GarageCond'].isna().sum() ,test['GarageCond'].isna().sum()<rename_columns>
def step_fn(model: torch.nn.Module,
batch: torch.Tensor)-> torch.Tensor:

 inp = batch["image"]
return model(inp )
train['GarageCond'].fillna("No_Garage",inplace=True)
 test['GarageCond'].fillna("No_Garage",inplace=True )<count_missing_values>
def bce_accuracy(target: torch.Tensor,
preds: torch.Tensor,
thresh: bool = 0.5)-> float:
target = target.cpu().detach().numpy()
preds =(torch.sigmoid(preds ).cpu().detach().numpy() > thresh ).astype(int)
 return accuracy_score(target, preds)
 def roc_auc(target: torch.Tensor,
preds: torch.Tensor)-> float:
target = target.cpu().detach().numpy()
preds = torch.sigmoid(preds ).cpu().detach().numpy()
return roc_auc_score(target, preds )
train['PavedDrive'].isna().sum() ,test['PavedDrive'].isna().sum()<count_values>
keker = Keker(model=model,
dataowner=dataowner,
criterion=criterion,
step_fn=step_fn,
target_key="label",
metrics={"acc": bce_accuracy, 'auc': roc_auc},
opt=torch.optim.SGD,
opt_params={"momentum": 0.99} )
train['PavedDrive'].value_counts()<count_values>
keker.unfreeze(model_attr="net")
 layer_num = -1
keker.freeze_to(layer_num, model_attr="net" )
test['PavedDrive'].value_counts()<count_missing_values>
keker.kek_one_cycle(max_lr=1e-2,
cycle_len=5,
momentum_range=(0.95, 0.85),
div_factor=25,
increase_fraction=0.3,
logdir='train_logs')
 keker.plot_kek('train_logs' )
train['WoodDeckSF'].isna().sum() ,test['WoodDeckSF'].isna().sum()<count_missing_values>
keker.kek_one_cycle(max_lr=1e-3,
cycle_len=5,
momentum_range=(0.95, 0.85),
div_factor=25,
increase_fraction=0.2,
logdir='train_logs1')
 keker.plot_kek('train_logs1' )
train['OpenPorchSF'].isna().sum() ,test['OpenPorchSF'].isna().sum()<count_missing_values>
preds = keker.predict_loader(loader=test_dl )
train['EnclosedPorch'].isna().sum() ,test['EnclosedPorch'].isna().sum()<count_missing_values>
<count_missing_values><EOS> 
test_preds = pd.DataFrame({'imgs': test_df.id.values, 'preds': preds.reshape(-1,)})
 test_preds.columns = ['id', 'has_cactus']
test_preds.to_csv('sub.csv', index=False)
 test_preds.head()