Kaggle competition: Understanding Clouds from Satellite Images
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 = pd.read_csv('.. /input/cloud-classifier-for-post-processing/submission_segmentation_and_classifier.csv')
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}" )<save_to_csv>
class_names = ['Fish', 'Flower', 'Sugar', 'Gravel']
def rle_decode(mask_rle: str = '', 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')
 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 make_mask(df, image_label, shape =(1400, 2100), cv_shape =(525, 350),debug=False):

 if debug:
print(shape,cv_shape)
 df = df.set_index('Image_Label')
 encoded_mask = df.loc[image_label, 'EncodedPixels']
mask = np.zeros(( shape[0], shape[1]), dtype=np.float32)
 if encoded_mask is not np.nan:
mask = rle_decode(encoded_mask,shape=shape)
 return cv2.resize(mask, cv_shape)
 min_size = [10000 ,10000, 10000, 10000]
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
Y_pred = svc.predict(X_test)
 submission = pd.DataFrame({"PassengerId": test_data["PassengerId"], "Survived": Y_pred} ).sort_values(by="PassengerId")
 submission.to_csv("submission.csv", index=False )<import_modules>
mode='convex'
test_imgs_folder = '.. /input/understanding-clouds-resized/test_images_525/test_images_525'
NN=5
folder_images=test_imgs_folder
images_list = ['e0cacb5.jpg', '05fcce5.jpg', 'e949e4b.jpg', 'ce8df6b.jpg', '41be432.jpg']
current_batch = images_list[0: NN]
print(current_batch)
 for i, image_name in enumerate(current_batch):
path = os.path.join(folder_images, image_name)
 print(path)
 img = cv2.imread(path ).astype(np.float32)
 print(img.max() , img.min())
 img = img/255.
 img2 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
 img2 = np.stack([img2,img2,img2],axis=-1)
 img_mask=[]
img_mask_convex=[]
img_mask_convex_minsize=[]
for class_id in range(4):
img_mask.append(make_mask(sub, image_name + '_' + class_names[class_id],shape=(350,525)))
 print(img_mask[class_id].min() , img_mask[class_id].max())
 img_mask_convex.append(draw_convex_hull(img_mask[class_id].astype(np.uint8), mode=mode))
img_mask_convex[class_id][img2[:,:,0]<=2/255.] = 0
img_mask_convex_minsize.append(post_process_minsize(img_mask_convex[class_id], min_size[class_id]))
img3 = draw_masks(img2,img_mask)
 img4 = draw_masks(img2,img_mask_convex_minsize)
 show_Nimages([img2,img3,img4])
 show_Nimages(img_mask)
 show_Nimages(img_mask_convex_minsize )
import os
import json
import numpy as np
import pandas as pd
import keras
from keras import layers
from keras.applications import DenseNet121
from keras.callbacks import Callback, ModelCheckpoint
from keras.preprocessing.image import ImageDataGenerator
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.models import Sequential
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix, f1_score, precision_score, recall_score<load_pretrained>
mode='approx'
test_imgs_folder = '.. /input/understanding-clouds-resized/test_images_525/test_images_525'
NN=5
folder_images=test_imgs_folder
images_list = ['e0cacb5.jpg', '05fcce5.jpg', 'e949e4b.jpg', 'ce8df6b.jpg', '41be432.jpg']
current_batch = images_list[0: NN]
print(current_batch)
 for i, image_name in enumerate(current_batch):
path = os.path.join(folder_images, image_name)
 print(path)
 img = cv2.imread(path ).astype(np.float32)
 print(img.max() , img.min())
 img = img/255.
 img2 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
 img2 = np.stack([img2,img2,img2],axis=-1)
 img_mask=[]
img_mask_convex=[]
img_mask_convex_minsize=[]
for class_id in range(4):
img_mask.append(make_mask(sub, image_name + '_' + class_names[class_id],shape=(350,525)))
 print(img_mask[class_id].min() , img_mask[class_id].max())
 img_mask_convex.append(draw_convex_hull(img_mask[class_id].astype(np.uint8), mode=mode))
img_mask_convex[class_id][img2[:,:,0]<=2/255.] = 0
img_mask_convex_minsize.append(post_process_minsize(img_mask_convex[class_id], min_size[class_id]))
img3 = draw_masks(img2,img_mask)
 img4 = draw_masks(img2,img_mask_convex_minsize)
 show_Nimages([img2,img3,img4])
 show_Nimages(img_mask)
 show_Nimages(img_mask_convex_minsize )
x_train = np.load('.. /input/reducing-image-sizes-to-32x32/X_train.npy')
 x_test = np.load('.. /input/reducing-image-sizes-to-32x32/X_test.npy')
 y_train = np.load('.. /input/reducing-image-sizes-to-32x32/y_train.npy')
 print('x_train shape:', x_train.shape)
 print(x_train.shape[0], 'train samples')
 print(x_test.shape[0], 'test samples' )<data_type_conversions>
model_class_names=['Fish', 'Flower', 'Gravel', 'Sugar']
<compute_train_metric><EOS> 
sub_test=pd.read_csv('./sub_convex.csv')
 sub_test.head(10 )