Kaggle competition: Histopathologic Cancer Detection
<SOS> metric: AUC Kaggle data source: histopathologic-cancer-detection<data_type_conversions>
%matplotlib inline
X_train = torch.tensor(X_train)
 X_val = torch.tensor(X_val)
 y_train = torch.tensor(y_train)
 y_val = torch.tensor(y_val)
 attention_masks_train = torch.tensor(attention_masks_train)
 attention_masks_val = torch.tensor(attention_masks_val)
 test_input_ids = torch.tensor(test_input_ids)
 test_attention_masks = torch.tensor(test_attention_masks )<create_dataframe>
!pip install livelossplot
BATCH_SIZE = 32
train_data = TensorDataset(X_train, attention_masks_train, y_train)
 val_data = TensorDataset(X_val, attention_masks_val, y_val)
 test_data = TensorDataset(test_input_ids, test_attention_masks)
 train_sampler = RandomSampler(train_data)
 val_sampler = SequentialSampler(val_data)
 test_sampler = SequentialSampler(test_data)
 train_dataloader = DataLoader(train_data, sampler = train_sampler, batch_size = BATCH_SIZE)
 val_dataloader = DataLoader(val_data, sampler = val_sampler, batch_size = BATCH_SIZE)
 test_dataloader = DataLoader(test_data, sampler = test_sampler, batch_size = BATCH_SIZE )<load_pretrained>
TRAINING_LOGS_FILE = "training_logs.csv"
MODEL_SUMMARY_FILE = "model_summary.txt"
MODEL_FILE = "histopathologic_cancer_detector.h5"
TRAINING_PLOT_FILE = "training.png"
VALIDATION_PLOT_FILE = "validation.png"
ROC_PLOT_FILE = "roc.png"
KAGGLE_SUBMISSION_FILE = "kaggle_submission.csv"
INPUT_DIR = '.. /input/'
model = BertForSequenceClassification.from_pretrained('bert-base-uncased', num_labels = 6)
 model.cuda()<choose_model_class>
SAMPLE_COUNT = 85000
TRAINING_RATIO = 0.9
IMAGE_SIZE = 96
EPOCHS = 12
BATCH_SIZE = 216
VERBOSITY = 1
TESTING_BATCH_SIZE = 5000
param_optimizer = list(model.named_parameters())
 no_decay = ['bias', 'gamma', 'beta']
optimizer_grouped_parameters = [
{
'params': [p for n, p in param_optimizer if not any(nd in n for nd in no_decay)],
'weight_decay_rate': 0.01
},
{
'params': [p for n, p in param_optimizer if any(nd in n for nd in no_decay)],
'weight_decay_rate': 0.0
}
]
optimizer = BertAdam(optimizer_grouped_parameters, lr = 2e-5, warmup =.1 )<set_options>
training_dir = INPUT_DIR + 'train/'
data_frame = pd.DataFrame({'path': glob(os.path.join(training_dir,'*.tif')) })
 data_frame['id'] = data_frame.path.map(lambda x: x.split('/')[3].split('.')[0])
 labels = pd.read_csv(INPUT_DIR + 'train_labels.csv')
 data_frame = data_frame.merge(labels, on = 'id')
 negatives = data_frame[data_frame.label == 0].sample(SAMPLE_COUNT)
 positives = data_frame[data_frame.label == 1].sample(SAMPLE_COUNT)
 data_frame = pd.concat([negatives, positives] ).reset_index()
data_frame = data_frame[['path', 'id', 'label']]
data_frame['image'] = data_frame['path'].map(imread)
 training_path = '.. /training'
validation_path = '.. /validation'
for folder in [training_path, validation_path]:
for subfolder in ['0', '1']:
path = os.path.join(folder, subfolder)
 os.makedirs(path, exist_ok=True)
 training, validation = train_test_split(data_frame, train_size=TRAINING_RATIO, stratify=data_frame['label'])
 data_frame.set_index('id', inplace=True)
 for images_and_path in [(training, training_path),(validation, validation_path)]:
images = images_and_path[0]
path = images_and_path[1]
for image in images['id'].values:
file_name = image + '.tif'
label = str(data_frame.loc[image,'label'])
 destination = os.path.join(path, label, file_name)
 if not os.path.exists(destination):
source = os.path.join(INPUT_DIR + 'train', file_name)
 shutil.copyfile(source, destination )
torch.cuda.empty_cache()
gc.collect()<train_model>
training_data_generator = ImageDataGenerator(rescale=1./255,
horizontal_flip=True,
vertical_flip=True,
rotation_range=90,
zoom_range=0.2,
width_shift_range=0.1,
height_shift_range=0.1,
shear_range=0.05,
channel_shift_range=0.1 )
torch.cuda.empty_cache()
epochs = 2
for _ in trange(epochs, desc = "Epoch"):
model.train()
tr_loss = 0
nb_tr_examples, nb_tr_steps = 0, 0
val_loss = 0
nb_val_examples, nb_val_steps = 0, 0
for batch in tqdm(train_dataloader):
batch = tuple(t.to(device)for t in batch)
 b_input_ids, b_input_mask, b_labels = batch
optimizer.zero_grad()
logits = model(b_input_ids, token_type_ids = None, attention_mask = b_input_mask)
 loss_func = BCEWithLogitsLoss()
loss = loss_func(logits, b_labels.type_as(logits))
loss.backward()
optimizer.step()
tr_loss += loss.item()
nb_tr_examples += b_input_ids.size(0)
 nb_tr_steps += 1
print("Train Loss: {}".format(tr_loss/nb_tr_examples))
model.eval()
for step, batch in enumerate(val_dataloader):
batch = tuple(t.to(device)for t in batch)
 with torch.no_grad() :
b_input_ids, b_input_mask, b_labels = batch
logits = model(b_input_ids, token_type_ids = None, attention_mask = b_input_mask)
 loss_func = BCEWithLogitsLoss()
loss = loss_func(logits, b_labels.type_as(logits))
val_loss += loss.item()
nb_val_examples += b_input_ids.size(0)
 nb_val_steps += 1
print("Valid Loss: {}".format(val_loss/nb_val_examples))<define_variables>
training_generator = training_data_generator.flow_from_directory(training_path,
target_size=(IMAGE_SIZE,IMAGE_SIZE),
batch_size=BATCH_SIZE,
class_mode='binary')
 validation_generator = ImageDataGenerator(rescale=1./255 ).flow_from_directory(validation_path,
target_size=(IMAGE_SIZE,IMAGE_SIZE),
batch_size=BATCH_SIZE,
class_mode='binary' )
outputs = []
for step, batch in enumerate(test_dataloader):
batch = tuple(t.to(device)for t in batch)
 with torch.no_grad() :
b_input_ids, b_input_mask = batch
logits = model(b_input_ids, token_type_ids = None, attention_mask = b_input_mask)
 s = Sigmoid()
out = s(logits ).to('cpu' ).numpy()
outputs.extend(out)
 df_test = pd.merge(df_test, sample_submission, on = "id")
 df_test[["toxic","severe_toxic","obscene","threat","insult","identity_hate"]] = outputs
df_test.drop(["comment_text"], axis = 1, inplace = True)
 df_test.to_csv("sample_submission.csv", index = False )<define_variables>
input_shape =(IMAGE_SIZE, IMAGE_SIZE, 3)
 inputs = Input(input_shape)
 xception = Xception(include_top=False, input_shape=input_shape)
 nas_net = NASNetMobile(include_top=False, input_shape=input_shape)
 outputs = Concatenate(axis=-1 )([GlobalAveragePooling2D()(xception(inputs)) ,
GlobalAveragePooling2D()(nas_net(inputs)) ])
 outputs = Dropout(0.5 )(outputs)
 outputs = Dense(1, activation='sigmoid' )(outputs)
 model = Model(inputs, outputs)
 model.compile(optimizer=Adam(lr=0.0001, decay=0.00001),
loss='binary_crossentropy',
metrics=['accuracy'])
 model.summary()
SVG(model_to_dot(model ).create(prog='dot', format='svg'))
plot_model(model, to_file='model_plot.png', show_shapes=True, show_layer_names=True, expand_nested=True )
f_lstm_glove = '.. /input/improved-lstm-baseline-glove-dropout/submission.csv'
f_nbsvm = '.. /input/nb-svm-strong-linear-baseline/submission.csv'
f_lstm_fast = '.. /input/improved-lstm-baseline-fasttext-dropout/submission.csv'<load_from_csv>
history = model.fit_generator(training_generator,
steps_per_epoch=len(training_generator),
validation_data=validation_generator,
validation_steps=len(validation_generator),
epochs=EPOCHS,
verbose=VERBOSITY,
callbacks=[PlotLossesKeras() ,
ModelCheckpoint(MODEL_FILE,
monitor='val_acc',
verbose=VERBOSITY,
save_best_only=True,
mode='max'),
CSVLogger(TRAINING_LOGS_FILE,
append=False,
separator=';')])
 model.load_weights(MODEL_FILE )
<feature_engineering><EOS> 
testing_files = glob(os.path.join(INPUT_DIR+'test/','*.tif'))
submission = pd.DataFrame()
for index in range(0, len(testing_files), TESTING_BATCH_SIZE):
data_frame = pd.DataFrame({'path': testing_files[index:index+TESTING_BATCH_SIZE]})
 data_frame['id'] = data_frame.path.map(lambda x: x.split('/')[3].split(".")[0])
 data_frame['image'] = data_frame['path'].map(imread)
 images = np.stack(data_frame.image, axis=0)
 predicted_labels = [model.predict(np.expand_dims(image/255.0, axis=0)) [0][0] for image in images]
predictions = np.array(predicted_labels)
 data_frame['label'] = predictions
submission = pd.concat([submission, data_frame[["id", "label"]]])
 submission.to_csv(KAGGLE_SUBMISSION_FILE, index=False, header=True )