{"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":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_kg_hide-input":false,"_kg_hide-output":false,"execution":{"iopub.status.busy":"2021-12-17T17:49:54.505594Z","iopub.execute_input":"2021-12-17T17:49:54.506242Z","iopub.status.idle":"2021-12-17T17:50:04.575505Z","shell.execute_reply.started":"2021-12-17T17:49:54.506133Z","shell.execute_reply":"2021-12-17T17:50:04.574648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train =  pd.read_csv('/kaggle/input/cassava-leaf-disease-classification/train.csv')\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2021-12-17T17:50:25.999936Z","iopub.execute_input":"2021-12-17T17:50:26.000193Z","iopub.status.idle":"2021-12-17T17:50:26.047303Z","shell.execute_reply.started":"2021-12-17T17:50:26.000165Z","shell.execute_reply":"2021-12-17T17:50:26.046403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.ndim","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path = '/kaggle/input/cassava-leaf-disease-classification/train_images/'\ntest_path = '/kaggle/input/cassava-leaf-disease-classification/test_images/'","metadata":{"execution":{"iopub.status.busy":"2021-12-17T17:50:32.730035Z","iopub.execute_input":"2021-12-17T17:50:32.730331Z","iopub.status.idle":"2021-12-17T17:50:32.734626Z","shell.execute_reply.started":"2021-12-17T17:50:32.730297Z","shell.execute_reply":"2021-12-17T17:50:32.733683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def image_path(image):\n    return os.path.join(train_path,image)\n\ntrain['image_id'] = train['image_id'].apply(image_path)","metadata":{"execution":{"iopub.status.busy":"2021-12-17T17:50:38.095574Z","iopub.execute_input":"2021-12-17T17:50:38.095876Z","iopub.status.idle":"2021-12-17T17:50:38.155562Z","shell.execute_reply.started":"2021-12-17T17:50:38.095844Z","shell.execute_reply":"2021-12-17T17:50:38.154471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['label'] = train['label'].astype(str)","metadata":{"execution":{"iopub.status.busy":"2021-12-17T17:50:42.766065Z","iopub.execute_input":"2021-12-17T17:50:42.76641Z","iopub.status.idle":"2021-12-17T17:50:42.798958Z","shell.execute_reply.started":"2021-12-17T17:50:42.766376Z","shell.execute_reply":"2021-12-17T17:50:42.797693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head(4)","metadata":{"execution":{"iopub.status.busy":"2021-12-17T13:29:42.93171Z","iopub.execute_input":"2021-12-17T13:29:42.931988Z","iopub.status.idle":"2021-12-17T13:29:42.941324Z","shell.execute_reply.started":"2021-12-17T13:29:42.93196Z","shell.execute_reply":"2021-12-17T13:29:42.940635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nfrom matplotlib.image import imread","metadata":{"execution":{"iopub.status.busy":"2021-12-17T17:50:51.089064Z","iopub.execute_input":"2021-12-17T17:50:51.089387Z","iopub.status.idle":"2021-12-17T17:50:51.093804Z","shell.execute_reply.started":"2021-12-17T17:50:51.089348Z","shell.execute_reply":"2021-12-17T17:50:51.092712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nplt.imshow(imread(train['image_id'][500]))\nplt.title('Cassava Leaf')\n\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2021-12-17T17:50:55.486302Z","iopub.execute_input":"2021-12-17T17:50:55.486604Z","iopub.status.idle":"2021-12-17T17:50:55.835577Z","shell.execute_reply.started":"2021-12-17T17:50:55.486575Z","shell.execute_reply":"2021-12-17T17:50:55.834678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn","metadata":{"execution":{"iopub.status.busy":"2021-12-17T17:51:01.196009Z","iopub.execute_input":"2021-12-17T17:51:01.197069Z","iopub.status.idle":"2021-12-17T17:51:02.102061Z","shell.execute_reply.started":"2021-12-17T17:51:01.197023Z","shell.execute_reply":"2021-12-17T17:51:02.101261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"seaborn.countplot(train['label'])\n#plt.title('Count of the various disease types in Cassava leaves')\nplt.grid()\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2021-12-17T17:51:05.085011Z","iopub.execute_input":"2021-12-17T17:51:05.086046Z","iopub.status.idle":"2021-12-17T17:51:05.302641Z","shell.execute_reply.started":"2021-12-17T17:51:05.085991Z","shell.execute_reply":"2021-12-17T17:51:05.301876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#from sklearn.model_selection import train_test_split\nimport tensorflow as tf","metadata":{"execution":{"iopub.status.busy":"2021-12-17T17:51:10.629854Z","iopub.execute_input":"2021-12-17T17:51:10.630977Z","iopub.status.idle":"2021-12-17T17:51:15.764073Z","shell.execute_reply.started":"2021-12-17T17:51:10.630898Z","shell.execute_reply":"2021-12-17T17:51:15.763225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_generator = tf.keras.preprocessing.image.ImageDataGenerator(\n                                                                 validation_split=0.2,\n                                                                  rescale = 1./255,\n                                                                  dtype='float32')","metadata":{"execution":{"iopub.status.busy":"2021-12-17T17:51:24.494711Z","iopub.execute_input":"2021-12-17T17:51:24.494999Z","iopub.status.idle":"2021-12-17T17:51:25.631453Z","shell.execute_reply.started":"2021-12-17T17:51:24.494968Z","shell.execute_reply":"2021-12-17T17:51:25.630585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#train_generator = tf.keras.preprocessing.image.ImageDataGenerator()\ntrain_data = image_generator.flow_from_dataframe(dataframe=train,\n                                                      directory=None,\n                                                      x_col='image_id',\n                                                      y_col='label',\n                                                      subset='training',\n                                                      color_mode='rgb',\n                                                      batch_size=32,\n                                                      shuffle=True,\n                                                      class_mode='categorical',\n                                                      target_size=(224,224))","metadata":{"execution":{"iopub.status.busy":"2021-12-17T17:51:36.885449Z","iopub.execute_input":"2021-12-17T17:51:36.885772Z","iopub.status.idle":"2021-12-17T17:51:45.086088Z","shell.execute_reply.started":"2021-12-17T17:51:36.885734Z","shell.execute_reply":"2021-12-17T17:51:45.084296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validation_generator = image_generator.flow_from_dataframe(dataframe=train,\n                                                           directory=None,\n                                                           x_col='image_id',\n                                                           y_col='label',\n                                                           subset='validation',\n                                                           color_mode='rgb',\n                                                           batch_size=32,\n                                                           seed=42,\n                                                           shuffle=False,\n                                                           class_mode='categorical',\n                                                           target_size=(224,224))\n","metadata":{"execution":{"iopub.status.busy":"2021-12-17T17:51:56.655268Z","iopub.execute_input":"2021-12-17T17:51:56.65584Z","iopub.status.idle":"2021-12-17T17:52:05.269058Z","shell.execute_reply.started":"2021-12-17T17:51:56.655791Z","shell.execute_reply":"2021-12-17T17:52:05.268176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_model = tf.keras.models.Sequential()\nmy_model.add(tf.keras.layers.Conv2D(32, (3,3), activation='relu', input_shape=(224, 224, 3)))\nmy_model.add(tf.keras.layers.MaxPooling2D(2,2))\nmy_model.add(tf.keras.layers.Conv2D(64, (3,3), activation='relu'))\nmy_model.add(tf.keras.layers.MaxPooling2D(2,2))\nmy_model.add(tf.keras.layers.Conv2D(128, (3,3), activation='relu'))\nmy_model.add(tf.keras.layers.MaxPooling2D(2,2))\n#my_model.add(tf.keras.layers.Conv2D(512, (3,3), activation='relu'))\n#my_model.add(tf.keras.layers.MaxPooling2D(2,2))\nmy_model.add(tf.keras.layers.Flatten())\nmy_model.add(tf.keras.layers.Dense(256, activation='relu'))\nmy_model.add(tf.keras.layers.Dense(5, activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2021-12-17T18:10:34.755259Z","iopub.execute_input":"2021-12-17T18:10:34.755856Z","iopub.status.idle":"2021-12-17T18:10:34.925968Z","shell.execute_reply.started":"2021-12-17T18:10:34.755815Z","shell.execute_reply":"2021-12-17T18:10:34.925124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-12-17T18:10:40.726301Z","iopub.execute_input":"2021-12-17T18:10:40.726632Z","iopub.status.idle":"2021-12-17T18:10:40.737727Z","shell.execute_reply.started":"2021-12-17T18:10:40.726599Z","shell.execute_reply":"2021-12-17T18:10:40.73688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_model.compile(\n    loss='categorical_crossentropy',\n    optimizer='rmsprop',\n    metrics=['accuracy']\n)","metadata":{"execution":{"iopub.status.busy":"2021-12-17T18:10:49.631002Z","iopub.execute_input":"2021-12-17T18:10:49.63169Z","iopub.status.idle":"2021-12-17T18:10:49.64488Z","shell.execute_reply.started":"2021-12-17T18:10:49.631643Z","shell.execute_reply":"2021-12-17T18:10:49.643984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = my_model.fit(\n    train_data,\n    steps_per_epoch = 50,\n    epochs=5,\n    validation_data=validation_generator)","metadata":{"execution":{"iopub.status.busy":"2021-12-17T18:10:56.181077Z","iopub.execute_input":"2021-12-17T18:10:56.181732Z","iopub.status.idle":"2021-12-17T18:22:18.825953Z","shell.execute_reply.started":"2021-12-17T18:10:56.18168Z","shell.execute_reply":"2021-12-17T18:22:18.825207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_accuracy = history.history['accuracy']\nvalidation_accuracy = history.history['val_accuracy']\n\nepoks = range(0, 5)\nplt.plot(epoks, train_accuracy, '--', label='Training Acc')\nplt.plot(epoks, validation_accuracy, 'b', label='Validation Acc')\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_model.summary()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}