Kaggle competition: Human Protein Atlas Image Classification
y_score = clf.predict_proba(X_test )<compute_test_metric>
df = pd.read_csv(".. /input/train.csv")
 print("Total number of unique ids:",df.Id.count())
 print("Total number of images:", df.Id.count() *4 )
average_precision = average_precision_score(y_test, y_score[:, 0])
 average_precision<import_modules>
path_to_train = '/kaggle/input/train/'
data = pd.read_csv('/kaggle/input/train.csv')
 train_dataset_info = []
for name, labels in zip(data['Id'], data['Target'].str.split(' ')) :
train_dataset_info.append({
'path':os.path.join(path_to_train, name),
'labels':np.array([int(label)for label in labels])})
 train_dataset_info = np.array(train_dataset_info)
 train_ids, test_ids, train_targets, test_target = train_test_split(
 data['Id'], data['Target'], test_size=0.2, random_state=42 )
import scikitplot as skplt<predict_on_test>
class data_generator:
def create_train(dataset_info, batch_size, shape, augument=True):
while True:
random_indexes = np.random.choice(len(dataset_info), batch_size)
 batch_images = np.empty(( batch_size, shape[0], shape[1], shape[2]))
batch_labels = np.zeros(( batch_size, 28))
for i, idx in enumerate(random_indexes):
image = data_generator.load_image(
 dataset_info[idx]['path'], shape)
 if augument:
image = data_generator.augment(image)
 batch_images[i] = image
batch_labels[i][dataset_info[idx]['labels']] = 1
yield batch_images, batch_labels
def load_image(path, shape):
R = np.array(Image.open(path+'_red.png'))
G = np.array(Image.open(path+'_green.png'))
B = np.array(Image.open(path+'_blue.png'))
Y = np.array(Image.open(path+'_yellow.png'))
image = np.stack((
R,
G, 
(B+Y)/2),-1)
 image = cv2.resize(image,(shape[0], shape[1]))
image = np.divide(image, 255)
 return image
def augment(image):
augment_img = iaa.Sequential([
iaa.OneOf([
iaa.Affine(rotate=0),
iaa.Affine(rotate=90),
iaa.Affine(rotate=180),
iaa.Affine(rotate=270),
iaa.Fliplr(0.5),
iaa.Flipud(0.5),
])], random_order=True)
 image_aug = augment_img.augment_image(image)
 return image_aug
test_prediction = clf.predict(test )<predict_on_test>
input_shape=(299,299,3)
 train_datagen = data_generator.create_train(
 train_dataset_info, 5, input_shape, augument=True)
 images, labels = next(train_datagen)
 fig, ax = plt.subplots(1,5,figsize=(25,5))
for i in range(5):
ax[i].imshow(images[i])
 print('min: {0}, max: {1}'.format(images.min() , images.max()))
prediction = clf.predict(test )<save_to_csv>
def create_model(input_shape, n_out):
model = Sequential()
model.add(InceptionResNetV2(include_top=False,input_shape= input_shape, pooling='avg', weights="imagenet"))
model.add(Dense(1616))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(n_out, activation='sigmoid'))
return model
submission = pd.DataFrame({
"PassengerId": test_key,
"Survived": prediction
})
 submission.to_csv('submission.csv', index=False )<set_options>
def f1(y_true, y_pred):
tp = K.sum(K.cast(y_true*y_pred, 'float'), axis=0)
 fp = K.sum(K.cast(( 1-y_true)*y_pred, 'float'), axis=0)
 fn = K.sum(K.cast(y_true*(1-y_pred), 'float'), axis=0)
 p = tp /(tp + fp + K.epsilon())
 r = tp /(tp + fn + K.epsilon())
 f1 = 2*p*r /(p+r+K.epsilon())
 f1 = tf.where(tf.is_nan(f1), tf.zeros_like(f1), f1)
 return K.mean(f1)
 def focal_loss_fixed(y_true, y_pred):
gamma=2
alpha=.25
pt_1 = tf.where(tf.equal(y_true, 1), y_pred, tf.ones_like(y_pred))
pt_0 = tf.where(tf.equal(y_true, 0), y_pred, tf.zeros_like(y_pred))
pt_1 = K.clip(pt_1, 1e-3,.999)
 pt_0 = K.clip(pt_0, 1e-3,.999)
 return -K.sum(alpha * K.pow(1.- pt_1, gamma)* K.log(pt_1)) -K.sum(( 1-alpha)* K.pow(pt_0, gamma)* K.log(1.- pt_0))
def show_history(history):
fig, ax = plt.subplots(1, 3, figsize=(15,5))
ax[0].set_title('loss')
 ax[0].plot(history.epoch, history.history["loss"], label="Train loss")
 ax[0].plot(history.epoch, history.history["val_loss"], label="Validation loss")
 ax[1].set_title('f1')
 ax[1].plot(history.epoch, history.history["f1"], label="Train f1")
 ax[1].plot(history.epoch, history.history["val_f1"], label="Validation f1")
 ax[2].set_title('acc')
 ax[2].plot(history.epoch, history.history["acc"], label="Train acc")
 ax[2].plot(history.epoch, history.history["val_acc"], label="Validation acc")
 ax[0].legend()
ax[1].legend()
ax[2].legend()
warnings.filterwarnings('ignore')
 % matplotlib inline<load_from_csv>
model = create_model(
 input_shape,
n_out=28)
 checkpointer = ModelCheckpoint(
 '/kaggle/working/InceptionResNetV2.model',
verbose=2, save_best_only=True)
 BATCH_SIZE = 10
INPUT_SHAPE =(299,299,3)
 train_generator = data_generator.create_train(
 train_dataset_info[train_ids.index], BATCH_SIZE, INPUT_SHAPE, augument=False)
 validation_generator = data_generator.create_train(
 train_dataset_info[test_ids.index], 256, INPUT_SHAPE, augument=False)
 model.layers[0].trainable = True
model.compile(
 loss= focal_loss_fixed,
optimizer=Adam(0.5e-3,beta_1=0.9, beta_2=0.99),
metrics=['acc', f1])
 history = model.fit_generator(
 train_generator,
steps_per_epoch=500,
validation_data=next(validation_generator),
epochs=30,
verbose=1,
callbacks=[checkpointer])
 show_history(history )
<train_model><EOS> 
submit = pd.read_csv('.. /input/sample_submission.csv')
 predicted = []
for name in tqdm(submit['Id']):
path = os.path.join('.. /input/test/', name)
 image = data_generator.load_image(path, INPUT_SHAPE)
 score_predict = model.predict(image[np.newaxis])[0]
label_predict = np.arange(28)[score_predict>=0.1]
str_predict_label = ' '.join(str(l)for l in label_predict)
 predicted.append(str_predict_label)
 submit['Predicted'] = predicted
submit.to_csv('submission.csv', index=False )