Kaggle competition: Aerial Cactus Identification
print("Training shape: {} and Testing shape: {}
\
Training Label:{}".format(traindf.shape,
testdf.shape,
train_y.shape))<import_modules>
path_to_train = '.. /input/train/train'
glob_train_imgs = os.path.join(path_to_train, '*.jpg')
 train_img_paths = glob(glob_train_imgs)
 print(train_img_paths[:10])
 path_to_test = '.. /input/test/test'
glob_test_imgs = os.path.join(path_to_test, '*.jpg')
 test_img_paths = glob(glob_test_imgs)
 print(test_img_paths[:10])
 def get_img_basename(img_path):
img_basename = os.path.basename(img_path)
 return img_basename
def get_img_id(img_path):
img_basename = os.path.basename(img_path)
 img_id = os.path.splitext(img_basename)[0]
return img_id
path = '.. /input/train/train/655c71d8c3f3d61f3797545e7d0414ce.jpg'
print(get_img_basename(path))
from sklearn.model_selection import train_test_split
from sklearn.model_selection import KFold
from sklearn.model_selection import cross_val_score
from sklearn.metrics import accuracy_score
from sklearn.metrics import confusion_matrix<split>
train = pd.read_csv('.. /input/train.csv')
 def get_class(img_path):
img_basename = get_img_basename(img_path)
 A = train[train['id'] == img_basename]
label = A['has_cactus'].values[0]
return label
def image_gen(img_paths, img_size=(32, 32)) :
for img_path in img_paths:
label = get_class(img_path)
 img = imread(img_path)
 img = resize(img, img_size, preserve_range=True)
 yield img, label
X_train, X_test, Y_train, Y_test = train_test_split(traindf,
train_y,
test_size=0.3,
random_state=42)
 X_train.shape, X_test.shape, Y_train.shape, Y_test.shape<train_model>
%matplotlib inline
def horizontal_augmentation(img):
aug = mx.image.HorizontalFlipAug(1)
 img = mx.nd.array(img)
 aug_img = aug(img)
 return aug_img.asnumpy()
def vertical_augmentation(img):
img = np.flipud(img)
 return img
def rotate_augmentation(img):
new_img = np.rot90(img)
 return new_img
def brightness_augmentation(img):
aug = mx.image.BrightnessJitterAug(brightness=.5)
 img = mx.nd.array(img)
 aug_img = aug(img)
 aug_img = aug_img.astype('int')
 return aug_img.asnumpy()
LogisticRegression = LogisticRegression(max_iter=10000)
 LogisticRegression.fit(X_train, Y_train )<predict_on_test>
original = imread(train_img_paths[10])
 img_id = get_img_id(train_img_paths[10])
 aug = brightness_augmentation(original)
 print('Original')
 plt.imshow(original/255)
 plt.show()
print('Brightness Augmentation')
 plt.imshow(aug/255)
 plt.show()
print('Rotation')
 img = rotate_augmentation(original)
 plt.imshow(img/255)
 plt.show()
print('Horizontal Flip')
 img = horizontal_augmentation(original)
 plt.imshow(img/255)
 plt.show()
print('Vertical Flip')
 img = vertical_augmentation(original)
 plt.imshow(img/255)
 plt.show()
predictions = LogisticRegression.predict(X_test)
 predictions<compute_test_metric>
model = Sequential()
model.add(Conv2D(32, 3, activation='relu', padding='same', input_shape=(32, 32, 3)))
 model.add(BatchNormalization())
 model.add(Conv2D(32, 3, activation='relu', padding='same'))
model.add(MaxPooling2D(2))
model.add(Conv2D(64, 3, activation='relu', padding='same'))
model.add(BatchNormalization())
 model.add(Conv2D(64, 3, activation='relu', padding='same'))
model.add(MaxPooling2D(2))
model.add(Conv2D(128, 3, activation='relu', padding='same'))
model.add(BatchNormalization())
 model.add(Conv2D(128, 3, activation='relu', padding='same'))
model.add(MaxPooling2D(2))
model.add(Conv2D(256, 3, activation='relu', padding='same'))
model.add(BatchNormalization())
 model.add(Conv2D(256, 3, activation='relu', padding='same'))
model.add(MaxPooling2D(2))
model.add(Flatten())
 model.add(Dense(256, activation='relu'))
model.add(Dropout(0.1))
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.1))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.1))
model.add(Dense(32, activation='relu'))
model.add(Dropout(0.1))
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer=Adam(lr=0.001),
loss='binary_crossentropy',
metrics=['accuracy'])
 model.summary()
Linear_Reg_acc = accuracy_score(predictions, Y_test)* 100<choose_model_class>
def segnet_mod() :
kernel = 3
pad = 1
pool_size = 2
model = Sequential()
model.add(Layer(input_shape=(32, 32 ,3)))
 model.add(ZeroPadding2D(padding=(pad,pad)))
 model.add(Conv2D(64, kernel, kernel, border_mode='valid'))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(pool_size, pool_size)))
 model.add(ZeroPadding2D(padding=(pad,pad)))
 model.add(Conv2D(128, kernel, kernel, border_mode='valid'))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(pool_size, pool_size)))
 model.add(ZeroPadding2D(padding=(pad,pad)))
 model.add(Conv2D(256, kernel, kernel, border_mode='valid'))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(pool_size, pool_size)))
 model.add(ZeroPadding2D(padding=(pad,pad)))
 model.add(Conv2D(512, kernel, kernel, border_mode='valid'))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(pool_size, pool_size)))
 model.add(UpSampling2D(size=(pool_size,pool_size)))
 model.add(ZeroPadding2D(padding=(pad,pad)))
 model.add(Conv2D(512, kernel, kernel, border_mode='valid'))
model.add(BatchNormalization())
 model.add(UpSampling2D(size=(pool_size,pool_size)))
 model.add(ZeroPadding2D(padding=(pad,pad)))
 model.add(Conv2D(256, kernel, kernel, border_mode='valid'))
model.add(BatchNormalization())
 model.add(UpSampling2D(size=(pool_size,pool_size)))
 model.add(ZeroPadding2D(padding=(pad,pad)))
 model.add(Conv2D(128, kernel, kernel, border_mode='valid'))
model.add(BatchNormalization())
 model.add(UpSampling2D(size=(pool_size,pool_size)))
 model.add(ZeroPadding2D(padding=(pad,pad)))
 model.add(Conv2D(64, kernel, kernel, border_mode='valid'))
model.add(BatchNormalization())
 model.add(Flatten())
 model.add(Dense(256, activation='relu'))
model.add(Dropout(0.1))
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.1))
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer=Adam(lr=0.001),
loss='binary_crossentropy',
metrics=['accuracy'])
 return model
model_random = RandomForestClassifier(n_estimators = 700,
max_features='auto',
oob_score=True,
random_state=1,
n_jobs=1,
min_samples_leaf=1,
min_samples_split=10
)<predict_on_test>
def image_batch_generator(img_paths, set_type, batchsize=32):
while True:
ig = image_gen(img_paths)
 batch_img, batch_label = [], []
for img, label in ig:
if set_type == 'train':
vertical = vertical_augmentation(img)
 vertical =(vertical - vertical.mean())/vertical.std()
batch_img.append(vertical)
 batch_label.append(label)
 img =(img - img.mean())/img.std()
batch_img.append(img)
 batch_label.append(label)
 if len(batch_img)== batchsize:
yield np.stack(batch_img, axis=0), np.stack(batch_label, axis=0)
 batch_img, batch_label = [], []
if len(batch_img)!= 0:
yield np.stack(batch_img, axis=0), np.stack(batch_label, axis=0)
 batch_img, batch_label = [], []
model_random.fit(X_train, Y_train)
 predictions_random = model_random.predict(X_test )<compute_test_metric>
device_lib.list_local_devices()
Random_Forest_Acc = accuracy_score(predictions_random, Y_test)* 100
print("Random FOrest acc: {}".format(Random_Forest_Acc))<train_model>
BATCHSIZE = 32
train_img_paths, val_img_paths = train_test_split(train_img_paths, test_size=0.10)
 traingen = image_batch_generator(train_img_paths,'val', batchsize=BATCHSIZE)
 valgen = image_batch_generator(val_img_paths, 'val', batchsize=BATCHSIZE)
 def calc_steps(data_len, batchsize):
return(data_len + batchsize - 1)// batchsize
train_steps = calc_steps(len(train_img_paths), BATCHSIZE)
 val_steps = calc_steps(len(val_img_paths), BATCHSIZE)
 history = model.fit_generator(
 traingen,
steps_per_epoch=train_steps,
epochs = 20,
validation_data=valgen,
validation_steps=val_steps,
verbose=1,
max_queue_size=1
)
svc_model = SVC()
svc_model.fit(X_train, Y_train)
 predictions_svc = svc_model.predict(X_test)
<compute_test_metric>
def generate_predictions_generator(val_paths):
for img_path in val_paths:
img = imread(img_path)
 img_id = get_img_basename(img_path)
 img =(img - img.mean())/img.std()
y = model.predict(img.reshape(1,32,32,3))
yield img_id, y
def create_submission(csv_name, predictions_gen):
sub = pd.DataFrame()
ids = []
encodings = []
num_images = len(test_img_paths)
 for i in range(num_images):
if(i+1)%(num_images//10)== 0:
print(i, num_images)
 img_id, pred = next(predictions_gen)
 ids.append(img_id)
 encodings.append(pred[0][0])
 sub['id'] = ids
sub['has_cactus'] = encodings
sub.to_csv(csv_name, index=False)
 tic = time.time()
create_submission("cactus.csv", generate_predictions_generator(test_img_paths))
toc = time.time()
print(toc - tic)