{"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 os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\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":"8144f8bf-4050-4dcf-8fc9-0d49db6b98c6","_cell_guid":"151c81c5-9b93-490d-acff-44f9370a5953","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-10-22T10:09:49.707535Z","iopub.execute_input":"2023-10-22T10:09:49.707940Z","iopub.status.idle":"2023-10-22T10:10:03.466603Z","shell.execute_reply.started":"2023-10-22T10:09:49.707907Z","shell.execute_reply":"2023-10-22T10:10:03.465072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport tensorflow as tf\nimport tensorflow_addons as tfa\nimport glob, warnings\nimport matplotlib.pyplot as plt\nfrom sklearn.metrics import confusion_matrix, classification_report\nimport seaborn as sns\n\nwarnings.filterwarnings('ignore')\nprint('TensorFlow Version ' +tf.__version__)","metadata":{"execution":{"iopub.status.busy":"2023-10-22T10:14:43.470843Z","iopub.execute_input":"2023-10-22T10:14:43.471277Z","iopub.status.idle":"2023-10-22T10:14:43.477931Z","shell.execute_reply.started":"2023-10-22T10:14:43.471242Z","shell.execute_reply":"2023-10-22T10:14:43.476996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMAGE_SIZE = 224\nBATCH_SIZE = 16\nEPOCHS = 7\n\nTRAIN_PATH = '/kaggle/input/cassava-leaf-disease-classification/train_images'\nTEST_PATH = '/kaggle/input/cassava-leaf-disease-classification/test_images'\n\nDF_TRAIN = pd.read_csv('/kaggle/input/cassava-leaf-disease-classification/train.csv', dtype='str')\nTEST_IMAGES = glob.glob(TEST_PATH + '/*.jpg')\nDF_TEST = pd.DataFrame(TEST_IMAGES, columns = ['image_path'])\n\nclasses = {0 : \"Cassava Bacterial Blight (CBB)\",\n           1 : \"Cassava Brown Streak Disease (CBSD)\",\n           2 : \"Cassava Green Mottle (CGM)\",\n           3 : \"Cassava Mosaic Disease (CMD)\",\n           4 : \"Healthy\"}","metadata":{"execution":{"iopub.status.busy":"2023-10-22T10:14:48.912804Z","iopub.execute_input":"2023-10-22T10:14:48.913213Z","iopub.status.idle":"2023-10-22T10:14:48.948693Z","shell.execute_reply.started":"2023-10-22T10:14:48.913182Z","shell.execute_reply":"2023-10-22T10:14:48.947550Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def data_augment(image):\n    p_spatial = tf.random.uniform([], 0, 1.0, dtype = tf.float32)\n    p_rotate = tf.random.uniform([], 0, 1.0, dtype = tf.float32)\n    p_pixel_1 = tf.random.uniform([], 0, 1.0, dtype = tf.float32)\n    p_pixel_2 = tf.random.uniform([], 0, 1.0, dtype = tf.float32)\n    p_pixel_3 = tf.random.uniform([], 0, 1.0, dtype = tf.float32)\n    \n    # Flips\n    image = tf.image.random_flip_left_right(image)\n    image = tf.image.random_flip_up_down(image)\n    \n    if p_spatial > .75:\n        image = tf.image.transpose(image)\n        \n    # Rotates\n    if p_rotate > .75:\n        image = tf.image.rot90(image, k = 3) # rotate 270º\n    elif p_rotate > .5:\n        image = tf.image.rot90(image, k = 2) # rotate 180º\n    elif p_rotate > .25:\n        image = tf.image.rot90(image, k = 1) # rotate 90º\n\n# Pixel-level transforms\n    if p_pixel_1 >= .4:\n        image = tf.image.random_saturation(image, lower = .7, upper = 1.3)\n    if p_pixel_2 >= .4:\n        image = tf.image.random_contrast(image, lower = .8, upper = 1.2)\n    if p_pixel_3 >= .4:\n        image = tf.image.random_brightness(image, max_delta = .1)\n        \n    return image","metadata":{"execution":{"iopub.status.busy":"2023-10-22T10:15:41.076900Z","iopub.execute_input":"2023-10-22T10:15:41.077782Z","iopub.status.idle":"2023-10-22T10:15:41.092180Z","shell.execute_reply.started":"2023-10-22T10:15:41.077736Z","shell.execute_reply":"2023-10-22T10:15:41.090818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen = tf.keras.preprocessing.image.ImageDataGenerator(rescale = 1./255,\n                                                          samplewise_center = True,\n                                                          samplewise_std_normalization = True,\n                                                          validation_split = 0.2,\n                                                          preprocessing_function = data_augment)\n\ntrain_gen = datagen.flow_from_dataframe(dataframe = DF_TRAIN,\n                                        directory = TRAIN_PATH,\n                                        x_col = 'image_id',\n                                        y_col = 'label',\n                                        subset = 'training',\n                                        batch_size = BATCH_SIZE,\n                                        seed = 1,\n                                        color_mode = 'rgb',\n                                        shuffle = True,\n                                        class_mode = 'categorical',\n                                        target_size = (IMAGE_SIZE, IMAGE_SIZE))\n\nvalid_gen = datagen.flow_from_dataframe(dataframe = DF_TRAIN,\n                                        directory = TRAIN_PATH,\n                                        x_col = 'image_id',\n                                        y_col = 'label',\n                                        subset = 'validation',\n                                        batch_size = BATCH_SIZE,\n                                        seed = 1,\n                                        color_mode = 'rgb',\n                                        shuffle = False,\n                                        class_mode = 'categorical',\n                                        target_size = (IMAGE_SIZE, IMAGE_SIZE))\n\ntest_gen = datagen.flow_from_dataframe(dataframe = DF_TEST,\n                                       x_col = 'image_path',\n                                       y_col = None,\n                                       batch_size = BATCH_SIZE,\n                                       seed = 1,\n                                       color_mode = 'rgb',\n                                       shuffle = False,\n                                       class_mode = None,\n                                       target_size = (IMAGE_SIZE, IMAGE_SIZE))","metadata":{"execution":{"iopub.status.busy":"2023-10-22T10:16:09.079983Z","iopub.execute_input":"2023-10-22T10:16:09.080373Z","iopub.status.idle":"2023-10-22T10:16:33.541005Z","shell.execute_reply.started":"2023-10-22T10:16:09.080342Z","shell.execute_reply":"2023-10-22T10:16:33.539561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = [train_gen[0][0][i] for i in range(16)]\nfig, axes = plt.subplots(3, 5, figsize = (10, 10))\n\naxes = axes.flatten()\n\nfor img, ax in zip(images, axes):\n    ax.imshow(img.reshape(IMAGE_SIZE, IMAGE_SIZE, 3))\n    ax.axis('off')\n\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-22T10:17:10.873810Z","iopub.execute_input":"2023-10-22T10:17:10.874251Z","iopub.status.idle":"2023-10-22T10:17:16.358015Z","shell.execute_reply.started":"2023-10-22T10:17:10.874213Z","shell.execute_reply":"2023-10-22T10:17:16.356781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install --quiet vit-keras\nfrom vit_keras import vit","metadata":{"execution":{"iopub.status.busy":"2023-10-22T10:18:40.618404Z","iopub.execute_input":"2023-10-22T10:18:40.619509Z","iopub.status.idle":"2023-10-22T10:18:57.015720Z","shell.execute_reply.started":"2023-10-22T10:18:40.619463Z","shell.execute_reply":"2023-10-22T10:18:57.014189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vit_model = vit.vit_b32(\n        image_size = IMAGE_SIZE,\n        activation = 'softmax',\n        pretrained = True,\n        include_top = False,\n        pretrained_top = False,\n        classes = 5)","metadata":{"execution":{"iopub.status.busy":"2023-10-22T10:25:21.908374Z","iopub.execute_input":"2023-10-22T10:25:21.908836Z","iopub.status.idle":"2023-10-22T10:25:31.746318Z","shell.execute_reply.started":"2023-10-22T10:25:21.908804Z","shell.execute_reply":"2023-10-22T10:25:31.744990Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Fine- Tuning model","metadata":{}},{"cell_type":"code","source":"model = tf.keras.Sequential([\n        vit_model,\n        tf.keras.layers.Flatten(),\n        tf.keras.layers.BatchNormalization(),\n        tf.keras.layers.Dense(11, activation = tfa.activations.gelu),\n        tf.keras.layers.BatchNormalization(),\n        tf.keras.layers.Dense(5, 'softmax')\n    ],\n    name = 'vision_transformer')\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-10-22T10:29:21.093639Z","iopub.execute_input":"2023-10-22T10:29:21.094334Z","iopub.status.idle":"2023-10-22T10:29:23.248664Z","shell.execute_reply.started":"2023-10-22T10:29:21.094301Z","shell.execute_reply":"2023-10-22T10:29:23.247475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#  Training the model","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* 1. * Import % install Libraries\n* 1. Read / set Data Path & Other Parameters\n* 1. Read  / Do Data Augumentation & mage process\n* 1. Split training and testing Data\n* 1. uilding The DL Model - PreTrained DL Model - VIT\n* 1. Compile the model and train the model\n* 1. Test Model and display ","metadata":{}},{"cell_type":"code","source":"learning_rate = 1e-4\n\noptimizer = tfa.optimizers.RectifiedAdam(learning_rate = learning_rate)\n\nmodel.compile(optimizer = optimizer, \n              loss = tf.keras.losses.CategoricalCrossentropy(label_smoothing = 0.2), \n              metrics = ['accuracy'])\n\nSTEP_SIZE_TRAIN = train_gen.n // train_gen.batch_size\nSTEP_SIZE_VALID = valid_gen.n // valid_gen.batch_size","metadata":{"execution":{"iopub.status.busy":"2023-10-22T10:36:45.823211Z","iopub.execute_input":"2023-10-22T10:36:45.823620Z","iopub.status.idle":"2023-10-22T10:36:45.855303Z","shell.execute_reply.started":"2023-10-22T10:36:45.823589Z","shell.execute_reply":"2023-10-22T10:36:45.853704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"reduce_lr = tf.keras.callbacks.ReduceLROnPlateau(monitor = 'val_accuracy',\n                                                 factor = 0.2,\n                                                 patience = 2,\n                                                 verbose = 1,\n                                                 min_delta = 1e-4,\n                                                 min_lr = 1e-6,\n                                                 mode = 'max')","metadata":{"execution":{"iopub.status.busy":"2023-10-22T10:37:01.651809Z","iopub.execute_input":"2023-10-22T10:37:01.652253Z","iopub.status.idle":"2023-10-22T10:37:01.658407Z","shell.execute_reply.started":"2023-10-22T10:37:01.652219Z","shell.execute_reply":"2023-10-22T10:37:01.657366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"earlystopping = tf.keras.callbacks.EarlyStopping(monitor = 'val_accuracy',\n                                                 min_delta = 1e-4,\n                                                 patience = 5,\n                                                 mode = 'max',\n                                                 restore_best_weights = True,\n                                                 verbose = 1)","metadata":{"execution":{"iopub.status.busy":"2023-10-22T10:37:17.408061Z","iopub.execute_input":"2023-10-22T10:37:17.409163Z","iopub.status.idle":"2023-10-22T10:37:17.415562Z","shell.execute_reply.started":"2023-10-22T10:37:17.409120Z","shell.execute_reply":"2023-10-22T10:37:17.414275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"checkpointer = tf.keras.callbacks.ModelCheckpoint(filepath = './model.hdf5',\n                                                  monitor = 'val_accuracy', \n                                                  verbose = 1, \n                                                  save_best_only = True,\n                                                  save_weights_only = True,\n                                                  mode = 'max')","metadata":{"execution":{"iopub.status.busy":"2023-10-22T10:37:32.786652Z","iopub.execute_input":"2023-10-22T10:37:32.787022Z","iopub.status.idle":"2023-10-22T10:37:32.792809Z","shell.execute_reply.started":"2023-10-22T10:37:32.786993Z","shell.execute_reply":"2023-10-22T10:37:32.791666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_classes = np.argmax(model.predict(valid_gen, steps = valid_gen.n // valid_gen.batch_size + 1), axis = 1)\ntrue_classes = valid_gen.classes\nclass_labels = list(valid_gen.class_indices.keys())  \n\nconfusionmatrix = confusion_matrix(true_classes, predicted_classes)\nplt.figure(figsize = (16, 16))\nsns.heatmap(confusionmatrix, cmap = 'Blues', annot = True, cbar = True)\n\nprint(classification_report(true_classes, predicted_classes))","metadata":{"execution":{"iopub.status.busy":"2023-10-22T10:37:47.985033Z","iopub.execute_input":"2023-10-22T10:37:47.985463Z","iopub.status.idle":"2023-10-22T10:44:29.901664Z","shell.execute_reply.started":"2023-10-22T10:37:47.985430Z","shell.execute_reply":"2023-10-22T10:44:29.900320Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}