{"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":"!pip install -U efficientnet","metadata":{"execution":{"iopub.status.busy":"2021-07-25T09:41:24.582992Z","iopub.execute_input":"2021-07-25T09:41:24.583324Z","iopub.status.idle":"2021-07-25T09:41:31.078675Z","shell.execute_reply.started":"2021-07-25T09:41:24.583288Z","shell.execute_reply":"2021-07-25T09:41:31.077756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd \nimport os\nprint(os.listdir(\"../input\"))\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.models import Sequential, load_model\nfrom keras.layers import (Activation, Dropout, Flatten, Dense, GlobalMaxPooling2D,\n                          BatchNormalization, Input, Conv2D, GlobalAveragePooling2D,concatenate,Concatenate,multiply, LocallyConnected2D, Lambda)\nfrom keras.callbacks import ModelCheckpoint\nfrom keras import metrics\nfrom keras.optimizers import Adam \nfrom keras import backend as K\nimport keras\nfrom keras.models import Model\nimport matplotlib.pyplot as plt\nfrom efficientnet.keras import EfficientNetB3,EfficientNetB4,EfficientNetB5\nimport skimage.io\nfrom skimage.transform import resize\nimport imgaug as aug\nfrom imgaug import augmenters as iaa\nfrom tqdm import tqdm\nimport PIL\nfrom PIL import Image, ImageOps\nimport cv2\nfrom sklearn.utils import class_weight, shuffle\nfrom keras.losses import binary_crossentropy, categorical_crossentropy\n#from keras.applications.resnet50 import preprocess_input\nfrom keras.applications.densenet import DenseNet121,DenseNet169,preprocess_input\nimport keras.backend as K\nimport tensorflow as tf\nfrom sklearn.metrics import f1_score, fbeta_score, cohen_kappa_score\nfrom keras.utils import Sequence\nfrom keras.utils import to_categorical\nfrom sklearn.model_selection import train_test_split\nimport imgaug as ia\nimport keras.callbacks as callbacks\nfrom keras.callbacks import Callback\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nimport matplotlib.pyplot as plt\nfrom skimage.io import MultiImage,imsave,imread\nfrom skimage.transform import resize,rescale\nfrom skimage.color import rgb2gray\nfrom keras.layers import Input,Cropping2D,GlobalAveragePooling2D,Concatenate,Dense,Conv2D\nfrom keras.models import Model,load_model\nimport keras.applications as kl\nfrom keras.backend import name_scope\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import cohen_kappa_score\nfrom tqdm import tqdm\nimport tensorflow as tf\nfrom keras.utils import Sequence\nfrom keras.optimizers import Adam,Adamax\nfrom sklearn.utils import shuffle,class_weight\nfrom keras.utils import to_categorical\nimport efficientnet.keras as efn\n\nfrom albumentations import (\n    HorizontalFlip, IAAPerspective, ShiftScaleRotate, CLAHE, RandomRotate90,\n    Transpose, ShiftScaleRotate, Blur, OpticalDistortion, GridDistortion, HueSaturationValue,\n    IAAAdditiveGaussianNoise, GaussNoise, MotionBlur, MedianBlur, RandomBrightnessContrast, IAAPiecewiseAffine,\n    IAASharpen, IAAEmboss, Flip, OneOf, Compose\n)","metadata":{"execution":{"iopub.status.busy":"2021-07-25T09:44:33.523516Z","iopub.execute_input":"2021-07-25T09:44:33.523875Z","iopub.status.idle":"2021-07-25T09:44:33.566599Z","shell.execute_reply.started":"2021-07-25T09:44:33.523845Z","shell.execute_reply":"2021-07-25T09:44:33.565428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"main_path='../input/prostate-data/'","metadata":{"execution":{"iopub.status.busy":"2021-07-25T09:44:45.537264Z","iopub.execute_input":"2021-07-25T09:44:45.537611Z","iopub.status.idle":"2021-07-25T09:44:45.541323Z","shell.execute_reply.started":"2021-07-25T09:44:45.537577Z","shell.execute_reply":"2021-07-25T09:44:45.540413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_dim=(224,224,3)\nBATCH_SIZE=16\nEPOCHS=10","metadata":{"execution":{"iopub.status.busy":"2021-07-25T09:45:06.846509Z","iopub.execute_input":"2021-07-25T09:45:06.846889Z","iopub.status.idle":"2021-07-25T09:45:06.851441Z","shell.execute_reply.started":"2021-07-25T09:45:06.846856Z","shell.execute_reply":"2021-07-25T09:45:06.850391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df=pd.read_csv(os.path.join(main_path,'train_data.csv'))\ntest_df=pd.read_csv(os.path.join(main_path,'test2.csv'))\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-25T09:45:16.418907Z","iopub.execute_input":"2021-07-25T09:45:16.419226Z","iopub.status.idle":"2021-07-25T09:45:16.476419Z","shell.execute_reply.started":"2021-07-25T09:45:16.419195Z","shell.execute_reply":"2021-07-25T09:45:16.475697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['Gleason Score'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-07-25T09:45:28.002374Z","iopub.execute_input":"2021-07-25T09:45:28.002721Z","iopub.status.idle":"2021-07-25T09:45:28.016156Z","shell.execute_reply.started":"2021-07-25T09:45:28.002683Z","shell.execute_reply":"2021-07-25T09:45:28.013657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(641):\n    train_df['file_name'][i] = train_df['file_name'][i].replace('.png', '')\n    if train_df['Gleason Score'][i]>5:\n        train_df['Gleason Score'][i] = train_df['Gleason Score'][i]-5\n    \ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-25T09:45:36.647208Z","iopub.execute_input":"2021-07-25T09:45:36.647598Z","iopub.status.idle":"2021-07-25T09:45:36.76112Z","shell.execute_reply.started":"2021-07-25T09:45:36.64756Z","shell.execute_reply":"2021-07-25T09:45:36.759886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['Gleason Score'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-07-25T09:46:05.265392Z","iopub.execute_input":"2021-07-25T09:46:05.265744Z","iopub.status.idle":"2021-07-25T09:46:05.274661Z","shell.execute_reply.started":"2021-07-25T09:46:05.2657Z","shell.execute_reply":"2021-07-25T09:46:05.273619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rows,cols=3,3\nfig=plt.figure(figsize=(10,10))\nfor i in range(1,rows*cols+1):\n    img=MultiImage(os.path.join(main_path,'Gleason_train/Gleason_train',train_df.loc[i-1,'file_name']+'.jpg'))\n    img = resize(img[-1], (512, 512))\n    fig.add_subplot(rows,cols,i)\n    plt.imshow(img)\n    plt.title('Gleason Score: '+str(train_df.loc[i-1,'Gleason Score']))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-25T09:45:50.614405Z","iopub.execute_input":"2021-07-25T09:45:50.614809Z","iopub.status.idle":"2021-07-25T09:46:05.264168Z","shell.execute_reply.started":"2021-07-25T09:45:50.614763Z","shell.execute_reply":"2021-07-25T09:46:05.263171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"colums=['file_name','Gleason Score']\ntrain_df,val_df=train_test_split(train_df[colums],test_size=0.15)\nprint('Train shape: {}'.format(train_df.shape))\nprint('Validation shape: {}'.format(val_df.shape))","metadata":{"execution":{"iopub.status.busy":"2021-07-25T09:46:15.287954Z","iopub.execute_input":"2021-07-25T09:46:15.28829Z","iopub.status.idle":"2021-07-25T09:46:15.30003Z","shell.execute_reply.started":"2021-07-25T09:46:15.288258Z","shell.execute_reply":"2021-07-25T09:46:15.299234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Generator(Sequence):\n    def __init__(self,input_data,batch_size=BATCH_SIZE,dims=image_dim,is_shuffle=True,n_classes=6,is_train=True):\n        self.image_ids=input_data[0]\n        self.labels=input_data[1]\n        self.batch_size=batch_size\n        self.dims=image_dim\n        self.shuffle=is_shuffle\n        self.n_classes=n_classes\n        self.is_train=is_train\n        self.on_epoch_end()\n    \n    def __len__(self):\n        return int(np.floor(len(self.image_ids) / self.batch_size))\n    \n    def on_epoch_end(self):\n        self.indexes = np.arange(len(self.image_ids))\n        if self.shuffle == True:\n            np.random.shuffle(self.indexes)\n    \n    def __getitem__(self, index):\n        indexes = self.indexes[index*self.batch_size:(index+1)*self.batch_size]\n\n        image_ids_temp = [self.image_ids[k] for k in indexes]\n        labels_temp = [self.labels[k] for k in indexes]\n\n        # Generate data\n        X, y = self.__data_generation(image_ids_temp,labels_temp)\n\n        return X, y\n    \n    def augment_flips_color(self,p=.5):\n        return Compose([\n            Flip(),\n            RandomRotate90(),\n            Transpose(),\n            HorizontalFlip(),\n            ShiftScaleRotate(shift_limit=0.0625, scale_limit=0.50, rotate_limit=45, p=.75),\n            Blur(blur_limit=3),\n        ], p=p)\n    \n    def __data_generation(self, list_IDs_temp,lbls):\n        X = np.zeros((self.batch_size, *self.dims))\n        y = np.zeros((self.batch_size), dtype=int)\n\n        # Generate data\n        for i, ID in enumerate(list_IDs_temp):\n            # Store sample\n            img=MultiImage(os.path.join(main_path,'Gleason_train/Gleason_train',ID+'.jpg'))\n            img = resize(img[-1], (self.dims[0], self.dims[1]))\n            #Augmentation\n            if self.is_train:\n                aug = self.augment_flips_color(p=1)\n                img = aug(image=img)['image']\n                \n            X[i] = img\n\n            # Store class\n            y[i] = lbls[i]\n\n        return X, to_categorical(y, num_classes=self.n_classes)","metadata":{"execution":{"iopub.status.busy":"2021-07-25T09:46:24.821254Z","iopub.execute_input":"2021-07-25T09:46:24.821595Z","iopub.status.idle":"2021-07-25T09:46:24.834702Z","shell.execute_reply.started":"2021-07-25T09:46:24.821562Z","shell.execute_reply":"2021-07-25T09:46:24.833791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_gen=Generator([train_df['file_name'].values,train_df['Gleason Score'].values])\nval_gen=Generator([val_df['file_name'].values,val_df['Gleason Score'].values],is_shuffle=False,is_train=False)","metadata":{"execution":{"iopub.status.busy":"2021-07-25T09:46:34.094121Z","iopub.execute_input":"2021-07-25T09:46:34.094451Z","iopub.status.idle":"2021-07-25T09:46:34.101792Z","shell.execute_reply.started":"2021-07-25T09:46:34.094418Z","shell.execute_reply":"2021-07-25T09:46:34.100629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class QWKEvaluation(Callback):\n    def __init__(self, validation_data=(), batch_size=BATCH_SIZE, interval=1):\n        super(Callback, self).__init__()\n\n        self.interval = interval\n        self.batch_size = batch_size\n        self.valid_generator, self.y_val = validation_data\n        self.history = []\n\n    def on_epoch_end(self, epoch, logs={}):\n        if epoch % self.interval == 0:\n            y_pred = self.model.predict_generator(generator=self.valid_generator,\n                                                  steps=np.ceil(float(len(self.y_val)) / float(self.batch_size)),\n                                                  workers=1, use_multiprocessing=False,\n                                                  verbose=1)\n            def flatten(y):\n                return np.argmax(y, axis=1).reshape(-1)\n            \n            score = cohen_kappa_score(self.y_val,\n                                      flatten(y_pred),\n                                      labels=[0,1,2,3,4,5],\n                                      weights='quadratic')\n            print(\"\\n epoch: %d - QWK_score: %.6f \\n\" % (epoch+1, score))\n            self.history.append(score)\n            if score >= max(self.history):\n                print('saving checkpoint: ', score)\n                self.model.save('classifier.h5')","metadata":{"execution":{"iopub.status.busy":"2021-07-25T09:46:45.831624Z","iopub.execute_input":"2021-07-25T09:46:45.831955Z","iopub.status.idle":"2021-07-25T09:46:45.840189Z","shell.execute_reply.started":"2021-07-25T09:46:45.831925Z","shell.execute_reply":"2021-07-25T09:46:45.839389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"qwk = QWKEvaluation(validation_data=(val_gen, np.asarray(val_df['Gleason Score'][:val_gen.__len__()*BATCH_SIZE])),\n                    batch_size=BATCH_SIZE, interval=1)","metadata":{"execution":{"iopub.status.busy":"2021-07-25T09:46:56.6337Z","iopub.execute_input":"2021-07-25T09:46:56.634061Z","iopub.status.idle":"2021-07-25T09:46:56.639182Z","shell.execute_reply.started":"2021-07-25T09:46:56.63403Z","shell.execute_reply":"2021-07-25T09:46:56.638114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"in_lay = Input(shape=image_dim)\nbase_model = EfficientNetB5(weights=None, input_tensor = in_lay, include_top=False)\nbase_model.load_weights(\"../input/efficientnet-keras-weights-b0b5/efficientnet-b5_imagenet_1000_notop.h5\")\n\npt_features = base_model(in_lay)\npt_depth = base_model.get_output_shape_at(0)[-1]\nbn_features = BatchNormalization()(pt_features)\n\n#%% [markdown]\n# ## Attention model\n\n#%%\n# here we do an attention mechanism to turn pixels in the GAP on an off\nattn_layer = Conv2D(64, kernel_size = (1,1), padding = 'same', activation = 'relu')(Dropout(0.5)(bn_features))\nattn_layer = Conv2D(16, kernel_size = (1,1), padding = 'same', activation = 'relu')(attn_layer)\nattn_layer = Conv2D(8, kernel_size = (1,1), padding = 'same', activation = 'relu')(attn_layer)\nattn_layer = Conv2D(1, \n                    kernel_size = (1,1), \n                    padding = 'valid', \n                    activation = 'sigmoid')(attn_layer)\n# fan it out to all of the channels\nup_c2_w = np.ones((1, 1, 1, pt_depth))\nup_c2 = Conv2D(pt_depth, kernel_size = (1,1), padding = 'same', \n               activation = 'linear', use_bias = False, weights = [up_c2_w])\nup_c2.trainable = False\nattn_layer = up_c2(attn_layer)\n\nmask_features = multiply([attn_layer, bn_features])\ngap_features = GlobalAveragePooling2D()(mask_features)\ngap_mask = GlobalAveragePooling2D()(attn_layer)\n# to account for missing values from the attention model\ngap = Lambda(lambda x: x[0]/x[1], name = 'RescaleGAP')([gap_features, gap_mask])\ngap_dr = Dropout(0.25)(gap)\ndr_steps = Dropout(0.25)(Dense(128, activation = 'relu')(gap_dr))\nout_layer = Dense(6, activation = 'softmax')(dr_steps)\nretina_model = Model(inputs = [in_lay], outputs = [out_layer])","metadata":{"execution":{"iopub.status.busy":"2021-07-25T09:56:10.53031Z","iopub.execute_input":"2021-07-25T09:56:10.530676Z","iopub.status.idle":"2021-07-25T09:56:18.051849Z","shell.execute_reply.started":"2021-07-25T09:56:10.53064Z","shell.execute_reply":"2021-07-25T09:56:18.051005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"retina_model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-07-25T09:56:36.465054Z","iopub.execute_input":"2021-07-25T09:56:36.46539Z","iopub.status.idle":"2021-07-25T09:56:36.510113Z","shell.execute_reply.started":"2021-07-25T09:56:36.465359Z","shell.execute_reply":"2021-07-25T09:56:36.509351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.callbacks import (ModelCheckpoint, LearningRateScheduler,\n                             EarlyStopping, ReduceLROnPlateau,CSVLogger)\n\nepochs = 10; batch_size = 16\ncheckpoint = ModelCheckpoint('../working/model_.h5', monitor='val_loss', verbose=1, \n                             save_best_only=True, mode='min', save_weights_only = True)\nreduceLROnPlat = ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=4, \n                                   verbose=1, mode='auto', epsilon=0.0001)\nearly = EarlyStopping(monitor=\"val_loss\", \n                      mode=\"min\", \n                      patience=4)\ncsv_logger = CSVLogger(filename='../working/training_log.csv',\n                       separator=',',\n                       append=True)","metadata":{"execution":{"iopub.status.busy":"2021-07-25T09:58:35.246353Z","iopub.execute_input":"2021-07-25T09:58:35.246794Z","iopub.status.idle":"2021-07-25T09:58:35.255688Z","shell.execute_reply.started":"2021-07-25T09:58:35.246756Z","shell.execute_reply":"2021-07-25T09:58:35.254667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def kappa_loss(y_true, y_pred, y_pow=2, eps=1e-12, N=5, bsize=32, name='kappa'):\n    with tf.name_scope(name):\n        y_true = tf.to_float(y_true)\n        repeat_op = tf.to_float(tf.tile(tf.reshape(tf.range(0, N), [N, 1]), [1, N]))\n        repeat_op_sq = tf.square((repeat_op - tf.transpose(repeat_op)))\n        weights = repeat_op_sq / tf.to_float((N - 1) ** 2)\n    \n        pred_ = y_pred ** y_pow\n        try:\n            pred_norm = pred_ / (eps + tf.reshape(tf.reduce_sum(pred_, 1), [-1, 1]))\n        except Exception:\n            pred_norm = pred_ / (eps + tf.reshape(tf.reduce_sum(pred_, 1), [bsize, 1]))\n    \n        hist_rater_a = tf.reduce_sum(pred_norm, 0)\n        hist_rater_b = tf.reduce_sum(y_true, 0)\n    \n        conf_mat = tf.matmul(tf.transpose(pred_norm), y_true)\n    \n        nom = tf.reduce_sum(weights * conf_mat)\n        denom = tf.reduce_sum(weights * tf.matmul(\n            tf.reshape(hist_rater_a, [N, 1]), tf.reshape(hist_rater_b, [1, N])) /\n                              tf.to_float(bsize))\n    \n        return nom*0.5 / (denom + eps) + categorical_crossentropy(y_true, y_pred)*0.5\n\n\n#%%\nfrom keras.callbacks import Callback\nclass QWKEvaluation(Callback):\n    def __init__(self, validation_data=(), batch_size=BATCH_SIZE, interval=1):\n        super(Callback, self).__init__()\n\n        self.interval = interval\n        self.batch_size = batch_size\n        self.valid_generator, self.y_val = validation_data\n        self.history = []\n\n    def on_epoch_end(self, epoch, logs={}):\n        if epoch % self.interval == 0:\n            y_pred = self.model.predict_generator(generator=self.valid_generator,\n                                                  steps=np.ceil(float(len(self.y_val)) / float(self.batch_size)),\n                                                  workers=1, use_multiprocessing=False,\n                                                  verbose=1)\n            def flatten(y):\n                return np.argmax(y, axis=1).reshape(-1)\n            \n            score = cohen_kappa_score(self.y_val,\n                                      flatten(y_pred),\n                                      labels=[0,1,2,3,4,5],\n                                      weights='quadratic')\n            print(\"\\n epoch: %d - QWK_score: %.6f \\n\" % (epoch+1, score))\n            self.history.append(score)\n            if score >= max(self.history):\n                print('saving checkpoint: ', score)\n                self.model.save('classifier.h5')","metadata":{"execution":{"iopub.status.busy":"2021-07-25T11:31:55.213289Z","iopub.execute_input":"2021-07-25T11:31:55.213636Z","iopub.status.idle":"2021-07-25T11:31:55.229546Z","shell.execute_reply.started":"2021-07-25T11:31:55.213601Z","shell.execute_reply":"2021-07-25T11:31:55.228622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"qwk = QWKEvaluation(validation_data=(val_gen, np.asarray(val_df['Gleason Score'][:val_gen.__len__()*BATCH_SIZE])),\n                    batch_size=batch_size, interval=1)","metadata":{"execution":{"iopub.status.busy":"2021-07-25T11:31:55.399843Z","iopub.execute_input":"2021-07-25T11:31:55.400105Z","iopub.status.idle":"2021-07-25T11:31:55.404758Z","shell.execute_reply.started":"2021-07-25T11:31:55.400079Z","shell.execute_reply":"2021-07-25T11:31:55.403638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for layer in retina_model.layers:\n    layer.trainable = True\ncallbacks_list = [checkpoint, csv_logger, reduceLROnPlat, early, qwk]\nretina_model.compile(loss='categorical_crossentropy',\n#             loss=kappa_loss,\n            optimizer=Adam(lr=1e-4), metrics = ['accuracy'])\nretina_model.fit_generator(\n    train_gen,\n#     steps_per_epoch=np.ceil(float(len(train_x)) / float(batch_size)),\n    validation_data=val_gen,\n#     validation_steps=np.ceil(float(len(valid_x)) / float(batch_size)),\n    epochs=epochs,\n    verbose=1,\n    workers=1, use_multiprocessing=False,\n    callbacks=callbacks_list)","metadata":{"execution":{"iopub.status.busy":"2021-07-25T11:31:55.696371Z","iopub.execute_input":"2021-07-25T11:31:55.696705Z","iopub.status.idle":"2021-07-25T16:26:44.569126Z","shell.execute_reply.started":"2021-07-25T11:31:55.696675Z","shell.execute_reply":"2021-07-25T16:26:44.568289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss = retina_model.history.history['loss']\nval_loss = retina_model.history.history['val_loss']\nacc = retina_model.history.history['accuracy']\nval_acc = retina_model.history.history['val_accuracy']\nscore=qwk.history\nepochs=range(1,len(loss)+1)\nplt.plot(epochs,loss,'b',color='red',label='Training Loss')\nplt.plot(epochs,val_loss,'b',color='blue',label='Validation Loss')\nplt.title('Training and Validation Loss')\nplt.legend()\nplt.figure()\nplt.plot(epochs,acc,'b',color='red',label='Training Accuracy')\nplt.plot(epochs,val_acc,'b',color='blue',label='Validation Accuracy')\nplt.title('Training and Validation Accuracy')\nplt.legend()\nplt.figure()\nplt.plot(epochs,score,'b',color='red',label='Validation Kappa')\nplt.legend()\nplt.figure()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-25T16:35:34.344717Z","iopub.execute_input":"2021-07-25T16:35:34.345051Z","iopub.status.idle":"2021-07-25T16:35:34.74179Z","shell.execute_reply.started":"2021-07-25T16:35:34.345021Z","shell.execute_reply":"2021-07-25T16:35:34.741036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-25T16:39:19.195479Z","iopub.execute_input":"2021-07-25T16:39:19.195874Z","iopub.status.idle":"2021-07-25T16:39:19.210664Z","shell.execute_reply.started":"2021-07-25T16:39:19.195841Z","shell.execute_reply":"2021-07-25T16:39:19.209465Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dir='../input/prostate-data/Test_data/Test_data/Test_images'\nif os.path.exists(test_dir):\n    model=load_model('./classifier.h5')\n    predicted=[]\n    for ID in test_df['file_name']:\n        file = ID.replace('mask2_', '')\n        file = file.replace('.png', '')\n#         print(file)\n        img=MultiImage(os.path.join(test_dir,file+'.jpg'))\n        img = resize(img[-1], (image_dim[0], image_dim[1]))\n        preds=model.predict(np.expand_dims(img,0))\n        preds = np.argmax(preds)\n        predicted.append(preds)\n        \n    submission=pd.DataFrame({'file_name':test_df['file_name'],'Gleason Score':predicted})\n    submission.to_csv('submission1.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2021-07-25T17:04:43.037318Z","iopub.execute_input":"2021-07-25T17:04:43.037661Z","iopub.status.idle":"2021-07-25T17:15:02.727542Z","shell.execute_reply.started":"2021-07-25T17:04:43.03763Z","shell.execute_reply":"2021-07-25T17:15:02.726551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"res = pd.read_csv('./submission1.csv')","metadata":{"execution":{"iopub.status.busy":"2021-07-25T17:38:13.235244Z","iopub.execute_input":"2021-07-25T17:38:13.235624Z","iopub.status.idle":"2021-07-25T17:38:13.244014Z","shell.execute_reply.started":"2021-07-25T17:38:13.235589Z","shell.execute_reply":"2021-07-25T17:38:13.243191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.read_csv('../input/prostate-data/test2.csv')","metadata":{"execution":{"iopub.status.busy":"2021-07-25T17:42:01.432276Z","iopub.execute_input":"2021-07-25T17:42:01.432633Z","iopub.status.idle":"2021-07-25T17:42:01.454229Z","shell.execute_reply.started":"2021-07-25T17:42:01.432601Z","shell.execute_reply":"2021-07-25T17:42:01.453442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(245):\n    if(test['Gleason Score'][i] >5):\n        test['Gleason Score'][i] = test['Gleason Score'][i]-5\n    else:\n        continue","metadata":{"execution":{"iopub.status.busy":"2021-07-25T17:42:24.362826Z","iopub.execute_input":"2021-07-25T17:42:24.363148Z","iopub.status.idle":"2021-07-25T17:42:24.397116Z","shell.execute_reply.started":"2021-07-25T17:42:24.363117Z","shell.execute_reply":"2021-07-25T17:42:24.396327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"res.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-25T17:41:10.26492Z","iopub.execute_input":"2021-07-25T17:41:10.265232Z","iopub.status.idle":"2021-07-25T17:41:10.273615Z","shell.execute_reply.started":"2021-07-25T17:41:10.265203Z","shell.execute_reply":"2021-07-25T17:41:10.272669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_actual = test['Gleason Score']\nX_pred = res['Gleason Score']","metadata":{"execution":{"iopub.status.busy":"2021-07-25T17:43:09.0729Z","iopub.execute_input":"2021-07-25T17:43:09.07327Z","iopub.status.idle":"2021-07-25T17:43:09.078283Z","shell.execute_reply.started":"2021-07-25T17:43:09.073236Z","shell.execute_reply":"2021-07-25T17:43:09.076831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"res['Gleason Score'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-07-25T17:42:33.460896Z","iopub.execute_input":"2021-07-25T17:42:33.461217Z","iopub.status.idle":"2021-07-25T17:42:33.468081Z","shell.execute_reply.started":"2021-07-25T17:42:33.461188Z","shell.execute_reply":"2021-07-25T17:42:33.46696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test['Gleason Score'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-07-25T17:42:28.281234Z","iopub.execute_input":"2021-07-25T17:42:28.281567Z","iopub.status.idle":"2021-07-25T17:42:28.289195Z","shell.execute_reply.started":"2021-07-25T17:42:28.281518Z","shell.execute_reply":"2021-07-25T17:42:28.288127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(245):\n    if(test_df['Gleason Score'][i] == 0):\n        test_df['Gleason Score'][i] = 1\ntest_df['Gleason Score'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-07-25T17:27:16.989095Z","iopub.execute_input":"2021-07-25T17:27:16.989563Z","iopub.status.idle":"2021-07-25T17:27:17.00451Z","shell.execute_reply.started":"2021-07-25T17:27:16.989501Z","shell.execute_reply":"2021-07-25T17:27:17.003549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(245):\n    if(res['Gleason Score'][i] == 0):\n        res['Gleason Score'][i] = 1\nres['Gleason Score'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-07-25T17:27:49.28535Z","iopub.execute_input":"2021-07-25T17:27:49.285704Z","iopub.status.idle":"2021-07-25T17:27:49.29782Z","shell.execute_reply.started":"2021-07-25T17:27:49.28567Z","shell.execute_reply":"2021-07-25T17:27:49.296691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_actual.fillna(1, inplace=True)\nX_actual.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2021-07-25T17:43:13.755155Z","iopub.execute_input":"2021-07-25T17:43:13.755509Z","iopub.status.idle":"2021-07-25T17:43:13.762524Z","shell.execute_reply.started":"2021-07-25T17:43:13.755476Z","shell.execute_reply":"2021-07-25T17:43:13.76138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix\nx = confusion_matrix(X_actual, X_pred)\nx","metadata":{"execution":{"iopub.status.busy":"2021-07-25T17:43:17.213896Z","iopub.execute_input":"2021-07-25T17:43:17.214213Z","iopub.status.idle":"2021-07-25T17:43:17.225521Z","shell.execute_reply.started":"2021-07-25T17:43:17.214185Z","shell.execute_reply":"2021-07-25T17:43:17.224703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"N = len(X_actual)\nN","metadata":{"execution":{"iopub.status.busy":"2021-07-25T17:43:22.461278Z","iopub.execute_input":"2021-07-25T17:43:22.461609Z","iopub.status.idle":"2021-07-25T17:43:22.469906Z","shell.execute_reply.started":"2021-07-25T17:43:22.461578Z","shell.execute_reply":"2021-07-25T17:43:22.468819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"w = np.zeros((6, 6))\nw","metadata":{"execution":{"iopub.status.busy":"2021-07-25T17:43:23.871995Z","iopub.execute_input":"2021-07-25T17:43:23.872419Z","iopub.status.idle":"2021-07-25T17:43:23.880042Z","shell.execute_reply.started":"2021-07-25T17:43:23.872377Z","shell.execute_reply":"2021-07-25T17:43:23.879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(len(w)):\n    for j in range(len(w)):\n        w[i][j] = float(((i-j)**2)/((N-1)**2))","metadata":{"execution":{"iopub.status.busy":"2021-07-25T17:43:26.476448Z","iopub.execute_input":"2021-07-25T17:43:26.476819Z","iopub.status.idle":"2021-07-25T17:43:26.481124Z","shell.execute_reply.started":"2021-07-25T17:43:26.476788Z","shell.execute_reply":"2021-07-25T17:43:26.480312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"w","metadata":{"execution":{"iopub.status.busy":"2021-07-25T17:43:27.640149Z","iopub.execute_input":"2021-07-25T17:43:27.640484Z","iopub.status.idle":"2021-07-25T17:43:27.645751Z","shell.execute_reply.started":"2021-07-25T17:43:27.640453Z","shell.execute_reply":"2021-07-25T17:43:27.644876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"N = 6","metadata":{"execution":{"iopub.status.busy":"2021-07-25T17:43:30.807896Z","iopub.execute_input":"2021-07-25T17:43:30.808281Z","iopub.status.idle":"2021-07-25T17:43:30.812348Z","shell.execute_reply.started":"2021-07-25T17:43:30.808249Z","shell.execute_reply":"2021-07-25T17:43:30.811331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2021-07-25T17:45:33.217662Z","iopub.execute_input":"2021-07-25T17:45:33.218002Z","iopub.status.idle":"2021-07-25T17:45:33.226086Z","shell.execute_reply.started":"2021-07-25T17:45:33.217973Z","shell.execute_reply":"2021-07-25T17:45:33.225026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# calculation of actual histogram vector\nX_actual_hist=np.zeros([N]) ","metadata":{"execution":{"iopub.status.busy":"2021-07-25T17:45:05.395438Z","iopub.execute_input":"2021-07-25T17:45:05.395813Z","iopub.status.idle":"2021-07-25T17:45:05.420975Z","shell.execute_reply.started":"2021-07-25T17:45:05.39578Z","shell.execute_reply":"2021-07-25T17:45:05.419484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_actual_hist[0] = 10\nX_actual_hist[1] = 29\nX_actual_hist[2] = 81\nX_actual_hist[3] = 86\nX_actual_hist[4] = 26\nX_actual_hist[5] = 13","metadata":{"execution":{"iopub.status.busy":"2021-07-25T17:50:27.314344Z","iopub.execute_input":"2021-07-25T17:50:27.314682Z","iopub.status.idle":"2021-07-25T17:50:27.318918Z","shell.execute_reply.started":"2021-07-25T17:50:27.31465Z","shell.execute_reply":"2021-07-25T17:50:27.318071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Actuals value counts : {}'.format(X_actual_hist))","metadata":{"execution":{"iopub.status.busy":"2021-07-25T17:50:30.646058Z","iopub.execute_input":"2021-07-25T17:50:30.646377Z","iopub.status.idle":"2021-07-25T17:50:30.651711Z","shell.execute_reply.started":"2021-07-25T17:50:30.646347Z","shell.execute_reply":"2021-07-25T17:50:30.650847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# calculation of predicted histogram vector\nX_pred_hist=np.zeros([N]) \nX_pred_hist[0] = 9\nX_pred_hist[1] = 153\nX_pred_hist[2] = 32\nX_pred_hist[3] = 34\nX_pred_hist[4] = 6\nX_pred_hist[5] = 11","metadata":{"execution":{"iopub.status.busy":"2021-07-25T17:52:23.178972Z","iopub.execute_input":"2021-07-25T17:52:23.179305Z","iopub.status.idle":"2021-07-25T17:52:23.186177Z","shell.execute_reply.started":"2021-07-25T17:52:23.179275Z","shell.execute_reply":"2021-07-25T17:52:23.185419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"E = np.outer(X_actual_hist, X_pred_hist)\nE","metadata":{"execution":{"iopub.status.busy":"2021-07-25T17:52:36.621953Z","iopub.execute_input":"2021-07-25T17:52:36.622296Z","iopub.status.idle":"2021-07-25T17:52:36.629761Z","shell.execute_reply.started":"2021-07-25T17:52:36.622267Z","shell.execute_reply":"2021-07-25T17:52:36.628781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"E = E/E.sum()\nE.sum()","metadata":{"execution":{"iopub.status.busy":"2021-07-25T17:53:03.005892Z","iopub.execute_input":"2021-07-25T17:53:03.006258Z","iopub.status.idle":"2021-07-25T17:53:03.012793Z","shell.execute_reply.started":"2021-07-25T17:53:03.006225Z","shell.execute_reply":"2021-07-25T17:53:03.011772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = x/x.sum()\nx.sum()","metadata":{"execution":{"iopub.status.busy":"2021-07-25T17:53:42.876817Z","iopub.execute_input":"2021-07-25T17:53:42.877144Z","iopub.status.idle":"2021-07-25T17:53:42.882735Z","shell.execute_reply.started":"2021-07-25T17:53:42.877115Z","shell.execute_reply":"2021-07-25T17:53:42.881694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"E","metadata":{"execution":{"iopub.status.busy":"2021-07-25T17:53:52.666729Z","iopub.execute_input":"2021-07-25T17:53:52.667087Z","iopub.status.idle":"2021-07-25T17:53:52.673454Z","shell.execute_reply.started":"2021-07-25T17:53:52.667057Z","shell.execute_reply":"2021-07-25T17:53:52.672305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x","metadata":{"execution":{"iopub.status.busy":"2021-07-25T17:53:56.289091Z","iopub.execute_input":"2021-07-25T17:53:56.289443Z","iopub.status.idle":"2021-07-25T17:53:56.297786Z","shell.execute_reply.started":"2021-07-25T17:53:56.289412Z","shell.execute_reply":"2021-07-25T17:53:56.296717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Num=0\nDen=0\n\nfor i in range(len(w)):\n    for j in range(len(w)):\n        Num+=w[i][j]*x[i][j]\n        Den+=w[i][j]*E[i][j]\n        \nRes = Num/Den\n \nQWK = (1 - Res)\nprint('The QWK value is {}'.format(round(QWK,4)))","metadata":{"execution":{"iopub.status.busy":"2021-07-25T17:54:17.400768Z","iopub.execute_input":"2021-07-25T17:54:17.401106Z","iopub.status.idle":"2021-07-25T17:54:17.409243Z","shell.execute_reply.started":"2021-07-25T17:54:17.401079Z","shell.execute_reply":"2021-07-25T17:54:17.407214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}