{"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.0"}},"nbformat":4,"nbformat_minor":0,"cells":[{"metadata":{"_cell_guid":"892f8ed3-4c2a-34b4-6fd1-eec21a2225a5","_active":false,"collapsed":false},"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\nfrom subprocess import check_output\nprint(check_output([\"ls\", \"../input\"]).decode(\"utf8\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":2,"cell_type":"code","outputs":[],"execution_state":"idle"},{"metadata":{"_cell_guid":"91c903f6-d589-6676-10d6-c6bc7bf6047f","_active":false,"collapsed":false},"source":"import keras\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom skimage.io import imread, imshow\nimport PIL\nfrom PIL import Image\n# keras deps\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Flatten\nfrom keras.layers import Conv2D, MaxPooling2D\nfrom keras.optimizers import SGD","execution_count":30,"cell_type":"code","outputs":[],"execution_state":"idle"},{"metadata":{"_cell_guid":"37e7c83e-0aa9-f01e-5b95-1ccc37991fe4","_active":false,"collapsed":false},"source":"print(check_output([\"ls\", \"../input/train\"]).decode(\"utf8\"))","execution_count":5,"cell_type":"code","outputs":[],"execution_state":"idle"},{"metadata":{"_cell_guid":"b3571d5f-2353-f525-c591-00ded7753ecd","_active":false,"collapsed":false},"source":"from glob import glob\nbasepath = '../input/train/'\n\nall_cervix_images = []\n\nfor path in sorted(glob(basepath + \"*\")):\n    cervix_type = path.split(\"/\")[-1]\n    cervix_images = sorted(glob(basepath + cervix_type + \"/*\"))\n    all_cervix_images = all_cervix_images + cervix_images\n\nall_cervix_images = pd.DataFrame({'imagepath': all_cervix_images})\nall_cervix_images['filetype'] = all_cervix_images.apply(lambda row: row.imagepath.split(\".\")[-1], axis=1)\nall_cervix_images['type'] = all_cervix_images.apply(lambda row: row.imagepath.split(\"/\")[-2], axis=1)\n\ndef downsample_img(row):\n    try:\n        img = Image.open(row.imagepath)\n        wpercent = (basewidth / float(img.size[0]))\n        hsize = int((float(img.size[1]) * float(wpercent)))\n        return img.resize((basewidth, hsize), PIL.Image.ANTIALIAS)\n    except OSError:\n        print(\"Failed\")\n        return None\n\nall_cervix_images['img'] = all_cervix_images.apply(lambda row: downsample_img(row), axis=1)\nall_cervix_images.head()","execution_count":35,"cell_type":"code","outputs":[],"execution_state":"idle"},{"metadata":{"_cell_guid":"f6ec1323-8cd3-e79d-fb11-16684d79901f","_active":false,"collapsed":false},"source":"print(len(all_cervix_images))\nall_cervix_images = all_cervix_images.dropna()\nall_cervix_images","execution_count":50,"cell_type":"code","outputs":[],"execution_state":"idle"},{"metadata":{"_cell_guid":"9c31efc8-9b2d-448a-d23e-ac90aaefb610","_active":false,"collapsed":false},"source":"def to_arr(row):\n    print(row)\n    img = row['img']\n    if img is not None:\n        return np.array(img)\n    return None\n\nall_cervix_images['img_vec'] = all_cervix_images.apply(lambda row: to_arr(row), axis=1)\nall_cervix_images.head()","execution_count":51,"cell_type":"code","outputs":[],"execution_state":"idle"},{"metadata":{"_cell_guid":"d9934e1b-b312-3b73-dfd8-ca948cf1635a","_active":false,"collapsed":false},"source":"basewidth = 100\nimg = Image.open(all_cervix_images.values[6][0])\nwpercent = (basewidth / float(img.size[0]))\nhsize = int((float(img.size[1]) * float(wpercent)))\nimg = img.resize((basewidth, hsize), PIL.Image.ANTIALIAS)","execution_count":28,"cell_type":"code","outputs":[],"execution_state":"idle"},{"metadata":{"_cell_guid":"cd0b19aa-0dde-88f6-bcea-bd01eb284991","_active":false,"collapsed":false},"source":"# Let's define a VGG-like convnet\nmodel = Sequential()\n# input: 100x100 images with 3 channels -> (100, 100, 3) tensors.\n# this applies 32 convolution filters of size 3x3 each.\nmodel.add(Conv2D(32, (3, 3), activation='relu', input_shape=(100, 100, 3)))\nmodel.add(Conv2D(32, (3, 3), activation='relu'))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(Dropout(0.25))\n\nmodel.add(Conv2D(64, (3, 3), activation='relu'))\nmodel.add(Conv2D(64, (3, 3), activation='relu'))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(Dropout(0.25))\n\nmodel.add(Flatten())\nmodel.add(Dense(256, activation='relu'))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(10, activation='softmax'))","execution_count":32,"cell_type":"code","outputs":[],"execution_state":"idle"},{"metadata":{"_cell_guid":"abc88b14-4731-54d6-53b6-8a5f518b4352","_active":false,"collapsed":false},"source":"np.array(all_cervix_images.values[6][3])","execution_count":41,"cell_type":"code","outputs":[],"execution_state":"idle"},{"metadata":{"_cell_guid":"98295b56-31d0-62fb-2b57-c5d75c96c9fa","_active":false,"collapsed":false},"source":null,"execution_count":null,"cell_type":"code","outputs":[]}]}