Kaggle competition: Aerial Cactus Identification
<SOS> metric: AUC Kaggle data source: aerial-cactus-identification<compute_train_metric>
!pip install natsort
LR = LogisticRegression()
estimator.append(( 'LR',LogisticRegression()))
cv = cross_val_score(LR,x_train,x_test,cv=10)
 accuracy1 = cv.mean()
accuracy.append(accuracy1)
 print(cv)
 print(cv.mean() )<save_to_csv>
import numpy as np
import pandas as pd
from glob import glob
from skimage.io import imread
import skimage.io as sio
import os
from natsort import natsorted
import matplotlib.pyplot as plt
import tensorflow as tf
from sklearn.metrics import accuracy_score, recall_score, precision_score, f1_score, roc_auc_score
from skimage.transform import resize, rotate
import warnings; warnings.filterwarnings("ignore" )
LR.fit(x_train,x_test)
 model1pred = LR.predict(y_train)
 submission1 = pd.DataFrame(columns = ['PassengerId','Survived'])
 submission1['PassengerId'] = result['PassengerId']
submission1['Survived'] = model1pred
submission1.to_csv('LogisticRegression(No HT ).csv',index = False )<compute_test_metric>
root_dir = ".. /input"
train_dir = root_dir + "/train/train/"
test_dir = root_dir + "/test/test/"
csv_path = root_dir + "/train.csv"
sub_path = root_dir + "sample_submission.csv"
df = pd.read_csv(csv_path)
 x = np.array([ imread(train_dir+p)for p in df.id.values])
 y = df.has_cactus.values
LR.score(y_train,y_test )<compute_train_metric>
x_train, x_val, y_train, y_val = train_test_split(x, y, test_size=0.20,stratify=y )
SVC = LinearSVC()
cv = cross_val_score(SVC,x_train,x_test,cv=10)
 accuracy2 = cv.mean()
accuracy.append(accuracy2)
 print(cv)
 print(cv.mean())
<train_model>
x_input = tf.placeholder("float32",shape=(None,32,32,3))
y_label = tf.placeholder("uint8",shape=(None))
y_onehot= tf.one_hot(y_label, depth=2)
 h1 = conv(x_input, filters=16, kernel_size=3, strides=1, padding="same", batchnorm=True, activation="leakyrelu")
 h1 = conv(h1, filters=16, kernel_size=3, strides=1, padding="same", batchnorm=True, activation="leakyrelu")
 h1 = tf.layers.MaxPooling2D(pool_size=2,strides=2 )(h1)
 h2 = conv(h1, filters=32, kernel_size=3, strides=1, padding="same", batchnorm=True, activation="leakyrelu")
 h2 = conv(h2, filters=32, kernel_size=3, strides=1, padding="same", batchnorm=True, activation="leakyrelu")
 h2 = tf.layers.MaxPooling2D(pool_size=2,strides=2 )(h2)
 h3 = conv(h2, filters=64, kernel_size=3, strides=1, padding="same", batchnorm=True, activation="leakyrelu")
 h3 = conv(h3, filters=64, kernel_size=3, strides=1, padding="same", batchnorm=True, activation="leakyrelu")
 h3 = tf.layers.MaxPooling2D(pool_size=2,strides=2 )(h3)
 h4 = conv(h3, filters=128, kernel_size=3, strides=1, padding="same", batchnorm=True, activation="leakyrelu")
 h4 = conv(h4, filters=128, kernel_size=3, strides=1, padding="valid", batchnorm=True, activation="leakyrelu")
 h4 = tf.layers.MaxPooling2D(pool_size=2,strides=2 )(h4)
 h5 = tf.layers.Flatten()(h4)
 y_logits = tf.layers.Dense(2 )(h5)
 y_prob = tf.nn.softmax(y_logits)
 y_pred = tf.arg_max(y_logits, -1 )
SVC.fit(x_train,x_test)
 SVC.score(y_train,y_test )<save_to_csv>
hinge_loss = tf.losses.hinge_loss(labels=y_onehot, logits=y_logits,)
 ce_loss = tf.losses.softmax_cross_entropy(y_onehot, y_logits)
 total_loss = 0.0*hinge_loss + ce_loss
opt = tf.train.AdamOptimizer(learning_rate=0.001, ).minimize(total_loss)
 sess = tf.Session()
global_init = tf.global_variables_initializer()
local_init = tf.local_variables_initializer()
_ = sess.run([global_init, local_init] )
SVC.fit(x_train,x_test)
 model2pred = SVC.predict(y_train)
 submission2 = pd.DataFrame(columns = ['PassengerId','Survived'])
 submission2['PassengerId'] = result['PassengerId']
submission2['Survived'] = model2pred
submission2.to_csv('LinearSVC(No HT ).csv',index = False )<compute_train_metric>
epochs = 100
batchsize = 32
n_train = len(x_train)
 n_batches = len(x_train)//batchsize
for e in range(epochs):
shuffled_idx0 = np.random.permutation(np.where(y_train == 0)[0])
 shuffled_idx1 = np.random.permutation(np.where(y_train == 1)[0])
 run_metrics = {"loss":[],"accuracy":[], "recall":[], "precision":[],"f1":[], "roc":[]}
print("+-"*15+"\t epoch = %0.3d \t"%e+"-+"*15)
 for i in range(n_batches):
batch0_idx = np.random.randint(0, len(shuffled_idx0), batchsize//2)
 batch0_idx = shuffled_idx0[batch0_idx]
batch1_idx = np.random.randint(0, len(shuffled_idx1), batchsize//2)
 batch1_idx = shuffled_idx1[batch1_idx]
x0_batch = x_train[batch0_idx]
x1_batch = x_train[batch1_idx]
y0_batch = y_train[batch0_idx]
y1_batch = y_train[batch1_idx]
x_batch = np.concatenate([x0_batch, x1_batch])
 x_batch = augmenter(x_batch)
 y_batch = np.concatenate([y0_batch, y1_batch])
 _, y_pred_batch, y_prob_batch, loss_batch = sess.run([opt, y_pred, y_prob, total_loss],
feed_dict={x_input: x_batch,
y_label: y_batch.reshape(-1,1)})
 acc = accuracy_score(y_batch, y_pred_batch,)
 prec = precision_score(y_batch, y_pred_batch)
 rec = recall_score(y_batch, y_pred_batch)
 f1 = f1_score(y_batch, y_pred_batch)
 roc = roc_auc_score(y_batch, y_prob_batch[:,1])
 run_metrics["loss"].append(loss_batch)
 run_metrics["accuracy"].append(acc)
 run_metrics["recall"].append(rec)
 run_metrics["precision"].append(prec)
 run_metrics["f1"].append(f1)
 run_metrics["roc"].append(roc)
 print("loss = %0.3f \t acc = %0.3f \t roc = %0.3f \t precision = %0.3f \t recall = %0.3f \t f1 = %0.3f"% 
(np.mean(run_metrics["loss"]),
np.mean(run_metrics["accuracy"]),
np.mean(run_metrics["roc"]),
np.mean(run_metrics["precision"]),
np.mean(run_metrics["recall"]),
np.mean(run_metrics["f1"])) ,end="\r")
 print("train :: loss = %0.3f :: acc = %0.3f :: roc = %0.3f :: precision = %0.3f :: recall = %0.3f :: f1 = %0.3f"% 
(np.mean(run_metrics["loss"]),
np.mean(run_metrics["accuracy"]),
np.mean(run_metrics["roc"]),
np.mean(run_metrics["precision"]),
np.mean(run_metrics["recall"]),
np.mean(run_metrics["f1"])))
 y_pred_batch, y_prob_batch, loss_batch = sess.run([y_pred,y_prob, total_loss],
feed_dict={x_input: x_val,
y_label: y_val.reshape(-1,1)})
 acc = accuracy_score(y_val, y_pred_batch,)
 prec = precision_score(y_val, y_pred_batch)
 rec = recall_score(y_val, y_pred_batch)
 f1 = f1_score(y_val, y_pred_batch)
 roc = roc_auc_score(y_val, y_prob_batch[:,1])
 print("valid :: loss = %0.3f :: acc = %0.3f :: roc = %0.3f :: precision = %0.3f :: recall = %0.3f :: f1 = %0.3f
"% 
(loss_batch,acc,roc, prec, rec,f1))
poly = svm.SVC(kernel = 'poly', gamma = 'scale')
 cv = cross_val_score(poly,x_train,x_test,cv=10)
 accuracy3 = cv.mean()
accuracy.append(accuracy3)
 print(cv)
 print(cv.mean() )<train_model>
saver = tf.train.Saver()
saver.save(sess, "cactus_net.ckpt" )
<save_to_csv><EOS> 
df_test = pd.read_csv('.. /input/sample_submission.csv')
 x_test = np.array([ imread(test_dir+p)for p in df_test.id.values])
 x_test = np.array(x_test)
 batchsize = 32
n_batches =(len(x_test)// batchsize)+ batchsize
y_pred_test = []
last_iter = False
for i in tqdm(range(n_batches)) :
start = i*batchsize
end =(i+1)*batchsize
if end >= len(x_test):
if last_iter:
break
end = len(x_test)
 last_iter = True
y_prob_batch = sess.run([y_prob], feed_dict={x_input: x_test[start:end]})[0]
y_prob_batch = y_prob_batch[:,1]
y_pred_test.extend(y_prob_batch)
 y_pred_test = np.array(y_pred_test)
 df_test['has_cactus'] = y_pred_test
df_test.to_csv('cactus_net_submission.csv', index=False )