Kaggle competition: Aerial Cactus Identification
<SOS> metric: AUC Kaggle data source: aerial-cactus-identification<groupby>
!pip install albumentations > /dev/null 2>&1
!pip install pretrainedmodels > /dev/null 2>&1
!pip install catalyst > /dev/null 2>&1
train.groupby('Neighborhood')['SalePrice'].agg(['count','min','max','mean'] ).sort_values(by='count' )<groupby>
%matplotlib inline
train_on_gpu = True
train.groupby('Condition1')['SalePrice'].agg(['count','min','max','mean'] ).sort_values(by='count' )<drop_column>
from catalyst.dl.utils import UtilsFactory
from catalyst.dl.experiments import SupervisedRunner
from catalyst.dl.callbacks import EarlyStoppingCallback, OneCycleLR, InferCallback
train.drop('Condition2',axis=1,inplace=True)
 test.drop('Condition2',axis=1,inplace=True )<count_values>
data_transforms = albumentations.Compose([
albumentations.HorizontalFlip() ,
albumentations.VerticalFlip() ,
albumentations.RandomBrightness() ,
albumentations.Normalize(
 mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225], 
),
ToTensor()
])
 data_transforms_test = albumentations.Compose([
albumentations.Normalize(
 mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225], 
),
ToTensor()
] )
test['BldgType'].value_counts(dropna=False )<count_values>
train_df = pd.read_csv('.. /input/train.csv')
 train, valid = train_test_split(train_df.has_cactus, stratify=train_df.has_cactus, test_size=0.1)
 img_class_dict = {k:v for k, v in zip(train_df.id, train_df.has_cactus)}
train['HouseStyle'].value_counts(dropna=False )<count_values>
class CactusDataset(Dataset):
def __init__(self, datafolder, datatype='train', transform = transforms.Compose([transforms.CenterCrop(32),transforms.ToTensor() ]), labels_dict={}):
self.datafolder = datafolder
self.datatype = datatype
self.image_files_list = [s for s in os.listdir(datafolder)]
self.transform = transform
self.labels_dict = labels_dict
if self.datatype == 'train':
self.labels = [np.float32(labels_dict[i])for i in self.image_files_list]
else:
self.labels = [np.float32(0.0)for _ in range(len(self.image_files_list)) ]
def __len__(self):
return len(self.image_files_list)
 def __getitem__(self, idx):
img_name = os.path.join(self.datafolder, self.image_files_list[idx])
 img = cv2.imread(img_name)[:,:,::-1]
image = self.transform(image=img)
 image = image['image']
label = self.labels[idx]
return image, label
test['HouseStyle'].value_counts(dropna=False )<groupby>
dataset = CactusDataset(datafolder='.. /input/train/train', datatype='train', transform=data_transforms, labels_dict=img_class_dict)
 test_set = CactusDataset(datafolder='.. /input/test/test', datatype='test', transform=data_transforms_test )
train.groupby('OverallQual')['SalePrice'].agg(['count','min','max','mean'] ).sort_values(by='count' )<data_type_conversions>
loaders = collections.OrderedDict()
train_sampler = SubsetRandomSampler(list(train.index))
valid_sampler = SubsetRandomSampler(list(valid.index))
batch_size = 512
num_workers = 0
train_loader = torch.utils.data.DataLoader(dataset, batch_size=batch_size, sampler=train_sampler, num_workers=num_workers)
 valid_loader = torch.utils.data.DataLoader(dataset, batch_size=batch_size, sampler=valid_sampler, num_workers=num_workers)
 test_loader = torch.utils.data.DataLoader(test_set, batch_size=batch_size, num_workers=num_workers)
 loaders["train"] = train_loader
loaders["valid"] = valid_loader
loaders["test"] = test_loader
<groupby>
class Flatten(nn.Module):
def forward(self, input):
return input.view(input.size(0), -1)
 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 torch.squeeze(logits )
train.groupby('OverallCond')['SalePrice'].agg(['count','min','max','mean'] ).sort_values(by='count' )<sort_values>
num_epochs = 10
logdir = "./logs/simple"
model = Net(num_classes=1)
 criterion = nn.BCEWithLogitsLoss()
optimizer = torch.optim.SGD(model.parameters() , momentum=0.99, lr=1e-2)
 scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, factor=0.1, patience=2 )
train['YearBuilt'].value_counts(dropna=False ).sort_values(ascending=False ).head()<sort_values>
runner = SupervisedRunner()
runner.train(
 model=model,
criterion=criterion,
optimizer=optimizer,
scheduler=scheduler,
loaders=loaders,
logdir=logdir,
callbacks=[
OneCycleLR(
 cycle_len=num_epochs,
div_factor=3,
increase_fraction=0.3,
momentum_range=(0.95, 0.85))
],
num_epochs=num_epochs,
verbose=False
)
train['YearRemodAdd'].value_counts(dropna=False ).sort_values(ascending=False ).head()<count_values>
model = Net(num_classes=1)
 criterion = nn.BCEWithLogitsLoss()
optimizer = torch.optim.SGD(model.parameters() , momentum=0.99, lr=1e-2)
 scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, factor=0.1, patience=2)
 num_epochs = 20
logdir1 = "./logs/simple1"
runner1 = SupervisedRunner()
runner1.train(
 model=model,
criterion=criterion,
optimizer=optimizer,
scheduler=scheduler,
loaders=loaders,
callbacks=[
EarlyStoppingCallback(patience=4, min_delta=0.01)
 ],
logdir=logdir1,
num_epochs=num_epochs,
verbose=False
)
train['RoofMatl'].value_counts(dropna=False )<count_values>
test_loader = collections.OrderedDict([("infer", loaders["test"])])
 runner.infer(
 model=model,
loaders=test_loader,
callbacks=[InferCallback() ],
)
test['RoofMatl'].value_counts(dropna=False )<count_values>
test_img = os.listdir('.. /input/test/test')
 test_df = pd.DataFrame(test_img, columns=['id'])
 test_preds = pd.DataFrame({'imgs': test_df.id.values, 'preds': runner.callbacks[0].predictions["logits"]})
 test_preds.columns = ['id', 'has_cactus']
test_preds.to_csv('sub.csv', index=False)
 test_preds.head()
<count_values><EOS> 
runner1.infer(
 model=model,
loaders=test_loader,
callbacks=[InferCallback() ], 
)
 test_preds['has_cactus'] = runner1.callbacks[0].predictions["logits"]
test_preds.to_csv('sub1.csv', index=False )