{"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 tensorflow.keras as keras\nimport pandas as pd\nimport numpy as np\nfrom sklearn.model_selection import train_test_split\nimport cv2\nfrom tqdm.notebook import tqdm\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nimport imagehash\nimport PIL\nimport os","metadata":{"execution":{"iopub.status.busy":"2022-05-19T18:51:06.20921Z","iopub.execute_input":"2022-05-19T18:51:06.209934Z","iopub.status.idle":"2022-05-19T18:51:12.562967Z","shell.execute_reply.started":"2022-05-19T18:51:06.209808Z","shell.execute_reply":"2022-05-19T18:51:12.562239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#config\ntrain_dir = \"../input/plant-pathology-2021-fgvc8/train_images/\"\ntrain_df = pd.read_csv(\"../input/plant-pathology-2021-fgvc8/train.csv\")\n\nclasses = ['scab frog_eye_leaf_spot', 'scab frog_eye_leaf_spot complex', 'complex', 'rust frog_eye_leaf_spot', \n           'powdery_mildew complex', 'powdery_mildew', 'frog_eye_leaf_spot complex', \n           'rust complex', 'rust', 'frog_eye_leaf_spot', 'scab', 'healthy']\n\npaths = train_df['image']\ny_train = train_df['labels']\ny_train = [classes.index(i) for i in y_train]\ny_train = keras.utils.to_categorical(y_train,12)","metadata":{"execution":{"iopub.status.busy":"2022-05-19T18:51:12.564645Z","iopub.execute_input":"2022-05-19T18:51:12.564888Z","iopub.status.idle":"2022-05-19T18:51:12.613232Z","shell.execute_reply.started":"2022-05-19T18:51:12.564856Z","shell.execute_reply":"2022-05-19T18:51:12.612623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# x_train = []\n# for path in tqdm(paths, total=len(paths)):\n#     image = tf.io.read_file(os.path.join(train_dir, path))\n#     image = tf.image.decode_jpeg(image, channels=3)\n#     image = tf.image.resize(image, [224, 224])\n#     image = tf.cast(image, tf.uint8).numpy()\n#     x_train.append(image)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-05-19T14:02:57.208809Z","iopub.execute_input":"2022-05-19T14:02:57.209252Z","iopub.status.idle":"2022-05-19T14:41:04.601371Z","shell.execute_reply.started":"2022-05-19T14:02:57.209206Z","shell.execute_reply":"2022-05-19T14:41:04.600391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# #reshape\n# x_train = np.array(x_train)\n# x_train = x_train.reshape(-1,224,224,3)\n\n# print(len(x_train))\n# print(len(y_train))","metadata":{"execution":{"iopub.status.busy":"2022-05-19T14:42:09.536605Z","iopub.execute_input":"2022-05-19T14:42:09.536953Z","iopub.status.idle":"2022-05-19T14:42:13.084896Z","shell.execute_reply.started":"2022-05-19T14:42:09.536908Z","shell.execute_reply":"2022-05-19T14:42:13.083539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import pickle\n\n# filehandler = open(\"x_train.pkl\",\"wb\") \n# pickle.dump(x_train,filehandler) \n# filehandler.close()","metadata":{"execution":{"iopub.status.busy":"2022-05-19T14:42:15.811612Z","iopub.execute_input":"2022-05-19T14:42:15.812303Z","iopub.status.idle":"2022-05-19T14:42:26.2114Z","shell.execute_reply.started":"2022-05-19T14:42:15.812251Z","shell.execute_reply":"2022-05-19T14:42:26.210382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pickle\n\nfilehandler = open(\"../input/d/datasets/krishadawut/resizeddata/x_train.pkl\",\"rb\") \nx_train = pickle.load(filehandler) \nfilehandler.close()","metadata":{"execution":{"iopub.status.busy":"2022-05-19T18:51:13.77737Z","iopub.execute_input":"2022-05-19T18:51:13.778102Z","iopub.status.idle":"2022-05-19T18:51:33.082759Z","shell.execute_reply.started":"2022-05-19T18:51:13.778067Z","shell.execute_reply":"2022-05-19T18:51:33.082025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nx_train, x_val, y_train, y_val = train_test_split(x_train, y_train, test_size=0.2, random_state=42)\nx_train, x_test, y_train, y_test = train_test_split(x_train, y_train, test_size=0.2, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2022-05-19T18:35:38.042252Z","iopub.execute_input":"2022-05-19T18:35:38.042495Z","iopub.status.idle":"2022-05-19T18:35:40.013824Z","shell.execute_reply.started":"2022-05-19T18:35:38.042461Z","shell.execute_reply":"2022-05-19T18:35:40.013076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train.shape","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Model\n\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import (\n    Dense,\n    Conv2D,\n    MaxPool2D,\n    Flatten,\n    Dropout,\n    BatchNormalization,  \n    RandomFlip,\n    RandomRotation,\n    InputLayer,\n    RandomContrast,\n    RandomZoom,\n    Dropout,\n    GaussianNoise\n)\n\nVGG16_MODEL = tf.keras.applications.VGG16(weights=\"imagenet\",include_top=False , input_shape=(224, 224, 3))\nVGG16_MODEL.trainable = True\nmodel = Sequential()\nmodel.add(VGG16_MODEL)\nmodel.add(Dense(units=256, activation=\"relu\"))\nmodel.add(Dropout(0.2))\nmodel.add(Dense(units=128, activation=\"relu\"))\nmodel.add(Flatten())\nmodel.add(Dense(units=12, activation=\"softmax\"))\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-05-19T18:35:40.015119Z","iopub.execute_input":"2022-05-19T18:35:40.015372Z","iopub.status.idle":"2022-05-19T18:35:43.828836Z","shell.execute_reply.started":"2022-05-19T18:35:40.01534Z","shell.execute_reply":"2022-05-19T18:35:43.827909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Compiling the Model\nopt = keras.optimizers.Adam(learning_rate=0.00001)\nmodel.compile(loss='categorical_crossentropy', optimizer=opt, metrics=[\"accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2022-05-19T18:35:43.830782Z","iopub.execute_input":"2022-05-19T18:35:43.831037Z","iopub.status.idle":"2022-05-19T18:35:43.846354Z","shell.execute_reply.started":"2022-05-19T18:35:43.831002Z","shell.execute_reply":"2022-05-19T18:35:43.845708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator\ndatagen = ImageDataGenerator(\n        rotation_range=10,\n        horizontal_flip=True,\n        vertical_flip=True)","metadata":{"execution":{"iopub.status.busy":"2022-05-19T18:20:25.517524Z","iopub.execute_input":"2022-05-19T18:20:25.518174Z","iopub.status.idle":"2022-05-19T18:20:25.522102Z","shell.execute_reply.started":"2022-05-19T18:20:25.518141Z","shell.execute_reply":"2022-05-19T18:20:25.521232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # Fit the model\nhistory = model.fit(x_train,y_train, batch_size = 32 , epochs = 15, validation_data = (x_val,y_val), verbose = 1)","metadata":{"execution":{"iopub.status.busy":"2022-05-19T18:50:31.062088Z","iopub.execute_input":"2022-05-19T18:50:31.062341Z","iopub.status.idle":"2022-05-19T18:50:38.013185Z","shell.execute_reply.started":"2022-05-19T18:50:31.062312Z","shell.execute_reply":"2022-05-19T18:50:38.010418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = history.history['accuracy']\nval_acc = history.history['val_accuracy']\n\nloss = history.history['loss']\nval_loss = history.history['val_loss']\n\nplt.figure(figsize=(8, 8))\nplt.subplot(2, 1, 1)\nplt.plot(acc, label='Training Accuracy')\nplt.plot(val_acc, label='Validation Accuracy')\nplt.legend(loc='lower right')\nplt.ylabel('Accuracy')\nplt.ylim([min(plt.ylim()),1])\nplt.title('Training and Validation Accuracy')\n\nplt.subplot(2, 1, 2)\nplt.plot(loss, label='Training Loss')\nplt.plot(val_loss, label='Validation Loss')\nplt.legend(loc='upper right')\nplt.ylabel('Cross Entropy')\nplt.ylim([0,1.0])\nplt.title('Training and Validation Loss')\nplt.xlabel('epoch')\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Evaluate on test data\")\nresults = model.evaluate(x_test, y_test, batch_size=32)\nprint(\"test loss, test acc:\", results)","metadata":{"execution":{"iopub.status.busy":"2022-05-19T18:50:13.803312Z","iopub.execute_input":"2022-05-19T18:50:13.803592Z","iopub.status.idle":"2022-05-19T18:50:20.931072Z","shell.execute_reply.started":"2022-05-19T18:50:13.803554Z","shell.execute_reply":"2022-05-19T18:50:20.930375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('plantDiseaseModel2.h5')","metadata":{"execution":{"iopub.status.busy":"2022-05-12T04:10:57.478781Z","iopub.execute_input":"2022-05-12T04:10:57.479064Z","iopub.status.idle":"2022-05-12T04:10:57.826442Z","shell.execute_reply.started":"2022-05-12T04:10:57.479034Z","shell.execute_reply":"2022-05-12T04:10:57.825693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# # **Demo Here**","metadata":{}},{"cell_type":"code","source":"model = keras.models.load_model('../input/platdiseasemodels/plantDiseaseModel2.h5')","metadata":{"execution":{"iopub.status.busy":"2022-05-12T04:18:45.671433Z","iopub.execute_input":"2022-05-12T04:18:45.671771Z","iopub.status.idle":"2022-05-12T04:18:47.705582Z","shell.execute_reply.started":"2022-05-12T04:18:45.671731Z","shell.execute_reply":"2022-05-12T04:18:47.704822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testImagesList = ['../input/testimages/8002cb321f8bfcdf.jpg','../input/testimages/800f85dc5f407aef.jpg','../input/testimages/801d6dcd96e48ebc.jpg','../input/testimages/802b34badefa2ed0.jpg']","metadata":{"execution":{"iopub.status.busy":"2022-05-12T04:18:51.021074Z","iopub.execute_input":"2022-05-12T04:18:51.021776Z","iopub.status.idle":"2022-05-12T04:18:51.026741Z","shell.execute_reply.started":"2022-05-12T04:18:51.021739Z","shell.execute_reply":"2022-05-12T04:18:51.026044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# read list of image\nx_test = []\nfor path in tqdm(testImagesList, total=len(testImagesList)):\n    image = tf.io.read_file(path)\n    image = tf.image.decode_jpeg(image, channels=3)\n    image = tf.image.resize(image, [112, 112])\n    image = tf.cast(image, tf.uint8).numpy()\n    x_test.append(image)","metadata":{"execution":{"iopub.status.busy":"2022-05-12T04:19:49.409488Z","iopub.execute_input":"2022-05-12T04:19:49.409758Z","iopub.status.idle":"2022-05-12T04:19:49.825982Z","shell.execute_reply.started":"2022-05-12T04:19:49.409728Z","shell.execute_reply":"2022-05-12T04:19:49.825204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test = np.array(x_test)\nx_test = x_test.reshape(-1,112,112,3)","metadata":{"execution":{"iopub.status.busy":"2022-05-12T04:20:31.988798Z","iopub.execute_input":"2022-05-12T04:20:31.989054Z","iopub.status.idle":"2022-05-12T04:20:31.993923Z","shell.execute_reply.started":"2022-05-12T04:20:31.989024Z","shell.execute_reply":"2022-05-12T04:20:31.993165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2022-05-12T04:27:52.268902Z","iopub.execute_input":"2022-05-12T04:27:52.269175Z","iopub.status.idle":"2022-05-12T04:27:52.273215Z","shell.execute_reply.started":"2022-05-12T04:27:52.269145Z","shell.execute_reply":"2022-05-12T04:27:52.272337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = x_test[3:4]\npredictions = model.predict(test)\npredictions = np.argmax(predictions, axis=1)\nimgplot = plt.imshow(test.reshape(112,112,3))\nplt.show()\nprint(testImagesList[3:4])\nprint(f'Prediction: {classes[predictions[0]]}')","metadata":{"execution":{"iopub.status.busy":"2022-05-12T04:32:45.643589Z","iopub.execute_input":"2022-05-12T04:32:45.643884Z","iopub.status.idle":"2022-05-12T04:32:45.896204Z","shell.execute_reply.started":"2022-05-12T04:32:45.643852Z","shell.execute_reply":"2022-05-12T04:32:45.895358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}}]}