Kaggle competition: Human Protein Atlas Image Classification
<SOS> metric: MacroFScore Kaggle data source: human-protein-atlas-image-classification<filter>
warnings.filterwarnings("ignore" )
def other_women_in_family(data, match_data):
match_list = []
for i in range(len(data)) :
if data.Sex[i] == 1:
match =(match_data[(match_data['Last_Name'] == data['Last_Name'][i])\
&(data['Family_size'][i] == match_data['Family_size'])\
&(data['Pclass'][i] == match_data['Pclass'])].Sex.sum())
 if match > 1:
match_list.append(1)
 else:
match_list.append(0)
 else:
match_list.append(0)
 return match_list<categorify>
path_to_train = '.. /input/train/'
data = pd.read_csv('.. /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['Other_women_in_family'] = other_women_in_family(train, total_data)
 test['Other_women_in_family'] = other_women_in_family(test, total_data )<compute_test_metric>
class DataGenerator:
def __init__(self):
self.image_generator = ImageDataGenerator(rescale=1./ 255,
vertical_flip=True,
horizontal_flip=True,
rotation_range=180,
fill_mode='reflect')
 def create_train(self, dataset_info, batch_size, shape, augument=True):
assert shape[2] == 3
while True:
random_indexes = np.random.choice(len(dataset_info), batch_size)
 batch_images1 = np.empty(( batch_size, shape[0], shape[1], shape[2]))
batch_images2 = np.empty(( batch_size, shape[0], shape[1], shape[2]))
batch_labels = np.zeros(( batch_size, 28))
for i, idx in enumerate(random_indexes):
image1, image2 = self.load_image(
 dataset_info[idx]['path'], shape)
 batch_images1[i] = image1
batch_images2[i] = image2
batch_labels[i][dataset_info[idx]['labels']] = 1
yield [batch_images1, batch_images2], batch_labels
def load_image(self, path, shape):
image_red_ch = skimage.io.imread(path+'_red.png')
 image_yellow_ch = skimage.io.imread(path+'_yellow.png')
 image_green_ch = skimage.io.imread(path+'_green.png')
 image_blue_ch = skimage.io.imread(path+'_blue.png')
 image1 = np.stack((
image_red_ch,
image_yellow_ch,
image_blue_ch), -1)
 image2 = np.stack((
image_green_ch,
image_green_ch,
image_green_ch), -1)
 image1 = resize(image1,(shape[0], shape[1], 3), mode='reflect')
 image2 = resize(image2,(shape[0], shape[1], 3), mode='reflect')
 return image1.astype(np.float), image2.astype(np.float )
def is_oldest_woman(data, match_data):
match_list = []
for i in range(len(data)) :
if(data.Sex[i] == 1):
match =(data.loc[i].Age == max(match_data[(match_data['Last_Name'] == data['Last_Name'][i])\
&(data['Family_size'][i] == match_data['Family_size'])\
&(data['Pclass'][i] == match_data['Pclass'])].Age)) *1
match_list.append(match)
 else:
match_list.append(0)
 return match_list<feature_engineering>
train_datagen = DataGenerator()
generator = train_datagen.create_train(
 train_dataset_info, 5,(299,299,3))
train['Is_oldest_woman'] = is_oldest_woman(train, total_data)
 test['Is_oldest_woman'] = is_oldest_woman(test, total_data )<feature_engineering>
data['target_list'] = data['Target'].map(lambda x: [int(a)for a in x.split(' ')])
 all_labels = list(chain.from_iterable(data['target_list'].values))
c_val = Counter(all_labels)
 n_keys = c_val.keys()
max_idx = max(n_keys)
 data['target_vec'] = data['target_list'].map(lambda ck: [i in ck for i in range(max_idx+1)])
 train_df, valid_df = train_test_split(data,
test_size = 0.2,
stratify = data['Target'].map(lambda x: x[:3] if '27' not in x else '0'), random_state=42)
 print(train_df.shape[0], 'training masks')
 print(valid_df.shape[0], 'validation masks')
 train_df.to_csv('train_part.csv')
 valid_df.to_csv('valid_part.csv')
for column in ['Is_oldest_woman', 'Other_women_in_family']:
get_categorical_feature_info(train, column=column, y_column='Survived', positive_state='Survived', negative_state='Dead' )<compute_test_metric>
train_dataset_info = []
for name, labels in zip(train_df['Id'], train_df['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)
 valid_dataset_info = []
for name, labels in zip(valid_df['Id'], valid_df['Target'].str.split(' ')) :
valid_dataset_info.append({
'path':os.path.join(path_to_train, name),
'labels':np.array([int(label)for label in labels])})
 valid_dataset_info = np.array(valid_dataset_info)
 print(train_dataset_info.shape, valid_dataset_info.shape )
def is_youngest_boy(data, match_data):
match_list = []
for i in range(len(data)) :
if(data.Sex[i] == 0):
match =(data.loc[i].Age <= min(match_data[(match_data['Last_Name'] == data['Last_Name'][i])\
&(data['Family_size'][i] == match_data['Family_size'])\
&(data['Pclass'][i] == match_data['Pclass'])].Age)) *1
match_list.append(match)
 else:
match_list.append(0)
 return match_list<feature_engineering>
def create_model(input_shape, n_out):
inp_image = Input(shape=input_shape)
 inp_mask = Input(shape=input_shape)
 pretrain_model_image = Xception(
 include_top=False,
weights='imagenet',
pooling='max')
 pretrain_model_image.name='xception_image'
pretrain_model_mask = Xception(
 include_top=False,
weights='imagenet',
pooling='max')
 pretrain_model_mask.name='xception_mask'
x = Multiply()([pretrain_model_image(inp_image), pretrain_model_mask(inp_mask)])
 out = Dense(n_out, activation='sigmoid' )(x)
 model = Model(inputs=[inp_image, inp_mask], outputs=[out])
 return model
def is_oldest_man(data, match_data):
match_list = []
for i in range(len(data)) :
if(data.Sex[i] == 0):
match =(data.loc[i].Age == max(match_data[(match_data['Last_Name'] == data['Last_Name'][i])\
&(data['Family_size'][i] == match_data['Family_size'])\
&(data['Pclass'][i] == match_data['Pclass'])].Age)) *1
match_list.append(match)
 else:
match_list.append(0)
 return match_list<merge>
def f1(y_true, y_pred):
y_pred = K.round(y_pred)
 tp = K.sum(K.cast(y_true*y_pred, 'float'), axis=0)
 tn = K.sum(K.cast(( 1-y_true)*(1-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 man_with_woman(data, match_data):
match_list = []
for i in range(len(data)) :
if(data.Sex[i] == 0)&(data.Title[i] != 5):
match =(match_data[(match_data['Last_Name'] == data['Last_Name'][i])\
&(data['Family_size'][i] == match_data['Family_size'])\
&(data['Pclass'][i] == match_data['Pclass'])].Sex.sum())
 if match > 1:
match_list.append(1)
 else:
match_list.append(0)
 else:
match_list.append(0)
 return match_list<merge>
keras.backend.clear_session()
model = create_model(
 input_shape=(299,299,3),
n_out=28)
 model.compile(
 loss='binary_crossentropy',
optimizer='adam',
metrics=['acc', f1])
 model.summary()
def man_with_other_men(data, match_data):
match_list = []
for i in range(len(data)) :
if data.Sex[i] == 0:
match = len(match_data[(match_data['Last_Name'] == data['Last_Name'][i])\
&(data['Family_size'][i] == match_data['Family_size'])\
&(data['Pclass'][i] == match_data['Pclass'])\
&(match_data['Sex'] == 0)].Sex)
 if match > 1:
match_list.append(1)
 else:
match_list.append(0)
 else:
match_list.append(0)
 return match_list<categorify>
epochs = 4; batch_size = 12
checkpointer = ModelCheckpoint(
 '.. /working/Xception.model',
verbose=2,
save_best_only=False)
 train_generator = train_datagen.create_train(
 train_dataset_info, batch_size,(299,299,3))
validation_generator = train_datagen.create_train(
 valid_dataset_info, batch_size,(299,299,3))
K.set_value(model.optimizer.lr, 0.0002)
 history = model.fit_generator(
 train_generator,
steps_per_epoch=len(train_df)//batch_size,
validation_data=validation_generator,
validation_steps=len(valid_df)//batch_size//10,
epochs=epochs,
verbose=1,
callbacks=[checkpointer] )
train['Is_youngest_man'] = is_youngest_boy(train, total_data)
 test['Is_youngest_man'] = is_youngest_boy(test, total_data )<feature_engineering>
submit = pd.read_csv('.. /input/sample_submission.csv' )
train['Is_oldest_man'] = is_oldest_man(train, total_data)
 test['Is_oldest_man'] = is_oldest_man(test, total_data )<feature_engineering>
%%time
predicted = []
for name in tqdm(submit['Id']):
path = os.path.join('.. /input/test/', name)
 image1, image2 = train_datagen.load_image(path,(299,299,3))
score_predict = model.predict([image1[np.newaxis], image2[np.newaxis]])[0]
label_predict = np.arange(28)[score_predict>=0.5]
str_predict_label = ' '.join(str(l)for l in label_predict)
 predicted.append(str_predict_label )
train['Man_with_woman'] = man_with_woman(train, total_data)
 test['Man_with_woman'] = man_with_woman(test, total_data )<feature_engineering>
submit['Predicted'] = predicted
submit.to_csv('submission.csv', index=False )
train['Man_with_other_men'] = man_with_woman(train, total_data)
 test['Man_with_other_men'] = man_with_woman(test, total_data )<prepare_x_and_y>
name_label_dict = {
0: "Nucleoplasm",
1: "Nuclear membrane",
2: "Nucleoli",
3: "Nucleoli fibrillar center",
4: "Nuclear speckles",
5: "Nuclear bodies",
6: "Endoplasmic reticulum",
7: "Golgi apparatus",
8: "Peroxisomes",
9: "Endosomes",
10: "Lysosomes",
11: "Intermediate filaments",
12: "Actin filaments",
13: "Focal adhesion sites",
14: "Microtubules",
15: "Microtubule ends",
16: "Cytokinetic bridge",
17: "Mitotic spindle",
18: "Microtubule organizing center",
19: "Centrosome",
20: "Lipid droplets",
21: "Plasma membrane",
22: "Cell junctions",
23: "Mitochondria",
24: "Aggresome",
25: "Cytosol",
26: "Cytoplasmic bodies",
27: "Rods & rings"
}
<filter><EOS> 
submit['target_list'] = submit['Predicted'].map(lambda x: [int(a)for a in str(x ).split(' ')])
 submit['target_vec'] = submit['target_list'].map(lambda ck: [i in ck for i in range(max_idx+1)])
 all_labels = list(chain.from_iterable(submit['target_list'].values))
c_val = Counter(all_labels)
 n_keys = c_val.keys()
max_idx = max(n_keys)
 for k,v in name_label_dict.items() :
print(v, 'count:', c_val[k] if k in c_val else 0 )