{"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 sklearn\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom keras.optimizers import SGD\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom keras.applications.inception_v3 import InceptionV3\nimport seaborn as sns\n\n# Ignore Warnings\nimport warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"execution":{"iopub.status.busy":"2023-04-21T18:19:21.189221Z","iopub.execute_input":"2023-04-21T18:19:21.190502Z","iopub.status.idle":"2023-04-21T18:19:30.439687Z","shell.execute_reply.started":"2023-04-21T18:19:21.190449Z","shell.execute_reply":"2023-04-21T18:19:30.438573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Preprocessing the Training set\ntrain_datagen = ImageDataGenerator(  rescale=1./255,zoom_range=0.2,horizontal_flip=True)\n\ntrain_data = train_datagen.flow_from_directory(\"/kaggle/input/apple12/NewApple1/train\",\n                                              target_size=(128,128),\n                                              batch_size=64,\n                                              class_mode='categorical')\n\nval_data = train_datagen.flow_from_directory(\"/kaggle/input/apple12/NewApple1/val\",\n                                            target_size=(128, 128),\n                                            batch_size=64,\n                                            class_mode='categorical')\ntest_data = train_datagen.flow_from_directory('/kaggle/input/apple12/NewApple1/test',\n                                            target_size=(128,128),\n                                            batch_size=64,\n                                            class_mode='categorical',shuffle=False)\nprint(train_data.class_indices)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-04-21T18:19:30.442990Z","iopub.execute_input":"2023-04-21T18:19:30.444156Z","iopub.status.idle":"2023-04-21T18:19:36.751702Z","shell.execute_reply.started":"2023-04-21T18:19:30.444116Z","shell.execute_reply":"2023-04-21T18:19:36.750552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_pretrained = InceptionV3(weights='imagenet', \n                      include_top=False, \n                      input_shape=(128,128,3))\n\nmodel=keras.models.Sequential()\nmodel.add(model_pretrained)\nmodel.add(keras.layers.Flatten())\nmodel.add(keras.layers.Dense(300, activation=\"relu\"))\nmodel.add(keras.layers.Dropout(0.2))\nmodel.add(keras.layers.Dense(100,activation='relu'))\nmodel.add(keras.layers.Dropout(0.2))\nmodel.add(keras.layers.Dense(6,activation=\"sigmoid\"))\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-04-21T18:19:36.753338Z","iopub.execute_input":"2023-04-21T18:19:36.754018Z","iopub.status.idle":"2023-04-21T18:19:43.717115Z","shell.execute_reply.started":"2023-04-21T18:19:36.753977Z","shell.execute_reply":"2023-04-21T18:19:43.715898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Time based learning rate scheduling\nepoch=15\nlearning_rate=0.01\ndecay_rate=learning_rate/epoch\nmomentum=0.8\nsgd=SGD(lr=learning_rate,momentum=momentum,decay=decay_rate)","metadata":{"execution":{"iopub.status.busy":"2023-04-21T18:19:43.720432Z","iopub.execute_input":"2023-04-21T18:19:43.721476Z","iopub.status.idle":"2023-04-21T18:19:43.728739Z","shell.execute_reply.started":"2023-04-21T18:19:43.721428Z","shell.execute_reply":"2023-04-21T18:19:43.727333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"callback = keras.callbacks.EarlyStopping(monitor='val_loss', patience=5)","metadata":{"execution":{"iopub.status.busy":"2023-04-21T18:19:43.731100Z","iopub.execute_input":"2023-04-21T18:19:43.732149Z","iopub.status.idle":"2023-04-21T18:19:43.746298Z","shell.execute_reply.started":"2023-04-21T18:19:43.732104Z","shell.execute_reply":"2023-04-21T18:19:43.744952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='categorical_crossentropy',\n              optimizer='sgd',\n              metrics='accuracy')","metadata":{"execution":{"iopub.status.busy":"2023-04-21T18:19:43.747881Z","iopub.execute_input":"2023-04-21T18:19:43.748499Z","iopub.status.idle":"2023-04-21T18:19:43.775587Z","shell.execute_reply.started":"2023-04-21T18:19:43.748451Z","shell.execute_reply":"2023-04-21T18:19:43.774314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(train_data,steps_per_epoch=12091//64,epochs=20,validation_data=val_data,validation_steps=1726//64,callbacks=[callback])","metadata":{"execution":{"iopub.status.busy":"2023-04-21T18:23:56.832879Z","iopub.execute_input":"2023-04-21T18:23:56.833325Z","iopub.status.idle":"2023-04-22T04:59:26.758482Z","shell.execute_reply.started":"2023-04-21T18:23:56.833282Z","shell.execute_reply":"2023-04-22T04:59:26.755568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc_train=history.history['accuracy']\nacc_val=history.history['val_accuracy']\nepochs=range(len(acc_train))\nplt.figure(figsize=(12,12))\nplt.plot(epochs,acc_train,'g',label='Training Accuracy')\nplt.plot(epochs,acc_val,'b',label='Validation Accuracy')\nplt.title(\"Training and Validation Accuracy\")\nplt.xlabel(\"Epochs\")\nplt.ylabel(\"Accuracy\")\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-22T04:59:26.765682Z","iopub.execute_input":"2023-04-22T04:59:26.766001Z","iopub.status.idle":"2023-04-22T04:59:27.110891Z","shell.execute_reply.started":"2023-04-22T04:59:26.765970Z","shell.execute_reply":"2023-04-22T04:59:27.109785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss_train=history.history['loss']\nloss_val=history.history['val_loss']\nepochs=range(len(loss_train))\nplt.figure(figsize=(12,12))\nplt.plot(epochs,loss_train,'g',label='Training Loss')\nplt.plot(epochs,loss_val,'b',label='Validation Loss')\nplt.title(\"Training and Validation Loss\")\nplt.xlabel(\"Epochs\")\nplt.ylabel(\"Loss\")\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-22T04:59:27.112423Z","iopub.execute_input":"2023-04-22T04:59:27.113451Z","iopub.status.idle":"2023-04-22T04:59:27.399455Z","shell.execute_reply.started":"2023-04-22T04:59:27.113390Z","shell.execute_reply":"2023-04-22T04:59:27.398409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_accuracy =model.evaluate(test_data,batch_size=64)[1] * 100\nprint('Test accuracy is : ',test_accuracy, '%' )","metadata":{"execution":{"iopub.status.busy":"2023-04-22T04:59:27.402458Z","iopub.execute_input":"2023-04-22T04:59:27.403119Z","iopub.status.idle":"2023-04-22T05:09:00.612167Z","shell.execute_reply.started":"2023-04-22T04:59:27.403085Z","shell.execute_reply":"2023-04-22T05:09:00.610799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_accuracy =model.evaluate(train_data,batch_size=64)[1] * 100\nprint('Train accuracy is : ',train_accuracy, '%' )","metadata":{"execution":{"iopub.status.busy":"2023-04-22T05:09:00.614111Z","iopub.execute_input":"2023-04-22T05:09:00.614524Z","iopub.status.idle":"2023-04-22T05:38:32.487158Z","shell.execute_reply.started":"2023-04-22T05:09:00.614483Z","shell.execute_reply":"2023-04-22T05:38:32.485971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_accuracy =model.evaluate(val_data,batch_size=64)[1] * 100\nprint('Train accuracy is : ',val_accuracy, '%' )","metadata":{"execution":{"iopub.status.busy":"2023-04-22T05:38:32.488939Z","iopub.execute_input":"2023-04-22T05:38:32.489346Z","iopub.status.idle":"2023-04-22T05:43:18.261164Z","shell.execute_reply.started":"2023-04-22T05:38:32.489307Z","shell.execute_reply":"2023-04-22T05:43:18.260067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"true_classes = test_data.classes\nclass_labels = list(test_data.class_indices.keys())","metadata":{"execution":{"iopub.status.busy":"2023-04-22T05:43:18.263179Z","iopub.execute_input":"2023-04-22T05:43:18.263881Z","iopub.status.idle":"2023-04-22T05:43:18.273060Z","shell.execute_reply.started":"2023-04-22T05:43:18.263838Z","shell.execute_reply":"2023-04-22T05:43:18.268137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_steps_per_epoch = np.math.ceil(test_data.samples / test_data.batch_size)\n\npredictions = model.predict(test_data, steps = test_steps_per_epoch)\n\npredicted_classes = np.argmax(predictions, axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-04-22T05:43:18.274946Z","iopub.execute_input":"2023-04-22T05:43:18.275780Z","iopub.status.idle":"2023-04-22T05:52:26.299439Z","shell.execute_reply.started":"2023-04-22T05:43:18.275738Z","shell.execute_reply":"2023-04-22T05:52:26.298021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import sklearn.metrics as metrics\nreport = sklearn.metrics.classification_report(true_classes, predicted_classes, target_names = class_labels)\nprint(report) ","metadata":{"execution":{"iopub.status.busy":"2023-04-22T05:52:26.301033Z","iopub.execute_input":"2023-04-22T05:52:26.301505Z","iopub.status.idle":"2023-04-22T05:52:26.479894Z","shell.execute_reply.started":"2023-04-22T05:52:26.301438Z","shell.execute_reply":"2023-04-22T05:52:26.478436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import sklearn.metrics as metrics\nfrom sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay  \ncm = sklearn.metrics.confusion_matrix(true_classes, predicted_classes)\nplt.figure(figsize=(7,7))\nsns.heatmap(cm, fmt='.0f', cmap=\"crest\", annot=True, linewidths=0.2, xticklabels=class_labels, yticklabels=class_labels)\nplt.title('confusion matrix')\nplt.xlabel('predicted classes')\nplt.ylabel('True classes')\nplt.show()\nprint(sklearn.metrics.confusion_matrix(true_classes, predicted_classes))","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-04-22T05:52:26.483940Z","iopub.execute_input":"2023-04-22T05:52:26.485198Z","iopub.status.idle":"2023-04-22T05:52:27.008005Z","shell.execute_reply.started":"2023-04-22T05:52:26.485146Z","shell.execute_reply":"2023-04-22T05:52:27.006706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import scikitplot as skplt\n\ny_true = true_classes\ny_probas = predictions\nskplt.metrics.plot_roc_curve(y_true, y_probas)\nplt.figure(figsize=(12,12))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-22T05:52:27.009972Z","iopub.execute_input":"2023-04-22T05:52:27.010453Z","iopub.status.idle":"2023-04-22T05:52:27.468881Z","shell.execute_reply.started":"2023-04-22T05:52:27.010387Z","shell.execute_reply":"2023-04-22T05:52:27.467921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}