{"cells":[{"metadata":{"_cell_guid":"fa172ee2-24f7-4896-a2bd-13206e3e15d6","collapsed":true,"_uuid":"d5eedc86575d73253d4918090590286b6c2e1439","trusted":true},"cell_type":"code","source":"import random, os, sys\nimport numpy as np\nimport pandas as pd\n\nimport skimage.io\nimport skimage.transform\nimport skimage.filters\nfrom skimage.feature import corner_peaks\nfrom skimage.morphology import label\n\nfrom scipy.ndimage.interpolation import map_coordinates\nfrom scipy.ndimage.filters import gaussian_filter\n\nfrom matplotlib import pyplot as plt","execution_count":1,"outputs":[]},{"metadata":{"_cell_guid":"4d1f7a6a-b0cc-4c6c-a0ba-70780d243689","collapsed":true,"_uuid":"370eef3fa21effd4871ffc0030c471acbf637f0d","trusted":true},"cell_type":"code","source":"random.seed = 42\nnp.random.seed = 42","execution_count":2,"outputs":[]},{"metadata":{"_cell_guid":"a01d55fb-7907-4aec-93f4-aac0640ef61b","collapsed":true,"_uuid":"edf0747cdfaccbf676d0fec10dd577f57ac6796d","trusted":true},"cell_type":"code","source":"IMG_WIDTH = 192\nIMG_HEIGHT = 192\nN_CHANEL = 3","execution_count":3,"outputs":[]},{"metadata":{"_cell_guid":"0c37eb6f-fc5a-44e7-8fda-9f5b78b4c1a4","collapsed":true,"_uuid":"5d15392db1a98f858cf5ed0ee010d6bb6dda479a","trusted":true},"cell_type":"code","source":"def get_images_and_masks_id(path='../input/stage1_train/'):\n    train_id = next(os.walk(path))[1]\n    list_train_set = []\n    for i in train_id:\n        list_train_set.append({\n            'image_id':i,\n            'masks_id':next(os.walk('{0}/{1}/masks/'.format(path, i)))[2]})\n    return list_train_set","execution_count":4,"outputs":[]},{"metadata":{"_cell_guid":"01c094fa-caa7-4719-85ce-bf20d0df2c06","collapsed":true,"_uuid":"6d042f12f3fbac15fb141bf0891efc218b476649","trusted":true},"cell_type":"code","source":"def stack_masks(masks_np_list):\n    masks = np.mean(masks_np_list.transpose(1,2,0), axis=2)\n    return masks.reshape(masks.shape[0], masks.shape[1], 1)","execution_count":5,"outputs":[]},{"metadata":{"_cell_guid":"e2244221-6daa-444d-b673-792416bfaa3e","collapsed":true,"_uuid":"9a31e62d9b21d649f3c185e481b7b16006e7dfc4","trusted":true},"cell_type":"code","source":"def get_image_and_masks(image_id, list_masks_id, path='../input/stage1_train/'):\n    image_file = \"{0}/{1}/images/{1}.png\".format(path, image_id)\n    image = skimage.io.imread(image_file)\n    masks_list = []\n    for mask_id in list_masks_id:\n        mask_file = \"{0}/{1}/masks/{2}\".format(path, image_id, mask_id)\n        masks_list.append(skimage.io.imread(mask_file))\n    return {'image':image, 'masks':stack_masks(np.array(masks_list))}","execution_count":6,"outputs":[]},{"metadata":{"_cell_guid":"33a09cf5-8c43-4ee5-b361-c852b3c427b6","collapsed":true,"_uuid":"90dd75304dd92c30ee4265364f94fbe5abd94618","trusted":true},"cell_type":"code","source":"def img_resize(img, output_shape=(IMG_HEIGHT, IMG_WIDTH, N_CHANEL)):\n    if img.shape[0] == output_shape[0] and img.shape[1] == output_shape[1]:\n        return img[:,:,:N_CHANEL] / 255\n    else:\n        return skimage.transform.resize(\n            img[:,:,:N_CHANEL], \n            output_shape,\n            mode='reflect')","execution_count":7,"outputs":[]},{"metadata":{"_cell_guid":"116ba649-8494-475b-888a-fa8ae6eb59c0","collapsed":true,"_uuid":"99b8e371b774aaaa3496e1a14a7288a88cb39859","trusted":true},"cell_type":"code","source":"def mask_resize(mask, output_shape=(IMG_HEIGHT, IMG_WIDTH, 1)):\n    if mask.shape[0] == output_shape[0] and mask.shape[1] == output_shape[1]:\n        return (mask != 0).astype(np.uint)\n    else:\n        result = skimage.transform.resize(\n            mask[:,:,:1], \n            output_shape,\n            mode='reflect')\n        result = (result > 0.5).astype(np.uint)\n        return result","execution_count":8,"outputs":[]},{"metadata":{"_cell_guid":"71c44795-040b-42ae-9a9e-82dd28230f63","_uuid":"29d85bd42185606e0bb1b0c1ba07e787d6d2d180"},"cell_type":"markdown","source":"## Showing train dataset"},{"metadata":{"_cell_guid":"48e6da83-fd2e-4198-b1b6-1d5592e137f1","_uuid":"bcffb9bb9f262e6c5030f8e0eb442d2e367ce125","trusted":true},"cell_type":"code","source":"for image_and_masks_id in get_images_and_masks_id()[:10]:\n    test_instance = get_image_and_masks(\n        image_and_masks_id['image_id'], \n        image_and_masks_id['masks_id'])\n    img = test_instance['image'] #img_resize\n    mask = test_instance['masks'] #mask_resize\n    plt.figure(1, figsize=(15,5))\n    plt.subplot(1,2,1); plt.imshow(img)\n    plt.subplot(1,2,2); plt.imshow(mask[:,:,0])\n    plt.show()","execution_count":9,"outputs":[]},{"metadata":{"_cell_guid":"e1009d18-e6fe-4328-8f37-465d38897c93","_uuid":"1b38d7a295a3bc1c2fe96e54c62c7430da8cbfee"},"cell_type":"markdown","source":"## Create train dataset"},{"metadata":{"_cell_guid":"edfc64a3-6bc5-4f54-93e4-aa145f4f7e41","collapsed":true,"_uuid":"a1b644675a4f2dd5bb34972ea51e9b318bd953de","trusted":true},"cell_type":"code","source":"input_data = []; output_data = []\nfor image_and_masks_id in get_images_and_masks_id():\n    test_instance = get_image_and_masks(\n        image_and_masks_id['image_id'], \n        image_and_masks_id['masks_id'])\n    input_data.append(img_resize(test_instance['image']))\n    output_data.append(mask_resize(test_instance['masks']))\ninput_data = np.array(input_data)\noutput_data = np.array(output_data)","execution_count":10,"outputs":[]},{"metadata":{"_cell_guid":"d59cf5c9-6eb6-4e58-96b3-8d44d0ed1073","collapsed":true,"_uuid":"5b825d046020af76f987a3e8e82acff55ef4e792","trusted":true},"cell_type":"code","source":"assert input_data.shape[0] == output_data.shape[0]","execution_count":11,"outputs":[]},{"metadata":{"_cell_guid":"40172282-7ea2-4cbf-8784-65e648bb5ac7","_uuid":"9a4e9f8eb3577eabae8c2bc4c71471201b3fe05a"},"cell_type":"markdown","source":"### increase dataset"},{"metadata":{"_cell_guid":"657c2e04-2c4f-43be-b4fe-3cbdaaa11546","collapsed":true,"_uuid":"875dd516b353e56b3f53f1d720fe90ae0f1a8ae5","trusted":true},"cell_type":"code","source":"from scipy import ndimage\nfrom skimage.transform import swirl\nimport skimage.util\nfrom random import randrange, uniform","execution_count":12,"outputs":[]},{"metadata":{"_cell_guid":"942348c7-166e-4571-9ef6-00f996386048","collapsed":true,"_uuid":"995b63d465776943131207263bc26133f7bd97f0","trusted":true},"cell_type":"code","source":"def swirl_generator(image, mask):\n    img = image[:,:,:3]\n    msk = mask[:,:,0]\n    \n    for i in range(randrange(5,10)):\n        r_radius = randrange(int(image.shape[0]/2), int(image.shape[0]/1.5))\n        r_center_x = randrange(0, image.shape[0])\n        r_center_y = randrange(0, image.shape[1])\n\n        img = swirl(\n            img.astype(np.float64), \n            center=(r_center_x, r_center_y), strength=1, radius=r_radius, mode='constant')\n        msk = swirl(\n            msk.astype(np.float64), \n            center=(r_center_x, r_center_y), strength=1, radius=r_radius, mode='constant')\n    msk = (msk > 0.5).astype(np.uint)\n    return img, msk","execution_count":13,"outputs":[]},{"metadata":{"_cell_guid":"00c8583e-d2a9-4ec2-8357-26e7a387156c","collapsed":true,"_uuid":"9468870e139a5a53951d03425889c04e65eb35ea","trusted":true},"cell_type":"code","source":"def gaus_filter(img):\n    sigma = uniform(0.5, 2.5)\n    return ndimage.gaussian_filter(img, sigma)","execution_count":14,"outputs":[]},{"metadata":{"_cell_guid":"21c937d3-d1f6-4324-af71-50d6496c9404","collapsed":true,"_uuid":"5f2a55f99075d7cce69f1dbe4c3d7db052b0d034","trusted":true},"cell_type":"code","source":"def rand_noise(img):\n    sigma = uniform(0, 0.03)\n    return skimage.util.random_noise(img, var=sigma)","execution_count":15,"outputs":[]},{"metadata":{"_cell_guid":"8bc0aecf-6191-4fa0-92f0-6bd124662634","_uuid":"d44da1cf7ddb58d609dc78817b694ae43503e02f","trusted":true},"cell_type":"code","source":"# test swirl_generator\nfig, ax = plt.subplots(2,4, figsize=(14,8))\n\nplots = []; num = 100\nplots.append((input_data[num], np.squeeze(output_data[num])))\nplots.append((swirl_generator(input_data[num], output_data[num])))\nplots.append((gaus_filter(input_data[num]), output_data[num][:,:,0]))\nplots.append((rand_noise(input_data[num]), output_data[num][:,:,0]))\n\nfor i, p in enumerate(plots):\n    ax[0][i].imshow(p[0])\n    ax[1][i].imshow(p[1])","execution_count":16,"outputs":[]},{"metadata":{"_cell_guid":"34ef5b2e-9f38-4052-87f1-8172bbcc1a7b","_uuid":"6ec46231f80301680680fa2ab59a87a0edd93f84"},"cell_type":"markdown","source":"### rotate and flip"},{"metadata":{"_cell_guid":"24655bf1-0eb6-4176-90c8-28e8d6664ddf","collapsed":true,"_uuid":"700de70e71aeabcc86b972f2ead6138668deba17","trusted":true},"cell_type":"code","source":"from skimage.transform import rotate","execution_count":17,"outputs":[]},{"metadata":{"_cell_guid":"6b1938f6-7c39-4cc6-b93b-508173820c84","collapsed":true,"_kg_hide-output":true,"_uuid":"80745e4b9ea7a728eaec3d4b80867b85850a3623","trusted":true},"cell_type":"code","source":"X = []; y = []\nfor img, msk in zip(input_data, output_data):\n    for i in range(randrange(0,2)):\n        r_angle = randrange(0, 360)\n        rot_img = rotate(img.astype(np.float64), angle=r_angle)\n        rot_msk = rotate(msk.astype(np.float64), angle=r_angle)\n        if np.max(rot_msk) < 0.5: continue\n        X.append(gaus_filter(np.fliplr(rot_img))) # vertical flip + gaus filte\n        y.append(np.fliplr(rot_msk))\n        X.append(rand_noise(np.flipud(rot_img))) # horisontal flip + noise\n        y.append(np.flipud(rot_msk))\n    X.append(img)\n    y.append(msk)\nX = np.array(X)\ny = np.array(y)","execution_count":18,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"dd8bb67710cd4e2da169c2275438f1d7726057db"},"cell_type":"code","source":"del input_data, output_data","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"645b41d0-52ed-44f1-acbe-b5ed34bbe0c6","_uuid":"c2323f87147d0b0ecfe66d5ea60f717be95b51f1","trusted":true},"cell_type":"code","source":"X.shape","execution_count":19,"outputs":[]},{"metadata":{"_cell_guid":"c77bbb62-3f90-428d-a863-71f55a979137","_uuid":"447f0d931852f559cf9c35b96a9264ab3b09899d","trusted":true},"cell_type":"code","source":"y.shape","execution_count":20,"outputs":[]},{"metadata":{"_cell_guid":"533071d5-1a93-4729-92a2-6865c9e98c6f","collapsed":true,"_uuid":"1617922a2ca8081334b31671637fb732d8c3f3e8","trusted":true},"cell_type":"code","source":"indexes = np.arange(X.shape[0])\nnp.random.shuffle(indexes)\ntrain_indexes = indexes","execution_count":21,"outputs":[]},{"metadata":{"_cell_guid":"c86bd0e8-f74a-41f8-bc1c-6195d1021a4c","collapsed":true,"_uuid":"f58ef1d93b4bac1aca81a5836069501b3ed4ecbd","trusted":true},"cell_type":"code","source":"# test normalization\nassert 1.0 >= np.max(X) and np.min(X) <= 0.0\nassert 1.0 >= np.max(y) and np.min(y) <= 0.0\n\ny_max_list = np.max(y, axis=(1,2)) # find max in mask\nassert (y_max_list < 0.5).any() == False","execution_count":22,"outputs":[]},{"metadata":{"_cell_guid":"f407f424-04ea-47e3-8d87-e117583ea28d","collapsed":true,"_uuid":"ec0ff153357c62d9261e9a2f83a85dd1fd0f69c7"},"cell_type":"markdown","source":"## creat U-Net model"},{"metadata":{"_cell_guid":"e206fd6b-6ee8-42bb-9c8e-465349ee1a0f","_uuid":"b59fde37e1c7807d5156c29f488b8c74980f455f","trusted":true},"cell_type":"code","source":"from tqdm import tqdm\nfrom itertools import chain\nfrom skimage.io import imread, imshow, imread_collection, concatenate_images\nfrom skimage.transform import resize\nfrom skimage.morphology import label\n\nfrom keras.models import Model, load_model\nfrom keras.layers import Input\nfrom keras.optimizers import Adam\nfrom keras.layers.merge import concatenate\nfrom keras.layers import Input, Conv2D, MaxPooling2D, UpSampling2D, Reshape, core, Dropout\nfrom keras.layers.core import Lambda\nfrom keras.layers.pooling import MaxPooling2D\nfrom keras.layers.merge import concatenate\nfrom keras.callbacks import EarlyStopping, ModelCheckpoint\nfrom keras import backend as K\n\nimport tensorflow as tf","execution_count":23,"outputs":[]},{"metadata":{"_cell_guid":"7f784c71-1f0b-4611-8d99-0e8921856271","collapsed":true,"_uuid":"0c091155c9bfd4ca09b8ae81a460460f41461f84","trusted":true},"cell_type":"code","source":"def mean_iou(y_true, y_pred):\n    prec = []\n    for t in np.arange(0.5, 1.0, 0.05):\n        y_pred_ = tf.to_int32(y_pred > t)\n        Y_negatives = tf.subtract(1.,y_pred)\n        tp = tf.reduce_sum(tf.reduce_sum(tf.multiply(y_true,y_pred),1),1)  # True positives (i.e. the intersection)\n        fp = tf.reduce_sum(tf.reduce_sum(tf.multiply(y_true,Y_negatives),1),1)  # False positives\n        fn = tf.reduce_sum(tf.reduce_sum(tf.multiply(tf.subtract(y_true,y_pred),y_pred),1),1)  # False negatives\n        p = tp / (tp + fp + fn)\n        prec.append(p)\n    return K.mean(K.stack(prec), axis=0)","execution_count":24,"outputs":[]},{"metadata":{"_cell_guid":"15e8ead4-5645-425f-b2d6-c64be29c514d","collapsed":true,"_uuid":"3b3fdd42939a3a7a43cc68177fd0815b7e96b919","trusted":true},"cell_type":"code","source":"def create_model():\n    print('model creat')\n    k = 4\n    inputs = Input((IMG_HEIGHT, IMG_WIDTH, N_CHANEL))\n\n    conv1 = Conv2D(2*k, (3, 3), activation='relu', padding='same')(inputs)\n    conv1 = Conv2D(2*k, (3, 3), activation='relu', padding='same')(conv1)\n    pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)\n\n    conv2 = Conv2D(4*k, (3, 3), activation='relu', padding='same')(pool1)\n    conv2 = Conv2D(4*k, (3, 3), activation='relu', padding='same')(conv2)\n    pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)\n\n    conv3 = Conv2D(8*k, (3, 3), activation='relu', padding='same')(pool2)\n    conv3 = Conv2D(8*k, (3, 3), activation='relu', padding='same')(conv3)\n    pool3 = MaxPooling2D(pool_size=(2, 2))(conv3)\n\n    conv4 = Conv2D(16*k, (3, 3), activation='relu', padding='same')(pool3)\n    conv4 = Conv2D(16*k, (3, 3), activation='relu', padding='same')(conv4)\n    pool4 = MaxPooling2D(pool_size=(2, 2))(conv4)\n\n    conv5 = Conv2D(32*k, (3, 3), activation='relu', padding='same')(pool4)\n    conv5 = Conv2D(32*k, (3, 3), activation='relu', padding='same')(conv5)\n\n    up6 = concatenate([UpSampling2D(size=(2, 2))(conv5), conv4])\n    conv6 = Conv2D(16*k, (3, 3), activation='relu', padding='same')(up6)\n    conv6 = Conv2D(16*k, (3, 3), activation='relu', padding='same')(conv6)\n\n    up7 = concatenate([UpSampling2D(size=(2, 2))(conv6), conv3])\n    conv7 = Conv2D(8*k, (3, 3), activation='relu', padding='same')(up7)\n    conv7 = Conv2D(8*k, (3, 3), activation='relu', padding='same')(conv7)\n\n    up8 = concatenate([UpSampling2D(size=(2, 2))(conv7), conv2])\n    conv8 = Conv2D(4*k, (3, 3), activation='relu', padding='same')(up8)\n    conv8 = Conv2D(4*k, (3, 3), activation='relu', padding='same')(conv8)\n\n    up9 = concatenate([UpSampling2D(size=(2, 2))(conv8), conv1])\n    conv9 = Conv2D(2*k, (3, 3), activation='relu', padding='same')(up9)\n    conv9 = Conv2D(2*k, (3, 3), activation='relu', padding='same')(conv9)\n    \n    outputs = Conv2D(1, (1, 1), activation='sigmoid') (conv9)\n\n    model = Model(inputs=[inputs], outputs=[outputs])\n    return model","execution_count":25,"outputs":[]},{"metadata":{"_cell_guid":"963bbd81-06a3-4dff-a2fe-8d0d953ea2ab","collapsed":true,"_uuid":"0c473d365b865cbb5b3d24b5e3e30f6721716745","trusted":true},"cell_type":"code","source":"def compile_model(model):\n    model.compile(\n        optimizer='adam', \n        loss = 'binary_crossentropy',\n        metrics=[mean_iou])\n    return model","execution_count":26,"outputs":[]},{"metadata":{"_cell_guid":"40f7e0aa-65b7-44ca-b92e-b2793e07258a","collapsed":true,"_uuid":"272c9214f6ddaf69773cbda75e9599df719271ee","trusted":true},"cell_type":"code","source":"def fit_model(model, batch_size, validation_split, epochs):\n    checkpointer = ModelCheckpoint('U-Net_model.h5', verbose=1, save_best_only=True)\n    earlystopper = EarlyStopping(patience=5, verbose=1)\n    history = model.fit(\n        X[train_indexes], \n        y[train_indexes], \n        validation_split=validation_split, \n        batch_size=batch_size, \n        epochs=epochs,\n        callbacks=[checkpointer, earlystopper])\n    return history","execution_count":27,"outputs":[]},{"metadata":{"_cell_guid":"66840590-fa0a-467a-8bd1-822d36035d86","_uuid":"db43effd546607ae7a16bbbba7d1d92f3fbe2161","trusted":true},"cell_type":"code","source":"#delete old model\n!rm ./U-Net_model.h5","execution_count":28,"outputs":[]},{"metadata":{"_cell_guid":"154db164-9587-4701-bf4b-233daf5a07bc","_uuid":"a870685bc66b9e6df19eac0a40d44a921676c6de","trusted":true},"cell_type":"code","source":"%%time\nfrom keras.models import load_model\nif os.path.isfile('../working/U-Net_model.h5'):\n    print('model load')\n    model = load_model('../working/U-Net_model.h5',  custom_objects={'mean_iou': mean_iou})\nelse:\n    model = create_model()\nmodel = compile_model(model)\nfit_model(model, 4, 0.1, 12)","execution_count":29,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"89444cd97f1266080ec1e7b60e20ecc5c05be661"},"cell_type":"code","source":"del X, y","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"3284d9a1-14a3-46dd-a313-429d7eb6e97b","_uuid":"2b28cfe0a875613567d389f5c67169eae4236cf9"},"cell_type":"markdown","source":"## Create submission"},{"metadata":{"_cell_guid":"bf1dda21-ab74-4762-918c-0f157d3e6951","_uuid":"e9fa9bc77fca820459df6fd6dce95eb328297808","trusted":true},"cell_type":"code","source":"# load test dataset\ntest_img_name = [img for img in next(os.walk('../input/stage1_test/'))[1]]\ntest_img = [skimage.io.imread('../input/stage1_test/{0}/images/{0}.png'.format(img)) for img in test_img_name]\ninput_shape = [img.shape[:2] for img in test_img]\ntest_img = [img_resize(img) for img in test_img] # вернуть размеры изображений назад\ntest_img = np.array(test_img)\ntest_img.shape","execution_count":30,"outputs":[]},{"metadata":{"_cell_guid":"1933af7e-21df-41a0-81f7-79d2bab9ea9c","collapsed":true,"_uuid":"f99165639128e5b49b080bb69db42ae262868ea7","trusted":true},"cell_type":"code","source":"assert 1.0 >= np.max(test_img) and np.min(test_img) <= 0.0","execution_count":31,"outputs":[]},{"metadata":{"_cell_guid":"d5d2f543-fca2-49da-a972-f9b4707a8706","_uuid":"c72a5516ddc001844f4e8dc9e2373f07df6cbc19","trusted":true},"cell_type":"code","source":"model = load_model('../working/U-Net_model.h5',  custom_objects={'mean_iou': mean_iou})\npreds_test= model.predict(test_img, verbose=1)","execution_count":32,"outputs":[]},{"metadata":{"_cell_guid":"21b710f1-1f65-4a43-9994-a64198b9a732","collapsed":true,"_uuid":"98d1fd040ca364d234c2b277c5f02275c02c79d9","trusted":true},"cell_type":"code","source":"def return_shape_masks(masks, shapes):\n    reshape_mask = []\n    for i, shape in enumerate(shapes):\n        mask = skimage.transform.resize(\n            masks[i], \n            shape, \n            mode='reflect')\n        mask = (mask > 0.5).astype(np.uint8)\n        reshape_mask.append(mask)\n    return np.array(reshape_mask)\n\ndef return_shape_img(images, shapes):\n    reshape_images = []\n    for i, shape in enumerate(shapes):\n        img = skimage.transform.resize(\n            images[i],\n            (shape[0], shape[1], 3),\n            mode='reflect')\n        reshape_images.append(img)\n    return np.array(reshape_images)","execution_count":33,"outputs":[]},{"metadata":{"_cell_guid":"409d33ce-afeb-4d41-a871-da96ac2be4c8","collapsed":true,"_uuid":"ec3c8bb23654e763b0de5cf57418a323dc7ae499","trusted":true},"cell_type":"code","source":"from skimage.morphology import watershed\nfrom scipy import ndimage as ndi\nfrom skimage.feature import peak_local_max\n\ndef rle_encoding(x): # функция находит все точки на изображении\n    '''x: numpy array of shape (height, width), 1 - mask, 0 - background\n    Returns run length as list'''\n    dots = np.where(x.T.flatten() > 0.5)[0] # .T sets Fortran order down-then-right\n    run_lengths = []\n    prev = -2\n    for b in dots:\n        if (b>prev+1): run_lengths.extend((b+1, 0))\n        run_lengths[-1] += 1\n        prev = b\n    return run_lengths\n\ndef prob_to_rles(x, cutoff=0.50): # функция разделяет пятна на группы\n    lab_img = label(x > cutoff)\n    for i in range(1, lab_img.max() + 1):\n        yield rle_encoding(lab_img == i)","execution_count":34,"outputs":[]},{"metadata":{"_cell_guid":"b63c81fb-944c-404a-9503-446a6edd5acd","collapsed":true,"_uuid":"367265cfef1e6a01b6d63c1a253501a5cd74d0ad","trusted":true},"cell_type":"code","source":"images = return_shape_img(test_img, input_shape)\nmasks = return_shape_masks(preds_test, input_shape)\n\nfor i, mask in enumerate(masks[:10]):\n    plt.figure(figsize = (14, 5))\n    plt.subplot(1, 2, 1); plt.imshow(images[i]);\n    plt.subplot(1, 2, 2); plt.imshow(mask[:,:,0])\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"7723056e-5865-4e3a-a9c5-8ef76842adbf","collapsed":true,"_uuid":"447bd6f55ddcc1815c958fbd45e17b87f5576a02","trusted":false},"cell_type":"code","source":"new_test_ids = []\nrles = []\nfor i, id in enumerate(test_img_name):\n    rle = list(prob_to_rles(masks[i][:,:,0]))\n    rles.extend(rle)\n    new_test_ids.extend([id] * len(rle))","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"6329cca0-9c67-4cca-927c-5eb455a76edc","collapsed":true,"_uuid":"13512eff04db1e9879f6cf6d47c1e069f567ba60","scrolled":true,"trusted":false},"cell_type":"code","source":"submission_df = pd.DataFrame()\nsubmission_df['ImageId'] = new_test_ids\nsubmission_df['EncodedPixels'] = pd.Series(rles).apply(lambda x: ' '.join(str(y) for y in x))\nsubmission_df.to_csv('submission.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"14af0eca-7e8b-4b57-9a3f-068008e33cfe","collapsed":true,"_uuid":"af4ce30cc44dc73d513bb821518f6bf871aca619"},"cell_type":"markdown","source":"# Submission test"},{"metadata":{"_cell_guid":"9b83bf5e-4303-47f5-bee0-63795f960730","collapsed":true,"_uuid":"c2b671d30b3965753c6c6167775b6e8e9e06cd0f","trusted":false},"cell_type":"code","source":"def rls_decode(text_line, out_shape):\n    img = np.zeros(out_shape[0]*out_shape[1])\n    \n    str_numbers = text_line.split()\n    \n    int_numbers = []\n    for str_nb in str_numbers:\n        int_numbers.append(int(str_nb))\n    \n    pixcels = []; lengs = []\n    for i, num in enumerate(int_numbers):\n        if (i % 2 == 0) or (i == 0):\n            pixcels.append(num)\n        if (i % 2 == 1) or (i == 1):\n            lengs.append(num)\n            \n    for px, l in zip(pixcels, lengs):\n        for x in range(l):\n            img[px+x] = 1\n    return img","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"a211fc50-865b-4d66-842a-4639fd788d16","collapsed":true,"_uuid":"b7a25e77f946ef0743d865265e0891818611d82c","trusted":false},"cell_type":"code","source":"test_mask_id = '0999dab07b11bc85fb8464fc36c947fbd8b5d6ec49817361cb780659ca805eac'\ntest_mask_shape = (253, 519)","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"ab5e5007-0e8c-4206-b9e4-ce9543984b13","collapsed":true,"_uuid":"1a6ccb41ff0904c89917b34ca3d42480c20f2de4","trusted":false},"cell_type":"code","source":"n_nuclei = submission_df['EncodedPixels'][submission_df['ImageId'] == test_mask_id].shape[0]\n\ndecode_masks = []\nfor i in range(n_nuclei):\n    nucl = submission_df['EncodedPixels'][submission_df['ImageId'] == test_mask_id].iloc[i]\n    nucl_mask = rls_decode(nucl, test_mask_shape)\n    decode_masks.append(nucl_mask.reshape(test_mask_shape))\n    \ndecode_masks = [e*i for i, e in enumerate(decode_masks)]\nimage = np.sum(np.array(decode_masks), axis=0)\nplt.imshow(image)\nprint('count nuclei:',np.max(image))","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"b76c96c4-d7c5-4eb3-8eb9-36011a368f9a","collapsed":true,"_uuid":"eadbb468fdd4df5c695f336b5e72df5d44e4a27a","trusted":false},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}