Kaggle competition: Histopathologic Cancer Detection
<SOS> metric: AUC Kaggle data source: histopathologic-cancer-detection<train_model>
base_dir = '.. /input/'
print(os.listdir(base_dir))
plt.style.use("ggplot")
gm = GaussianMixture(n_components=4)
 gm.fit(X_temp )<predict_on_test>
full_train_df = pd.read_csv(".. /input/train_labels.csv")
 full_train_df.head()
X_temp['Gaussian_Mixture']=gm.predict(X_temp )<predict_on_test>
SAMPLE_SIZE = 80000
train_path = '.. /input/train/'
test_path = '.. /input/test/'
df_negatives = full_train_df[full_train_df['label'] == 0].sample(SAMPLE_SIZE, random_state=42)
 df_positives = full_train_df[full_train_df['label'] == 1].sample(SAMPLE_SIZE, random_state=42)
 train_df = sklearn.utils.shuffle(pd.concat([df_positives, df_negatives], axis=0 ).reset_index(drop=True))
train_df.shape
test['Gaussian_Mixture']=gm.predict(test )<categorify>
class CreateDataset(Dataset):
def __init__(self, df_data, data_dir = './', transform=None):
super().__init__()
self.df = df_data.values
self.data_dir = data_dir
self.transform = transform
def __len__(self):
return len(self.df)
 def __getitem__(self, index):
img_name,label = self.df[index]
img_path = os.path.join(self.data_dir, img_name+'.tif')
 image = cv2.imread(img_path)
 if self.transform is not None:
image = self.transform(image)
 return image, label
X_oh_temp=pd.get_dummies(X_temp.drop('id', axis=1), columns=X_temp.drop('id', axis=1 ).columns, drop_first=True, sparse=True )<compute_test_metric>
transforms_train = transforms.Compose([
transforms.ToPILImage() ,
transforms.RandomHorizontalFlip(p=0.4),
transforms.RandomVerticalFlip(p=0.4),
transforms.RandomRotation(20),
transforms.ToTensor() ,
transforms.Normalize(( 0.5, 0.5, 0.5),(0.5, 0.5, 0.5))
])
 train_data = CreateDataset(df_data=train_df, data_dir=train_path, transform=transforms_train )
X_oh=scipy.sparse.csr_matrix(X_oh_temp.values )<categorify>
batch_size = 128
valid_size = 0.1
num_train = len(train_data)
 indices = list(range(num_train))
split = int(np.floor(valid_size * num_train))
train_idx, valid_idx = indices[split:], indices[:split]
train_sampler = SubsetRandomSampler(train_idx)
 valid_sampler = SubsetRandomSampler(valid_idx)
 train_loader = DataLoader(train_data, batch_size=batch_size, sampler=train_sampler)
 valid_loader = DataLoader(train_data, batch_size=batch_size, sampler=valid_sampler )
test_oh_temp=pd.get_dummies(test.drop('id', axis=1), columns=test.drop('id', axis=1 ).columns, drop_first=True, sparse=True )<compute_test_metric>
transforms_test = transforms.Compose([
transforms.ToPILImage() ,
transforms.ToTensor() ,
transforms.Normalize(( 0.5, 0.5, 0.5),(0.5, 0.5, 0.5))
])
 sample_sub = pd.read_csv(".. /input/sample_submission.csv")
 test_data = CreateDataset(df_data=sample_sub, data_dir=test_path, transform=transforms_test)
 test_loader = DataLoader(test_data, batch_size=batch_size, shuffle=False )
test_oh=scipy.sparse.csr_matrix(test_oh_temp.values )<split>
class CNN(nn.Module):
def __init__(self):
super(CNN,self ).__init__()
self.conv1=nn.Sequential(
 nn.Conv2d(in_channels=3,out_channels=32,kernel_size=3,stride=1,padding=0),
nn.BatchNorm2d(32),
nn.ReLU(inplace=True),
nn.MaxPool2d(2,2))
self.conv2=nn.Sequential(
 nn.Conv2d(in_channels=32,out_channels=64,kernel_size=2,stride=1,padding=1),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.MaxPool2d(2,2))
self.conv3=nn.Sequential(
 nn.Conv2d(in_channels=64,out_channels=128,kernel_size=3,stride=1,padding=1),
nn.BatchNorm2d(128),
nn.ReLU(inplace=True),
nn.MaxPool2d(2,2))
self.conv4=nn.Sequential(
 nn.Conv2d(in_channels=128,out_channels=256,kernel_size=3,stride=1,padding=1),
nn.BatchNorm2d(256),
nn.ReLU(inplace=True),
nn.MaxPool2d(2,2))
self.conv5=nn.Sequential(
 nn.Conv2d(in_channels=256, out_channels=512, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(512),
nn.ReLU(inplace=True),
nn.MaxPool2d(2,2))
self.dropout2d = nn.Dropout2d()
self.fc=nn.Sequential(
 nn.Linear(512*3*3,1024),
nn.ReLU(inplace=True),
nn.Dropout(0.4),
nn.Linear(1024,512),
nn.Dropout(0.4),
nn.Linear(512, 1),
nn.Sigmoid())
 def forward(self,x):

 x=self.conv1(x)
 x=self.conv2(x)
 x=self.conv3(x)
 x=self.conv4(x)
 x=self.conv5(x)
 x=x.view(x.shape[0],-1)
 x=self.fc(x)
 return x
X_train, X_valid, Y_train, Y_valid = train_test_split(X_oh,
Y,
test_size = 0.20,
random_state=42)
<train_model>
train_on_gpu = torch.cuda.is_available()
if not train_on_gpu:
print('CUDA is not available.Training on CPU...')
 else:
print('CUDA is available! Training on GPU...' )
lr=LogisticRegression(C=0.125, solver="lbfgs", max_iter=500)
 lr.fit(X_train, Y_train)
<predict_on_test>
model = CNN()
print(model)
 if train_on_gpu: model.cuda()
y_pred_lr=lr.predict_proba(X_valid )<compute_test_metric>
pytorch_total_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
 print("Number of trainable parameters:
{}".format(pytorch_total_params))
roc_auc_score(Y_valid.values, y_pred_lr[:,1] )<train_model>
criterion = nn.BCELoss()
optimizer = optim.Adam(model.parameters() , lr=0.00015 )
lr.fit(X_oh, Y )<compute_train_metric>
n_epochs = 20
valid_loss_min = np.Inf
train_losses = []
valid_losses = []
val_auc = []
test_accuracies = []
valid_accuracies = []
auc_epoch = []
for epoch in range(1, n_epochs+1):
train_loss = 0.0
valid_loss = 0.0
model.train()
for data, target in train_loader:
if train_on_gpu:
data, target = data.cuda() , target.cuda().float()
target = target.view(-1, 1)
 optimizer.zero_grad()
output = model(data)
 loss = criterion(output, target)
 loss.backward()
optimizer.step()
train_loss += loss.item() *data.size(0)
 model.eval()
for data, target in valid_loader:
if train_on_gpu:
data, target = data.cuda() , target.cuda().float()
target = target.view(-1, 1)
 output = model(data)
 loss = criterion(output, target)
 valid_loss += loss.item() *data.size(0)
 y_actual = target.data.cpu().numpy()
y_pred = output[:,-1].detach().cpu().numpy()
val_auc.append(roc_auc_score(y_actual, y_pred))
train_loss = train_loss/len(train_loader.sampler)
 valid_loss = valid_loss/len(valid_loader.sampler)
 valid_auc = np.mean(val_auc)
 auc_epoch.append(np.mean(val_auc))
train_losses.append(train_loss)
 valid_losses.append(valid_loss)
 print('Epoch: {} | Training Loss: {:.6f} | Validation Loss: {:.6f} | Validation AUC: {:.4f}'.format(
 epoch, train_loss, valid_loss, valid_auc))
if valid_loss <= valid_loss_min:
print('Validation loss decreased({:.6f} --> {:.6f} ).Saving model...'.format(
 valid_loss_min,
valid_loss))
torch.save(model.state_dict() , 'best_model.pt')
 valid_loss_min = valid_loss
y_pred=lr.predict_proba(X_oh)
 roc_auc_score(Y.values, y_pred[:,1] )<predict_on_test>
model.load_state_dict(torch.load('best_model.pt'))
test_pred_lr=lr.predict_proba(test_oh )<prepare_output>
model.eval()
preds = []
for batch_i,(data, target)in enumerate(test_loader):
data, target = data.cuda() , target.cuda()
output = model(data)
 pr = output.detach().cpu().numpy()
for i in pr:
preds.append(i)
 sample_sub['label'] = preds
output_dict = {'id': test.id,
'target': test_pred_lr[:,1]}
output = pd.DataFrame(output_dict, columns = ['id', 'target'])
 output.head(10 )<save_to_csv>
for i in range(len(sample_sub)) :
sample_sub.label[i] = np.float(sample_sub.label[i] )
<install_modules><EOS> 
sample_sub.to_csv('submission.csv', index=False)
 sample_sub.head()