{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport os","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"os.getcwd()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"df_train = pd.read_csv(\"../input/prostate-cancer-grade-assessment/train.csv\")\ndf_train.head(10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_img_path = \"/kaggle/input/prostate-cancer-grade-assessment/train_images\"\nlabel_path = \"/kaggle/input/prostate-cancer-grade-assessment/train_label_masks\"\n\ntrain_img = [img for img in os.listdir(train_img_path)]\ntrain_label = [label for label in os.listdir(label_path)]\n\ntrain_img = list(sorted(train_img))\ntrain_label = list(sorted(train_label))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from matplotlib import rcParams\nimport openslide\nimport cv2\nfrom IPython.display import display\n\n# rcParams[\"figure.figsize\"] = 15, 15\n\nfor i in range(22, 25):\n    img = openslide.OpenSlide(train_img_path + \"/\" + train_img[i])\n    display(img.get_thumbnail(size=(600, 400)))\n    img.close()\n    d = df_train.loc[df_train[\"image_id\"] == train_img[i][:-5]]\n    print(f\"PROVIDED BY: {d.data_provider.values[0]}\")\n    print(f\"ISUP Grade: {d.isup_grade.values[0]}, Gleason Grade: {d.gleason_score.values[0]}\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**RADBOUDUMC:**\n* 0: background (non tissue) or unknown\n* 1: stroma (connective tissue, non-epithelium tissue)\n* 2: healthy (benign) epithelium\n* 3: cancerous epithelium (Gleason 3)\n* 4: cancerous epithelium (Gleason 4)\n* 5: cancerous epithelium (Gleason 5)\n\n**KAROLINSKA:**\n* 0: background (non tissue) or unknown\n* 1: benign tissue (stroma and epithelium combined)\n* 2: cancerous tissue (stroma and epithelium combined)","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"import matplotlib\n\nrcParams[\"figure.figsize\"] = 7, 6\n\nfor i in range(22, 25):\n    mask = openslide.OpenSlide(label_path + \"/\" + train_label[i])\n    mask = mask.get_thumbnail(size=(600, 400))\n    mask = np.asarray(mask)\n    mask = mask[:,:,0]\n    cmap = matplotlib.colors.ListedColormap(['black', 'gray', 'green', 'yellow', 'orange', 'red'])\n\n    plt.imshow(mask, cmap=cmap, interpolation='nearest', vmin=0, vmax=5)\n    plt.axis('off')\n    plt.show()\n#     mask.close()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## IMAGE PREPROCESSING","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"import cv2\n\nSIZE = 200\n\nresized_imgs_path = \"../input/panda-resized-train-data-512x512/train_images/train_images\"\nimg_array = []\nfor i in os.listdir(resized_imgs_path):\n    img = resized_imgs_path + \"/\" + i\n    img = cv2.resize(cv2.imread(img), (SIZE, SIZE))\n    img_array.append(img)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.imshow(img_array[1])\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.preprocessing import LabelBinarizer\n\ntrain_y = list(df_train['isup_grade'].values)\nlb = LabelBinarizer()\ntrain_y = lb.fit_transform(train_y)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_y_multi = np.empty(train_y.shape, dtype=train_y.dtype)\ntrain_y_multi[:, 5] = train_y[:, 5]\n\nfor i in range(4, -1, -1):\n    train_y_multi[:, i] = np.logical_or(train_y[:, i], train_y_multi[:,i+1])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_y_multi.sum(axis=0)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_x = np.reshape(img_array, (len(img_array), SIZE, SIZE, 3))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_test = pd.read_csv(\"../input/prostate-cancer-grade-assessment/test.csv\")\ndf_test.head(10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_img = [img+\".tiff\" for img in df_test['image_id'].values]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_img_path = '../input/prostate-cancer-grade-assessment/test_images'\ntest_x = []\n\nif os.path.exists(test_img_path):\n    for i in range(len(test_img)):\n        img = test_img_path + \"/\" + test_img[i]\n        img = preprocessing_img(img)\n        test_x.append(img)\n    test_x = np.reshape(train_img, (len(test_img), SIZE, SIZE, 3))\nelse:\n    test_x = np.random.rand(len(test_img),SIZE, SIZE, 3)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\ntrain_x, val_x, train_y, val_y = train_test_split(\n    train_x, train_y_multi, train_size=0.8, random_state=42)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.applications import DenseNet121\n\ndensenet = DenseNet121(\n    weights = '../input/densenet-keras/DenseNet-BC-121-32-no-top.h5',\n    include_top=False,\n    input_shape=(SIZE, SIZE, 3)\n)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom tensorflow.keras import layers as ly\nfrom tensorflow.keras.optimizers import Adam\n\nmodel = Sequential([\n    densenet,\n    ly.GlobalAveragePooling2D(),\n    ly.Dropout(0.8),\n    ly.Dense(6, activation=\"sigmoid\")\n])\n\nmodel.compile(loss=\"binary_crossentropy\", optimizer=Adam(lr=0.01), metrics=[\"accuracy\"])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator\n\ndata = ImageDataGenerator(\n    zoom_range = 0.15,\n    fill_mode=\"nearest\",\n    cval=0.,\n    horizontal_flip=True,\n    vertical_flip=True\n)\n\ndata = data.flow(train_x, train_y, batch_size=10, seed=42)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"history = model.fit_generator(\n    data, steps_per_epoch=train_x.shape[0] / 10,\n    epochs=10,\n    validation_data=(val_x, val_y)\n)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"acc = history.history[\"accuracy\"]\nval_acc = history.history[\"val_accuracy\"]\nloss = history.history[\"loss\"]\nval_loss = history.history[\"val_loss\"]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.plot(acc)\nplt.plot(val_acc)\nplt.legend([\"accuracy\", \"val_accuracy\"])\nplt.title(\"Accuracy and Validation Accuracy throughout epochs\")\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.plot(loss)\nplt.plot(val_loss)\nplt.title(\"Loss and Validation Loss throughout epochs\")\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from random import randint\n\nif os.path.exists(test_img_path):\n    test_y = model.predict(test_x)\n    test_y = test_y > 0.37757874193797547\n    test_y = test_y.astype(int).sum(axis=1) - 1\nelse:\n    test_y = [randint(0, 5) for i in range(3)]\n\ndf_test['isup_grade'] = test_y\ndf_test = df_test[[\"image_id\", \"isup_grade\"]]\ndf_test.to_csv(\"submission.csv\", index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","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":4}