Kaggle competition: Leaf Classification
<SOS> metric: MulticlassLoss Kaggle data source: leaf-classification<load_from_csv>
%matplotlib inline
target_size =(256, 256)
 grayscale = True
train_path = '.. /input/train.csv'
test_path = '.. /input/test.csv'
submission_path = '.. /input/sample_submission.csv'
submission_output = './submission.csv'
def load_image(id):
img_path = '.. /input/images/%d.jpg' %(id,)
 img = image.load_img(img_path,
grayscale=grayscale)
 img.thumbnail(target_size)
 bg = Image.new('L', target_size,(0,))
bg.paste(
 img,(int(( target_size[0] - img.size[0])/ 2), int(( target_size[1] - img.size[1])/ 2)) 
)
 img_arr = image.img_to_array(bg)
 return img_arr
model1_bert_base_uncased_pred_df = pd.read_csv("model1_bert_base_uncased_pred.csv")
 model2_bert_base_cased_pred_df = pd.read_csv("model2_bert_base_cased_pred.csv")
 model4_bart_large_pred_df = pd.read_csv("model4_bart_large_pred.csv" )<load_from_csv>
train_data = pd.read_csv(train_path)
 x_ids = train_data.iloc[:, 0]
x_images = list()
for i in x_ids:
x_images.append(load_image(i))
x_images = np.array(x_images)
 plt.imshow(x_images[0].squeeze())
 plt.show()
print('Shape of images', x_images[0].shape)
 x_features = train_data.iloc[:, 2:].values
print('Number of features', x_features.shape[1])
 y = train_data['species']
le = LabelEncoder()
le.fit(y)
 y = le.transform(y)
 nb_classes = len(le.classes_)
 print('Number of classes', nb_classes)
 print('Number of instances', len(y))
plt.hist(y, bins=nb_classes)
 plt.title('Number of instances in each class')
 plt.xlabel('Class id')
 plt.ylabel('Number of instances')
 plt.show()
y = np_utils.to_categorical(y)
 test_data = pd.read_csv(test_path)
 test_ids = test_data.iloc[:, 0]
test_images = list()
for i in test_ids:
test_images.append(load_image(i))
test_images = np.array(test_images)
 submission_data = pd.read_csv(submission_path )
roberta_base_dfs = [pd.read_csv(
 os.path.join("roberta-base-output", "fold-{}.csv".format(fold)))
 for fold in range(5)]
model3_roberta_pred_df = roberta_base_dfs[0].copy()
for col in target_columns:
model3_roberta_pred_df[col] = np.mean([df[col] for df in roberta_base_dfs], axis=0 )<feature_engineering>
sss = StratifiedShuffleSplit(10, 0.2, random_state=15)
 for train_index, test_index in sss.split(x_images, y):
	x_train_images, x_test_images, x_train_features, x_test_features = x_images[train_index], x_images[test_index], x_features[train_index], x_features[test_index]
	y_train, y_test = y[train_index], y[test_index]
print('Shape of x train images', x_train_images.shape)
 print('Shape of x train features', x_train_features.shape)
 print('Shape of y train', y_train.shape)
 print('Shape of x test images', x_test_images.shape)
 print('Shape of x test features', x_test_features.shape)
 print('Shape of y test', y_test.shape )
blended_df = model3_roberta_pred_df.copy()
for col in target_columns:
blended_df[col] =(
 model1_bert_base_uncased_pred_df[col] * 0.1 +
model2_bert_base_cased_pred_df[col] * 0.2 +
model3_roberta_pred_df[col] * 0.1 +
model4_bart_large_pred_df[col] * 0.3
)<feature_engineering>
def construct_feature_model() :
print('Contructing the model')
 model1 = Sequential([
Dense(nb_classes * 2, input_shape=x_train_features.shape[1:]),
BatchNormalization() ,
Activation('tanh'),
Dropout(0.25)
 ])
 model2 = Sequential([
Convolution2D(32, 3, 3, border_mode='same',
input_shape=x_images.shape[1:]),
Activation('tanh'),
MaxPooling2D(pool_size=(2, 2), strides=(2, 2)) ,
Dropout(0.5),
Flatten() ,
Dense(nb_classes),
Activation('tanh')
 ])
 model = Sequential([
Merge([model1, model2], mode='concat', concat_axis=1),
Dense(nb_classes * 2),
Activation('tanh'),
Dropout(0.5),
Dense(nb_classes),
Activation('softmax')
 ])
 optimizer = RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0)
 model.compile(optimizer=optimizer,
loss='categorical_crossentropy',
metrics=['accuracy'])
 print('Finish construction of the model')
 return model
model = construct_feature_model()
def postprocess_single(target, ref):

 ids = np.argsort(target)
 counts = sorted(Counter(ref ).items() , key=lambda s: s[0])
 scores = np.zeros_like(target)
 last_pos = 0
v = 0
for value, count in counts:
next_pos = last_pos + int(round(count / len(ref)* len(target)))
 if next_pos == last_pos:
next_pos += 1
cond = ids[last_pos:next_pos]
scores[cond] = v
last_pos = next_pos
v += 1
return scores / scores.max()<compute_test_metric>
print('Start to fit')
 s = time.time()
best_model_file = 'leaf.h5'
best_model_cb = ModelCheckpoint(best_model_file, monitor='val_loss', verbose=0, save_best_only=True)
 batch_size = 32
nb_epoch = 8
verbose = 2
callbacks = [best_model_cb]
validation_split = 0.0
validation_data =([x_test_features, x_test_images], y_test)
 shuffle = True
class_weight = None
sample_weight = None
data_augmentation = False
if not data_augmentation:
print('Not using data augmentation')
 history = model.fit([x_features, x_images], y,
batch_size=batch_size,
nb_epoch=nb_epoch,
verbose=verbose,
callbacks=callbacks,
validation_split=validation_split,
validation_data=validation_data,
shuffle=shuffle,
class_weight=class_weight,
sample_weight=sample_weight)
 print('Finish fitting
Fitting time', time.time() - s )
def postprocess_prediction(prediction, actual):
postprocessed = prediction.copy()
for col in target_columns:
scores = postprocess_single(prediction[col].values, actual[col].values)
 if col in(
 "question_conversational",
"question_type_compare",
"question_type_definition",
"question_type_entity",
"question_has_commonly_accepted_answer",
"question_type_consequence",
"question_type_spelling" 
):
postprocessed[col] = scores
v = postprocessed[col].values
postprocessed[col] =(v - v.min())/(v.max() - v.min())
 return postprocessed<predict_on_test>
model = load_model(best_model_file)
 y_prob = model.predict([test_data.iloc[:, 1:].values, test_images])
 submission_data.iloc[:, 1:] = y_prob
submission_data.tail()
f = open(submission_output, 'w')
 f.write(pd.DataFrame(submission_data ).to_csv(index = False))
f.close()
<save_to_csv><EOS>