Kaggle competition: Airbus Ship Detection Challenge
test.to_csv('submissions.csv',index=False )<set_options>
boat_df = pd.read_csv(".. /input/transfer-learning-for-boat-or-no-boat/submission.csv")
 boat_df.head()
warnings.filterwarnings("ignore")
 %load_ext autoreload
%autoreload<define_variables>
fullres_model = load_model('.. /input/u-net-model-with-submission/fullres_model.h5' )
model_path='.'
path='.. /input/'
train_folder=f'{path}train'
test_folder=f'{path}test'
train_lbl=f'{path}train_labels.csv'
ORG_SIZE=96
bs=64
num_workers=None
sz=96<load_from_csv>
ship_dir = '.. /input/airbus-ship-detection/'
train_image_dir = os.path.join(ship_dir, 'train_v2')
 test_image_dir = os.path.join(ship_dir, 'test_v2')
 def predict(img, path=test_image_dir):
c_img = imread(os.path.join(path, c_img_name))
c_img = np.expand_dims(c_img, 0)/ 255.0
cur_seg = fullres_model.predict(c_img)[0]
cur_seg = binary_opening(cur_seg > 0.9, np.expand_dims(disk(2), -1))
return cur_seg, c_img
def multi_rle_encode(img, **kwargs):

 labels = label(img)
 if img.ndim > 2:
return [rle_encode(np.sum(labels==k, axis=2), **kwargs)for k in np.unique(labels[labels>0])]
else:
return [rle_encode(labels==k, **kwargs)for k in np.unique(labels[labels>0])]
def rle_encode(img, min_max_threshold=1e-3, max_mean_threshold=None):

 if np.max(img)< min_max_threshold:
return ''
if max_mean_threshold and np.mean(img)> max_mean_threshold:
return ''
pixels = img.T.flatten()
pixels = np.concatenate([[0], pixels, [0]])
 runs = np.where(pixels[1:] != pixels[:-1])[0] + 1
runs[1::2] -= runs[::2]
return ' '.join(str(x)for x in runs)
 def pred_encode(img, **kwargs):
cur_seg, _ = predict(img)
 cur_rles = multi_rle_encode(cur_seg, **kwargs)
 return [[img, rle] for rle in cur_rles if rle is not None]
df_trn=pd.read_csv(train_lbl )<feature_engineering>
out_pred_rows = []
for c_img_name in tqdm_notebook(boat_df.ImageId[is_boat]):
out_pred_rows += pred_encode(c_img_name, min_max_threshold=1.0 )
tfms = get_transforms(do_flip=True, flip_vert=True, max_rotate=.0, max_zoom=.1,
max_lighting=0.05, max_warp=0.)<load_from_csv>
sub = pd.DataFrame(out_pred_rows)
 sub.columns = ['ImageId', 'EncodedPixels']
sub = sub[sub.EncodedPixels.notnull() ]
sub.head()
data = ImageDataBunch.from_csv(path,csv_labels=train_lbl,folder='train', ds_tfms=tfms, size=sz, suffix='.tif',test=test_folder,bs=bs);
stats=data.batch_stats()
data.normalize(stats )<define_variables>
def rle_decode(mask_rle, shape=(768, 768)) :

 s = mask_rle.split()
starts, lengths = [np.asarray(x, dtype=int)for x in(s[0:][::2], s[1:][::2])]
starts -= 1
ends = starts + lengths
img = np.zeros(shape[0]*shape[1], dtype=np.uint8)
 for lo, hi in zip(starts, ends):
img[lo:hi] = 1
return img.reshape(shape ).T
def masks_as_color(in_mask_list):
all_masks = np.zeros(( 768, 768), dtype = np.float)
 scale = lambda x:(len(in_mask_list)+x+1)/(len(in_mask_list)*2)
 for i,mask in enumerate(in_mask_list):
if isinstance(mask, str):
all_masks[:,:] += scale(i)* rle_decode(mask)
 return all_masks
data.show_batch(rows=5, figsize=(12,9))<import_modules>
TOP_PREDICTIONS = 6
fig, m_axs = plt.subplots(TOP_PREDICTIONS, 3, figsize =(14, TOP_PREDICTIONS*4))
[c_ax.axis('off')for c_ax in m_axs.flatten() ]
def raw_prediction(img, path=test_image_dir):
c_img = imread(os.path.join(path, c_img_name))
c_img = np.expand_dims(c_img, 0)/255.0
cur_seg = fullres_model.predict(c_img)[0]
return cur_seg, c_img[0]
for(ax1, ax2, ax3), c_img_name in zip(m_axs, sub.ImageId.unique() [:TOP_PREDICTIONS]):
pred, c_img = raw_prediction(c_img_name)
 ax1.imshow(c_img)
 ax1.set_title('Image: ' + c_img_name)
 ax2.imshow(pred[...,0], cmap=get_cmap('jet'))
ax2.set_title('Prediction')
 ax3.imshow(masks_as_color(sub.query('ImageId=="{}"'.format(c_img_name)) ['EncodedPixels']))
ax3.set_title('Masks' )
<compute_test_metric><EOS> 
sub1 = pd.read_csv('.. /input/airbus-ship-detection/sample_submission_v2.csv')
 sub1 = pd.DataFrame(np.setdiff1d(sub1['ImageId'].unique() , sub['ImageId'].unique() , assume_unique=True), columns=['ImageId'])
 sub1['EncodedPixels'] = None
print(len(sub1), len(sub))
sub = pd.concat([sub, sub1])
 print(len(sub))
sub.to_csv('blended_submission.csv', index=False)
 sub.head()