Kaggle competition: Histopathologic Cancer Detection
<SOS> metric: AUC Kaggle data source: histopathologic-cancer-detection<feature_engineering>
Conv2D, SeparableConv1D, Add, BatchNormalization, Activation, GlobalAveragePooling2D, LeakyReLU, Flatten
Lambda, Multiply, LSTM, Bidirectional, PReLU, MaxPooling1D
print(os.listdir(".. /input"))
train['ord_2'] = train['ord_2'].replace(map_ord_2)
 test['ord_2'] = test['ord_2'].replace(map_ord_2 )<count_unique_values>
df_train = pd.read_csv(".. /input/train_labels.csv")
 id_label_map = {k:v for k,v in zip(df_train.id.values, df_train.label.values)}
df_train.head()
train[ord_cols].nunique()<feature_engineering>
def get_id_from_file_path(file_path):
return file_path.split(os.path.sep)[-1].replace('.tif', '' )
train['ord_4_band'] = pd.qcut(train['ord_4'], 6)
 bands=train.ord_4_band.unique()
keys_bands=np.sort(bands)
 values_bands=np.arange(len(keys_bands))
map_bands = dict(zip(keys_bands, values_bands))<feature_engineering>
labeled_files = glob('.. /input/train/*.tif')
 test_files = glob('.. /input/test/*.tif' )
train['ord_4_band'] = train['ord_4_band'].replace(map_bands)
 test['ord_4_band']=pd.cut(test.ord_4,pd.IntervalIndex(keys_bands))<categorify>
train, val = train_test_split(labeled_files, test_size=0.1, random_state=101010 )
test['ord_4_band'] = test['ord_4_band'].replace(map_bands)
 test.ord_4_band.head()<feature_engineering>
def get_model_classif_mobilenetv2() :
inputs = Input(( 96, 96, 3))
base_model = DenseNet169(include_top=False, input_shape=(96, 96, 3))
x = base_model(inputs)
 out1 = GlobalMaxPooling2D()(x)
 out2 = GlobalAveragePooling2D()(x)
 out3 = Flatten()(x)
 out = Concatenate(axis=-1 )([out1, out2, out3])
 out = Dropout(0.5 )(out)
 out = Dense(1, activation="sigmoid", name="3_" )(out)
 model = Model(inputs, out)
 model.compile(optimizer=Adam(0.0001), loss=binary_crossentropy, metrics=['acc'])
 model.summary()
return model
train['ord_5_band'] = pd.qcut(train['ord_5'], 6)
 bands=train.ord_5_band.unique()
keys_bands=np.sort(bands)
 values_bands=np.arange(len(keys_bands))
map_bands = dict(zip(keys_bands, values_bands))<feature_engineering>
model = get_model_classif_mobilenetv2()
train['ord_5_band'] = train['ord_5_band'].replace(map_bands)
 test['ord_5_band']=pd.cut(test.ord_5,pd.IntervalIndex(keys_bands))<categorify>
batch_size=32
h5_path = "model.h5"
checkpoint = ModelCheckpoint(h5_path, monitor='val_acc', verbose=1, save_best_only=True, mode='max')
 history = model.fit_generator(
 data_gen(train, id_label_map, batch_size, augment=True),
validation_data=data_gen(val, id_label_map, batch_size),
epochs=2, verbose=1,
callbacks=[checkpoint],
steps_per_epoch=len(train)// batch_size,
validation_steps=len(val)// batch_size)
 batch_size=64
history = model.fit_generator(
 data_gen(train, id_label_map, batch_size, augment=True),
validation_data=data_gen(val, id_label_map, batch_size),
epochs=6, verbose=1,
callbacks=[checkpoint],
steps_per_epoch=len(train)// batch_size,
validation_steps=len(val)// batch_size)
 model.load_weights(h5_path )
test['ord_5_band'] = test['ord_5_band'].replace(map_bands)
 test.ord_5_band.head()<count_unique_values>
preds = []
ids = []
train[nom_cols].nunique()<count_unique_values>
for batch in chunker(test_files, batch_size):
X = [preprocess_input(cv2.imread(x)) for x in batch]
ids_batch = [get_id_from_file_path(x)for x in batch]
X = np.array(X)
 preds_batch =(( model.predict(X ).ravel() *model.predict(X[:, ::-1, :, :] ).ravel() *model.predict(X[:, ::-1, ::-1, :] ).ravel() *model.predict(X[:, :, ::-1, :] ).ravel())**0.25 ).tolist()
preds += preds_batch
ids += ids_batch
test[nom_cols].nunique()<count_unique_values>
df = pd.DataFrame({'id':ids, 'label':preds})
 df.to_csv("baseline_nasnet.csv", index=False)
 df.head()