Kaggle competition: Aerial Cactus Identification
SVC_acc = accuracy_score(predictions_svc, Y_test)* 100
print("SVC accuracy: {}".format(SVC_acc))<train_model>
data_dir = '.. /input'
train_dir = data_dir + '/train/train/'
test_dir = data_dir + '/test/test/'
n_model = KNeighborsClassifier(n_neighbors = 4)
 n_model.fit(X_train, Y_train )<predict_on_test>
labels = pd.read_csv(".. /input/train.csv")
 labels.head()
predictions_knn = n_model.predict(X_test )<compute_test_metric>
balance = labels['has_cactus'].value_counts()
print(balance )
accuracy_score(predictions_knn, Y_test)* 100<find_best_params>
train, valid = train_test_split(labels, stratify=labels.has_cactus, test_size=0.2 )
knn_data = {}
for i in range(1,30):
model = KNeighborsClassifier(n_neighbors=i)
 model.fit(X_train, Y_train)
 prediction_data = model.predict(X_test)
 knn_data[i] = accuracy_score(prediction_data, Y_test)* 100 
<compute_train_metric>
print('Number of training samples:{0}'.format(len(train)))
 print('Number of validation samples:{0}'.format(len(valid)))
 print('Number of testing samples:{0}'.format(len(os.listdir(test_dir))))
n_model = KNeighborsClassifier(n_neighbors = 14)
 n_model.fit(X_train, Y_train)
 predictions_knn = n_model.predict(X_test)
 knn_Ac = accuracy_score(predictions_knn, Y_test)* 100
print(knn_Ac )<train_model>
num_epochs = 25
num_classes = 2
batch_size = 128
learning_rate = 0.002
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu' )
knn_acc = knn_data[14]
print("KNN accuracy: {}".format(knn_acc))<train_model>
class CactiImageData(Dataset):
def __init__(self, split_data, data_root = './', transform=None):
super().__init__()
self.df = split_data.values
self.data_root = data_root
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_root, img_name)
 image = mpimg.imread(img_path)
 if self.transform is not None:
image = self.transform(image)
 return image, label
model_naive = GaussianNB()
model_naive.fit(X_train, Y_train)
 prediction_naive = model_naive.predict(X_test )<compute_test_metric>
mean = [0.5, 0.5, 0.5]
std = [0.5, 0.5, 0.5]
train_transf = transforms.Compose([transforms.ToPILImage() ,
transforms.ToTensor() ])
 valid_transf = transforms.Compose([transforms.ToPILImage() ,
transforms.ToTensor() ] )
gaussian_acc = accuracy_score(prediction_naive, Y_test)* 100
print("Gaussian Acc: {}".format(gaussian_acc))<import_modules>
train_data = CactiImageData(split_data = train, data_root = train_dir, transform = train_transf)
 valid_data = CactiImageData(split_data = valid, data_root = train_dir, transform = valid_transf)
 train_loader = DataLoader(dataset = train_data, batch_size = batch_size, shuffle = True, num_workers = 0)
 valid_loader = DataLoader(dataset = valid_data, batch_size = batch_size//2, shuffle = False, num_workers = 0 )
from sklearn.tree import DecisionTreeClassifier<find_best_params>
class CactiCNN(nn.Module):
def __init__(self):
super(CactiCNN, 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.fc = nn.Sequential(
 nn.Linear(256 * 2 * 2, 1024),
nn.ReLU(inplace=True),
nn.Dropout(0.25),
nn.Linear(1024, 2))
def forward(self,x):
x = self.conv1(x)
 x = self.conv2(x)
 x = self.conv3(x)
 x = self.conv4(x)
 x = x.view(x.shape[0],-1)
 x = self.fc(x)
 return x
tree_data = {}
for i in range(2,40):
tree_model = DecisionTreeClassifier(criterion='gini',
min_samples_split=i,
max_features='auto',
min_samples_leaf=1)
 tree_model.fit(X_train, Y_train)
 tree_predict = tree_model.predict(X_test)
 tree_data[i]=accuracy_score(tree_predict, Y_test)
<sort_values>
model = CactiCNN().to(device)
 criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters() , lr=learning_rate )
tree_val = max(tree_data, key=tree_data.get)
 print(tree_val )<choose_model_class>
total_step = len(train_loader)
 for epoch in range(num_epochs):
for i,(images, labels)in enumerate(train_loader):
images = images.to(device)
 labels = labels.to(device)
 outputs = model(images)
 loss = criterion(outputs, labels)
 optimizer.zero_grad()
loss.backward()
optimizer.step()
if(i+1)% 100 == 0:
print('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}' 
.format(epoch+1, num_epochs, i+1, total_step, loss.item()))
tree_model = DecisionTreeClassifier(criterion='gini',
min_samples_split=12,
max_features='auto',
min_samples_leaf=12 )<train_model>
model.eval()
with torch.no_grad() :
correct = 0
total = 0
for images, labels in valid_loader:
images = images.to(device)
 labels = labels.to(device)
 outputs = model(images)
 _, predicted = torch.max(outputs.data, 1)
 total += labels.size(0)
 correct +=(predicted == labels ).sum().item()
print('Test Accuracy: {} %'.format(100 * correct / total))
tree_model.fit(X_train, Y_train)
 tree_predict = tree_model.predict(X_test)
 tree_acc = accuracy_score(tree_predict, Y_test)
 print("Tree accuarcy: {}".format(tree_acc))<train_model>
submit = pd.read_csv('.. /input/sample_submission.csv')
 test_data = CactiImageData(split_data = submit, data_root = test_dir, transform = valid_transf)
 test_loader = DataLoader(dataset = test_data, batch_size=32, shuffle=False, num_workers=0 )
model_ada = AdaBoostClassifier(n_estimators=1000, learning_rate=0.1)
 model_ada.fit(X_train, Y_train )<predict_on_test>
model.eval()
predict = []
for batch_i,(data, target)in enumerate(test_loader):
data, target = data.to(device), target.to(device)
 output = model(data)
 pr = output[:,1].detach().cpu().numpy()
for i in pr:
predict.append(i)
 submit['has_cactus'] = predict
submit.to_csv('submission.csv', index=False )