{"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 system libs\nimport os\nimport time\nimport shutil\nimport pathlib\nimport itertools\nfrom PIL import Image\n\n# import data handling tools\nimport zipfile\nimport cv2\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\nsns.set_style('darkgrid')\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix, classification_report\n\n# import Deep learning Libraries\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.optimizers import Adam, Adamax,SGD\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Activation, Dropout, BatchNormalization\nfrom tensorflow.keras import regularizers\nfrom keras.callbacks import EarlyStopping\nfrom keras.regularizers import l2\nfrom keras.utils import to_categorical\n# Ignore Warnings\nimport warnings\nwarnings.filterwarnings(\"ignore\")\n\nprint ('modules loaded')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-08-31T17:49:43.714029Z","iopub.execute_input":"2023-08-31T17:49:43.714594Z","iopub.status.idle":"2023-08-31T17:49:43.726290Z","shell.execute_reply.started":"2023-08-31T17:49:43.714555Z","shell.execute_reply":"2023-08-31T17:49:43.725181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zip_obj = zipfile.ZipFile('/kaggle/input/dogs-vs-cats-redux-kernels-edition/train.zip')\nzip_obj.extractall()\nzip_obj.close()\nfilenames = os.listdir('/kaggle/working/train')\ncategories = []\n\nfor filename in filenames:\n    category = filename.split('.')[0]\n    if category == 'dog':\n        categories.append('1')\n    else:\n        categories.append('0')\ndf = pd.DataFrame({\n    'filename': filenames,\n    'category': categories\n})","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:49:43.728845Z","iopub.execute_input":"2023-08-31T17:49:43.729317Z","iopub.status.idle":"2023-08-31T17:49:53.987668Z","shell.execute_reply.started":"2023-08-31T17:49:43.729285Z","shell.execute_reply":"2023-08-31T17:49:53.986550Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"index = np.random.choice(df.index)\nfilename = df.loc[index, 'filename']\ncategory = df.loc[index, 'category']\nimg = cv2.imread('/kaggle/working/train/' + filename)\nplt.imshow(img)\nplt.title(f'Category: {category}')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:49:53.989168Z","iopub.execute_input":"2023-08-31T17:49:53.990240Z","iopub.status.idle":"2023-08-31T17:49:54.512345Z","shell.execute_reply.started":"2023-08-31T17:49:53.990201Z","shell.execute_reply":"2023-08-31T17:49:54.511443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:49:54.515138Z","iopub.execute_input":"2023-08-31T17:49:54.515711Z","iopub.status.idle":"2023-08-31T17:49:54.785375Z","shell.execute_reply.started":"2023-08-31T17:49:54.515676Z","shell.execute_reply":"2023-08-31T17:49:54.784140Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"strat = df['category']\ntrain_df,dummy_df = train_test_split(df,train_size = 0.70,shuffle = True, random_state = 42, stratify = strat)\n\nstrat = dummy_df['category']\ntest_df,validate_df = train_test_split(dummy_df,train_size = 0.50,shuffle = True, random_state = 42, stratify = strat)","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:49:54.787351Z","iopub.execute_input":"2023-08-31T17:49:54.788039Z","iopub.status.idle":"2023-08-31T17:49:54.842037Z","shell.execute_reply.started":"2023-08-31T17:49:54.787998Z","shell.execute_reply":"2023-08-31T17:49:54.841059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 64\n\nimg_size = (128,128)\nchannels = 3\nimg_shape = (img_size[0],img_size[1],channels)\n\n# Create different ImageDataGenerator objects for training and testing/validation sets\ntr_gen = ImageDataGenerator(rescale=1./255, \n                               shear_range=0.15, \n                               zoom_range=0.2, \n                               horizontal_flip=True)\nts_gen = ImageDataGenerator(rescale=1./255 )\n\n# Specify the directory argument for each set\ntrain_gen = tr_gen.flow_from_dataframe(dataframe = train_df, directory = '/kaggle/working/train',x_col = 'filename' ,y_col = 'category', target_size = img_size, class_mode = 'categorical',color_mode = 'rgb'\n                                      , shuffle = True, batch_size = batch_size)\ntest_gen = ts_gen.flow_from_dataframe(dataframe = test_df, directory = '/kaggle/working/train', x_col = 'filename' ,y_col = 'category', target_size = img_size, class_mode = 'categorical',color_mode = 'rgb'\n                                      , shuffle = True, batch_size = batch_size)\nvalidate_gen = ts_gen.flow_from_dataframe(dataframe = validate_df, directory = '/kaggle/working/train', x_col = 'filename' ,y_col = 'category', target_size = img_size, class_mode = 'categorical',color_mode = 'rgb'\n                                      , shuffle = True, batch_size = batch_size)\n","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:49:54.843701Z","iopub.execute_input":"2023-08-31T17:49:54.844095Z","iopub.status.idle":"2023-08-31T17:49:55.297217Z","shell.execute_reply.started":"2023-08-31T17:49:54.844061Z","shell.execute_reply":"2023-08-31T17:49:55.296111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"c_dict = train_gen.class_indices \nclasses = list(c_dict.keys())\nimages , category = next(train_gen)\n\nfig, axs = plt.subplots(4, 4, figsize=(16,16), gridspec_kw={'width_ratios': [1, 1, 1, 1], 'height_ratios': [1, 1, 1, 1]})\n\nfor i in range(16):\n    row = i // 4\n    col = i % 4\n    axs[row, col].imshow(images[i])\n    index = np.argmax(category[i])\n    class_name = classes[index]\n    axs[row, col].set_title(class_name)\n    axs[row, col].axis('off')\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:49:55.299125Z","iopub.execute_input":"2023-08-31T17:49:55.299490Z","iopub.status.idle":"2023-08-31T17:49:58.924384Z","shell.execute_reply.started":"2023-08-31T17:49:55.299457Z","shell.execute_reply":"2023-08-31T17:49:58.923503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nbase_model = tf.keras.applications.EfficientNetB4(\n    include_top=False,\n    input_shape = img_shape,\n    weights=\"imagenet\",\n    pooling='max',\n    \n)\nmodel = Sequential([\n    base_model,\n    BatchNormalization(),\n    Dense(256,activation = 'relu'),\n    Dropout(rate =0.5,seed = 42),\n    Dense(2,activation = 'softmax')\n    \n])\n\nmodel.compile(Adam(learning_rate = 0.00001), loss = 'categorical_crossentropy' , metrics = ['accuracy'])\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:49:58.925859Z","iopub.execute_input":"2023-08-31T17:49:58.926425Z","iopub.status.idle":"2023-08-31T17:50:04.561305Z","shell.execute_reply.started":"2023-08-31T17:49:58.926391Z","shell.execute_reply":"2023-08-31T17:50:04.560360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(x= train_gen, epochs = 30 ,verbose = 1,validation_data = test_gen)","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:50:04.563078Z","iopub.execute_input":"2023-08-31T17:50:04.563907Z","iopub.status.idle":"2023-08-31T18:53:59.572644Z","shell.execute_reply.started":"2023-08-31T17:50:04.563870Z","shell.execute_reply":"2023-08-31T18:53:59.571620Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define needed variables\ntr_acc = history.history['accuracy']\ntr_loss = history.history['loss']\nval_acc = history.history['val_accuracy']\nval_loss = history.history['val_loss']\nindex_loss = np.argmin(val_loss)\nval_lowest = val_loss[index_loss]\nindex_acc = np.argmax(val_acc)\nacc_highest = val_acc[index_acc]\nEpochs = [i+1 for i in range(len(tr_acc))]\nloss_label = f'best epoch= {str(index_loss + 1)}'\nacc_label = f'best epoch= {str(index_acc + 1)}'\n\n# Plot training history\nplt.figure(figsize= (20, 8))\nplt.style.use('fivethirtyeight')\n\nplt.subplot(1, 2, 1)\nplt.plot(Epochs, tr_loss, 'r', label= 'Training loss')\nplt.plot(Epochs, val_loss, 'g', label= 'Validation loss')\nplt.scatter(index_loss + 1, val_lowest, s= 150, c= 'blue', label= loss_label)\nplt.title('Training and Validation Loss')\nplt.xlabel('Epochs')\nplt.ylabel('Loss')\nplt.legend()\n\nplt.subplot(1, 2, 2)\nplt.plot(Epochs, tr_acc, 'r', label= 'Training Accuracy')\nplt.plot(Epochs, val_acc, 'g', label= 'Validation Accuracy')\nplt.scatter(index_acc + 1 , acc_highest, s= 150, c= 'blue', label= acc_label)\nplt.title('Training and Validation Accuracy')\nplt.xlabel('Epochs')\nplt.ylabel('Accuracy')\nplt.legend()\n\nplt.tight_layout\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-31T18:53:59.576458Z","iopub.execute_input":"2023-08-31T18:53:59.576779Z","iopub.status.idle":"2023-08-31T18:54:00.358547Z","shell.execute_reply.started":"2023-08-31T18:53:59.576738Z","shell.execute_reply":"2023-08-31T18:54:00.357659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ts_length = len(test_df)\ntest_batch_size = max(sorted([ts_length // n for n in range(1, ts_length + 1) if ts_length%n == 0 and ts_length/n <= 80]))\ntest_steps = ts_length // test_batch_size\n\ntrain_score = model.evaluate(train_gen, steps= test_steps, verbose= 1)\nvalid_score = model.evaluate(validate_gen, steps= test_steps, verbose= 1)\ntest_score = model.evaluate(test_gen, steps= test_steps, verbose= 1)\n\nprint(\"Train Loss: \", train_score[0])\nprint(\"Train Accuracy: \", train_score[1])\nprint('-' * 20)\nprint(\"Validation Loss: \", valid_score[0])\nprint(\"Validation Accuracy: \", valid_score[1])\nprint('-' * 20)\nprint(\"Test Loss: \", test_score[0])\nprint(\"Test Accuracy: \", test_score[1])","metadata":{"execution":{"iopub.status.busy":"2023-08-31T18:54:00.360142Z","iopub.execute_input":"2023-08-31T18:54:00.360807Z","iopub.status.idle":"2023-08-31T18:54:36.980781Z","shell.execute_reply.started":"2023-08-31T18:54:00.360771Z","shell.execute_reply":"2023-08-31T18:54:36.979816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = model.predict_generator(test_gen)\ny_pred = np.argmax(preds, axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-08-31T18:54:36.982086Z","iopub.execute_input":"2023-08-31T18:54:36.982434Z","iopub.status.idle":"2023-08-31T18:54:45.636127Z","shell.execute_reply.started":"2023-08-31T18:54:36.982399Z","shell.execute_reply":"2023-08-31T18:54:45.635098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"g_dict = test_gen.class_indices\nclasses = list(g_dict.keys())\n\n# Confusion matrix\ncm = confusion_matrix(test_gen.classes, y_pred)\n\nplt.figure(figsize= (10, 10))\nplt.imshow(cm, interpolation= 'nearest', cmap= plt.cm.Blues)\nplt.title('Confusion Matrix')\nplt.colorbar()\n\ntick_marks = np.arange(len(classes))\nplt.xticks(tick_marks, classes, rotation= 45)\nplt.yticks(tick_marks, classes)\n\n\nthresh = cm.max() / 2.\nfor i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):\n    plt.text(j, i, cm[i, j], horizontalalignment= 'center', color= 'white' if cm[i, j] > thresh else 'black')\n\nplt.tight_layout()\nplt.ylabel('True Label')\nplt.xlabel('Predicted Label')\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-31T18:54:45.637851Z","iopub.execute_input":"2023-08-31T18:54:45.638211Z","iopub.status.idle":"2023-08-31T18:54:46.237557Z","shell.execute_reply.started":"2023-08-31T18:54:45.638177Z","shell.execute_reply":"2023-08-31T18:54:46.236627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}],"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"}}