{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nimport os\n# Any results you write to the current directory are saved as output.\n\n\nfrom matplotlib.pyplot import imshow\nimport matplotlib.pyplot as plt\nimport imageio\nfrom skimage.data import imread\nimport math\nfrom matplotlib.pylab import *\nfrom tqdm import tqdm\nfrom keras.models import *\nfrom keras.layers import *\nfrom keras.optimizers import *\nfrom keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau, LearningRateScheduler\nfrom keras import backend as K\nfrom keras.preprocessing.image import load_img","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c64fd4ffbb6330c01b3090a5969900af1e6d1fd5"},"cell_type":"code","source":"print(os.listdir(\"../input\"))","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"train_seg = pd.read_csv('../input/train_ship_segmentations_v2.csv')\n#test_seg = pd.read_csv('../input/test_ship_segmentations_v2.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cba54ef6509160b8e9d8d38348d5ed42c3bba00c"},"cell_type":"code","source":"train = os.path.join('../input','train_v2')\n#test = os.listdir('../input/test')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a8c64b6976612d30fb921bd2b49ca654dada367d"},"cell_type":"code","source":"#train = pd.read_csv(os.path.join('../input','train_v2'))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5ade0d7115b75172fc7682674bb40376b241766e"},"cell_type":"code","source":"train_seg.loc[0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"113190433b9d3a68748a37b2cb2af600594da719"},"cell_type":"code","source":"train","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2caae64e24e1590a0a21375eca2a651512a04090"},"cell_type":"code","source":"def run_len_decode(mask,shape=(768,768)):\n    s = mask.split()\n    start,length = [np.asarray(x,dtype=int) for x in (s[0:][::2],s[1:][::2])]\n    start -=1\n    end = start+length\n    img = np.zeros(shape[0]*shape[1],dtype=np.uint8)\n    for lo,hi in zip(start,end):\n        img[lo:hi] = 1\n    return img.reshape(shape).T","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ae49911a2f85b52b1eb3a89baa841861a68a4985"},"cell_type":"code","source":"def make_mask(img_mask):\n    tot_mask = np.zeros((768,768))\n    img_mask = img_mask.tolist()\n    if not nan in img_mask:\n        for mask in img_mask:\n            tot_mask+=run_len_decode(mask)\n    return(tot_mask)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3fe0907f91359e3b1f79ab33282b6de51b1ba73c"},"cell_type":"code","source":"def show(num,imgage=False):\n    idx = train_seg.loc[num]['ImageId']\n    img = imread('../input/train_v2/'+idx)\n    fig,ax = plt.subplots(1,3,figsize=(15,40))\n    tot_mask = np.zeros((768,768))\n    img_mask = train_seg.loc[train_seg['ImageId']==idx,'EncodedPixels'].tolist()\n    if not nan in img_mask:\n        for mask in img_mask:\n            tot_mask+=run_len_decode(mask)\n    ax[0].axis('off')\n    ax[1].axis('off')\n    ax[2].axis('off')\n    ax[0].imshow(img)\n    ax[1].imshow(tot_mask)\n    ax[2].imshow(img)\n    ax[2].imshow(tot_mask, alpha = 0.4)\n    print(\"image shape: \",img.shape)\n    print(\"output shap: \",tot_mask.shape)\n    if imgage:\n        return tot_mask","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"65a116e32d401c842b724b288bff54a2d9ca2d5d"},"cell_type":"code","source":"import random","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4f30093b6a42148bfffd7b76c692f0fe89dc1246"},"cell_type":"code","source":"random.randint(1,100)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1b8b37ced1c93ddcf279902a8fe2da43972cd321","scrolled":true},"cell_type":"code","source":"k = show(random.randint(1,100),True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2373756c5f7e951e390be8fa0674c9a5ffc868d7"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8ae93a54fff2d4fac3ee5c5c79d1951756705841"},"cell_type":"code","source":"def run_len_ecoding(img):\n    px = img.T.flatten()\n    px = np.concatenate([[0],px,[0]])\n    px = where(px[1:]!=px[:-1])[0]+1\n    px[1::2]-=px[::2]\n    return ' '.join(str(x) for x in px)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6d378311e1c9f2d8decaea18a2777c774ccd641d"},"cell_type":"code","source":"imshow(run_len_decode(run_len_ecoding(k)))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"487e5042c5aa794a85235fc96d47419565a9e19b"},"cell_type":"code","source":"idx = train_seg.loc[30]['ImageId']\nimg = imread('../input/train_v2/'+idx)\nprint(img.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fe5f048a848d7d51262f90fb23edec6553f516ab"},"cell_type":"code","source":"img = load_img('../input/train/'+idx,grayscale=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a4a8eab711011b3c29795759f5c617d55bec6759"},"cell_type":"code","source":"imshow(img)\nprint(img.size)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7a87dbdbca619ebe0158d7e206a9f8caf0b96c2b"},"cell_type":"code","source":"def unet(pretrained_weights=None,input_size=(768,768,3)):\n    inputs = Input(input_size)\n    conv1 = Conv2D(32,(3,3),activation='relu',padding='same',kernel_initializer='he_normal')(inputs)\n    conv1 = Conv2D(32,(3,3),activation='relu',padding='same',kernel_initializer='he_normal')(conv1)\n    pool1 = MaxPooling2D(pool_size=(2,2))(conv1)\n    \n    conv2 = Conv2D(64,(3,3),activation='relu',padding='same',kernel_initializer='he_normal')(pool1)\n    conv2 = Conv2D(64,(3,3),activation='relu',padding='same',kernel_initializer='he_normal')(conv2)\n    pool2 = MaxPooling2D(pool_size=(2,2))(conv2)\n    \n    conv3 = Conv2D(128,(3,3),activation='relu',padding='same',kernel_initializer='he_normal')(pool2)\n    conv3 = Conv2D(128,(3,3),activation='relu',padding='same',kernel_initializer='he_normal')(conv3)\n    pool3 = MaxPooling2D(pool_size=(2,2))(conv3)\n    \n    conv4 = Conv2D(256,(3,3),activation='relu',padding='same',kernel_initializer='he_normal')(pool3)\n    conv4 = Conv2D(256,(3,3),activation='relu',padding='same',kernel_initializer='he_normal')(conv4)\n    drop4 = Dropout(0.5)(conv4)\n    pool4 = MaxPooling2D(pool_size=(2,2))(drop4)\n    \n    conv5 = Conv2D(512,(3,3),activation='relu',padding='same',kernel_initializer='he_normal')(pool4)\n    conv5 = Conv2D(512,(3,3),activation='relu',padding='same',kernel_initializer='he_normal')(conv5)\n    drop5 = Dropout(0.5)(conv5)\n    \n#     up6 = Conv2D(512, 2, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(UpSampling2D(size = (2,2))(drop5))\n    up6 = Conv2D(256,(2,2),activation='relu',padding='same',kernel_initializer='he_normal')(UpSampling2D(size=(2,2))(drop5))\n#     merge6 = merge([drop4,up6],mode='concat',concat_axis=3)\n    merge6 = concatenate([drop4,up6])\n    conv6 = Conv2D(256,(3,3),activation='relu',padding='same',kernel_initializer='he_normal')(merge6)\n    conv6 = Conv2D(256,(3,3),activation='relu',padding='same',kernel_initializer='he_normal')(conv6)\n    \n    up7 = Conv2D(128,(2,2),activation='relu',padding='same',kernel_initializer='he_normal')(UpSampling2D(size=(2,2))(conv6))\n#     merge7 = merge([conv3,up7],mode='concat',concat_axis=3)\n    merge7 = concatenate([conv3,up7])\n    conv7 = Conv2D(128,(3,3),activation='relu',padding='same',kernel_initializer='he_normal')(merge7)\n    conv7 = Conv2D(128,(3,3),activation='relu',padding='same',kernel_initializer='he_normal')(conv7)\n    \n    up8 = Conv2D(64,(2,2),activation='relu',padding='same',kernel_initializer='he_normal')(UpSampling2D(size=(2,2))(conv7))\n#     merge8 = merge([conv2,up8], mode='concat',concat_axis=3)\n    merge8 = concatenate([conv2,up8])\n    conv8 = Conv2D(64,(3,3),activation='relu',padding='same',kernel_initializer='he_normal')(merge8)\n    conv8 = Conv2D(64,(3,3),activation='relu',padding='same',kernel_initializer='he_normal')(conv8)\n    \n    up9 = Conv2D(32,(2,2),activation='relu',padding='same',kernel_initializer='he_normal')(UpSampling2D(size=(2,2))(conv8))\n#     merge9 = merge([conv1,up9],mode='concat',concat_axis=3)\n    merge9 = concatenate([conv1,up9])\n    conv9 = Conv2D(32,(3,3),activation='relu',padding='same',kernel_initializer='he_normal')(merge9)\n    conv9 = Conv2D(32,(3,3),activation='relu',padding='same',kernel_initializer='he_normal')(conv9)\n    conv9 = Conv2D(32,(3,3),activation='relu',padding='same',kernel_initializer='he_normal')(conv9)\n    conv10 = Conv2D(1,1,activation='sigmoid')(conv9)\n    \n    model = Model(input=inputs,output=conv10)\n    \n    model.compile(optimizer=Adam(lr=1e-4),loss='binary_crossentropy',metrics=['accuracy'])\n    \n    #model.summary()\n    if(pretrained_weights):\n        model.load_weights(pretrained_weights)\n    \n    return model\n    \n    ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"164f48b715750ba60af0e5d05db58d1e5fe576c2"},"cell_type":"code","source":"k = unet()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7f83e43695d6d9ef5b21f7e2e6edaaffd8872594"},"cell_type":"code","source":"k.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1ea6ce7d44c5f7754251e7803802227880d26558"},"cell_type":"code","source":"os.listdir('../input')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5c9fee52e7c7389d820c2599254945a7fd7fdcd8"},"cell_type":"code","source":"path = os.path.join('../input','train')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3ea695460ffcaecb29d7b74a5f3e22bf9452b2fb"},"cell_type":"code","source":"imshow(imread(os.path.join(path,train_seg.loc[0]['ImageId'])))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4121679afdfc62235414421fa4f227d831681d52"},"cell_type":"code","source":"def givexy():\n    X = np.zeros((4,768,768,3))\n    Y = np.zeros((4,768,768,1))\n    for i,id_ in tqdm(enumerate(train_seg['ImageId'])):\n         X[i,...] = imread(os.path.join(path,id_))\n         k = resize(make_mask(train_seg[train_seg['ImageId']==id_]['EncodedPixels']),(768,768,1))\n         Y[i] = k\n         if i==3:\n                break\n    return X,Y","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"aa324bac8aa073673a217700f6add00ac767abd2"},"cell_type":"code","source":"resize","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4f504404c68473058321832e26cbcfe7c0dcad59"},"cell_type":"code","source":"x,y = givexy()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8549f94f243142b7f18cbc7f89147ee2e6429151"},"cell_type":"code","source":"x.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b1b83a7a2d0fd86d019e2b4400e91f5e7ecad26f"},"cell_type":"code","source":"y.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d90d8233a424bb9e3da96db6f46136e35500f5f9"},"cell_type":"code","source":"callbacks = [\n    EarlyStopping(patience=5, verbose=1),\n    ReduceLROnPlateau(patience=3, verbose=1),\n    ModelCheckpoint('Model1.h5', verbose=1, save_best_only=True, save_weights_only=True)\n]\n\n#results = model.fit({'img': X_train, 'feat': X_feat_train}, y_train, batch_size=16, epochs=50, callbacks=callbacks,\n#                     validation_data=({'img': X_valid, 'feat': X_feat_valid}, y_valid))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a9cf9295e0340a607f5b41eaf6aa3846cb8bc340"},"cell_type":"code","source":"k.fit(x,y,batch_size=10,epochs=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a1aac15192036587fb986068d58761284b0f88de"},"cell_type":"code","source":"k.predict()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"25fd242e72b00b23bf9868a299a6c4832a34c435"},"cell_type":"code","source":"img = ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6d99be05773d0c1dbf245ecef9141eb944e6cebe"},"cell_type":"code","source":"os.listdir('../input')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c2fe7fb208667702a95def10f0a9895eec80960f"},"cell_type":"code","source":"os.path.join('../train','train')","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}