{"cells":[{"metadata":{"_uuid":"ad77aaca1acdb723e6cb23034713490efede55c6"},"cell_type":"markdown","source":"# Kaggle 2018 Data Science Bowl  \n\nFind the nuclei in divergent images to advance medical discovery  "},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"5bb8722a188cab03a73d0ad427d781ee1b07983a"},"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd\nfrom pathlib import Path\nimport random\nimport matplotlib.pyplot as plt\n\nroot_path = Path('/media/share/data/kaggle/2018-dsb')","execution_count":1,"outputs":[]},{"metadata":{"_uuid":"0062c230d08b1407f8b5d177436c5374fb87045a"},"cell_type":"markdown","source":"## Naive computer vision method\nFirst try naive cv method to setup a baseline "},{"metadata":{"trusted":true,"_uuid":"006747fa7cf57eb243afec1289b940cc6ff61424"},"cell_type":"code","source":"from skimage import io\nfrom skimage.color import rgb2gray\nfrom skimage.filters import threshold_otsu, threshold_adaptive\nfrom skimage.morphology import remove_small_objects, extrema\n\ntrainfile = os.listdir(root_path / 'stage1_train')\n\nidx = random.choice(trainfile)\nfname = idx + '.png'\nimg = io.imread(root_path / 'stage1_train' / idx / 'images' / fname)\n\nimg_gray = rgb2gray(img)\nthre = threshold_otsu(img_gray)\nimg_f1 = remove_small_objects(img_gray > thre, 16)\n\nfig, (ax1, ax2) = plt.subplots(1, 2)\nax1.imshow(img_gray, cmap='gray')\nax2.imshow(img_f1)","execution_count":2,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"28fed29077165df9ba1cc52f50154952265371be"},"cell_type":"code","source":"g_mean = np.mean(img_gray)\nimg_sm = (img_gray - np.mean(img_gray)) / np.std(img_gray)\n\nkk_t = threshold_otsu(img_sm)\nimg_f2 = remove_small_objects(img_sm > kk_t, 16)\n\nplt.imshow(img_f2, cmap='gray')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"4b0c3f72c6b95be5aa34cd8e018f28e1d01fd70b"},"cell_type":"markdown","source":"## Testing"},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"a7ead306ab2c25a111b751e238177bce30bd4508"},"cell_type":"code","source":"def rle_encoding(x):\n    '''\n    x: numpy array of shape (height, width), 1 - mask, 0 - background\n    Returns run length as list\n    '''\n    dots = np.where(x.T.flatten()==1)[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 \" \".join([str(i) for i in run_lengths])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"301f2c5f28167b1b76bc9ca6beee55718f1b885d"},"cell_type":"code","source":"from skimage import io\nfrom skimage.color import rgb2gray\nfrom skimage.filters import threshold_otsu, threshold_adaptive\nfrom skimage.morphology import remove_small_objects\nfrom scipy import ndimage\n    \ntestfile = os.listdir(str(root_path / 'stage2_test'))\n\ndef img_proc(indexlist):\n    \n    imgn = indexlist + '.png'\n    img = io.imread(str(root_path / 'stage2_test' / indexlist / 'images' / imgn))\n\n    img_gray = rgb2gray(img)\n    img_p = (img_gray - np.mean(img_gray)) / np.std(img_gray)\n    thre = threshold_otsu(img_p)\n    img_f = remove_small_objects(img_p > thre, 16)\n    \n    im_df = pd.DataFrame([])\n    \n    # label image regions\n    labels, nlabels = ndimage.label(img_f)\n\n    im_df = pd.DataFrame()\n    for label_num in range(1, nlabels+1):\n        label_mask = np.where(labels == label_num, 1, 0)\n        if label_mask.flatten().sum() > 10:\n            rle = rle_encoding(label_mask)\n            s = pd.Series({'ImageId': indexlist, 'EncodedPixels': rle})\n            im_df = im_df.append(s, ignore_index=True)\n    \n    return im_df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"539be0a0b68490ea71f5c09d9ce945a84f426df3"},"cell_type":"code","source":"from multiprocessing import Pool, cpu_count\n\nwith Pool(cpu_count()) as pool:\n    img = pool.map(img_proc, testfile)\n    \n    \nsubm_df = pd.DataFrame([])\n\nfor i in img:\n    subm_df = pd.concat([subm_df, i], 0)\n    \n    \nsubm_df.reset_index(drop=True, inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"c0913e03eb38ea74d078c45da312f8d4b3833630"},"cell_type":"code","source":"submition = pd.concat([subm_df['ImageId'], subm_df['EncodedPixels']], 1)\nsubmition.to_csv('/media/share/jiaxin/kaggle/kaggle_2018dsb/stage2_naive_cv.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"c2dedd30c81f01129fe0a264ed0b71da6b070bd8"},"cell_type":"markdown","source":"## Results\nLB: 0.241"}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.6.4"}},"nbformat":4,"nbformat_minor":1}