Kaggle competition: Human Protein Atlas Image Classification
abs(corr.Survived ).sort_values(ascending=False )<import_modules>
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 )
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.svm import SVC
import numpy as np<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 )
from sklearn.model_selection import train_test_split
from sklearn.metrics import precision_recall_curve, make_scorer, recall_score, precision_score, average_precision_score, confusion_matrix, accuracy_score
from sklearn.model_selection import GridSearchCV<split>
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
X_train, X_test, y_train, y_test = train_test_split(train_data, train.Survived, test_size=0.10, stratify=train['Survived'].values, random_state=42 )<choose_model_class>
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()))
k_fold = KFold(n_splits=10, shuffle=True, random_state=0 )<compute_train_metric>
def create_model(input_shape, n_out):
input_tensor = Input(shape=input_shape)
 base_model = InceptionResNetV2(include_top=False,
weights='imagenet',
input_shape=input_shape)
 bn = BatchNormalization()(input_tensor)
 x = base_model(bn)
 x = Conv2D(32, kernel_size=(1,1), activation='relu' )(x)
 x = Flatten()(x)
 x = Dropout(0.5 )(x)
 x = Dense(1024, activation='relu' )(x)
 x = Dropout(0.5 )(x)
 output = Dense(n_out, activation='sigmoid' )(x)
 model = Model(input_tensor, output)
 return model
clf = SVC(C=1.0, kernel='rbf', degree=3, gamma='auto', probability=True)
 scoring = 'accuracy'
score = cross_val_score(clf, train_data, target, cv=k_fold, n_jobs=1, scoring=scoring)
 print(score)
 print('The total score is: '+str(score.mean()))<train_model>
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 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()
clf.fit(train_data, target )<predict_on_test>
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='binary_crossentropy',
optimizer=Adam(1e-4),
metrics=['acc', f1])
 history = model.fit_generator(
 train_generator,
steps_per_epoch=500,
validation_data=next(validation_generator),
epochs=35,
verbose=1,
callbacks=[checkpointer])
 show_history(history )
predictions = clf.predict(X_test )<predict_on_test>
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 )