Kaggle competition: Leaf Classification
<SOS> metric: MulticlassLoss Kaggle data source: leaf-classification<load_pretrained>
%pylab inline
def image_read(path):
g = tf.io.read_file(path)
 im = tf.io.decode_image(g)
 return im<import_modules>
from sklearn.preprocessing import StandardScaler
from sklearn.cross_validation import train_test_split
from sklearn.preprocessing import LabelEncoder
from tqdm import tqdm_notebook<define_variables>
from keras.models import Sequential
from keras.layers import Dense,Dropout,Activation
from keras.utils.np_utils import to_categorical
from keras.callbacks import EarlyStopping
test_images = []
for path in tqdm_notebook(eval_paths):
test_images.append(image_read(path)) 
<define_variables>
data = pd.read_csv('.. /input/train.csv')
 parent_data = data.copy()
ID = data.pop('id' )
test_ds = tf.data.Dataset.from_tensor_slices(eval_paths)
 test_ds = test_ds.map(image_read)
 test_ds = test_ds.batch(batch_size )<predict_on_test>
y = data.pop('species')
 y = LabelEncoder().fit(y ).transform(y)
 print(y.shape )
pred = model.predict(test_ds )<save_to_csv>
X = StandardScaler().fit(data ).transform(data)
 print(X.shape )
submit_df = pd.read_csv('.. /input/aerial-cactus-identification/sample_submission.csv')
 test_fnames = submit_df['id']
test_labels = pred
submit_file = pd.DataFrame({'id':test_fnames, 'has_cactus':test_labels}, columns = ['id', 'has_cactus'])
 submit_file
submit_file.to_csv('submission.csv', index=False )<load_pretrained>
y_cat = to_categorical(y)
 print(y_cat.shape )
data_from = "kaggle"
if data_from == "colab":
files.upload()
!pip install -q kaggle
!mkdir -p ~/.kaggle
!cp kaggle.json ~/.kaggle/
!chmod 600 ~/.kaggle/kaggle.json
!mkdir /content/cactus/
!mkdir /content/cactus/data/
!kaggle competitions download -c aerial-cactus-identification
!mv test.zip train.zip train.csv sample_submission.csv /content/cactus/data/
def unzip(path):
with zipfile.ZipFile(path,"r")as z:
z.extractall('.')
 train_zip = "/content/cactus/data/train"
unzip(train_zip+".zip")
 test_zip = "/content/cactus/data/test"
unzip(test_zip+".zip")
 train_dir = "/content/train"
test_dir = "/content/test"
train_labels_path = "/content/cactus/data/train.csv"
data_folder_path = "/content"
csv_path = "/content/cactus/data/sample_submission.csv"
elif data_from == "local":
data_folder_path = "aerial-cactus-identification"
train_dir = "aerial-cactus-identification/train"
test_dir = "aerial-cactus-identification/test"
train_labels_path = "aerial-cactus-identification/train.csv"
csv_path = "aerial-cactus-identification/sample_submission.csv"
elif data_from == "kaggle":
def unzip(path):
with zipfile.ZipFile(path,"r")as z:
z.extractall('.')
 train_zip = ".. /input/aerial-cactus-identification/train"
unzip(train_zip+".zip")
 test_zip = ".. /input/aerial-cactus-identification/test"
unzip(test_zip+".zip")
 train_dir = "/kaggle/working/train"
test_dir = "/kaggle/working/test"
train_labels_path = ".. /input/aerial-cactus-identification/train.csv"
data_folder_path = "/kaggle/working"
csv_path = ".. /input/aerial-cactus-identification/sample_submission.csv" 
<set_options>
2
model = Sequential()
model.add(Dense(600,input_dim=192, init='uniform', activation='relu'))
model.add(Dropout(0.3))
model.add(Dense(300, activation='sigmoid'))
model.add(Dropout(0.3))
model.add(Dense(99, activation='softmax'))
%matplotlib inline<set_options>
model.compile(loss='categorical_crossentropy',optimizer='rmsprop', metrics = ["accuracy"] )
torch.manual_seed(100)
 np.random.seed(100 )<load_from_csv>
early_stopping = EarlyStopping(monitor='val_loss', patience=280)
 history = model.fit(X,y_cat,batch_size=192,
nb_epoch=800 ,verbose=0, validation_split=0.1, callbacks=[early_stopping] )
class CactusDataset2(Dataset):
def __init__(self, labels_csv_path, root_dir, transform=None):

 df = pd.read_csv(labels_csv_path)
 self.labels = df['has_cactus']
self.images = []
print("Reading images to memory")
 for _, img_name in tqdm(df['id'].items()):
img_path = os.path.join(root_dir, img_name)
 image = plt.imread(img_path)
 self.images.append(image)
 if transform is None:
self.transform = transforms.Compose([transforms.ToTensor() ,
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])])
 else:
self.transform = transform
def __len__(self):
return len(self.labels)
 def __getitem__(self, idx):
if torch.is_tensor(idx):
idx = idx.item()
label = self.labels.iloc[idx].astype(np.float32 ).reshape(-1)
 label = torch.tensor(label ).long()
image = self.images[idx]
image = self.transform(image)
 return image, label<normalization>
print('val_acc: ',max(history.history['val_acc']))
print('val_loss: ',min(history.history['val_loss']))
print('train_acc: ',max(history.history['acc']))
print('train_loss: ',min(history.history['loss']))
print()
print("train/val loss ratio: ", min(history.history['loss'])/min(history.history['val_loss']))
resize = 128
transform = transforms.Compose([transforms.ToPILImage() , transforms.Resize(resize),
transforms.RandomHorizontalFlip() , transforms.RandomVerticalFlip() ,
transforms.ColorJitter(brightness=0.25, contrast=0.25, saturation=0.25),
transforms.RandomAffine(degrees=10, translate=(0.05, 0.05), scale=(1, 1.1)) ,
transforms.ToTensor() ,
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])])
 test_transform = transforms.Compose([transforms.ToPILImage() , transforms.Resize(resize), transforms.ToTensor() ,
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])])
 dataset_train = CactusDataset2(train_labels_path, train_dir, transform=transform)
 dataset_val = CactusDataset2(train_labels_path, train_dir, transform=test_transform)
 testset = CactusDataset2(csv_path, test_dir, transform=test_transform)
 batch_size = 64
num_train = len(dataset_train)
 valid_percent = 0.01
valid_size = round(valid_percent * num_train)
 train_size = num_train - valid_size
indices = list(range(num_train))
np.random.shuffle(indices)
 train_idx, valid_idx = indices[:train_size], indices[train_size:]
train_sampler = SubsetRandomSampler(train_idx)
 valid_sampler = SubsetRandomSampler(valid_idx)
 train_loader = DataLoader(dataset_train, batch_size=batch_size, sampler=train_sampler, num_workers=4)
 valid_loader = DataLoader(dataset_val, batch_size=batch_size, sampler=valid_sampler, num_workers=4)
 test_loader = DataLoader(testset, batch_size=batch_size, shuffle=False )<choose_model_class>
test = pd.read_csv('.. /input/test.csv')
 index = test.pop('id')
 test = StandardScaler().fit(test ).transform(test)
 yPred = model.predict_proba(test )
class AdaptiveConcatPool2d(nn.Module):
"Layer that concats `AdaptiveAvgPool2d` and `AdaptiveMaxPool2d`."
def __init__(self, sz=None):
super().__init__()
"Output will be 2*sz or 2 if sz is None"
self.output_size = sz or 1
self.ap = nn.AdaptiveAvgPool2d(self.output_size)
 self.mp = nn.AdaptiveMaxPool2d(self.output_size)
 def forward(self, x): return torch.cat([self.mp(x), self.ap(x)], 1)
 class TransferHead(nn.Module):
def __init__(self, in_features):

 super().__init__()
self.avg = AdaptiveConcatPool2d()
self.in_features = in_features
self.layer1 = nn.Sequential(nn.BatchNorm1d(2*in_features), nn.Dropout(0.25),
nn.Linear(2*in_features, 512), nn.ReLU(inplace=True))
self.layer2 = nn.Sequential(nn.BatchNorm1d(512), nn.Dropout(0.5),
nn.Linear(512, 2))
def forward(self, x):
out = self.avg(x)
 out = out.view(-1, self.in_features*2)
 out = self.layer1(out)
 out = self.layer2(out)
 return out
class TransCactusTron(nn.Module):
def __init__(self, freeze=True):
super().__init__()
base_arch = models.densenet161(pretrained=True)
 n_channels = base_arch.classifier.in_features
self.body = nn.Sequential(base_arch.features, nn.ReLU(inplace=True))
self.head = TransferHead(n_channels)
 for x in self.body.parameters() :
x.requires_grad = False
for x in self.body.modules() :
if isinstance(x, nn.modules.batchnorm._BatchNorm):
x.bias.requires_grad = True
x.weight.requires_grad = True
x.reset_running_stats()
def forward(self, x):
out = self.body(x)
 return self.head(out)
 def predict(self, loader, device=torch.device('cpu')) :
y_pred = torch.tensor([])
 with torch.no_grad() :
for data in loader:
img = data[0].to(device)
 curr_pred = self(img)
 y_pred = torch.cat(( y_pred, curr_pred.cpu().detach()))
softmax = nn.Softmax(dim=1)
 y_pred = softmax(y_pred)
 return y_pred[:, 1].numpy()
def unfreeze(self):
for x in self.body.parameters() :
x.requires_grad = True 
<train_on_grid>
yPred = pd.DataFrame(yPred,index=index,columns=sort(parent_data.species.unique()))
find_lr = True
if find_lr:
num_steps = len(train_loader)
 lrs = np.logspace(-8, 0, num=num_steps)
 device = torch.device("cuda:0" if torch.cuda.is_available else "cpu")
 criterion = nn.CrossEntropyLoss(reduction='mean')
 model = TransCactusTron()
model.train()
model.to(device)
 optimizer = torch.optim.Adam(model.parameters() ,lr=lrs[0])
 train_loss = []
best_loss = np.inf
i = 0
model.train()
for img, label in tqdm(train_loader):
img, label = img.to(device), label.to(device ).squeeze()
optimizer.zero_grad()
predicted_label = model(img)
 loss = criterion(predicted_label, label)
 loss.backward()
optimizer.step()
train_loss.append(loss.item())
 if loss.item() < best_loss:
best_loss = loss.item()
if loss.item() > 20*best_loss:
print("Loss diverged")
 break
optimizer.param_groups[0]['lr'] = lrs[i]
i += 1
train_loss = pd.Series(train_loss)
 train_loss_smooth = train_loss.ewm(alpha=0.02 ).mean()
plt.plot(lrs[:len(train_loss)], train_loss_smooth)
 plt.grid()
plt.xscale('log' )<choose_model_class>
fp = open('submission_nn_kernel.csv','w')
 fp.write(yPred.to_csv() )