Kaggle competition: Aerial Cactus Identification
<SOS> metric: AUC Kaggle data source: aerial-cactus-identification<feature_engineering>
%matplotlib inline
print(os.listdir(".. /input"))
housing_transform = housing_transform.apply(lambda x: np.sign(x)* np.log(1 + np.abs(x)) )<train_model>
def read_pix(jpg_dir):
filenames = glob.glob(os.path.join(jpg_dir, '*.jpg'))
img_array = np.zeros(( len(filenames), 32, 32, 3), dtype=int)
 img_index = []
for idx, filename in enumerate(filenames):
im_tmp = matplotlib.image.imread(filename)
 img_array[idx, :, :, :] = np.array(im_tmp, dtype=int)
 img_index.append(os.path.basename(filename))
return img_array, img_index
def prepare_data(img_array, img_index, train_response, test_size=0.25):
y_train_series, y_test_series = train_test_split(train_response,
shuffle=True,
random_state=12,
test_size=test_size)
 y_train = y_train_series.values[:, np.newaxis]
y_test = y_test_series.values[:, np.newaxis]
train_index = [img_index.index(idx)for idx in y_train_series.index]
test_index = [img_index.index(idx)for idx in y_test_series.index]
x_train = img_array[train_index, :, :, :]
x_test = img_array[test_index, :, :, :]
return x_train, y_train, x_test, y_test, y_train_series, y_test_series
def simple_oversample(x, y, oversample_class, oversample_factor=3):
new_x = x.copy()
new_y = y.copy()
oversample_mask = np.ravel(y == oversample_class)
 oversample_x = x[oversample_mask, :, :, :]
oversample_y = y[oversample_mask, :]
for i in range(oversample_factor):
new_x = np.concatenate([new_x, oversample_x], axis=0)
 new_y = np.concatenate([new_y, oversample_y], axis=0)
 return new_x, new_y
class LogModulusTransformer(BaseEstimator, TransformerMixin):
def fit(self, X, y=None):
return self
def transform(self, X, y=None):
return np.apply_along_axis(lambda x: np.sign(x)* np.log(1 + np.abs(x)) , 1, X)
<categorify>
train_labels = pd.read_csv('.. /input/train.csv')
 sample_submission = pd.read_csv('.. /input/sample_submission.csv')
 train_img_array, train_img_index = read_pix('.. /input/train/train')
 test_img_array, test_img_index = read_pix('.. /input/test/test')
 train_response = train_labels['has_cactus']
train_response.index = train_labels['id']
train_response = train_response.loc[train_img_index]
BsmtFullBath_ix = list(num_attributes ).index('BsmtFullBath')
 BsmtHalfBath_ix = list(num_attributes ).index('BsmtHalfBath')
 HalfBath_ix = list(num_attributes ).index('HalfBath')
 extra_attributes = ['TotalBath']
class CombinedNumAttributesAdder(BaseEstimator, TransformerMixin):
def fit(self, X, y=None):
return self
def transform(self, X, y=None):
total_bath = X[:, BsmtFullBath_ix] + X[:, BsmtHalfBath_ix] + X[:, HalfBath_ix]
return np.c_[X, total_bath]<import_modules>
batch_size = 128
num_classes = 2
datagen = ImageDataGenerator(
 rotation_range=90,
width_shift_range=.1,
height_shift_range=.1,
shear_range=.2,
zoom_range=.1,
horizontal_flip=True,
vertical_flip=True,
fill_mode='nearest', 
)
 x_train, y_train, x_test, y_test, y_train_series, y_test_series = prepare_data(train_img_array, train_img_index, train_response, test_size=0.2)
 x_train, y_train = simple_oversample(x_train, y_train, oversample_class=0, oversample_factor=2)
 x_test, y_test = simple_oversample(x_test, y_test, oversample_class=0, oversample_factor=2)
 print("Number of 'no cactus' samples in y_train: {}".format(( y_train == 0 ).sum()))
print("Number of 'cactus' samples in y_train: {}".format(( y_train == 1 ).sum()))
num_pipeline = Pipeline([ 
('selector', DataFrameSelector(num_attributes)) , 
('imputer', SimpleImputer(strategy='mean')) , 
('attr_adder', CombinedNumAttributesAdder()), 
('transformer', LogModulusTransformer()), 
('scaler', StandardScaler()),
] )<feature_engineering>
x_train = x_train / 255
x_test = x_test / 255
train_generator = datagen.flow(x_train, y_train)
 test_datagen = ImageDataGenerator()
validation_generator = test_datagen.flow(x_test, y_test )
num_pipeline.fit_transform(housing )<import_modules>
def convnet_model() :
model = Sequential()
model.add(Conv2D(32,(5, 5), input_shape=(32, 32, 3), activation='relu'))
model.add(Conv2D(32,(5, 5), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Conv2D(64,(5, 5), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.5))
model.add(Conv2D(64,(3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Flatten())
 model.add(Dropout(0.5))
model.add(Dense(128, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer=Adam(lr=1e-4, decay=1e-6), metrics=['accuracy'])
 return model
preprocess_pipeline = FeatureUnion(transformer_list=[ 
("num_pipeline", num_pipeline), 
("cat_pipeline", cat_pipeline)
 ] )<create_dataframe>
model = convnet_model()
hist = model.fit_generator(train_generator,
steps_per_epoch=np.ceil(x_train.shape[0] / 32),
epochs=120,
validation_data=validation_generator,
validation_steps=np.ceil(x_test.shape[0] / 32)
)
 scores = model.evaluate(x_test, y_test, verbose=0)
 print('CNN error: {:.2f}'.format(100-scores[1]*100))
<prepare_x_and_y><EOS> 
probability = model.predict_proba(test_img_array/255)
 res = pd.DataFrame({
'id': test_img_index,
'has_cactus': probability.ravel() ,
})
 res.to_csv('submission.csv', index=False )