{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport tensorflow as tf\nfrom matplotlib import pyplot as plt\nfrom sklearn.metrics import cohen_kappa_score\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.applications.densenet import DenseNet121\nimport keras\nimport cv2\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\nimport cv2\nimport os\nfrom keras.callbacks import Callback\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix\nfrom sklearn.utils.multiclass import unique_labels\nfrom sklearn.utils import class_weight\nprint(os.listdir(\"../input\"))\n\n        \n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-04-14T01:15:00.078117Z","iopub.execute_input":"2023-04-14T01:15:00.078708Z","iopub.status.idle":"2023-04-14T01:15:00.09056Z","shell.execute_reply.started":"2023-04-14T01:15:00.078652Z","shell.execute_reply":"2023-04-14T01:15:00.089212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class QWKCallback(Callback):\n    def __init__(self,validation_data):\n        super(Callback, self).__init__()\n        self.X = validation_data[0]\n        self.Y = validation_data[1]\n        self.history = []\n    def on_epoch_end(self, epoch, logs={}):\n        pred = self.model.predict(self.X)\n        score = cohen_kappa_score(np.argmax(self.Y,axis=1),np.argmax(pred,axis=1),labels=[0,1,2,3,4],weights='quadratic')\n        print(\"Epoch {} : QWK: {}\".format(epoch,score))\n        self.history.append(score)\n        if score >= max(self.history):\n            print('saving checkpoint: ', score)\n            self.model.save('../working/Resnet50_bestqwk.h5')","metadata":{"execution":{"iopub.status.busy":"2023-04-14T01:15:00.092426Z","iopub.execute_input":"2023-04-14T01:15:00.093506Z","iopub.status.idle":"2023-04-14T01:15:00.107967Z","shell.execute_reply.started":"2023-04-14T01:15:00.093459Z","shell.execute_reply":"2023-04-14T01:15:00.106226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# borrowed from https://github.com/yu4u/mixup-generator\nclass MixupGenerator():\n    def __init__(self, X_train, y_train, batch_size=32, alpha=0.2, shuffle=True, datagen=None):\n        self.X_train = X_train\n        self.y_train = y_train\n        self.batch_size = batch_size\n        self.alpha = alpha\n        self.shuffle = shuffle\n        self.sample_num = len(X_train)\n        self.datagen = datagen\n\n    def __call__(self):\n        while True:\n            indexes = self.__get_exploration_order()\n            itr_num = int(len(indexes) // (self.batch_size * 2))\n\n            for i in range(itr_num):\n                batch_ids = indexes[i * self.batch_size * 2:(i + 1) * self.batch_size * 2]\n                X, y = self.__data_generation(batch_ids)\n\n                yield X, y\n\n    def __get_exploration_order(self):\n        indexes = np.arange(self.sample_num)\n\n        if self.shuffle:\n            np.random.shuffle(indexes)\n\n        return indexes\n\n    def __data_generation(self, batch_ids):\n        _, h, w, c = self.X_train.shape\n        l = np.random.beta(self.alpha, self.alpha, self.batch_size)\n        X_l = l.reshape(self.batch_size, 1, 1, 1)\n        y_l = l.reshape(self.batch_size, 1)\n\n        X1 = self.X_train[batch_ids[:self.batch_size]]\n        X2 = self.X_train[batch_ids[self.batch_size:]]\n        X = X1 * X_l + X2 * (1 - X_l)\n\n        if self.datagen:\n            for i in range(self.batch_size):\n                X[i] = self.datagen.random_transform(X[i])\n                X[i] = self.datagen.standardize(X[i])\n\n        if isinstance(self.y_train, list):\n            y = []\n\n            for y_train_ in self.y_train:\n                y1 = y_train_[batch_ids[:self.batch_size]]\n                y2 = y_train_[batch_ids[self.batch_size:]]\n                y.append(y1 * y_l + y2 * (1 - y_l))\n        else:\n            y1 = self.y_train[batch_ids[:self.batch_size]]\n            y2 = self.y_train[batch_ids[self.batch_size:]]\n            y = y1 * y_l + y2 * (1 - y_l)\n\n        return X, y","metadata":{"execution":{"iopub.status.busy":"2023-04-14T01:15:00.110215Z","iopub.execute_input":"2023-04-14T01:15:00.112958Z","iopub.status.idle":"2023-04-14T01:15:00.135595Z","shell.execute_reply.started":"2023-04-14T01:15:00.112877Z","shell.execute_reply":"2023-04-14T01:15:00.134163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# borrowed from scikit learn\ndef plot_confusion_matrix(y_true, y_pred, classes,\n                          normalize=False,\n                          title=None,\n                          cmap=plt.cm.Blues):\n    \"\"\"\n    This function prints and plots the confusion matrix.\n    Normalization can be applied by setting `normalize=True`.\n    \"\"\"\n    if not title:\n        if normalize:\n            title = 'Normalized confusion matrix'\n        else:\n            title = 'Confusion matrix, without normalization'\n\n    # Compute confusion matrix\n    cm = confusion_matrix(y_true, y_pred)\n    # Only use the labels that appear in the data\n    classes = classes[unique_labels(y_true, y_pred)]\n    if normalize:\n        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]\n        print(\"Normalized confusion matrix\")\n    else:\n        print('Confusion matrix, without normalization')\n\n    print(cm)\n\n    fig, ax = plt.subplots()\n    im = ax.imshow(cm, interpolation='nearest', cmap=cmap)\n    ax.figure.colorbar(im, ax=ax)\n    # We want to show all ticks...\n    ax.set(xticks=np.arange(cm.shape[1]),\n           yticks=np.arange(cm.shape[0]),\n           # ... and label them with the respective list entries\n           xticklabels=classes, yticklabels=classes,\n           title=title,\n           ylabel='True label',\n           xlabel='Predicted label')\n\n    # Rotate the tick labels and set their alignment.\n    plt.setp(ax.get_xticklabels(), rotation=45, ha=\"right\",\n             rotation_mode=\"anchor\")\n\n    # Loop over data dimensions and create text annotations.\n    fmt = '.2f' if normalize else 'd'\n    thresh = cm.max() / 2.\n    for i in range(cm.shape[0]):\n        for j in range(cm.shape[1]):\n            ax.text(j, i, format(cm[i, j], fmt),\n                    ha=\"center\", va=\"center\",\n                    color=\"white\" if cm[i, j] > thresh else \"black\")\n    fig.tight_layout()\n    return ax\n","metadata":{"execution":{"iopub.status.busy":"2023-04-14T01:15:00.13772Z","iopub.execute_input":"2023-04-14T01:15:00.139252Z","iopub.status.idle":"2023-04-14T01:15:00.155699Z","shell.execute_reply.started":"2023-04-14T01:15:00.139191Z","shell.execute_reply":"2023-04-14T01:15:00.154085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_raw_images_df(data_frame,filenamecol,labelcol,img_size,n_classes):\n    n_images = len(data_frame)\n    X = np.empty((n_images,img_size,img_size,3))\n    Y = np.zeros((n_images,n_classes))\n    for index,entry in data_frame.iterrows():\n        Y[index,entry[labelcol]] = 1 # one hot encoding of the label\n        # Load the image and resize\n        img = cv2.imread(entry[filenamecol])\n        X[index,:] = cv2.resize(img, (img_size, img_size))\n        X[index,:] = X[index,:] / 255.0\n    return X,Y","metadata":{"execution":{"iopub.status.busy":"2023-04-14T01:15:00.158158Z","iopub.execute_input":"2023-04-14T01:15:00.158534Z","iopub.status.idle":"2023-04-14T01:15:00.174206Z","shell.execute_reply.started":"2023-04-14T01:15:00.158501Z","shell.execute_reply":"2023-04-14T01:15:00.172357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 32\nimg_size = 224","metadata":{"execution":{"iopub.status.busy":"2023-04-14T01:15:00.265005Z","iopub.execute_input":"2023-04-14T01:15:00.266246Z","iopub.status.idle":"2023-04-14T01:15:00.271112Z","shell.execute_reply.started":"2023-04-14T01:15:00.266182Z","shell.execute_reply":"2023-04-14T01:15:00.270139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_raw_data = pd.read_csv(\"../input/aptos2019-blindness-detection/train.csv\")\ntrain_raw_data[\"filename\"] = train_raw_data[\"id_code\"].map(lambda x:os.path.join(\"../input/aptos2019-blindness-detection/train_images\",x+\".png\"))\ntrain_raw_data.diagnosis.hist() # See the distribution of the classes\n# train_raw_data.dtypes\n\n# # train_data[\"diagnosis\"] = train_data[\"diagnosis\"].astype(str)\n# # print(train_data.head())\n# # print(train_data.diagnosis.unique()) # Look at different types of classes\n# # labels = list(map(str,range(5)))\n# # print(labels)","metadata":{"execution":{"iopub.status.busy":"2023-04-14T01:15:00.273194Z","iopub.execute_input":"2023-04-14T01:15:00.273785Z","iopub.status.idle":"2023-04-14T01:15:00.510231Z","shell.execute_reply.started":"2023-04-14T01:15:00.273751Z","shell.execute_reply":"2023-04-14T01:15:00.508932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_title = {\"0\" : \"No DR\",\"1\" : \"Mild\",\"2\" : \"Moderate\",\"3\" :\"Severe\",\"4\" : \"Proliferative DR\"}\nclass_labels=[\"No DR\",\"Mild\",\"Moderate\",\"Severe\",\"Proliferative DR\"]","metadata":{"execution":{"iopub.status.busy":"2023-04-14T01:15:00.511596Z","iopub.execute_input":"2023-04-14T01:15:00.51193Z","iopub.status.idle":"2023-04-14T01:15:00.519548Z","shell.execute_reply.started":"2023-04-14T01:15:00.511899Z","shell.execute_reply":"2023-04-14T01:15:00.518146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Display some images\nfigure, ax = plt.subplots(5,2)\nax = ax.flatten()\nfor i,row in train_raw_data.iloc[0:10,:].iterrows():\n    ax[i].imshow(cv2.imread(os.path.join(\"../input/aptos2019-blindness-detection/train_images\",row[\"id_code\"]+\".png\")))\n    ax[i].set_title(label_title[str(row[\"diagnosis\"])])","metadata":{"execution":{"iopub.status.busy":"2023-04-14T01:15:00.521234Z","iopub.execute_input":"2023-04-14T01:15:00.521825Z","iopub.status.idle":"2023-04-14T01:15:08.450705Z","shell.execute_reply.started":"2023-04-14T01:15:00.521788Z","shell.execute_reply":"2023-04-14T01:15:08.448578Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df,val_df = train_test_split(train_raw_data,random_state=42,shuffle=True,test_size=0.333)\ntrain_df.reset_index(drop=True,inplace=True)\nval_df.reset_index(drop=True,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2023-04-14T01:15:08.454539Z","iopub.execute_input":"2023-04-14T01:15:08.455593Z","iopub.status.idle":"2023-04-14T01:15:08.468271Z","shell.execute_reply.started":"2023-04-14T01:15:08.455542Z","shell.execute_reply":"2023-04-14T01:15:08.466351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train,Y_train = load_raw_images_df(train_df,\"filename\",\"diagnosis\",img_size,5)\nX_val,Y_val = load_raw_images_df(val_df,\"filename\",\"diagnosis\",img_size,5)\n","metadata":{"execution":{"iopub.status.busy":"2023-04-14T01:15:08.471716Z","iopub.execute_input":"2023-04-14T01:15:08.473184Z","iopub.status.idle":"2023-04-14T01:20:37.38424Z","shell.execute_reply.started":"2023-04-14T01:15:08.473115Z","shell.execute_reply":"2023-04-14T01:20:37.383146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_train_labels = np.argmax(Y_train,axis=1)\nclass_weights = class_weight.compute_class_weight('balanced',np.unique(Y_train_labels),Y_train_labels)\ncls_wt_dict = dict(enumerate(class_weights))\nprint(cls_wt_dict)","metadata":{"execution":{"iopub.status.busy":"2023-04-14T01:36:50.587421Z","iopub.execute_input":"2023-04-14T01:36:50.587828Z","iopub.status.idle":"2023-04-14T01:36:50.619327Z","shell.execute_reply.started":"2023-04-14T01:36:50.587794Z","shell.execute_reply":"2023-04-14T01:36:50.617183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen = ImageDataGenerator(\n            \n            zoom_range=0.15,  # set range for random zoom\n        # set mode for filling points outside the input boundaries\n        fill_mode='constant',\n        cval=0.,  # value used for fill_mode = \"constant\"\n        horizontal_flip=True,  # randomly flip images\n        vertical_flip=True,  # randomly flip images\n)\ntraining_generator = MixupGenerator(X_train, Y_train, batch_size=batch_size, alpha=0.2, datagen=datagen)()","metadata":{"execution":{"iopub.status.busy":"2023-04-14T01:20:37.424459Z","iopub.status.idle":"2023-04-14T01:20:37.42486Z","shell.execute_reply.started":"2023-04-14T01:20:37.424667Z","shell.execute_reply":"2023-04-14T01:20:37.424686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def buildModel():\n    DenseNet121_model = DenseNet121(include_top=False,weights=None,input_tensor=keras.layers.Input(shape=(img_size,img_size,3)))\n    DenseNet121_model.load_weights('../input/densenet-keras/DenseNet-BC-121-32-no-top.h5')\n#     model = keras.Sequential()\n    \n#     model.add(keras.layers.Conv2D(filters = 32, kernel_size = (5,5),padding = 'same',activation ='relu', \n#                       input_shape = (img_size,img_size,3)))\n#     model.add(keras.layers.MaxPooling2D(pool_size=(2,2)))\n    \n#     model.add(keras.layers.Conv2D(filters = 64, kernel_size = (3,3),padding = 'Same',activation ='relu'))\n#     model.add(keras.layers.MaxPooling2D(pool_size=(2,2), strides=(2,2)))\n    \n#     model.add(keras.layers.Conv2D(filters =96, kernel_size = (3,3),padding = 'Same',activation ='relu'))\n#     model.add(keras.layers.MaxPooling2D(pool_size=(2,2), strides=(2,2)))\n\n#     model.add(keras.layers.Conv2D(filters = 96, kernel_size = (3,3),padding = 'Same',activation ='relu'))\n#     model.add(keras.layers.MaxPooling2D(pool_size=(2,2), strides=(2,2)))\n    \n#     model.add(keras.layers.Flatten())\n#     model.add(keras.layers.Dense(units = 512, activation = 'relu'))\n#     model.add(keras.layers.Dense(units = 5, activation = 'softmax'))\n    \n    p  = keras.layers.GlobalAveragePooling2D()(DenseNet121_model.output)\n#     fl = keras.layers.Flatten()(p)\n#     d2 = keras.layers.Dense(units = 1024, activation = 'relu',kernel_regularizer= keras.regularizers.l2(0.001))(p)\n#     d1 = keras.layers.Dense(units = 512, activation = 'relu',kernel_regularizer= keras.regularizers.l2(0.001))(d2)\n    d11 = keras.layers.Dense(units = 256, activation = 'relu',kernel_regularizer= keras.regularizers.l2(0.0001))(p)\n    o1 = keras.layers.Dense(units = 5, activation = 'softmax')(d11)\n    model = keras.models.Model(inputs = DenseNet121_model.input,outputs = o1)\n    sgd = keras.optimizers.SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)\n    model.compile(optimizer=sgd,loss='categorical_crossentropy', metrics = ['accuracy'])\n    print(model.summary())\n    return model","metadata":{"execution":{"iopub.status.busy":"2023-04-14T01:20:37.427016Z","iopub.status.idle":"2023-04-14T01:20:37.427596Z","shell.execute_reply.started":"2023-04-14T01:20:37.427372Z","shell.execute_reply":"2023-04-14T01:20:37.427402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mymodel = buildModel()","metadata":{"execution":{"iopub.status.busy":"2023-04-14T01:20:37.430672Z","iopub.status.idle":"2023-04-14T01:20:37.431329Z","shell.execute_reply.started":"2023-04-14T01:20:37.431036Z","shell.execute_reply":"2023-04-14T01:20:37.431088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"EPOCHS = 50\nearlystop = keras.callbacks.EarlyStopping(patience=10)\nlearning_rate_reduction = keras.callbacks.ReduceLROnPlateau(monitor='val_acc', \n                                            patience=2, \n                                            verbose=1, \n                                            factor=0.5, \n                                            min_lr=0.00001)\ncheckpoint = keras.callbacks.ModelCheckpoint('../working/DenseNet121.h5', monitor='val_loss', verbose=1, \n                             save_best_only=True, mode='min', save_weights_only = True)\nqwk = QWKCallback((X_val,Y_val))\nmycallbacks = [earlystop, learning_rate_reduction,checkpoint,qwk]","metadata":{"execution":{"iopub.status.busy":"2023-04-14T01:20:37.433618Z","iopub.status.idle":"2023-04-14T01:20:37.434142Z","shell.execute_reply.started":"2023-04-14T01:20:37.433924Z","shell.execute_reply":"2023-04-14T01:20:37.433947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(qwk)","metadata":{"execution":{"iopub.status.busy":"2023-04-14T01:20:37.435134Z","iopub.status.idle":"2023-04-14T01:20:37.435438Z","shell.execute_reply.started":"2023-04-14T01:20:37.435291Z","shell.execute_reply":"2023-04-14T01:20:37.435307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Warm up the model with class weights\nEPOCHS = 10\nhistory = mymodel.fit_generator(training_generator,steps_per_epoch = X_train.shape[0] // batch_size,epochs = EPOCHS,\n                         validation_data = (X_val,Y_val),\n                         validation_steps = 10,\n                         workers = 2,use_multiprocessing=True,\n                         verbose=2, callbacks=mycallbacks,\n                         class_weight=cls_wt_dict)\n","metadata":{"execution":{"iopub.status.busy":"2023-04-14T01:20:37.436197Z","iopub.status.idle":"2023-04-14T01:20:37.436487Z","shell.execute_reply.started":"2023-04-14T01:20:37.436333Z","shell.execute_reply":"2023-04-14T01:20:37.436348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"EPOCHS = 50\nhistory = mymodel.fit_generator(training_generator,steps_per_epoch = X_train.shape[0] // batch_size,epochs = EPOCHS,\n                         validation_data = (X_val,Y_val),\n                         validation_steps = 10,\n                         workers = 2,use_multiprocessing=True,\n                         verbose=2, callbacks=mycallbacks)","metadata":{"execution":{"iopub.status.busy":"2023-04-14T01:20:37.437249Z","iopub.status.idle":"2023-04-14T01:20:37.437547Z","shell.execute_reply.started":"2023-04-14T01:20:37.437402Z","shell.execute_reply":"2023-04-14T01:20:37.437417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mymodel.save_weights(\"model.h5\")","metadata":{"execution":{"iopub.status.busy":"2023-04-14T01:20:37.438744Z","iopub.status.idle":"2023-04-14T01:20:37.439039Z","shell.execute_reply.started":"2023-04-14T01:20:37.43889Z","shell.execute_reply":"2023-04-14T01:20:37.438906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_val_pred = mymodel.predict_on_batch(X_val)","metadata":{"execution":{"iopub.status.busy":"2023-04-14T01:20:37.439754Z","iopub.status.idle":"2023-04-14T01:20:37.440035Z","shell.execute_reply.started":"2023-04-14T01:20:37.439882Z","shell.execute_reply":"2023-04-14T01:20:37.439896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_val_pred_hot = np.argmax(Y_val_pred,axis=1)\nY_val_actual_hot = np.argmax(Y_val,axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-04-14T01:20:37.440716Z","iopub.status.idle":"2023-04-14T01:20:37.440979Z","shell.execute_reply.started":"2023-04-14T01:20:37.440842Z","shell.execute_reply":"2023-04-14T01:20:37.440856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_confusion_matrix(Y_val_actual_hot, Y_val_pred_hot, np.array(class_labels))","metadata":{"execution":{"iopub.status.busy":"2023-04-14T01:20:37.44196Z","iopub.status.idle":"2023-04-14T01:20:37.442278Z","shell.execute_reply.started":"2023-04-14T01:20:37.442127Z","shell.execute_reply":"2023-04-14T01:20:37.442144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(12, 12))\nax1.plot(history.history['loss'], color='b', label=\"Training loss\")\nax1.plot(history.history['val_loss'], color='r', label=\"validation loss\")\n# ax1.set_xticks(np.arange(1, epochs, 1))\n# ax1.set_yticks(np.arange(0, 1, 0.1))\n\nax2.plot(history.history['acc'], color='b', label=\"Training accuracy\")\nax2.plot(history.history['val_acc'], color='r',label=\"Validation accuracy\")\n# ax2.set_xticks(np.arange(1, epochs, 1))\n\nlegend = plt.legend(loc='best', shadow=True)\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-14T01:20:37.44349Z","iopub.status.idle":"2023-04-14T01:20:37.443788Z","shell.execute_reply.started":"2023-04-14T01:20:37.443639Z","shell.execute_reply":"2023-04-14T01:20:37.443655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data = pd.read_csv(\"../input/aptos2019-blindness-detection/test.csv\")\ntest_data[\"filename\"] = test_data[\"id_code\"].map(lambda x:x+\".png\")\ntest_data.head()","metadata":{"execution":{"iopub.status.busy":"2023-04-14T01:20:37.4447Z","iopub.status.idle":"2023-04-14T01:20:37.444986Z","shell.execute_reply.started":"2023-04-14T01:20:37.444841Z","shell.execute_reply":"2023-04-14T01:20:37.444856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_gen = ImageDataGenerator(rescale=1./255)\ntest_generator = test_gen.flow_from_dataframe(  \n        dataframe=test_data,\n        directory = \"../input/aptos2019-blindness-detection/test_images\",    \n        x_col=\"filename\",\n        y_col=None,\n        target_size = (img_size,img_size),\n        batch_size = 1,\n        shuffle = False,\n        class_mode = None\n        )","metadata":{"execution":{"iopub.status.busy":"2023-04-14T01:20:37.445866Z","iopub.status.idle":"2023-04-14T01:20:37.446163Z","shell.execute_reply.started":"2023-04-14T01:20:37.445994Z","shell.execute_reply":"2023-04-14T01:20:37.446009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = mymodel.predict_generator(test_generator, steps = len(test_generator.filenames))","metadata":{"execution":{"iopub.status.busy":"2023-04-14T01:20:37.446828Z","iopub.status.idle":"2023-04-14T01:20:37.44711Z","shell.execute_reply.started":"2023-04-14T01:20:37.44695Z","shell.execute_reply":"2023-04-14T01:20:37.446964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filenames=test_generator.filenames\nresults=pd.DataFrame({\"id_code\":filenames,\n                      \"diagnosis\":np.argmax(predictions,axis=1)})\nresults['id_code'] = results['id_code'].map(lambda x: str(x)[:-4])\nresults.to_csv(\"submission.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2023-04-14T01:20:37.447781Z","iopub.status.idle":"2023-04-14T01:20:37.448034Z","shell.execute_reply.started":"2023-04-14T01:20:37.447901Z","shell.execute_reply":"2023-04-14T01:20:37.447915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results.diagnosis.hist()\nresults.diagnosis.unique()","metadata":{"execution":{"iopub.status.busy":"2023-04-14T01:20:37.448685Z","iopub.status.idle":"2023-04-14T01:20:37.448935Z","shell.execute_reply.started":"2023-04-14T01:20:37.4488Z","shell.execute_reply":"2023-04-14T01:20:37.448814Z"},"trusted":true},"execution_count":null,"outputs":[]}]}