{"cells":[{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"# data visualisation and manipulation\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\n\nimport os\nimport time\n\n#model selection\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.metrics import accuracy_score\nfrom sklearn.model_selection import train_test_split\n\n# specifically for manipulating zipped images and getting numpy arrays of pixel values of images.\nimport cv2                  \n\n# to make this notebook's output stable across runs\nnp.random.seed(42)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Load and Prepare Data"},{"metadata":{"trusted":true},"cell_type":"code","source":"IMG_SIZE = 128\nDATA_DIR = os.path.join(r'/kaggle/input/', 'aptos2019-blindness-detection')\nTRAIN_DIR = os.path.join(DATA_DIR,'train_images')\nTEST_DIR = os.path.join(DATA_DIR, 'test_images')\n\ndef searchCircle(img):\n    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\n    gray = cv2.medianBlur(gray, 5)\n    rows = gray.shape[0]\n    circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, rows / 8,\n                               param1=100, param2=30,\n                               minRadius=int(img.shape[1]/4), maxRadius=int(img.shape[1]/2))\n    \n    if circles is not None:\n        circles = np.int32(np.around(circles))\n        return circles[0,0]\n    \n    cx = int(img.shape[1]/2)\n    cy = int(img.shape[0]/2)\n    r = max(cx, cy)\n    return np.array([cx, cy, r])\n\ndef circle_crop(img, cx, cy, r):\n    mask = np.zeros((img.shape[1], img.shape[0]), np.uint8)\n    cv2.circle(mask, (cx, cy), r, (255,255,255), thickness=-1)\n    return cv2.bitwise_and(img, img, mask=mask)\n\ndef rect_crop(img, x0, y0, x1, y1):\n    mask = np.zeros((img.shape[1], img.shape[0]), np.uint8)\n    cv2.rectangle(mask, (x0, y0), (x1, y1), (255,255,255), thickness=-1)\n    return cv2.bitwise_and(img, img, mask=mask)\n\ndef processImg(img):\n    # find the circle and resize\n    cir = searchCircle(img)     \n    scale = (float(IMG_SIZE)) / (cir[2] * 2)\n    img = cv2.resize(img,(0,0),fx=scale, fy=scale)\n    \n    cir = np.int32(np.around(cir * scale))\n    \n    # crop the circle\n    x0 = max(cir[0]-cir[2], 0)\n    x1 = min(cir[0]+cir[2], img.shape[1])\n    y0 = max(cir[1]-cir[2], 0)\n    y1 = min(cir[1]+cir[2], img.shape[0])\n    img = img[y0:y1, x0:x1,:]\n    \n    # copy the circle to the center of square\n    img1 = np.zeros([cir[2] * 2, cir[2] * 2, img.shape[2]])\n    dx = max(cir[2] - cir[0], 0)\n    dy = max(cir[2] - cir[1], 0)\n    img1[dy:dy+y1-y0,dx:dx+x1-x0,:] = img\n    \n    # crop the cirle\n    r = int(IMG_SIZE/2)\n    img1 = circle_crop(img1, r, r, r)\n    img1 = rect_crop(img1, dx, dy, dx+x1-x0, dy+y1-y0)\n\n    img1 = np.array(img1)/255 # normalize the image\n    return img1\n\ndef getImageData(path):\n    img = cv2.imread(path, cv2.IMREAD_COLOR)\n\n    if img is not None:\n        return processImg(img)\n    \n    return None\n\ndef idcode2Path(imgDir, idCode):\n    return os.path.join(imgDir, '{}.png'.format(idCode))\n\ndef getImgArray(idList, imgDir):\n    imgs = []\n    for i, idCode in enumerate(idList):\n        img = getImageData(idcode2Path(imgDir, idCode))\n        if img is None:\n            print(idCode , \" is none\")\n        imgs.append(img)\n        \n    return np.array(imgs)\n\ndef showOne(ax, imgs, index):\n    if(index >= len(imgs)): \n        return False\n    \n    img1 = imgs[index].reshape(IMG_SIZE, IMG_SIZE, 3)\n    ax.imshow(img1)\n    ax.axis(\"off\")\n    \n    return True\n\ndef plotMatrix(funcOne, paramList, row=1, col=3, figsize=None):\n    if figsize is None:\n        figsize=(col * 5, row * 4)\n    fig, axes = plt.subplots(row, col,figsize=figsize)\n    if((row > 1) | (col > 1)):\n        axes = axes.ravel()\n    else:\n        axes = [axes]\n        \n    for i, ax in enumerate(axes):\n        if(not funcOne(ax, paramList, i)): \n            break\n   \n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df = pd.read_csv(os.path.join(DATA_DIR, 'train.csv'))\nprint(df.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"t0 = time.time()\nX = getImgArray(df['id_code'], TRAIN_DIR)\nX = X.reshape(X.shape[0], X.shape[1]*X.shape[2]*X.shape[3])\nY = df['diagnosis']\n\nprint(\"getImgArray elapsed time:\", time.time() - t0)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plotMatrix(showOne, X, 2, 3)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x_train,x_valid,y_train,y_valid = train_test_split(X,Y,test_size=0.2,random_state=42)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Model and Estimate "},{"metadata":{"trusted":true},"cell_type":"code","source":"model = RandomForestClassifier(random_state=42, bootstrap=False, criterion='entropy', \n           max_features=10,min_samples_split=10, max_depth=38, n_estimators=27)\n\nmodel.fit(x_train, y_train)\n\ny_valid_pred = model.predict(x_valid)\n\nscore = accuracy_score(y_valid, y_valid_pred)\nprint(\"Valid Score = {0:.4f}\".format(score))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Submission For Test Data"},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df = pd.read_csv(os.path.join(DATA_DIR, 'test.csv'))\nx_test = getImgArray(test_df['id_code'], TEST_DIR)\n\nprint(x_test.shape)\nx_test = x_test.reshape(x_test.shape[0], x_test.shape[1]*x_test.shape[2]*x_test.shape[3])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.fit(X, np.array(Y))\npred = model.predict(x_test)\n\ntest_df['diagnosis'] = pred\ntest_df.to_csv('submission.csv',index=False)","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":1}