{"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":"markdown","source":"Some good references:\n1. https://towardsdatascience.com/a-bunch-of-tips-and-tricks-for-training-deep-neural-networks-3ca24c31ddc8\n2. https://towardsdatascience.com/review-densenet-image-classification-b6631a8ef803","metadata":{}},{"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 in \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# Any results you write to the current directory are saved as output.","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-06-15T06:18:07.651075Z","iopub.execute_input":"2022-06-15T06:18:07.651382Z","iopub.status.idle":"2022-06-15T06:18:09.537913Z","shell.execute_reply.started":"2022-06-15T06:18:07.651329Z","shell.execute_reply":"2022-06-15T06:18:09.535666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# borrowed from https://www.kaggle.com/mathormad/aptos-resnet50-baseline\nclass 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')\n        \n        \n    ","metadata":{"execution":{"iopub.status.busy":"2022-06-15T06:18:09.540336Z","iopub.execute_input":"2022-06-15T06:18:09.540923Z","iopub.status.idle":"2022-06-15T06:18:09.558517Z","shell.execute_reply.started":"2022-06-15T06:18:09.540610Z","shell.execute_reply":"2022-06-15T06:18:09.557410Z"},"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":"2022-06-15T06:18:09.559941Z","iopub.execute_input":"2022-06-15T06:18:09.560262Z","iopub.status.idle":"2022-06-15T06:18:09.603855Z","shell.execute_reply.started":"2022-06-15T06:18:09.560202Z","shell.execute_reply":"2022-06-15T06:18:09.602977Z"},"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":"2022-06-15T06:18:09.610084Z","iopub.execute_input":"2022-06-15T06:18:09.613119Z","iopub.status.idle":"2022-06-15T06:18:09.634679Z","shell.execute_reply.started":"2022-06-15T06:18:09.612996Z","shell.execute_reply":"2022-06-15T06:18:09.633570Z"},"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":"2022-06-15T06:18:09.644569Z","iopub.execute_input":"2022-06-15T06:18:09.646863Z","iopub.status.idle":"2022-06-15T06:18:09.657847Z","shell.execute_reply.started":"2022-06-15T06:18:09.646809Z","shell.execute_reply":"2022-06-15T06:18:09.656724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 32\nimg_size = 224","metadata":{"execution":{"iopub.status.busy":"2022-06-15T06:18:09.666177Z","iopub.execute_input":"2022-06-15T06:18:09.669114Z","iopub.status.idle":"2022-06-15T06:18:09.675346Z","shell.execute_reply.started":"2022-06-15T06:18:09.669034Z","shell.execute_reply":"2022-06-15T06:18:09.674434Z"},"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":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","execution":{"iopub.status.busy":"2022-06-15T06:18:09.681257Z","iopub.execute_input":"2022-06-15T06:18:09.682221Z","iopub.status.idle":"2022-06-15T06:18:10.069754Z","shell.execute_reply.started":"2022-06-15T06:18:09.682169Z","shell.execute_reply":"2022-06-15T06:18:10.068922Z"},"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":"2022-06-15T06:18:10.071062Z","iopub.execute_input":"2022-06-15T06:18:10.071477Z","iopub.status.idle":"2022-06-15T06:18:10.076967Z","shell.execute_reply.started":"2022-06-15T06:18:10.071430Z","shell.execute_reply":"2022-06-15T06:18:10.076027Z"},"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":"2022-06-15T06:18:10.078427Z","iopub.execute_input":"2022-06-15T06:18:10.078864Z","iopub.status.idle":"2022-06-15T06:18:16.872362Z","shell.execute_reply.started":"2022-06-15T06:18:10.078812Z","shell.execute_reply":"2022-06-15T06:18:16.871492Z"},"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":"2022-06-15T06:18:16.877130Z","iopub.execute_input":"2022-06-15T06:18:16.879697Z","iopub.status.idle":"2022-06-15T06:18:16.893471Z","shell.execute_reply.started":"2022-06-15T06:18:16.879628Z","shell.execute_reply":"2022-06-15T06:18:16.892419Z"},"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)","metadata":{"execution":{"iopub.status.busy":"2022-06-15T06:18:16.898771Z","iopub.execute_input":"2022-06-15T06:18:16.901133Z","iopub.status.idle":"2022-06-15T06:24:40.808205Z","shell.execute_reply.started":"2022-06-15T06:18:16.901072Z","shell.execute_reply":"2022-06-15T06:24:40.807215Z"},"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":"2022-06-15T06:24:40.810128Z","iopub.execute_input":"2022-06-15T06:24:40.810406Z","iopub.status.idle":"2022-06-15T06:24:40.819126Z","shell.execute_reply.started":"2022-06-15T06:24:40.810360Z","shell.execute_reply":"2022-06-15T06:24:40.818313Z"},"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":"2022-06-15T06:24:40.820411Z","iopub.execute_input":"2022-06-15T06:24:40.820875Z","iopub.status.idle":"2022-06-15T06:24:40.838033Z","shell.execute_reply.started":"2022-06-15T06:24:40.820818Z","shell.execute_reply":"2022-06-15T06:24:40.837067Z"},"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":"2022-06-15T06:24:40.840254Z","iopub.execute_input":"2022-06-15T06:24:40.840477Z","iopub.status.idle":"2022-06-15T06:24:40.853124Z","shell.execute_reply.started":"2022-06-15T06:24:40.840433Z","shell.execute_reply":"2022-06-15T06:24:40.852312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Original\n# lr=0.01","metadata":{"execution":{"iopub.status.busy":"2022-06-15T06:24:40.854515Z","iopub.execute_input":"2022-06-15T06:24:40.854792Z","iopub.status.idle":"2022-06-15T06:24:40.863219Z","shell.execute_reply.started":"2022-06-15T06:24:40.854744Z","shell.execute_reply":"2022-06-15T06:24:40.862512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mymodel = buildModel()","metadata":{"execution":{"iopub.status.busy":"2022-06-15T06:24:40.865141Z","iopub.execute_input":"2022-06-15T06:24:40.865650Z","iopub.status.idle":"2022-06-15T06:25:03.148958Z","shell.execute_reply.started":"2022-06-15T06:24:40.865593Z","shell.execute_reply":"2022-06-15T06:25:03.148209Z"},"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":"2022-06-15T06:25:03.150567Z","iopub.execute_input":"2022-06-15T06:25:03.150803Z","iopub.status.idle":"2022-06-15T06:25:03.168366Z","shell.execute_reply.started":"2022-06-15T06:25:03.150757Z","shell.execute_reply":"2022-06-15T06:25:03.167658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#ORiginal\n# min_lr=0.00001","metadata":{"execution":{"iopub.status.busy":"2022-06-15T06:25:03.170800Z","iopub.execute_input":"2022-06-15T06:25:03.171130Z","iopub.status.idle":"2022-06-15T06:25:03.180059Z","shell.execute_reply.started":"2022-06-15T06:25:03.171077Z","shell.execute_reply":"2022-06-15T06:25:03.179288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(qwk)","metadata":{"execution":{"iopub.status.busy":"2022-06-15T06:25:03.184122Z","iopub.execute_input":"2022-06-15T06:25:03.184408Z","iopub.status.idle":"2022-06-15T06:25:03.193599Z","shell.execute_reply.started":"2022-06-15T06:25:03.184363Z","shell.execute_reply":"2022-06-15T06:25:03.192816Z"},"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=1, callbacks=mycallbacks,\n                         class_weight=cls_wt_dict)","metadata":{"execution":{"iopub.status.busy":"2022-06-15T06:25:03.194855Z","iopub.execute_input":"2022-06-15T06:25:03.195188Z","iopub.status.idle":"2022-06-15T06:35:12.304852Z","shell.execute_reply.started":"2022-06-15T06:25:03.195137Z","shell.execute_reply":"2022-06-15T06:35:12.303942Z"},"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=1, callbacks=mycallbacks)","metadata":{"execution":{"iopub.status.busy":"2022-06-15T06:35:12.307021Z","iopub.execute_input":"2022-06-15T06:35:12.307328Z","iopub.status.idle":"2022-06-15T06:46:46.073022Z","shell.execute_reply.started":"2022-06-15T06:35:12.307275Z","shell.execute_reply":"2022-06-15T06:46:46.072029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# mymodel.save_weights(\"model.h5\")","metadata":{"execution":{"iopub.status.busy":"2022-06-15T06:46:46.077188Z","iopub.execute_input":"2022-06-15T06:46:46.077504Z","iopub.status.idle":"2022-06-15T06:46:46.097177Z","shell.execute_reply.started":"2022-06-15T06:46:46.077445Z","shell.execute_reply":"2022-06-15T06:46:46.096184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mymodel.save(\"./mymodel\")","metadata":{"execution":{"iopub.status.busy":"2022-06-15T06:46:46.099333Z","iopub.execute_input":"2022-06-15T06:46:46.099670Z","iopub.status.idle":"2022-06-15T06:46:46.105561Z","shell.execute_reply.started":"2022-06-15T06:46:46.099616Z","shell.execute_reply":"2022-06-15T06:46:46.104190Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from IPython.display import FileLink\n# FileLink(r'./mymodel')","metadata":{"execution":{"iopub.status.busy":"2022-06-15T06:46:46.107178Z","iopub.execute_input":"2022-06-15T06:46:46.107749Z","iopub.status.idle":"2022-06-15T06:46:46.118158Z","shell.execute_reply.started":"2022-06-15T06:46:46.107481Z","shell.execute_reply":"2022-06-15T06:46:46.117313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_val_pred = mymodel.predict_on_batch(X_val)","metadata":{"execution":{"iopub.status.busy":"2022-06-15T06:46:46.119818Z","iopub.execute_input":"2022-06-15T06:46:46.120577Z","iopub.status.idle":"2022-06-15T06:46:54.816952Z","shell.execute_reply.started":"2022-06-15T06:46:46.120529Z","shell.execute_reply":"2022-06-15T06:46:54.816099Z"},"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":"2022-06-15T06:46:54.818314Z","iopub.execute_input":"2022-06-15T06:46:54.818590Z","iopub.status.idle":"2022-06-15T06:46:54.825300Z","shell.execute_reply.started":"2022-06-15T06:46:54.818546Z","shell.execute_reply":"2022-06-15T06:46:54.823277Z"},"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":"2022-06-15T06:46:54.826788Z","iopub.execute_input":"2022-06-15T06:46:54.827118Z","iopub.status.idle":"2022-06-15T06:46:55.228249Z","shell.execute_reply.started":"2022-06-15T06:46:54.827061Z","shell.execute_reply":"2022-06-15T06:46:55.227360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# history = mymodel.fit_generator(train_gen,steps_per_epoch = 10,epochs = EPOCHS,\n#                          validation_data = val_gen,\n#                          validation_steps = 10,\n#                          workers = 2,use_multiprocessing=True,\n#                      verbose=2, callbacks=mycallbacks)\n# mymodel.save_weights(\"model.h5\")","metadata":{"execution":{"iopub.status.busy":"2022-06-15T06:46:55.229761Z","iopub.execute_input":"2022-06-15T06:46:55.230294Z","iopub.status.idle":"2022-06-15T06:46:55.234216Z","shell.execute_reply.started":"2022-06-15T06:46:55.230244Z","shell.execute_reply":"2022-06-15T06:46:55.233498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_gen = img_gen.flow_from_dataframe(\n#     dataframe=train_data,\n#     directory=\"../input/aptos2019-blindness-detection/train_images\",\n#     x_col=\"filename\",\n#     y_col=\"diagnosis\",\n#     batch_size=batch_size,\n#     shuffle=True,\n#     class_mode=\"categorical\",\n#     classes=labels,\n#     target_size=(img_size,img_size),\n#     subset='training')","metadata":{"execution":{"iopub.status.busy":"2022-06-15T06:46:55.235840Z","iopub.execute_input":"2022-06-15T06:46:55.236520Z","iopub.status.idle":"2022-06-15T06:46:55.243545Z","shell.execute_reply.started":"2022-06-15T06:46:55.236455Z","shell.execute_reply":"2022-06-15T06:46:55.242686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# val_gen = img_gen.flow_from_dataframe(\n#     dataframe=train_data,\n#     directory=\"../input/aptos2019-blindness-detection/train_images\",\n#     x_col=\"filename\",\n#     y_col=\"diagnosis\",\n#     batch_size=batch_size,\n#     shuffle=True,\n#     class_mode=\"categorical\",\n#     classes=labels,\n#     target_size=(img_size,img_size),\n#     subset='validation'\n# )\n# # dir(val_gen)","metadata":{"execution":{"iopub.status.busy":"2022-06-15T06:46:55.245147Z","iopub.execute_input":"2022-06-15T06:46:55.246870Z","iopub.status.idle":"2022-06-15T06:46:55.253207Z","shell.execute_reply.started":"2022-06-15T06:46:55.246812Z","shell.execute_reply":"2022-06-15T06:46:55.252357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# class QWK(keras.callbacks.Callback):\n#     def on_train_begin(self, logs={}):\n#         self.val_kappas = []\n\n#     def on_epoch_end(self, epoch, logs={}):\n#         print(self.__dict__)\n#         X_val, y_val = self.model.validation_data[:2]\n#         y_pred = self.model.predict(X_val)\n\n#         _val_kappa = cohen_kappa_score(\n#             y_val.argmax(axis=1), \n#             y_pred.argmax(axis=1), \n#             weights='quadratic'\n#         )\n\n#         self.val_kappas.append(_val_kappa)\n\n#         print(f\"epoch: {epoch} \\t val_kappa: {_val_kappa:.4f}\")\n\n#         return\n    ","metadata":{"execution":{"iopub.status.busy":"2022-06-15T06:46:55.256151Z","iopub.execute_input":"2022-06-15T06:46:55.257492Z","iopub.status.idle":"2022-06-15T06:46:55.264617Z","shell.execute_reply.started":"2022-06-15T06:46:55.257427Z","shell.execute_reply":"2022-06-15T06:46:55.263660Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# val_gen = img_gen.flow_from_dataframe(\n#     dataframe=train_data,\n#     directory=\"../input/aptos2019-blindness-detection/train_images\",\n#     x_col=\"filename\",\n#     y_col=\"diagnosis\",\n#     batch_size=1000,\n#     shuffle=True,\n#     class_mode=\"categorical\",\n#     classes=labels,\n#     target_size=(img_size,img_size),\n#     subset='validation'\n# )\n# # print(val_gen.batch_index)\n# # for i in range(val_gen.batch_index):\n# batch_data, batch_labels = val_gen.next()\n# batch_labels_arg = np.argmax(batch_labels,axis=1)\n# # print(batch_data.shape)","metadata":{"execution":{"iopub.status.busy":"2022-06-15T06:46:55.277448Z","iopub.execute_input":"2022-06-15T06:46:55.277950Z","iopub.status.idle":"2022-06-15T06:46:55.285431Z","shell.execute_reply.started":"2022-06-15T06:46:55.277741Z","shell.execute_reply":"2022-06-15T06:46:55.284007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# cur_batch_size = batch_data.shape[0] \n# preds = np.empty((cur_batch_size,5))\n# preds_one_hot = np.zeros((cur_batch_size,5))\n# for i in range(cur_batch_size):\n#     preds[i,:] = mymodel.predict(batch_data[i,:,:,:][np.newaxis,:])\n# args_max = np.argmax(preds,axis=1)\n# preds_one_hot[np.arange(cur_batch_size),args_max] = 1","metadata":{"execution":{"iopub.status.busy":"2022-06-15T06:46:55.287789Z","iopub.execute_input":"2022-06-15T06:46:55.288293Z","iopub.status.idle":"2022-06-15T06:46:55.293628Z","shell.execute_reply.started":"2022-06-15T06:46:55.288172Z","shell.execute_reply":"2022-06-15T06:46:55.292191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plot_confusion_matrix(batch_labels_arg, args_max, np.array(class_labels))","metadata":{"execution":{"iopub.status.busy":"2022-06-15T06:46:55.295592Z","iopub.execute_input":"2022-06-15T06:46:55.296370Z","iopub.status.idle":"2022-06-15T06:46:55.302065Z","shell.execute_reply.started":"2022-06-15T06:46:55.295940Z","shell.execute_reply":"2022-06-15T06:46:55.300969Z"},"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":"2022-06-15T06:46:55.304332Z","iopub.execute_input":"2022-06-15T06:46:55.304874Z","iopub.status.idle":"2022-06-15T06:46:55.872696Z","shell.execute_reply.started":"2022-06-15T06:46:55.304762Z","shell.execute_reply":"2022-06-15T06:46:55.871718Z"},"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":"2022-06-15T06:46:55.874110Z","iopub.execute_input":"2022-06-15T06:46:55.874439Z","iopub.status.idle":"2022-06-15T06:46:55.910888Z","shell.execute_reply.started":"2022-06-15T06:46:55.874383Z","shell.execute_reply":"2022-06-15T06:46:55.910264Z"},"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":"2022-06-15T06:46:55.913960Z","iopub.execute_input":"2022-06-15T06:46:55.914242Z","iopub.status.idle":"2022-06-15T06:47:00.490243Z","shell.execute_reply.started":"2022-06-15T06:46:55.914192Z","shell.execute_reply":"2022-06-15T06:47:00.489400Z"},"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":"2022-06-15T06:47:00.491592Z","iopub.execute_input":"2022-06-15T06:47:00.491880Z","iopub.status.idle":"2022-06-15T06:48:37.836137Z","shell.execute_reply.started":"2022-06-15T06:47:00.491833Z","shell.execute_reply":"2022-06-15T06:48:37.835338Z"},"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":"2022-06-15T06:48:37.841661Z","iopub.execute_input":"2022-06-15T06:48:37.841919Z","iopub.status.idle":"2022-06-15T06:48:38.080563Z","shell.execute_reply.started":"2022-06-15T06:48:37.841867Z","shell.execute_reply":"2022-06-15T06:48:38.079855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a href=\"submission.csv\">submission.csv</a>","metadata":{}},{"cell_type":"code","source":"results.diagnosis.hist()\nresults.diagnosis.unique()","metadata":{"execution":{"iopub.status.busy":"2022-06-15T06:48:38.082036Z","iopub.execute_input":"2022-06-15T06:48:38.082348Z","iopub.status.idle":"2022-06-15T06:48:38.361215Z","shell.execute_reply.started":"2022-06-15T06:48:38.082300Z","shell.execute_reply":"2022-06-15T06:48:38.360207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_raw_data.head()","metadata":{"execution":{"iopub.status.busy":"2022-06-15T06:48:38.366002Z","iopub.execute_input":"2022-06-15T06:48:38.366475Z","iopub.status.idle":"2022-06-15T06:48:38.398131Z","shell.execute_reply.started":"2022-06-15T06:48:38.366287Z","shell.execute_reply":"2022-06-15T06:48:38.397273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_raw_data.filename[1]","metadata":{"execution":{"iopub.status.busy":"2022-06-15T06:48:38.402745Z","iopub.execute_input":"2022-06-15T06:48:38.405535Z","iopub.status.idle":"2022-06-15T06:48:38.416156Z","shell.execute_reply.started":"2022-06-15T06:48:38.405466Z","shell.execute_reply":"2022-06-15T06:48:38.415111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.preprocessing.image import load_img, img_to_array, ImageDataGenerator, image\n\n\n#load the image\nmy_image = load_img('../input/aptos2019-blindness-detection/train_images/001639a390f0.png', target_size=(224, 224))","metadata":{"execution":{"iopub.status.busy":"2022-06-15T06:48:38.421148Z","iopub.execute_input":"2022-06-15T06:48:38.423590Z","iopub.status.idle":"2022-06-15T06:48:38.643467Z","shell.execute_reply.started":"2022-06-15T06:48:38.423531Z","shell.execute_reply":"2022-06-15T06:48:38.641542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(my_image)","metadata":{"execution":{"iopub.status.busy":"2022-06-15T06:48:38.649757Z","iopub.execute_input":"2022-06-15T06:48:38.651997Z","iopub.status.idle":"2022-06-15T06:48:39.016442Z","shell.execute_reply.started":"2022-06-15T06:48:38.651941Z","shell.execute_reply":"2022-06-15T06:48:39.015488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_image","metadata":{"execution":{"iopub.status.busy":"2022-06-15T06:48:39.021370Z","iopub.execute_input":"2022-06-15T06:48:39.023792Z","iopub.status.idle":"2022-06-15T06:48:39.088514Z","shell.execute_reply.started":"2022-06-15T06:48:39.023726Z","shell.execute_reply":"2022-06-15T06:48:39.087689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_as_array = np.asarray(my_image)","metadata":{"execution":{"iopub.status.busy":"2022-06-15T06:48:39.089958Z","iopub.execute_input":"2022-06-15T06:48:39.090461Z","iopub.status.idle":"2022-06-15T06:48:39.097990Z","shell.execute_reply.started":"2022-06-15T06:48:39.090412Z","shell.execute_reply":"2022-06-15T06:48:39.097197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_as_array = image_as_array.reshape((1, image_as_array.shape[0], image_as_array.shape[1], image_as_array.shape[2]))\n","metadata":{"execution":{"iopub.status.busy":"2022-06-15T06:48:39.099453Z","iopub.execute_input":"2022-06-15T06:48:39.101163Z","iopub.status.idle":"2022-06-15T06:48:39.111683Z","shell.execute_reply.started":"2022-06-15T06:48:39.101095Z","shell.execute_reply":"2022-06-15T06:48:39.109809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_as_array.shape","metadata":{"execution":{"iopub.status.busy":"2022-06-15T06:48:39.115959Z","iopub.execute_input":"2022-06-15T06:48:39.116485Z","iopub.status.idle":"2022-06-15T06:48:39.123017Z","shell.execute_reply.started":"2022-06-15T06:48:39.116435Z","shell.execute_reply":"2022-06-15T06:48:39.121873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict = mymodel.predict(image_as_array)","metadata":{"execution":{"iopub.status.busy":"2022-06-15T06:48:39.126275Z","iopub.execute_input":"2022-06-15T06:48:39.127199Z","iopub.status.idle":"2022-06-15T06:48:39.160245Z","shell.execute_reply.started":"2022-06-15T06:48:39.127146Z","shell.execute_reply":"2022-06-15T06:48:39.159467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"the_result = np.argmax(predict)","metadata":{"execution":{"iopub.status.busy":"2022-06-15T06:48:39.161233Z","iopub.execute_input":"2022-06-15T06:48:39.161489Z","iopub.status.idle":"2022-06-15T06:48:39.166198Z","shell.execute_reply.started":"2022-06-15T06:48:39.161448Z","shell.execute_reply":"2022-06-15T06:48:39.165265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"the_result","metadata":{"execution":{"iopub.status.busy":"2022-06-15T06:48:39.167823Z","iopub.execute_input":"2022-06-15T06:48:39.168527Z","iopub.status.idle":"2022-06-15T06:48:39.178496Z","shell.execute_reply.started":"2022-06-15T06:48:39.168460Z","shell.execute_reply":"2022-06-15T06:48:39.177370Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}