{"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":"!wget https://storage.googleapis.com/cloud-tpu-checkpoints/efficientnet/noisystudent/noisy_student_efficientnet-b3.tar.gz","metadata":{"execution":{"iopub.status.busy":"2021-10-08T16:18:47.563168Z","iopub.execute_input":"2021-10-08T16:18:47.563494Z","iopub.status.idle":"2021-10-08T16:18:48.949732Z","shell.execute_reply.started":"2021-10-08T16:18:47.563463Z","shell.execute_reply":"2021-10-08T16:18:48.948849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"!tar -xf /kaggle/working/noisy_student_efficientnet-b3.tar.gz","metadata":{}},{"cell_type":"code","source":"!tar -xf /kaggle/working/noisy_student_efficientnet-b3.tar.gz","metadata":{"execution":{"iopub.status.busy":"2021-10-08T16:18:48.951911Z","iopub.execute_input":"2021-10-08T16:18:48.952288Z","iopub.status.idle":"2021-10-08T16:18:50.714715Z","shell.execute_reply.started":"2021-10-08T16:18:48.952242Z","shell.execute_reply":"2021-10-08T16:18:50.713726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!wget https://raw.githubusercontent.com/tensorflow/tensorflow/master/tensorflow/python/keras/applications/efficientnet_weight_update_util.py","metadata":{"execution":{"iopub.status.busy":"2021-10-08T16:18:50.716437Z","iopub.execute_input":"2021-10-08T16:18:50.7168Z","iopub.status.idle":"2021-10-08T16:18:51.843006Z","shell.execute_reply.started":"2021-10-08T16:18:50.716761Z","shell.execute_reply":"2021-10-08T16:18:51.84197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python ./efficientnet_weight_update_util.py --model b3 --notop --ckpt ./noisy_student_efficientnet-b3/model.ckpt --o ./efficientnetb3_notop.h5","metadata":{"execution":{"iopub.status.busy":"2021-10-08T16:18:51.844822Z","iopub.execute_input":"2021-10-08T16:18:51.845198Z","iopub.status.idle":"2021-10-08T16:19:06.469179Z","shell.execute_reply.started":"2021-10-08T16:18:51.845155Z","shell.execute_reply":"2021-10-08T16:19:06.467896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nfrom tensorflow.keras import models, layers\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom keras.callbacks import ModelCheckpoint, EarlyStopping, ReduceLROnPlateau\nfrom tensorflow.keras.applications import ResNet50, DenseNet121, EfficientNetB3\nfrom keras.optimizers import Adam\nfrom keras.losses import CategoricalCrossentropy\nimport os, cv2, json\n\n# ignoring warnings\nimport warnings\nwarnings.simplefilter(\"ignore\")","metadata":{"execution":{"iopub.status.busy":"2021-10-08T16:19:06.473544Z","iopub.execute_input":"2021-10-08T16:19:06.474302Z","iopub.status.idle":"2021-10-08T16:19:08.404749Z","shell.execute_reply.started":"2021-10-08T16:19:06.474223Z","shell.execute_reply":"2021-10-08T16:19:08.403917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# For easy acces to files\nWORK_DIR = \"../input/cassava-leaf-disease-classification/\"\nos.listdir(WORK_DIR)","metadata":{"execution":{"iopub.status.busy":"2021-10-08T16:19:08.408906Z","iopub.execute_input":"2021-10-08T16:19:08.409175Z","iopub.status.idle":"2021-10-08T16:19:08.420264Z","shell.execute_reply.started":"2021-10-08T16:19:08.409148Z","shell.execute_reply":"2021-10-08T16:19:08.419474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with open('../input/cassava-leaf-disease-classification/label_num_to_disease_map.json', 'r') as file:\n    labels = json.load(file)\n    \nlabels","metadata":{"execution":{"iopub.status.busy":"2021-10-08T16:19:08.423009Z","iopub.execute_input":"2021-10-08T16:19:08.423664Z","iopub.status.idle":"2021-10-08T16:19:08.599344Z","shell.execute_reply.started":"2021-10-08T16:19:08.423594Z","shell.execute_reply":"2021-10-08T16:19:08.598557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = pd.read_csv(WORK_DIR + \"train.csv\")","metadata":{"execution":{"iopub.status.busy":"2021-10-08T16:19:08.602618Z","iopub.execute_input":"2021-10-08T16:19:08.602926Z","iopub.status.idle":"2021-10-08T16:19:08.630451Z","shell.execute_reply.started":"2021-10-08T16:19:08.602898Z","shell.execute_reply":"2021-10-08T16:19:08.629755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#change for the ImageDatagen and flow_from_dataframe\ndata.label = data.label.astype(\"str\")","metadata":{"execution":{"iopub.status.busy":"2021-10-08T16:19:08.63186Z","iopub.execute_input":"2021-10-08T16:19:08.632223Z","iopub.status.idle":"2021-10-08T16:19:08.666036Z","shell.execute_reply.started":"2021-10-08T16:19:08.632185Z","shell.execute_reply":"2021-10-08T16:19:08.664957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.dtypes","metadata":{"execution":{"iopub.status.busy":"2021-10-08T16:19:08.667387Z","iopub.execute_input":"2021-10-08T16:19:08.668001Z","iopub.status.idle":"2021-10-08T16:19:08.679129Z","shell.execute_reply.started":"2021-10-08T16:19:08.667959Z","shell.execute_reply":"2021-10-08T16:19:08.678071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.shape[0]","metadata":{"execution":{"iopub.status.busy":"2021-10-08T16:19:08.681658Z","iopub.execute_input":"2021-10-08T16:19:08.681958Z","iopub.status.idle":"2021-10-08T16:19:08.691927Z","shell.execute_reply.started":"2021-10-08T16:19:08.681928Z","shell.execute_reply":"2021-10-08T16:19:08.690973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.label.value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-10-08T16:19:08.69293Z","iopub.execute_input":"2021-10-08T16:19:08.693189Z","iopub.status.idle":"2021-10-08T16:19:08.710377Z","shell.execute_reply.started":"2021-10-08T16:19:08.693163Z","shell.execute_reply":"2021-10-08T16:19:08.709593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMG_SIZE = 300","metadata":{"execution":{"iopub.status.busy":"2021-10-08T16:19:08.711897Z","iopub.execute_input":"2021-10-08T16:19:08.712502Z","iopub.status.idle":"2021-10-08T16:19:08.718596Z","shell.execute_reply.started":"2021-10-08T16:19:08.712462Z","shell.execute_reply":"2021-10-08T16:19:08.717335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,12))\ndata_sample = data.sample(9).reset_index(drop=True)\n\nfor i in range(8):\n    plt.subplot(2,4,i+1)\n    \n    img = cv2.imread(WORK_DIR + \"train_images/\" + data_sample.image_id[i])\n    img = cv2.resize(img, (IMG_SIZE, IMG_SIZE))\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    \n    plt.axis(\"off\")\n    plt.imshow(img)\n    plt.title(labels.get(data_sample.label[i]))\n    \nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-10-08T16:19:08.720086Z","iopub.execute_input":"2021-10-08T16:19:08.720554Z","iopub.status.idle":"2021-10-08T16:19:09.728727Z","shell.execute_reply.started":"2021-10-08T16:19:08.720514Z","shell.execute_reply":"2021-10-08T16:19:09.727778Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\ntrain_generator = ImageDataGenerator(\n                                    #featurewise_center=False,                                    \n                                    #samplewise_center=False,\n                                    #featurewise_std_normalization=False,\n                                    #samplewise_std_normalization=False, \n                                    #zca_whitening=False,\n                                    #zca_epsilon=1e-06,\n                                    rotation_range=270,\n                                    width_shift_range=0.2,\n                                    height_shift_range=0.2,\n                                    brightness_range=[0.1,0.9],\n                                    shear_range=25,\n                                    zoom_range=0.3,\n                                    channel_shift_range=0.2,\n                                    #fill_mode=\"nearest\",\n                                    #cval=0.0,\n                                    horizontal_flip=True,\n                                    vertical_flip=True,\n                                    #rescale=None,\n                                    #preprocessing_function=None,\n                                    #data_format=None,\n                                    validation_split=0.2,\n                                    #dtype=None,\n) \\\n        .flow_from_dataframe(\n                            data,\n                            directory = WORK_DIR + \"train_images\",\n                            x_col = \"image_id\",\n                            y_col = \"label\",\n                            #weight_col = None,\n                            target_size = (IMG_SIZE, IMG_SIZE),\n                            #color_mode = \"rgb\",\n                            #classes = None,\n                            class_mode = \"categorical\",\n                            batch_size = 32,\n                            shuffle = True,\n                            #seed = 34,\n                            #save_to_dir = None,\n                            #save_prefix = \"\",\n                            #save_format = \"png\",\n                            subset = \"training\",\n                            #interpolation = \"nearest\",\n                            #validate_filenames = True\n)\n","metadata":{"execution":{"iopub.status.busy":"2021-10-08T16:19:09.729944Z","iopub.execute_input":"2021-10-08T16:19:09.730274Z","iopub.status.idle":"2021-10-08T16:19:30.833903Z","shell.execute_reply.started":"2021-10-08T16:19:09.730242Z","shell.execute_reply":"2021-10-08T16:19:30.832978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_generator = ImageDataGenerator(\n                                    validation_split = 0.2\n) \\\n        .flow_from_dataframe(\n                            data,\n                            directory = WORK_DIR + \"train_images\",\n                            x_col = \"image_id\",\n                            y_col = \"label\",\n                            target_size = (IMG_SIZE, IMG_SIZE),\n                            class_mode = \"categorical\",\n                            batch_size = 32,\n                            shuffle = True,\n                            #seed = 34,\n                            subset = \"validation\")","metadata":{"execution":{"iopub.status.busy":"2021-10-08T16:19:30.835188Z","iopub.execute_input":"2021-10-08T16:19:30.835726Z","iopub.status.idle":"2021-10-08T16:19:41.748744Z","shell.execute_reply.started":"2021-10-08T16:19:30.835684Z","shell.execute_reply":"2021-10-08T16:19:41.748005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_generator.class_indices","metadata":{"execution":{"iopub.status.busy":"2021-10-08T16:19:41.750813Z","iopub.execute_input":"2021-10-08T16:19:41.751168Z","iopub.status.idle":"2021-10-08T16:19:41.760325Z","shell.execute_reply.started":"2021-10-08T16:19:41.75113Z","shell.execute_reply":"2021-10-08T16:19:41.759413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def modelEfficientNetB3():\n    \n    model = models.Sequential()\n    model.add(EfficientNetB3(include_top = False, weights = \"./efficientnetb3_notop.h5\",input_shape=(IMG_SIZE,IMG_SIZE, 3)))\n    model.add(layers.GlobalAveragePooling2D())\n    model.add(layers.Dense(256, activation = 'relu'))\n    model.add(layers.Dropout(0.5))\n    model.add(layers.Dense(5, activation = \"softmax\"))\n    \n    return model ","metadata":{"execution":{"iopub.status.busy":"2021-10-08T16:19:41.762189Z","iopub.execute_input":"2021-10-08T16:19:41.762627Z","iopub.status.idle":"2021-10-08T16:19:41.772237Z","shell.execute_reply.started":"2021-10-08T16:19:41.762566Z","shell.execute_reply":"2021-10-08T16:19:41.771473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = modelEfficientNetB3()","metadata":{"execution":{"iopub.status.busy":"2021-10-08T16:19:41.773793Z","iopub.execute_input":"2021-10-08T16:19:41.774228Z","iopub.status.idle":"2021-10-08T16:19:43.003033Z","shell.execute_reply.started":"2021-10-08T16:19:41.77419Z","shell.execute_reply":"2021-10-08T16:19:43.00084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-10-08T16:19:43.003842Z","iopub.status.idle":"2021-10-08T16:19:43.004277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras import utils\n\nutils.plot_model(model)","metadata":{"execution":{"iopub.status.busy":"2021-10-08T16:19:43.005527Z","iopub.status.idle":"2021-10-08T16:19:43.006134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_check = ModelCheckpoint(\n                            \"./firstTry.h5\",\n                            monitor = \"val_loss\",\n                            verbose = 1,\n                            save_best_only = True,\n                            save_weights_only = False,\n                            mode = \"min\")","metadata":{"execution":{"iopub.status.busy":"2021-10-08T16:19:43.007322Z","iopub.status.idle":"2021-10-08T16:19:43.008037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"early_stop= EarlyStopping(\n                                monitor = \"val_loss\",\n                                min_delta=0.001,\n                                patience=7,\n                                verbose=1,\n                                mode=\"min\",\n                                #baseline=None,\n                                restore_best_weights=False)","metadata":{"execution":{"iopub.status.busy":"2021-10-08T16:19:43.009446Z","iopub.status.idle":"2021-10-08T16:19:43.010249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"reduce_lr = ReduceLROnPlateau(\n                                monitor=\"val_loss\",\n                                factor=0.1,\n                                patience=2,\n                                verbose=1,\n                                mode=\"min\",\n                                min_delta=0.0001,\n                                #cooldown=0,\n                                #min_lr=0\n)","metadata":{"execution":{"iopub.status.busy":"2021-10-08T16:19:43.01151Z","iopub.status.idle":"2021-10-08T16:19:43.012407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer = \"adam\",\n            loss = CategoricalCrossentropy(label_smoothing=0.3,reduction=\"auto\",name=\"categorical_crossentropy\"),\n            metrics = [\"accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2021-10-08T16:19:43.013757Z","iopub.status.idle":"2021-10-08T16:19:43.014674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"callbacks = [model_check,reduce_lr]","metadata":{}},{"cell_type":"code","source":"history = model.fit_generator(train_generator,\n                            epochs = 30,\n                            validation_data = valid_generator,\n                            callbacks = [model_check,early_stop,reduce_lr])\n","metadata":{"execution":{"iopub.status.busy":"2021-10-08T16:19:43.015855Z","iopub.status.idle":"2021-10-08T16:19:43.016523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15, 5))\nplt.plot(history.history['accuracy'], 'b*-', label=\"train_acc\")\nplt.plot(history.history['val_accuracy'], 'r*-', label=\"val_acc\")\nplt.grid()\nplt.title(\"train_acc vs val_acc\")\nplt.ylabel(\"Accuracy\")\nplt.xlabel(\"Epochs\")\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-10-08T16:19:43.017834Z","iopub.status.idle":"2021-10-08T16:19:43.018548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15, 5))\nplt.plot(history.history['loss'], 'b*-', label=\"train_loss\")\nplt.plot(history.history['val_loss'], 'r*-', label=\"val_loss\")\nplt.grid()\nplt.title(\"train_loss - val_loss\")\nplt.ylabel(\"Loss\")\nplt.xlabel(\"Epochs\")\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-10-08T16:19:43.019794Z","iopub.status.idle":"2021-10-08T16:19:43.02055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Any suggestions are very valuable to me. please share with me**","metadata":{}},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}