Kaggle competition: Understanding Clouds from Satellite Images
X_train["Title"] = train_data["Title"]
X_dev["Title"] = dev_data["Title"]
X_test["Title"] = test_data["Title"]
X_train.shape, Y_train.shape, X_dev.shape, Y_dev.shape, X_test.shape 
<train_model>
def np_resize(img, input_shape):

 height, width = input_shape
return cv2.resize(img,(width, height))
def mask2rle(img):

 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 rle2mask(rle, input_shape):
width, height = input_shape[:2]
mask= np.zeros(width*height ).astype(np.uint8)
 array = np.asarray([int(x)for x in rle.split() ])
 starts = array[0::2]
lengths = array[1::2]
current_position = 0
for index, start in enumerate(starts):
mask[int(start):int(start+lengths[index])] = 1
current_position += lengths[index]
return mask.reshape(height, width ).T
def build_masks(rles, input_shape, reshape=None):
depth = len(rles)
 if reshape is None:
masks = np.zeros(( *input_shape, depth))
else:
masks = np.zeros(( *reshape, depth))
for i, rle in enumerate(rles):
if type(rle)is str:
if reshape is None:
masks[:, :, i] = rle2mask(rle, input_shape)
 else:
mask = rle2mask(rle, input_shape)
 reshaped_mask = np_resize(mask, reshape)
 masks[:, :, i] = reshaped_mask
return masks
def build_rles(masks, reshape=None):
width, height, depth = masks.shape
rles = []
for i in range(depth):
mask = masks[:, :, i]
if reshape:
mask = mask.astype(np.float32)
 mask = np_resize(mask, reshape ).astype(np.int64)
 rle = mask2rle(mask)
 rles.append(rle)
 return rles
def dice_coef(y_true, y_pred, smooth=1):
y_true_f = K.flatten(y_true)
 y_pred_f = K.flatten(y_pred)
 intersection = K.sum(y_true_f * y_pred_f)
 return(2.* intersection + smooth)/(K.sum(y_true_f)+ K.sum(y_pred_f)+ smooth)
 def dice_loss(y_true, y_pred):
smooth = 1.
 y_true_f = K.flatten(y_true)
 y_pred_f = K.flatten(y_pred)
 intersection = y_true_f * y_pred_f
score =(2.* K.sum(intersection)+ smooth)/(K.sum(y_true_f)+ K.sum(y_pred_f)+ smooth)
 return 1.- score
def bce_dice_loss(y_true, y_pred):
return binary_crossentropy(y_true, y_pred)+ dice_loss(y_true, y_pred)
 def post_process(probability, threshold, min_size):

 mask = cv2.threshold(probability, threshold, 1, cv2.THRESH_BINARY)[1]
num_component, component = cv2.connectedComponents(mask.astype(np.uint8))
predictions = np.zeros(( 350, 525), np.float32)
 num = 0
for c in range(1, num_component):
p =(component == c)
 if p.sum() > min_size:
predictions[p] = 1
num += 1
return predictions, num
def post_process_minsize(mask, min_size):

 num_component, component = cv2.connectedComponents(mask.astype(np.uint8))
predictions = np.zeros(mask.shape)
 num = 0
for c in range(1, num_component):
p =(component == c)
 if p.sum() > min_size:
predictions[p] = 1
num += 1
return predictions
def draw_convex_hull(mask, mode='convex'):
img = np.zeros(mask.shape)
 contours, hier = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
 for c in contours:
if mode=='rect':
x, y, w, h = cv2.boundingRect(c)
 cv2.rectangle(img,(x, y),(x+w, y+h),(255, 255, 255), -1)
 elif mode=='convex':
hull = cv2.convexHull(c)
 cv2.drawContours(img, [hull], 0,(255, 255, 255),-1)
 elif mode=='approx':
epsilon = 0.02*cv2.arcLength(c,True)
 approx = cv2.approxPolyDP(c,epsilon,True)
 cv2.drawContours(img, [approx], 0,(255, 255, 255),-1)
 else:
rect = cv2.minAreaRect(c)
 box = cv2.boxPoints(rect)
 box = np.int0(box)
 cv2.drawContours(img, [box], 0,(255, 255, 255),-1)
 return img/255.
 def dice(img1, img2):
img1 = np.asarray(img1 ).astype(np.bool)
 img2 = np.asarray(img2 ).astype(np.bool)
 intersection = np.logical_and(img1, img2)
 return 2.* intersection.sum() /(img1.sum() + img2.sum())
 def rle_decode(mask_rle, shape=(1400, 2100)) :
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, order='F' )
logit.fit(X_train, Y_train)
 acc_logit_train = round(logit.score(X_train, Y_train)*100, 2)
 acc_logit_dev = round(logit.score(X_dev, Y_dev)*100, 2)
 print(f"logit: train accuracy = {acc_logit_train}, dev accuracy = {acc_logit_dev}" )<train_model>
sub_1 = pd.read_csv('.. /input/submission-examples/sub66842.csv', keep_default_na=True)
 sub_1['ImageId'] = sub_1['Image_Label'].apply(lambda x: x.split('_')[0])
 sub_1['EncodedPixels'].replace({np.nan: ''}, inplace=True)
 sub_1.head(10 )
svc.fit(X_train, Y_train)
 acc_svc_train = round(svc.score(X_train, Y_train)* 100, 2)
 acc_svc_dev = round(svc.score(X_dev, Y_dev)* 100, 2)
 print(f"svc: train accuracy = {acc_svc_train}, dev accuracy = {acc_svc_dev}" )<feature_engineering>
sub_2 = pd.read_csv('.. /input/submission-examples/sub67042.csv', keep_default_na=True)
 sub_2['ImageId'] = sub_2['Image_Label'].apply(lambda x: x.split('_')[0])
 sub_2['EncodedPixels'].replace({np.nan: ''}, inplace=True)
 sub_2.head(10 )
mu = X_train["Fare"].mean()
sigma =(((X_train["Fare"]-mu)**2 ).mean())**0.5
for subset in X_full:
subset["Fare"] =(subset["Fare"] - mu)/sigma
print(mu, sigma )<train_model>
sub_3 = pd.read_csv('.. /input/submission-examples/sub67291.csv', keep_default_na=True)
 sub_3['ImageId'] = sub_3['Image_Label'].apply(lambda x: x.split('_')[0])
 sub_3['EncodedPixels'].replace({np.nan: ''}, inplace=True)
 sub_3.head(10 )