{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":18647,"databundleVersionId":1126921,"sourceType":"competition"}],"dockerImageVersionId":29867,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import sys\nprint(sys.version)","metadata":{"execution":{"iopub.status.busy":"2024-02-29T10:39:08.277789Z","iopub.execute_input":"2024-02-29T10:39:08.278014Z","iopub.status.idle":"2024-02-29T10:39:08.282146Z","shell.execute_reply.started":"2024-02-29T10:39:08.277989Z","shell.execute_reply":"2024-02-29T10:39:08.281272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install -U efficientnet","metadata":{"execution":{"iopub.status.busy":"2024-02-29T10:39:08.283356Z","iopub.execute_input":"2024-02-29T10:39:08.283568Z","iopub.status.idle":"2024-02-29T10:39:17.22398Z","shell.execute_reply.started":"2024-02-29T10:39:08.283544Z","shell.execute_reply":"2024-02-29T10:39:17.223247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import 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":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-02-29T10:39:17.225543Z","iopub.execute_input":"2024-02-29T10:39:17.225864Z","iopub.status.idle":"2024-02-29T10:39:25.983067Z","shell.execute_reply.started":"2024-02-29T10:39:17.225821Z","shell.execute_reply":"2024-02-29T10:39:25.98247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import keras\nimport skimage","metadata":{"execution":{"iopub.status.busy":"2024-02-29T10:39:25.984259Z","iopub.execute_input":"2024-02-29T10:39:25.984476Z","iopub.status.idle":"2024-02-29T10:39:25.99596Z","shell.execute_reply.started":"2024-02-29T10:39:25.984452Z","shell.execute_reply":"2024-02-29T10:39:25.995199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('skimage version: ',skimage.__version__)\nprint('keras version:',keras.__version__)","metadata":{"execution":{"iopub.status.busy":"2024-02-29T10:39:25.997503Z","iopub.execute_input":"2024-02-29T10:39:25.99778Z","iopub.status.idle":"2024-02-29T10:39:26.008311Z","shell.execute_reply.started":"2024-02-29T10:39:25.997747Z","shell.execute_reply":"2024-02-29T10:39:26.007629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"main_path='../input/prostate-cancer-grade-assessment/'","metadata":{"execution":{"iopub.status.busy":"2024-02-29T10:39:26.009487Z","iopub.execute_input":"2024-02-29T10:39:26.009767Z","iopub.status.idle":"2024-02-29T10:39:26.01828Z","shell.execute_reply.started":"2024-02-29T10:39:26.009732Z","shell.execute_reply":"2024-02-29T10:39:26.017717Z"},"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":"2024-02-29T10:39:26.019696Z","iopub.execute_input":"2024-02-29T10:39:26.019901Z","iopub.status.idle":"2024-02-29T10:39:26.030281Z","shell.execute_reply.started":"2024-02-29T10:39:26.019879Z","shell.execute_reply":"2024-02-29T10:39:26.029542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df=pd.read_csv(os.path.join(main_path,'train.csv'))\ntest_df=pd.read_csv(os.path.join(main_path,'test.csv'))\ntrain_df.head()","metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","execution":{"iopub.status.busy":"2024-02-29T10:39:26.031539Z","iopub.execute_input":"2024-02-29T10:39:26.031803Z","iopub.status.idle":"2024-02-29T10:39:26.102236Z","shell.execute_reply.started":"2024-02-29T10:39:26.031777Z","shell.execute_reply":"2024-02-29T10:39:26.101569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.gleason_score.value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-02-29T10:39:26.105933Z","iopub.execute_input":"2024-02-29T10:39:26.106165Z","iopub.status.idle":"2024-02-29T10:39:26.116872Z","shell.execute_reply.started":"2024-02-29T10:39:26.106116Z","shell.execute_reply":"2024-02-29T10:39:26.116033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mapper = {'3+3':1,'0+0':0,'3+4':2,'4+3':3,'4+4':4,'negative':0,'4+5':5,'5+4':6,'5+5':7,'3+5':8,'5+3':9}\ntrain_df['gleason_mapper'] = train_df.gleason_score.map(mapper)\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-02-29T10:39:26.118512Z","iopub.execute_input":"2024-02-29T10:39:26.1188Z","iopub.status.idle":"2024-02-29T10:39:26.139085Z","shell.execute_reply.started":"2024-02-29T10:39:26.118764Z","shell.execute_reply":"2024-02-29T10:39:26.138331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.loc[train_df.gleason_score=='negative']","metadata":{"execution":{"iopub.status.busy":"2024-02-29T10:39:26.140852Z","iopub.execute_input":"2024-02-29T10:39:26.14117Z","iopub.status.idle":"2024-02-29T10:39:26.163013Z","shell.execute_reply.started":"2024-02-29T10:39:26.14111Z","shell.execute_reply":"2024-02-29T10:39:26.162248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Lets take a look at isup_grade(target feature)\ntrain_df['gleason_mapper'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-02-29T10:39:26.164216Z","iopub.execute_input":"2024-02-29T10:39:26.164507Z","iopub.status.idle":"2024-02-29T10:39:26.171247Z","shell.execute_reply.started":"2024-02-29T10:39:26.164471Z","shell.execute_reply":"2024-02-29T10:39:26.170536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Plot some slides\nrows,cols=3,4\nfig=plt.figure(figsize=(10,10))\nfor i in range(1,rows*cols+1):\n    img=MultiImage(os.path.join(main_path,'train_images',train_df.loc[i-1,'image_id']+'.tiff'))\n    img = resize(img[-1], (512, 512))\n    fig.add_subplot(rows,cols,i)\n    plt.imshow(img)\n    plt.title('gleason_mapper: '+str(train_df.loc[i-1,'gleason_mapper']))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-02-29T10:39:26.172214Z","iopub.execute_input":"2024-02-29T10:39:26.172438Z","iopub.status.idle":"2024-02-29T10:39:32.060275Z","shell.execute_reply.started":"2024-02-29T10:39:26.172414Z","shell.execute_reply":"2024-02-29T10:39:32.059501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Convert and save images**","metadata":{}},{"cell_type":"code","source":"#for id_ in tqdm(train_df['image_id']):\n#    img=MultiImage(os.path.join(main_path,'train_images',id_+'.tiff'))\n#    img = resize(img[-1], (1024, 1024))\n#    imsave(id_+'.jpg',img)","metadata":{"execution":{"iopub.status.busy":"2024-02-29T10:39:32.061349Z","iopub.execute_input":"2024-02-29T10:39:32.061564Z","iopub.status.idle":"2024-02-29T10:39:32.064706Z","shell.execute_reply.started":"2024-02-29T10:39:32.061539Z","shell.execute_reply":"2024-02-29T10:39:32.063983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"colums=['image_id','gleason_mapper']\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":"2024-02-29T10:39:32.065807Z","iopub.execute_input":"2024-02-29T10:39:32.066069Z","iopub.status.idle":"2024-02-29T10:39:32.080892Z","shell.execute_reply.started":"2024-02-29T10:39:32.066034Z","shell.execute_reply":"2024-02-29T10:39:32.08005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sorted(train_df.gleason_mapper.unique())","metadata":{"execution":{"iopub.status.busy":"2024-02-29T10:39:32.082204Z","iopub.execute_input":"2024-02-29T10:39:32.082488Z","iopub.status.idle":"2024-02-29T10:39:32.090225Z","shell.execute_reply.started":"2024-02-29T10:39:32.082453Z","shell.execute_reply":"2024-02-29T10:39:32.089431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sorted(val_df.gleason_mapper.unique())","metadata":{"execution":{"iopub.status.busy":"2024-02-29T10:39:32.091363Z","iopub.execute_input":"2024-02-29T10:39:32.091556Z","iopub.status.idle":"2024-02-29T10:39:32.10037Z","shell.execute_reply.started":"2024-02-29T10:39:32.091534Z","shell.execute_reply":"2024-02-29T10:39:32.099449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Generator**","metadata":{}},{"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=10,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,'train_images',ID+'.tiff'))\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":"2024-02-29T10:39:32.101569Z","iopub.execute_input":"2024-02-29T10:39:32.101859Z","iopub.status.idle":"2024-02-29T10:39:32.122806Z","shell.execute_reply.started":"2024-02-29T10:39:32.101823Z","shell.execute_reply":"2024-02-29T10:39:32.122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_gen=Generator([train_df['image_id'].values,train_df['gleason_mapper'].values])\nval_gen=Generator([val_df['image_id'].values,val_df['gleason_mapper'].values],is_shuffle=False,is_train=False)","metadata":{"execution":{"iopub.status.busy":"2024-02-29T10:39:32.123837Z","iopub.execute_input":"2024-02-29T10:39:32.124036Z","iopub.status.idle":"2024-02-29T10:39:32.136836Z","shell.execute_reply.started":"2024-02-29T10:39:32.124013Z","shell.execute_reply":"2024-02-29T10:39:32.136213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_gen","metadata":{"execution":{"iopub.status.busy":"2024-02-29T10:39:32.137955Z","iopub.execute_input":"2024-02-29T10:39:32.138215Z","iopub.status.idle":"2024-02-29T10:39:32.148614Z","shell.execute_reply.started":"2024-02-29T10:39:32.138188Z","shell.execute_reply":"2024-02-29T10:39:32.147836Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from 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,6,7,8,9],\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":"2024-02-29T10:39:32.149593Z","iopub.execute_input":"2024-02-29T10:39:32.149803Z","iopub.status.idle":"2024-02-29T10:39:32.161987Z","shell.execute_reply.started":"2024-02-29T10:39:32.149779Z","shell.execute_reply":"2024-02-29T10:39:32.161238Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_weights = class_weight.compute_class_weight('balanced',\n                                                 np.unique(train_df['gleason_mapper']),\n                                                   train_df['gleason_mapper'])","metadata":{"execution":{"iopub.status.busy":"2024-02-29T10:39:32.163036Z","iopub.execute_input":"2024-02-29T10:39:32.163306Z","iopub.status.idle":"2024-02-29T10:39:32.178668Z","shell.execute_reply.started":"2024-02-29T10:39:32.163272Z","shell.execute_reply":"2024-02-29T10:39:32.177947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"qwk = QWKEvaluation(validation_data=(val_gen, np.asarray(val_df['gleason_mapper'][:val_gen.__len__()*BATCH_SIZE])),\n                    batch_size=BATCH_SIZE, interval=1)","metadata":{"execution":{"iopub.status.busy":"2024-02-29T10:39:32.179773Z","iopub.execute_input":"2024-02-29T10:39:32.179987Z","iopub.status.idle":"2024-02-29T10:39:32.185969Z","shell.execute_reply.started":"2024-02-29T10:39:32.179963Z","shell.execute_reply":"2024-02-29T10:39:32.185333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout","metadata":{"execution":{"iopub.status.busy":"2024-02-29T10:39:32.187008Z","iopub.execute_input":"2024-02-29T10:39:32.18725Z","iopub.status.idle":"2024-02-29T10:39:32.207205Z","shell.execute_reply.started":"2024-02-29T10:39:32.187226Z","shell.execute_reply":"2024-02-29T10:39:32.206644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # Create a Sequential model\n# model = Sequential()\n\n# # Add convolutional layers with pooling\n# model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(224, 224, 3)))\n# model.add(MaxPooling2D((2, 2)))\n\n# model.add(Conv2D(64, (3, 3), activation='relu'))\n# model.add(MaxPooling2D((2, 2)))\n\n# model.add(Conv2D(128, (3, 3), activation='relu'))\n# model.add(MaxPooling2D((2, 2)))\n\n# # Flatten the output and add dense layers\n# model.add(Flatten())\n# model.add(Dense(256, activation='relu'))\n# # model.add(Dropout(0.5))  # Adding dropout for regularization\n# model.add(Dense(128, activation='relu'))\n\n# # Output layer with the appropriate number of units for your classification task\n# # If it's a binary classification, use 1 unit with sigmoid activation\n# # If it's a multi-class classification, use the number of classes with softmax activation\n# model.add(Dense(10, activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2024-02-29T18:26:02.206501Z","iopub.execute_input":"2024-02-29T18:26:02.206781Z","iopub.status.idle":"2024-02-29T18:26:02.211044Z","shell.execute_reply.started":"2024-02-29T18:26:02.206743Z","shell.execute_reply":"2024-02-29T18:26:02.21006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # Compile the model\n# model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\n\n# # Display the model summary\n# model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-02-29T18:26:00.206477Z","iopub.execute_input":"2024-02-29T18:26:00.206816Z","iopub.status.idle":"2024-02-29T18:26:00.210396Z","shell.execute_reply.started":"2024-02-29T18:26:00.20678Z","shell.execute_reply":"2024-02-29T18:26:00.209531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_dim","metadata":{"execution":{"iopub.status.busy":"2024-02-29T10:39:36.296569Z","iopub.execute_input":"2024-02-29T10:39:36.29678Z","iopub.status.idle":"2024-02-29T10:39:36.301495Z","shell.execute_reply.started":"2024-02-29T10:39:36.296756Z","shell.execute_reply":"2024-02-29T10:39:36.300838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# #Model     \n# inp=Input(shape=image_dim)\n\n# base_model=base_model=efn.EfficientNetB6(weights='imagenet',include_top=False,input_tensor=inp)\n\n# for layer in base_model.layers:\n#     layer.trainable=True\n\n# feat=GlobalAveragePooling2D()(base_model.output)\n# out=Dense(10,activation='softmax')(feat)\n# model=Model(inp,out)\n# model.compile(loss='binary_crossentropy',optimizer=Adam(0.001),metrics=['acc'])","metadata":{"execution":{"iopub.status.busy":"2024-02-29T10:39:36.302635Z","iopub.execute_input":"2024-02-29T10:39:36.302922Z","iopub.status.idle":"2024-02-29T10:39:36.310021Z","shell.execute_reply.started":"2024-02-29T10:39:36.302892Z","shell.execute_reply":"2024-02-29T10:39:36.309374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import Model\nfrom keras.layers import Input, Conv2D, MaxPooling2D, Flatten, Dense\n\n# Define input shape\ninp = Input(shape=image_dim)\n\n# Define CNN model\nx = Conv2D(32, (3, 3), activation='relu')(inp)\nx = MaxPooling2D((2, 2))(x)\nx = Conv2D(64, (3, 3), activation='relu')(x)\nx = MaxPooling2D((2, 2))(x)\nx = Conv2D(128, (3, 3), activation='relu')(x)\nx = MaxPooling2D((2, 2))(x)\nx = Flatten()(x)\nx = Dense(256, activation='relu')(x)\nout = Dense(10, activation='softmax')(x)\n\n# Create model\nmodel = Model(inputs=inp, outputs=out)\n\n# Compile the model\nmodel.compile(loss='binary_crossentropy', optimizer='adam', metrics=['acc'])\n","metadata":{"execution":{"iopub.status.busy":"2024-02-29T10:39:36.311669Z","iopub.execute_input":"2024-02-29T10:39:36.312001Z","iopub.status.idle":"2024-02-29T10:39:36.405228Z","shell.execute_reply.started":"2024-02-29T10:39:36.311967Z","shell.execute_reply":"2024-02-29T10:39:36.404574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-02-29T10:39:36.406687Z","iopub.execute_input":"2024-02-29T10:39:36.407104Z","iopub.status.idle":"2024-02-29T10:39:36.413487Z","shell.execute_reply.started":"2024-02-29T10:39:36.407063Z","shell.execute_reply":"2024-02-29T10:39:36.412826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Assuming you have your training data (X_train, y_train) and validation data (X_val, y_val) ready\n\n# Example code for model.fit\nhistory = model.fit(\n    train_gen,\n    epochs=10,  # Adjust the number of epochs as needed\n#     batch_size=32,  # Adjust the batch size as needed\n    validation_data=val_gen,\n    verbose=1\n)\n","metadata":{"execution":{"iopub.status.busy":"2024-02-29T10:39:36.414764Z","iopub.execute_input":"2024-02-29T10:39:36.414983Z","iopub.status.idle":"2024-02-29T18:22:30.317406Z","shell.execute_reply.started":"2024-02-29T10:39:36.414958Z","shell.execute_reply":"2024-02-29T18:22:30.31661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# history=model.fit_generator(\n#     train_gen,\n#     epochs=EPOCHS,\n#     steps_per_epoch=100,\n#     validation_data=val_gen,\n#     validation_steps=10,\n#     callbacks=[qwk],\n#     class_weight=class_weights\n# )","metadata":{"execution":{"iopub.status.busy":"2024-02-29T07:21:37.072859Z","iopub.status.idle":"2024-02-29T07:21:37.073363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##### loss = history.history['loss']\nval_loss = history.history['val_loss']\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,score,'b',color='red',label='Validation Kappa')\nplt.legend()\nplt.figure()\nplt.show()","metadata":{}},{"cell_type":"code","source":"del train_gen,val_gen,train_df,val_df","metadata":{"execution":{"iopub.status.busy":"2024-02-29T18:23:57.642119Z","iopub.execute_input":"2024-02-29T18:23:57.642484Z","iopub.status.idle":"2024-02-29T18:23:57.646701Z","shell.execute_reply.started":"2024-02-29T18:23:57.642445Z","shell.execute_reply":"2024-02-29T18:23:57.645935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_df","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Test Prediction**","metadata":{}},{"cell_type":"code","source":"test_dir='../input/prostate-cancer-grade-assessment/test_images'\nif os.path.exists(test_dir):\n    model=load_model('classifier.h5')\n    predicted=[]\n    for ID in test_df['image_Id']:\n        img=MultiImage(os.path.join(test_dir,ID+'.tiff'))\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,axis=0)\n        predicted.append(preds)\n        \n    submission=pd.DataFrame({'image_id':test_df['image_id'],'isup_grade':predicted})\n    submission.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2024-02-29T07:21:37.077906Z","iopub.status.idle":"2024-02-29T07:21:37.078354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model=load_model('classifier.h5')","metadata":{"execution":{"iopub.status.busy":"2024-02-29T07:21:37.07939Z","iopub.status.idle":"2024-02-29T07:21:37.079789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}