{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-11-22T01:11:07.04996Z","iopub.execute_input":"2022-11-22T01:11:07.050537Z","iopub.status.idle":"2022-11-22T01:11:07.055651Z","shell.execute_reply.started":"2022-11-22T01:11:07.050482Z","shell.execute_reply":"2022-11-22T01:11:07.054519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nimport torch.optim as optim\nfrom torch.optim import lr_scheduler\nimport torchvision\nfrom torchvision import datasets, models, transforms\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport pandas as pd\nimport tensorflow as tf\nimport copy\nfrom tqdm.notebook import tqdm\nimport time\nimport os\nimport cv2\nimport shutil\nimport tensorflow \nimport keras\nimport random\nimport math\nimport seaborn as sns\nimport glob\nimport openslide","metadata":{"execution":{"iopub.status.busy":"2022-11-22T01:11:17.222642Z","iopub.execute_input":"2022-11-22T01:11:17.223152Z","iopub.status.idle":"2022-11-22T01:11:28.541687Z","shell.execute_reply.started":"2022-11-22T01:11:17.223114Z","shell.execute_reply":"2022-11-22T01:11:28.540465Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix\nfrom sklearn.preprocessing import LabelBinarizer\nfrom sklearn.model_selection import train_test_split\n\nimport matplotlib.pyplot as plt\n\nfrom tensorflow.keras.layers import Dense,GlobalAveragePooling2D,Convolution2D,BatchNormalization\nfrom tensorflow.keras.layers import Flatten,MaxPooling2D,Dropout\n\nfrom tensorflow.keras.applications import DenseNet121\nfrom tensorflow.keras.applications.densenet import preprocess_input\n\nfrom tensorflow.keras.preprocessing import image\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator,img_to_array\n\nfrom tensorflow.keras.models import Model\n\nfrom tensorflow.keras.optimizers import Adam\n\nfrom tensorflow.keras.callbacks import ModelCheckpoint, ReduceLROnPlateau\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"panda_path = '../input/prostate-cancer-grade-assessment'\npanda_img_path = panda_path + 'train_images'\npanda_df = panda_path + 'train.csv'","metadata":{"execution":{"iopub.status.busy":"2022-11-21T21:54:20.694677Z","iopub.execute_input":"2022-11-21T21:54:20.695078Z","iopub.status.idle":"2022-11-21T21:54:20.701571Z","shell.execute_reply.started":"2022-11-21T21:54:20.695036Z","shell.execute_reply":"2022-11-21T21:54:20.700136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_path = '../input/training-data/Training data'\ndata_df = pd.read_csv('../input/traindf/train.csv')","metadata":{"execution":{"iopub.status.busy":"2022-11-21T21:58:15.7946Z","iopub.execute_input":"2022-11-21T21:58:15.795049Z","iopub.status.idle":"2022-11-21T21:58:15.820887Z","shell.execute_reply.started":"2022-11-21T21:58:15.795005Z","shell.execute_reply":"2022-11-21T21:58:15.819429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from numpy import asarray","metadata":{"execution":{"iopub.status.busy":"2022-11-21T21:58:17.956123Z","iopub.execute_input":"2022-11-21T21:58:17.956603Z","iopub.status.idle":"2022-11-21T21:58:17.96345Z","shell.execute_reply.started":"2022-11-21T21:58:17.956568Z","shell.execute_reply":"2022-11-21T21:58:17.961807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resized_imgs_path = \"../input/training-data/Training data\"\nimg_array = []\nfor i in os.listdir(resized_imgs_path):\n    img = resized_imgs_path + \"/\" + i\n    img = cv2.imread(img)\n    img_array.append(img)","metadata":{"execution":{"iopub.status.busy":"2022-11-22T01:11:28.54356Z","iopub.execute_input":"2022-11-22T01:11:28.544439Z","iopub.status.idle":"2022-11-22T01:11:39.465696Z","shell.execute_reply.started":"2022-11-22T01:11:28.544398Z","shell.execute_reply":"2022-11-22T01:11:39.463868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(img_array[1])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-21T22:01:46.56852Z","iopub.execute_input":"2022-11-21T22:01:46.568952Z","iopub.status.idle":"2022-11-21T22:01:46.793888Z","shell.execute_reply.started":"2022-11-21T22:01:46.568918Z","shell.execute_reply":"2022-11-21T22:01:46.792581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import LabelBinarizer\n\ntrain_y = list(data_df['isup_grade'].values)\nlb = LabelBinarizer()\ntrain_y = lb.fit_transform(train_y)","metadata":{"execution":{"iopub.status.busy":"2022-11-21T22:06:33.331858Z","iopub.execute_input":"2022-11-21T22:06:33.332286Z","iopub.status.idle":"2022-11-21T22:06:33.345826Z","shell.execute_reply.started":"2022-11-21T22:06:33.332253Z","shell.execute_reply":"2022-11-21T22:06:33.344452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_x = np.reshape(img_array, (len(img_array), 128, 128, 3))\ntrain_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])","metadata":{"execution":{"iopub.status.busy":"2022-11-22T01:11:39.468106Z","iopub.execute_input":"2022-11-22T01:11:39.46868Z","iopub.status.idle":"2022-11-22T01:11:39.82013Z","shell.execute_reply.started":"2022-11-22T01:11:39.468626Z","shell.execute_reply":"2022-11-22T01:11:39.818094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_y_multi.sum(axis=0)","metadata":{"execution":{"iopub.status.busy":"2022-11-21T22:08:39.704402Z","iopub.execute_input":"2022-11-21T22:08:39.704817Z","iopub.status.idle":"2022-11-21T22:08:39.712431Z","shell.execute_reply.started":"2022-11-21T22:08:39.704784Z","shell.execute_reply":"2022-11-21T22:08:39.711155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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)","metadata":{"execution":{"iopub.status.busy":"2022-11-21T22:09:10.59899Z","iopub.execute_input":"2022-11-21T22:09:10.599372Z","iopub.status.idle":"2022-11-21T22:09:10.639895Z","shell.execute_reply.started":"2022-11-21T22:09:10.59934Z","shell.execute_reply":"2022-11-21T22:09:10.638533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_d=DenseNet121(weights='imagenet',include_top=False, input_shape=(128, 128, 3)) \n\nx=model_d.output\n\nx= GlobalAveragePooling2D()(x)\nx= BatchNormalization()(x)\nx= Dropout(0.5)(x)\nx= Dense(1024,activation='relu')(x) \nx= Dense(512,activation='relu')(x) \nx= BatchNormalization()(x)\nx= Dropout(0.5)(x)\n\npreds=Dense(6,activation='softmax')(x) #FC-layer","metadata":{"execution":{"iopub.status.busy":"2022-11-21T22:09:35.345572Z","iopub.execute_input":"2022-11-21T22:09:35.34598Z","iopub.status.idle":"2022-11-21T22:09:38.752962Z","shell.execute_reply.started":"2022-11-21T22:09:35.345946Z","shell.execute_reply":"2022-11-21T22:09:38.751392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model=Model(inputs=model_d.input,outputs=preds)\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-11-21T22:09:38.754841Z","iopub.execute_input":"2022-11-21T22:09:38.755174Z","iopub.status.idle":"2022-11-21T22:09:38.865859Z","shell.execute_reply.started":"2022-11-21T22:09:38.755142Z","shell.execute_reply":"2022-11-21T22:09:38.863994Z"},"scrolled":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for layer in model.layers[:-8]:\n    layer.trainable=False\n    \nfor layer in model.layers[-8:]:\n    layer.trainable=True  \n\n\nmodel.compile(optimizer='Adam',loss='categorical_crossentropy',metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-11-21T22:09:42.967311Z","iopub.execute_input":"2022-11-21T22:09:42.967728Z","iopub.status.idle":"2022-11-21T22:09:43.012893Z","shell.execute_reply.started":"2022-11-21T22:09:42.967695Z","shell.execute_reply":"2022-11-21T22:09:43.011444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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)","metadata":{"execution":{"iopub.status.busy":"2022-11-21T22:14:19.875193Z","iopub.execute_input":"2022-11-21T22:14:19.87563Z","iopub.status.idle":"2022-11-21T22:14:19.883109Z","shell.execute_reply.started":"2022-11-21T22:14:19.875596Z","shell.execute_reply":"2022-11-21T22:14:19.881257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = data.flow(train_x, train_y, batch_size=10, seed=42)\nvalidation = val_x, val_y","metadata":{"execution":{"iopub.status.busy":"2022-11-21T22:14:21.394466Z","iopub.execute_input":"2022-11-21T22:14:21.394899Z","iopub.status.idle":"2022-11-21T22:14:21.468733Z","shell.execute_reply.started":"2022-11-21T22:14:21.394864Z","shell.execute_reply":"2022-11-21T22:14:21.467194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"steps = train_x.shape[0] / 10","metadata":{"execution":{"iopub.status.busy":"2022-11-21T22:14:25.929523Z","iopub.execute_input":"2022-11-21T22:14:25.929982Z","iopub.status.idle":"2022-11-21T22:14:25.936748Z","shell.execute_reply.started":"2022-11-21T22:14:25.929948Z","shell.execute_reply":"2022-11-21T22:14:25.935225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_x.shape[0]","metadata":{"execution":{"iopub.status.busy":"2022-11-22T01:14:02.882495Z","iopub.execute_input":"2022-11-22T01:14:02.882997Z","iopub.status.idle":"2022-11-22T01:14:02.894583Z","shell.execute_reply.started":"2022-11-22T01:14:02.882958Z","shell.execute_reply":"2022-11-22T01:14:02.89289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.config.run_functions_eagerly(True)","metadata":{"execution":{"iopub.status.busy":"2022-11-21T22:31:12.414088Z","iopub.execute_input":"2022-11-21T22:31:12.414502Z","iopub.status.idle":"2022-11-21T22:31:12.419428Z","shell.execute_reply.started":"2022-11-21T22:31:12.41447Z","shell.execute_reply":"2022-11-21T22:31:12.418471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(\n    data,\n    steps_per_epoch = steps,\n    epochs = 10,\n    validation_data = validation\n)","metadata":{"execution":{"iopub.status.busy":"2022-11-21T22:31:13.437355Z","iopub.execute_input":"2022-11-21T22:31:13.437826Z","iopub.status.idle":"2022-11-21T23:04:12.381927Z","shell.execute_reply.started":"2022-11-21T22:31:13.437791Z","shell.execute_reply":"2022-11-21T23:04:12.380436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['accuracy'])\nplt.plot(history.history['val_accuracy'])\nplt.title('model accuracy')\nplt.ylabel('accuracy')\nplt.xlabel('epoch')\nplt.legend(['train', 'val'], loc='upper left')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-21T23:17:38.044781Z","iopub.execute_input":"2022-11-21T23:17:38.046692Z","iopub.status.idle":"2022-11-21T23:17:38.285817Z","shell.execute_reply.started":"2022-11-21T23:17:38.046651Z","shell.execute_reply":"2022-11-21T23:17:38.284457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['loss'])\nplt.plot(history.history['val_loss'])\nplt.title('model loss')\nplt.ylabel('val_loss')\nplt.xlabel('epoch')\nplt.legend(['train', 'val'], loc='upper left')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-21T23:18:42.907894Z","iopub.execute_input":"2022-11-21T23:18:42.908299Z","iopub.status.idle":"2022-11-21T23:18:43.148794Z","shell.execute_reply.started":"2022-11-21T23:18:42.908266Z","shell.execute_reply":"2022-11-21T23:18:43.14778Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}