Kaggle competition: Aerial Cactus Identification
<SOS> metric: AUC Kaggle data source: aerial-cactus-identification<feature_engineering>
plt.ion()
combine["Ticket_new"] = combine["Ticket"].str.extract('([0-9]+)$',expand = False )<filter>
df = pd.read_csv('.. /input/train.csv')
 print(len(df))
df.head()
combine[combine.Ticket_new.isna() ][["Ticket","Ticket_new"]]<feature_engineering>
train_dir = '.. /input/train/train/'
test_dir = '.. /input/test/test/'
combine.loc[combine.Ticket_new.isna() ,"Ticket_new"]="LINE"<feature_engineering>
class ImageData(Dataset):
def __init__(self, df, data_dir, transform):
super().__init__()
self.df = df
self.data_dir = data_dir
self.transform = transform
def __len__(self):
return len(self.df)
 def __getitem__(self, index):
img_name = self.df.id[index]
label = self.df.has_cactus[index]
img_path = os.path.join(self.data_dir, img_name)
 image = mpimg.imread(img_path)
 image = self.transform(image)
 return image, label
for i,tic in enumerate(combine.Ticket_new.values):
combine.loc[i,"Ticket"] = tic
combine.drop(["Ticket_new"],axis = 1,inplace = True )<count_values>
epochs = 15
batch_size = 20
device = torch.device('cuda:0' )
combine["Ticket"].value_counts() [:5]<feature_engineering>
data_transf = transforms.Compose([transforms.ToPILImage() , transforms.ToTensor() ])
 train_data = ImageData(df = df, data_dir = train_dir, transform = data_transf)
 train_loader = DataLoader(dataset = train_data, batch_size = batch_size )
tic_list = combine.Ticket.values
for i in tic_list:
combine.loc[combine["Ticket"]==i,"Ticket_Counts"] = combine[combine["Ticket"] == i].shape[0]
combine[["Ticket","Ticket_Counts"]].head(2 )<feature_engineering>
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu" )
for i,ti in enumerate(combine.Ticket.values):
t = list(ti)[:-2]+list("xx")
 d = ''
combine.loc[i,"Ticket2"] = d.join(t )<feature_engineering>
model = models.resnet50(pretrained=True)
 model.cuda()
optimizer = optim.Adam(model.parameters() , lr=0.001)
 loss_func = nn.CrossEntropyLoss()
tic2_list = combine.Ticket2.values
for i in tic2_list:
combine.loc[combine["Ticket2"]==i,"Ticket2_Counts"] = combine[combine["Ticket2"] == i].shape[0]
combine[["Ticket","Ticket2","Ticket2_Counts"]].head(2 )<filter>
%%time
for epoch in tqdm(range(epochs)) :
for i,(images, labels)in enumerate(train_loader):
images = images.to(device)
 labels = labels.to(device)
 outputs = model(images)
 loss = loss_func(outputs, labels)
 optimizer.zero_grad()
loss.backward()
optimizer.step()
if(i+1)% 500 == 0:
print('Epoch [{}/{}], Loss: {:.4f}'.format(epoch+1, epochs, loss.item()))
have_cabin = combine[combine.Cabin.notna() ]
have_cabin.reset_index(inplace = True)
 multi_cabin = []
single_cabin = []
for i,j in enumerate(have_cabin.Cabin.values):
passid = have_cabin.loc[i].PassengerId
if len(j)>4:
multi_cabin = multi_cabin+[passid]
else:
single_cabin = single_cabin+[passid]
print(multi_cabin )<feature_engineering>
submit = pd.read_csv('.. /input/sample_submission.csv')
 test_data = ImageData(df = submit, data_dir = test_dir, transform = data_transf)
 test_loader = DataLoader(dataset = test_data, shuffle=False )
cabin_title = have_cabin[have_cabin.PassengerId.isin(single_cabin)].Cabin.str.extract('^([A-Z])',expand=False)
 have_cabin.loc[have_cabin.PassengerId.isin(single_cabin),"Cabin_Title"] = cabin_title
have_cabin[["Cabin","Cabin_Title"]].head(2 )<feature_engineering>
predict = []
for batch_i,(data, target)in enumerate(test_loader):
data, target = data.to(device), target.to(device)
 output = model(data)
 _, pred = torch.max(output.data, 1)
 predict.append(pred )
<feature_engineering><EOS> 
submit['has_cactus'] = predict
submit.to_csv('submission.csv', index=False)
 submit.head()