{"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":"import numpy as np\nimport os\nimport PIL\nimport PIL.Image\nimport tensorflow as tf\nimport pathlib\nimport matplotlib.pyplot as plt\nfrom glob import glob\nimport sys\nimport sklearn.metrics as metrics\nfrom sklearn.metrics import accuracy_score,classification_report,confusion_matrix\nimport seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2022-07-10T11:57:12.61261Z","iopub.execute_input":"2022-07-10T11:57:12.613284Z","iopub.status.idle":"2022-07-10T11:57:18.538194Z","shell.execute_reply.started":"2022-07-10T11:57:12.613205Z","shell.execute_reply":"2022-07-10T11:57:18.537228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow.keras\nfrom tensorflow.keras.models import Sequential, Model, load_model\nfrom tensorflow.keras.applications.vgg16 import VGG16,preprocess_input\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator,load_img, img_to_array\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Dense, Dropout, Input, Flatten, Activation\nfrom tensorflow.keras.optimizers import Adam, SGD, RMSprop\nfrom tensorflow.keras.callbacks import Callback, EarlyStopping\nfrom tensorflow.keras.utils import to_categorical\nfrom sklearn.metrics import confusion_matrix\nfrom tensorflow.keras import backend as K\nfrom tensorflow.keras.layers import Input, Lambda, Dense, Flatten\nfrom tensorflow.keras.applications.inception_v3 import InceptionV3\nfrom tensorflow.keras.applications.inception_v3 import preprocess_input\nfrom tensorflow.keras.preprocessing import image\nfrom tensorflow.keras.layers import Input, Lambda, Dense, Flatten, BatchNormalization","metadata":{"execution":{"iopub.status.busy":"2022-07-10T11:57:18.540188Z","iopub.execute_input":"2022-07-10T11:57:18.540833Z","iopub.status.idle":"2022-07-10T11:57:19.994111Z","shell.execute_reply.started":"2022-07-10T11:57:18.540795Z","shell.execute_reply":"2022-07-10T11:57:19.993128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path = '../input/paddy-disease-classification/train_images'\ntest_path = '../input/paddy-disease-classification/test_images'","metadata":{"execution":{"iopub.status.busy":"2022-07-10T11:57:19.995621Z","iopub.execute_input":"2022-07-10T11:57:19.995963Z","iopub.status.idle":"2022-07-10T11:57:20.003408Z","shell.execute_reply.started":"2022-07-10T11:57:19.995928Z","shell.execute_reply":"2022-07-10T11:57:20.001659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_count = len(list(pathlib.Path(train_path).glob('*/*.jpg')))\nprint(image_count)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T11:57:20.005539Z","iopub.execute_input":"2022-07-10T11:57:20.006024Z","iopub.status.idle":"2022-07-10T11:57:20.866421Z","shell.execute_reply.started":"2022-07-10T11:57:20.005985Z","shell.execute_reply":"2022-07-10T11:57:20.865365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_set = tf.keras.utils.image_dataset_from_directory(\n    train_path,\n    validation_split=0.2,\n    subset=\"training\",\n    # shuffle = False,\n    seed=12,\n    image_size=(224, 224),\n    batch_size=32)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T12:07:11.536905Z","iopub.execute_input":"2022-07-10T12:07:11.537252Z","iopub.status.idle":"2022-07-10T12:07:12.242065Z","shell.execute_reply.started":"2022-07-10T12:07:11.537222Z","shell.execute_reply":"2022-07-10T12:07:12.240896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_set = tf.keras.utils.image_dataset_from_directory(\n    train_path,\n    validation_split=0.2,\n    subset=\"validation\",\n#     shuffle = False,\n    seed=12,\n    image_size=(224, 224),\n    batch_size=32)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T12:07:12.244259Z","iopub.execute_input":"2022-07-10T12:07:12.244666Z","iopub.status.idle":"2022-07-10T12:07:12.925055Z","shell.execute_reply.started":"2022-07-10T12:07:12.244628Z","shell.execute_reply":"2022-07-10T12:07:12.924004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_names = training_set.class_names\nprint(class_names)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T12:07:14.415378Z","iopub.execute_input":"2022-07-10T12:07:14.416059Z","iopub.status.idle":"2022-07-10T12:07:14.421462Z","shell.execute_reply.started":"2022-07-10T12:07:14.416019Z","shell.execute_reply":"2022-07-10T12:07:14.420224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nplt.figure(figsize=(20, 20))\nfor images, labels in training_set.take(1):\n    for i in range(15):\n        ax = plt.subplot(5, 3, i + 1)\n        plt.imshow(images[i].numpy().astype(\"uint8\"))\n        plt.title(class_names[labels[i]])\n        plt.axis(\"off\")","metadata":{"execution":{"iopub.status.busy":"2022-07-10T12:07:17.700773Z","iopub.execute_input":"2022-07-10T12:07:17.701453Z","iopub.status.idle":"2022-07-10T12:07:20.133049Z","shell.execute_reply.started":"2022-07-10T12:07:17.701404Z","shell.execute_reply":"2022-07-10T12:07:20.131904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# InceptionV3","metadata":{}},{"cell_type":"code","source":"IMAGE_SIZE = [224, 224]\ninception = InceptionV3(input_shape=IMAGE_SIZE + [3], weights='imagenet', include_top=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T12:07:57.041402Z","iopub.execute_input":"2022-07-10T12:07:57.041995Z","iopub.status.idle":"2022-07-10T12:07:59.39531Z","shell.execute_reply.started":"2022-07-10T12:07:57.041955Z","shell.execute_reply":"2022-07-10T12:07:59.394314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for layer in inception.layers:\n    layer.trainable = False","metadata":{"execution":{"iopub.status.busy":"2022-07-10T12:08:00.248087Z","iopub.execute_input":"2022-07-10T12:08:00.248674Z","iopub.status.idle":"2022-07-10T12:08:00.265548Z","shell.execute_reply.started":"2022-07-10T12:08:00.248634Z","shell.execute_reply":"2022-07-10T12:08:00.264581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = Flatten()(inception.output)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T12:08:03.229683Z","iopub.execute_input":"2022-07-10T12:08:03.230498Z","iopub.status.idle":"2022-07-10T12:08:03.245056Z","shell.execute_reply.started":"2022-07-10T12:08:03.230454Z","shell.execute_reply":"2022-07-10T12:08:03.244232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"folders = glob(train_path+'/*')","metadata":{"execution":{"iopub.status.busy":"2022-07-10T12:08:30.856876Z","iopub.execute_input":"2022-07-10T12:08:30.857242Z","iopub.status.idle":"2022-07-10T12:08:30.862389Z","shell.execute_reply.started":"2022-07-10T12:08:30.85721Z","shell.execute_reply":"2022-07-10T12:08:30.861489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction = Dense(len(folders), activation='softmax')(x)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T12:08:32.196368Z","iopub.execute_input":"2022-07-10T12:08:32.196733Z","iopub.status.idle":"2022-07-10T12:08:32.212066Z","shell.execute_reply.started":"2022-07-10T12:08:32.196701Z","shell.execute_reply":"2022-07-10T12:08:32.211182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Model(inputs=inception.input, outputs=prediction)\n\nmodel.compile(\n  loss='categorical_crossentropy',\n  optimizer='adam',\n  metrics=['accuracy']\n)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T12:08:40.210474Z","iopub.execute_input":"2022-07-10T12:08:40.210862Z","iopub.status.idle":"2022-07-10T12:08:40.247804Z","shell.execute_reply.started":"2022-07-10T12:08:40.210829Z","shell.execute_reply":"2022-07-10T12:08:40.246889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\n\ntrain_datagen = ImageDataGenerator(rescale = 1./255,\n                                   shear_range = 0.2,\n                                   zoom_range = 0.2,\n                                   validation_split=0.2,\n                                   horizontal_flip = True)\n\ntest_datagen = ImageDataGenerator(rescale = 1./255)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T12:14:24.16496Z","iopub.execute_input":"2022-07-10T12:14:24.165312Z","iopub.status.idle":"2022-07-10T12:14:24.171039Z","shell.execute_reply.started":"2022-07-10T12:14:24.165282Z","shell.execute_reply":"2022-07-10T12:14:24.16993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ds = train_datagen.flow_from_directory(train_path,\n                                             target_size = (224, 224),\n                                             batch_size = 32,\n                                             class_mode = 'categorical',\n                                             subset='training',\n                                             shuffle = False)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T12:16:35.080976Z","iopub.execute_input":"2022-07-10T12:16:35.081338Z","iopub.status.idle":"2022-07-10T12:16:35.626353Z","shell.execute_reply.started":"2022-07-10T12:16:35.081306Z","shell.execute_reply":"2022-07-10T12:16:35.625381Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_ds = train_datagen.flow_from_directory(train_path,\n                                             target_size = (224, 224),\n                                             batch_size = 32,\n                                             class_mode = 'categorical',\n                                             subset='validation',\n                                             shuffle = False)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T12:16:38.794005Z","iopub.execute_input":"2022-07-10T12:16:38.794349Z","iopub.status.idle":"2022-07-10T12:16:39.006042Z","shell.execute_reply.started":"2022-07-10T12:16:38.794319Z","shell.execute_reply":"2022-07-10T12:16:39.005032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.config.run_functions_eagerly(True)\nr = model.fit(\n  train_ds,\n  validation_data = val_ds,\n  epochs=10,\n)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T12:20:10.530334Z","iopub.execute_input":"2022-07-10T12:20:10.531178Z","iopub.status.idle":"2022-07-10T12:52:35.318039Z","shell.execute_reply.started":"2022-07-10T12:20:10.531133Z","shell.execute_reply":"2022-07-10T12:52:35.316981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(r.history['loss'], label='train loss')\nplt.plot(r.history['val_loss'], label='val loss')\nplt.legend()\nplt.show()\nplt.savefig('LossVal_loss')\n\n# plot the accuracy\nplt.plot(r.history['accuracy'], label='train acc')\nplt.plot(r.history['val_accuracy'], label='val acc')\nplt.legend()\nplt.show()\nplt.savefig('AccVal_acc')","metadata":{"execution":{"iopub.status.busy":"2022-07-10T12:52:35.688665Z","iopub.execute_input":"2022-07-10T12:52:35.689001Z","iopub.status.idle":"2022-07-10T12:52:36.057559Z","shell.execute_reply.started":"2022-07-10T12:52:35.688967Z","shell.execute_reply":"2022-07-10T12:52:36.055703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"probabilities = model.predict_generator(val_ds)\ny_pred = np.argmax(probabilities, axis=1)\n\nprint(confusion_matrix(val_ds.classes, y_pred))","metadata":{"execution":{"iopub.status.busy":"2022-07-10T12:52:36.060623Z","iopub.execute_input":"2022-07-10T12:52:36.061258Z","iopub.status.idle":"2022-07-10T12:53:10.23148Z","shell.execute_reply.started":"2022-07-10T12:52:36.061215Z","shell.execute_reply":"2022-07-10T12:53:10.230393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cf_matrix = confusion_matrix(val_ds.classes, y_pred)\nplt.figure(figsize=(12, 8))\nsns.heatmap(cf_matrix, annot=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T12:53:10.23293Z","iopub.execute_input":"2022-07-10T12:53:10.233522Z","iopub.status.idle":"2022-07-10T12:53:10.963421Z","shell.execute_reply.started":"2022-07-10T12:53:10.233483Z","shell.execute_reply":"2022-07-10T12:53:10.962492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(val_ds.classes, y_pred))","metadata":{"execution":{"iopub.status.busy":"2022-07-10T12:53:10.965093Z","iopub.execute_input":"2022-07-10T12:53:10.965875Z","iopub.status.idle":"2022-07-10T12:53:10.983356Z","shell.execute_reply.started":"2022-07-10T12:53:10.965833Z","shell.execute_reply":"2022-07-10T12:53:10.982362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# VGG19","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.applications.vgg16 import VGG16","metadata":{"execution":{"iopub.status.busy":"2022-07-10T12:55:34.955799Z","iopub.execute_input":"2022-07-10T12:55:34.956488Z","iopub.status.idle":"2022-07-10T12:55:34.961504Z","shell.execute_reply.started":"2022-07-10T12:55:34.956447Z","shell.execute_reply":"2022-07-10T12:55:34.960052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vgg_model = VGG16(include_top = False, weights = 'imagenet', input_shape = (224, 224, 3))","metadata":{"execution":{"iopub.status.busy":"2022-07-10T12:57:19.62523Z","iopub.execute_input":"2022-07-10T12:57:19.625603Z","iopub.status.idle":"2022-07-10T12:57:19.899631Z","shell.execute_reply.started":"2022-07-10T12:57:19.625571Z","shell.execute_reply":"2022-07-10T12:57:19.89864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for layer in vgg_model.layers:\n    layer.trainable = False","metadata":{"execution":{"iopub.status.busy":"2022-07-10T12:57:20.896524Z","iopub.execute_input":"2022-07-10T12:57:20.89798Z","iopub.status.idle":"2022-07-10T12:57:20.903631Z","shell.execute_reply.started":"2022-07-10T12:57:20.897935Z","shell.execute_reply":"2022-07-10T12:57:20.902542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" from tensorflow.keras import layers ","metadata":{"execution":{"iopub.status.busy":"2022-07-10T12:57:21.696569Z","iopub.execute_input":"2022-07-10T12:57:21.697859Z","iopub.status.idle":"2022-07-10T12:57:21.702923Z","shell.execute_reply.started":"2022-07-10T12:57:21.697809Z","shell.execute_reply":"2022-07-10T12:57:21.70181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = layers.Flatten()(vgg_model.output)\n\nx = layers.Dense(512, activation='relu')(x)\n\nx = layers.Dropout(0.5)(x)\n\nx = layers.Dense(10, activation='softmax')(x)\nmodel = tf.keras.models.Model(vgg_model.input, x)\nmodel.compile(optimizer = tf.keras.optimizers.RMSprop(lr=0.0001), loss = 'categorical_crossentropy',metrics=['categorical_accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-07-10T13:04:22.95Z","iopub.execute_input":"2022-07-10T13:04:22.950431Z","iopub.status.idle":"2022-07-10T13:04:22.984729Z","shell.execute_reply.started":"2022-07-10T13:04:22.950396Z","shell.execute_reply":"2022-07-10T13:04:22.983751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vgghist = model.fit(train_ds, validation_data = val_ds, steps_per_epoch = 100, epochs = 10)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-10T13:04:23.597716Z","iopub.execute_input":"2022-07-10T13:04:23.598729Z","iopub.status.idle":"2022-07-10T13:25:39.452697Z","shell.execute_reply.started":"2022-07-10T13:04:23.598683Z","shell.execute_reply":"2022-07-10T13:25:39.451669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(vgghist.history['loss'], label='train loss')\nplt.plot(vgghist.history['val_loss'], label='val loss')\nplt.legend()\nplt.show()\nplt.savefig('LossVal_loss')\n\n# plot the accuracy\nplt.plot(vgghist.history['categorical_accuracy'], label='train acc')\nplt.plot(vgghist.history['val_categorical_accuracy'], label='val acc')\nplt.legend()\nplt.show()\nplt.savefig('AccVal_acc')","metadata":{"execution":{"iopub.status.busy":"2022-07-10T13:25:39.455417Z","iopub.execute_input":"2022-07-10T13:25:39.455886Z","iopub.status.idle":"2022-07-10T13:25:39.888245Z","shell.execute_reply.started":"2022-07-10T13:25:39.45585Z","shell.execute_reply":"2022-07-10T13:25:39.88724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"probabilities = model.predict_generator(val_ds)\ny_pred = np.argmax(probabilities, axis=1)\n\nprint(confusion_matrix(val_ds.classes, y_pred))","metadata":{"execution":{"iopub.status.busy":"2022-07-10T13:25:50.815999Z","iopub.execute_input":"2022-07-10T13:25:50.816353Z","iopub.status.idle":"2022-07-10T13:26:22.148856Z","shell.execute_reply.started":"2022-07-10T13:25:50.816321Z","shell.execute_reply":"2022-07-10T13:26:22.147815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cf_matrix = confusion_matrix(val_ds.classes, y_pred)\nplt.figure(figsize=(12, 8))\nsns.heatmap(cf_matrix, annot=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T13:26:22.15073Z","iopub.execute_input":"2022-07-10T13:26:22.151325Z","iopub.status.idle":"2022-07-10T13:26:22.690086Z","shell.execute_reply.started":"2022-07-10T13:26:22.151285Z","shell.execute_reply":"2022-07-10T13:26:22.689149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(val_ds.classes, y_pred, target_names=class_names))","metadata":{"execution":{"iopub.status.busy":"2022-07-10T13:27:05.28658Z","iopub.execute_input":"2022-07-10T13:27:05.286966Z","iopub.status.idle":"2022-07-10T13:27:05.303154Z","shell.execute_reply.started":"2022-07-10T13:27:05.286933Z","shell.execute_reply":"2022-07-10T13:27:05.3021Z"},"trusted":true},"execution_count":null,"outputs":[]}]}