Kaggle competition: Open Images 2019 - Object Detection
<SOS> metric: OpenImagesObjectDetectionAP Kaggle data source: open-images-2019-object-detection<compute_test_metric>
import os
import numpy as np
import pandas as pd
import tensorflow as tf
import tensorflow_hub as th
import matplotlib.pyplot as pl
from pprint import pprint as pp
from six import BytesIO as io
from tqdm import tqdm
from PIL import Image, ImageColor, ImageDraw, ImageFont, ImageOps
class OptimizedRounder(object):
def __init__(self):
self.coef_ = 0
def _kappa_loss(self, coef, X, y):
X_p = np.copy(X)
 for i, pred in enumerate(X_p):
if pred < coef[0]:
X_p[i] = 0
elif pred >= coef[0] and pred < coef[1]:
X_p[i] = 1
elif pred >= coef[1] and pred < coef[2]:
X_p[i] = 2
elif pred >= coef[2] and pred < coef[3]:
X_p[i] = 3
else:
X_p[i] = 4
ll = metrics.cohen_kappa_score(y, X_p, weights='quadratic')
 return -ll
def fit(self, X, y):
loss_partial = partial(self._kappa_loss, X=X, y=y)
 initial_coef = [0.5, 1.5, 2.5, 3.5]
self.coef_ = sp.optimize.minimize(loss_partial, initial_coef, method='nelder-mead')
 print(-loss_partial(self.coef_['x']))
def predict(self, X, coef):
X_p = np.copy(X)
 for i, pred in enumerate(X_p):
if pred < coef[0]:
X_p[i] = 0
elif pred >= coef[0] and pred < coef[1]:
X_p[i] = 1
elif pred >= coef[1] and pred < coef[2]:
X_p[i] = 2
elif pred >= coef[2] and pred < coef[3]:
X_p[i] = 3
else:
X_p[i] = 4
return X_p
def coefficients(self):
return self.coef_['x']<train_model>
def pred(img_id, rs):
pred_str = []
for i in range(len(rs['detection_scores'])) :
cn = rs['detection_class_names'][i].decode("utf-8")
 YMin,XMin,YMax,XMax = rs['detection_boxes'][i]
sc = rs['detection_scores'][i]
pred_str.append(
 f"{cn} {sc} {XMin} {YMin} {XMax} {YMax}" 
)
 pred_str = " ".join(pred_str)
 return {
"ImageID": img_id,
"PredictionString": pred_str
}
def display_image(image):
fig = pl.figure(figsize =(20, 15))
pl.grid(False)
 pl.axis('off')
 pl.imshow(image )
optR = OptimizedRounder()
optR.fit(valid_preds[0],valid_preds[1] )<load_from_csv>
def bound_bx_img(image,
ymin,
xmin,
ymax,
xmax,
color,
font,
thickness = 4,
display_str_list =()):
drw = ImageDraw.Draw(image)
 im_width, im_height = image.size 
(left, right, top, bottom)=(xmin * im_width, xmax * im_width,
ymin * im_height, ymax * im_height)
 drw.line([(left, top),(left, bottom),(right, bottom),(right, top), 
(left, top)],
width = thickness,
fill = color)
 display_str_heights = [font.getsize(ds)[1] for ds in display_str_list]
total_display_str_height =(1 + 2 * 0.05)* sum(display_str_heights)
 if top > total_display_str_height:
txt_bm = top
else:
txt_bm = bottom + total_display_str_height
for display_str in display_str_list[::-1]:
txt_width, txt_height = font.getsize(display_str)
 margin = np.ceil(0.05 * txt_height)
 drw.rectangle([(left, txt_bm - txt_height - 2 * margin), 
(left + txt_width, txt_bm)],
fill = color)
 drw.text(( left + margin, txt_bm - txt_height - margin),
display_str,
fill = "black",
font = font)
 txt_bm -= txt_height - 2 * margin
sample_df = pd.read_csv('.. /input/aptos2019-blindness-detection/sample_submission.csv')
 sample_df.head()<define_variables>
sm_img_pth = ".. /input/open-images-2019-object-detection/test/4fce161b84175459.jpg"
with tf.Graph().as_default() :
img_str_ph = tf.placeholder(tf.string)
 do_img = tf.image.decode_jpeg(img_str_ph)
 de_img_fl = tf.image.convert_image_dtype(
 image = do_img, dtype = tf.float32 
)
 img_tf = tf.expand_dims(de_img_fl, 0)
 mdl_url = "https://tfhub.dev/google/openimages_v4/ssd/mobilenet_v2/1"
dtr = th.Module(mdl_url)
 dtr_ot = dtr(img_tf, as_dict = True)
 init_ops = [tf.global_variables_initializer() , tf.tables_initializer() ]
ss = tf.Session()
ss.run(init_ops)
 with tf.gfile.Open(sm_img_pth, "rb")as bf :
img_str = bf.read()
rs_ot, img_ot = ss.run(
 [dtr_ot, do_img],
feed_dict = {img_str_ph: img_str}
)
learn.data.add_test(ImageList.from_df(sample_df,'.. /input/aptos2019-blindness-detection',folder='test_images',suffix='.png'))<feature_engineering>
img_str_ph = tf.placeholder(tf.string)
 de_img = tf.image.decode_jpeg(img_str_ph)
 de_img_fl = tf.image.convert_image_dtype(
 image = de_img, dtype = tf.float32 
)
 img_ts = tf.expand_dims(de_img_fl, 0 )
preds,y = learn.TTA(ds_type=DatasetType.Test )<predict_on_test>
mdl_url = "https://tfhub.dev/google/openimages_v4/ssd/mobilenet_v2/1"
dc = th.Module(mdl_url)
 dc_ot = dc(img_ts, as_dict = True )
test_predictions = optR.predict(preds, coefficients )<data_type_conversions>
with tf.gfile.Open(sm_img_pth, "rb")as bf :
img_str = bf.read()
rs_ot, img_ot = ss.run(
 [dc_ot, de_img],
feed_dict = {img_str_ph: img_str}
)
sample_df.diagnosis = test_predictions.astype(int)
 sample_df.head()<save_to_csv>
sm_sub = pd.read_csv('.. /input/open-images-2019-object-detection/sample_submission.csv')
 img_ids = sm_sub['ImageId']
preds = []
for img_id in tqdm(img_ids):
img_ph = f'.. /input/open-images-2019-object-detection/test/{img_id}.jpg'
with tf.gfile.Open(img_ph, "rb")as bf :
img_str = bf.read()
rs_ot = ss.run(
 dc_ot,
feed_dict = {img_str_ph: img_str} 
)
 preds.append(pred(img_id, rs_ot))
ss.close()
sample_df.to_csv('submission.csv',index=False )<set_options>
submission = pd.DataFrame(preds)
 submission.to_csv('submission.csv', index=False )