{"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":"markdown","source":"# <p style=\"text-align:center;\"><span style=\"font-size:48px;\"><span style=\"color:#907FFB;\"><span style=\"font-family:cursive;\"> Cat & Dog Detection</span> </span>  </span></p>","metadata":{}},{"cell_type":"markdown","source":"![image](https://storage.googleapis.com/petbacker/images/blog/2017/dog-and-cat-cover.jpg)","metadata":{}},{"cell_type":"markdown","source":"<a id=\"contents_tabel\"></a>\n<div style=\"border-radius:10px; padding: 15px; background-color: #FFF545; font-size:115%; text-align:left\">\n\n<h3 align=\"left\"><font color=black>Table of Contents :</font></h3>\n\n* [Phase 1 : Import & Install Libraries](#import)    \n* [Phase 2 : About Dataset](#about)\n    - [Phase 2.1 : Import Dataset](#importdata)\n    - [Phase 2.2 : About Shape and Number](#features)\n* [Phase 3 : EDA ( Exploratory data analysis ) On Dataset](#eda)\n* [Phase 4 : Data Preprocessing](#prep)\n    - [Phase 4.1 : Delete Very Low Size Images](#lowsize)\n    - [Phase 4.2 : Increase Data Quality](#inc)\n    - [Phase 4.3 : Increase Images](#increaseimages)\n    - [Phase 4.4 : Train, Test, Validation Split](#ttsplit)\n    - [Phase 4.5 : Data Augmentation](#aug)\n* [Method 1](#m1)    \n    - [Phase 5 : Prepare Network](#net)    \n    - [Phase 6 : Prepare Train Objects](#trainobj)    \n    - [Phase 7 : Train](#train)    \n    - [Phase 8 : Evaluate](#eval)\n    - [Phase 9 : Test](#test)\n* [Method 2](#m2)    \n    - [Phase 5 : Feature Extraction](#fe)    \n    - [Phase 6 : Make ML Model](#ml)    \n    - [Phase 7 : Train](#train_ml)    \n    - [Phase 8 : Evaluate](#eval_ml)\n    - [Phase 9 : Test](#test_ml)","metadata":{}},{"cell_type":"markdown","source":"<a id=\"import\"></a>\n# <p style=\"background-color:#afeff9; padding: 15px; font-family:cursive; color:black; font-size:100%; text-align:center; border-radius:200px 200px;\">Phase 1 : Import & Install Libraries</p>\n\n⬆️ [Tabel of Contents](#contents_tabel)","metadata":{}},{"cell_type":"code","source":"!pip install imgaug\n!pip uninstall -y scipy\n!pip install --upgrade scipy","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2023-11-02T05:58:01.570228Z","iopub.execute_input":"2023-11-02T05:58:01.570533Z","iopub.status.idle":"2023-11-02T05:58:25.948008Z","shell.execute_reply.started":"2023-11-02T05:58:01.570512Z","shell.execute_reply":"2023-11-02T05:58:25.947142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install -U kecam","metadata":{"execution":{"iopub.status.busy":"2023-11-02T06:00:44.217070Z","iopub.execute_input":"2023-11-02T06:00:44.217459Z","iopub.status.idle":"2023-11-02T06:00:53.526265Z","shell.execute_reply.started":"2023-11-02T06:00:44.217434Z","shell.execute_reply":"2023-11-02T06:00:53.525561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Main Works\nimport os\nimport shutil\nimport tensorflow as tf\nimport numpy as np\nimport pandas as pd\nimport random\nfrom pathlib import Path\nimport pickle\nfrom tensorflow.keras.models import save_model\nfrom tensorflow.keras.models import load_model\n\n# EDA\nimport cv2\nimport glob\n\n# Visualization\nimport plotly.express as px\nimport plotly.graph_objects as go\nimport matplotlib.pyplot as plt\nfrom tensorflow.keras.utils import plot_model\n\n# Preprocessing\nimport imgaug.augmenters as iaa\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import LabelEncoder\n\n# Model Structured\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.applications.inception_v3 import InceptionV3\nfrom keras_cv_attention_models import moganet\nfrom tensorflow import keras\nfrom sklearn.svm import SVC\nfrom sklearn.model_selection import GridSearchCV\n\n# Model Evaluate\nfrom sklearn.metrics import classification_report\n\n# Train\nfrom tensorflow.keras.callbacks import ModelCheckpoint","metadata":{"execution":{"iopub.status.busy":"2023-11-02T06:03:59.355211Z","iopub.execute_input":"2023-11-02T06:03:59.356383Z","iopub.status.idle":"2023-11-02T06:03:59.363344Z","shell.execute_reply.started":"2023-11-02T06:03:59.356342Z","shell.execute_reply":"2023-11-02T06:03:59.362408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"importdata\"></a>\n## <p style=\"background-color:#23ff16; padding: 15px; font-family:cursive; color:black; font-size:100%; text-align:left; border-radius:200px 200px;\">Phase 2.1 : Import Dataset</p>\n\n⬆️ [Tabel of Contents](#contents_tabel)","metadata":{}},{"cell_type":"markdown","source":"if you want remove working folder :","metadata":{}},{"cell_type":"code","source":"!rm -r /kaggle/working/train\n!rm -r /kaggle/working/test1","metadata":{"execution":{"iopub.status.busy":"2023-10-28T12:45:53.437399Z","iopub.execute_input":"2023-10-28T12:45:53.437861Z","iopub.status.idle":"2023-10-28T12:45:54.392756Z","shell.execute_reply.started":"2023-10-28T12:45:53.437825Z","shell.execute_reply":"2023-10-28T12:45:54.391824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Unzip folders :","metadata":{}},{"cell_type":"code","source":"%%capture\n!unzip /kaggle/input/dogs-vs-cats/train.zip -d /kaggle/working/","metadata":{"execution":{"iopub.status.busy":"2023-11-01T07:57:30.823100Z","iopub.execute_input":"2023-11-01T07:57:30.824175Z","iopub.status.idle":"2023-11-01T07:57:56.220762Z","shell.execute_reply.started":"2023-11-01T07:57:30.824141Z","shell.execute_reply":"2023-11-01T07:57:56.219258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%capture\n!unzip /kaggle/input/dogs-vs-cats/test1.zip -d /kaggle/working/","metadata":{"execution":{"iopub.status.busy":"2023-10-29T07:18:30.009506Z","iopub.execute_input":"2023-10-29T07:18:30.010241Z","iopub.status.idle":"2023-10-29T07:18:36.546471Z","shell.execute_reply.started":"2023-10-29T07:18:30.010204Z","shell.execute_reply":"2023-10-29T07:18:36.545095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Seperate Classes files :","metadata":{}},{"cell_type":"code","source":"for path in glob.glob('/kaggle/working/train/*.jpg'):\n    if not(os.path.exists('/kaggle/working/train/cat') or os.path.exists('/kaggle/working/train/dog')):\n        os.makedirs('/kaggle/working/train/cat')\n        os.makedirs('/kaggle/working/train/dog')\n        print('Done!')\n        \n    if 'cat' in path:\n        shutil.move(path, '/kaggle/working/train/cat/' + path.split('/')[-1])\n    else:\n        shutil.move(path, '/kaggle/working/train/dog/' + path.split('/')[-1])\n    ","metadata":{"execution":{"iopub.status.busy":"2023-11-01T07:58:12.927358Z","iopub.execute_input":"2023-11-01T07:58:12.928499Z","iopub.status.idle":"2023-11-01T07:58:13.892385Z","shell.execute_reply.started":"2023-11-01T07:58:12.928453Z","shell.execute_reply":"2023-11-01T07:58:13.891550Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Continue :","metadata":{}},{"cell_type":"code","source":"dataset = {}\nfor class_name in glob.glob('/kaggle/working/train/*'):\n        dataset[class_name.split('/')[-1]] = glob.glob(class_name + '/*')\n","metadata":{"execution":{"iopub.status.busy":"2023-11-01T07:58:21.811258Z","iopub.execute_input":"2023-11-01T07:58:21.811599Z","iopub.status.idle":"2023-11-01T07:58:21.905286Z","shell.execute_reply.started":"2023-11-01T07:58:21.811574Z","shell.execute_reply":"2023-11-01T07:58:21.904436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset.keys()","metadata":{"execution":{"iopub.status.busy":"2023-10-28T09:32:30.625434Z","iopub.execute_input":"2023-10-28T09:32:30.626143Z","iopub.status.idle":"2023-10-28T09:32:30.636216Z","shell.execute_reply.started":"2023-10-28T09:32:30.626099Z","shell.execute_reply":"2023-10-28T09:32:30.634658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"features\"></a>\n## <p style=\"background-color:#23ff16; padding: 15px; font-family:cursive; color:black; font-size:100%; text-align:left; border-radius:200px 200px;\">Phase 2.2 : About Shape and Number</p>\n\n⬆️ [Tabel of Contents](#contents_tabel)","metadata":{}},{"cell_type":"code","source":"x = dataset.keys()\ny = [len(number) for number in dataset.values()]\nprint(y)\nprint(x)\n\nplt.bar(x, y)","metadata":{"execution":{"iopub.status.busy":"2023-10-28T09:32:34.962299Z","iopub.execute_input":"2023-10-28T09:32:34.962707Z","iopub.status.idle":"2023-10-28T09:32:35.250948Z","shell.execute_reply.started":"2023-10-28T09:32:34.962677Z","shell.execute_reply":"2023-10-28T09:32:35.249591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_dims(file):\n    '''Returns dimenstions for an image'''\n    image = cv2.imread(file)    \n    h, w, d = image.shape\n    return h, w, d","metadata":{"execution":{"iopub.status.busy":"2023-10-28T09:32:43.669386Z","iopub.execute_input":"2023-10-28T09:32:43.670938Z","iopub.status.idle":"2023-10-28T09:32:43.677306Z","shell.execute_reply.started":"2023-10-28T09:32:43.670881Z","shell.execute_reply":"2023-10-28T09:32:43.675877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame(columns=['Height',\n                           'Width',\n                           'Channels',\n                           'Class'])\n\nfor class_name in dataset.keys():\n    for path in dataset[class_name]:\n        h, w, d = get_dims(path)\n        df.loc[len(df)] = [h, w, d, class_name]\n\ndf","metadata":{"execution":{"iopub.status.busy":"2023-10-28T09:32:48.165697Z","iopub.execute_input":"2023-10-28T09:32:48.166161Z","iopub.status.idle":"2023-10-28T09:34:42.789529Z","shell.execute_reply.started":"2023-10-28T09:32:48.166127Z","shell.execute_reply":"2023-10-28T09:34:42.788054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = px.scatter_3d(df, x='Height', y='Width', z='Channels', color='Class', symbol='Class',\n                    labels={'Height':'Height', 'Width':'Width', 'Channels':'Channels'},\n                    title='3D Scatter plot of Image Dimensions by Class')\n\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-28T09:34:59.684361Z","iopub.execute_input":"2023-10-28T09:34:59.684875Z","iopub.status.idle":"2023-10-28T09:35:02.385991Z","shell.execute_reply.started":"2023-10-28T09:34:59.684836Z","shell.execute_reply":"2023-10-28T09:35:02.384257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"eda\"></a>\n# <p style=\"background-color:#afeff9; padding: 15px; font-family:cursive; color:black; font-size:100%; text-align:center; border-radius:200px 200px;\">Phase 3 : EDA ( Exploratory data analysis ) On Dataset</p>\n\n⬆️ [Tabel of Contents](#contents_tabel)","metadata":{}},{"cell_type":"markdown","source":"<p style=\"background-color:#efeeee; padding: 15px; font-family:cursive; color:black; font-size:200%; text-align:center; border-radius:200px 200px;\">Show some data</p>","metadata":{}},{"cell_type":"code","source":"count = 0\nplt.figure(figsize=(15, 15))\nfor class_type in dataset.keys():    \n    for path in dataset[class_type]:                \n        sp = plt.subplot(2, 4, count + 1, )\n        sp.axis('Off')\n        sp.set_title(class_type)        \n        image = cv2.imread(path)\n        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n        plt.imshow(image)\n        count += 1\n        if count % 4 == 0:            \n            break\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-28T09:37:26.532165Z","iopub.execute_input":"2023-10-28T09:37:26.533416Z","iopub.status.idle":"2023-10-28T09:37:27.846513Z","shell.execute_reply.started":"2023-10-28T09:37:26.533372Z","shell.execute_reply":"2023-10-28T09:37:27.844998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<p style=\"background-color:#efeeee; padding: 15px; font-family:cursive; color:black; font-size:200%; text-align:center; border-radius:200px 200px;\">Check contrast</p>","metadata":{}},{"cell_type":"code","source":"def compute_mean_histogram(image_paths):\n    histograms = {'red': [], 'green': [], 'blue': []}\n    for image_path in image_paths:\n        image = cv2.imread(image_path)  # Load image in color mode\n        for i, color in enumerate(('blue', 'green', 'red')):\n            hist = cv2.calcHist([image], [i], None, [256], [0, 256])\n            histograms[color].append(hist)\n    mean_histograms = {color: np.mean(hists, axis=0) for color, hists in histograms.items()}\n    return mean_histograms\n\nmean_histograms_per_class = {}\nfor class_label, image_paths in dataset.items():\n    mean_histograms_per_class[class_label] = compute_mean_histogram(image_paths)","metadata":{"execution":{"iopub.status.busy":"2023-10-28T16:30:45.702737Z","iopub.execute_input":"2023-10-28T16:30:45.703098Z","iopub.status.idle":"2023-10-28T16:30:45.710045Z","shell.execute_reply.started":"2023-10-28T16:30:45.703072Z","shell.execute_reply":"2023-10-28T16:30:45.708993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plotting\nfig = go.Figure()\nfor class_label, mean_histograms in mean_histograms_per_class.items():\n    for color, mean_histogram in mean_histograms.items():\n        fig.add_trace(go.Scatter(x=np.arange(256), y=mean_histogram.flatten(), mode='lines', name=f'{class_label} - {color}'))\n\nfig.update_layout(\n    title='Mean Histograms per Class per Channel',\n    xaxis_title='Pixel Value',\n    yaxis_title='Frequency',\n    legend_title='Class - Channel'\n)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-28T09:38:58.251361Z","iopub.execute_input":"2023-10-28T09:38:58.251984Z","iopub.status.idle":"2023-10-28T09:38:58.293555Z","shell.execute_reply.started":"2023-10-28T09:38:58.251892Z","shell.execute_reply":"2023-10-28T09:38:58.292279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<p style=\"background-color:#efeeee; padding: 15px; font-family:cursive; color:black; font-size:200%; text-align:center; border-radius:200px 200px;\">Check Standard Deviation</p>","metadata":{}},{"cell_type":"code","source":"def compute_avg_std(image_paths):\n    stds = {'red': [], 'green': [], 'blue': []}\n    for image_path in image_paths:\n        image = cv2.imread(image_path)  # Load image in color mode\n        for i, color in enumerate(('blue', 'green', 'red')):\n            std = np.std(image[:,:,i])\n            stds[color].append(std)\n    avg_stds = {color: np.mean(values) for color, values in stds.items()}\n    return avg_stds\n\navg_stds_per_class = {}\nfor class_label, image_paths in dataset.items():\n    avg_stds_per_class[class_label] = compute_avg_std(image_paths)","metadata":{"execution":{"iopub.status.busy":"2023-10-28T09:40:48.276453Z","iopub.execute_input":"2023-10-28T09:40:48.277504Z","iopub.status.idle":"2023-10-28T09:42:34.662315Z","shell.execute_reply.started":"2023-10-28T09:40:48.277461Z","shell.execute_reply":"2023-10-28T09:42:34.660753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plotting\ncategories = list(avg_stds_per_class.keys())\nfig = go.Figure()\nfor color in ['red', 'green', 'blue']:\n    values = [avg_stds_per_class[class_label][color] for class_label in categories]\n    fig.add_trace(go.Bar(x=categories, y=values, name=f'{color} channel'))\n\nfig.update_layout(\n    title='Average Standard Deviation of Pixel Values per Class per Channel',\n    xaxis_title='Class',\n    yaxis_title='Average Standard Deviation',\n    legend_title='Channel'\n)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-28T09:42:57.987261Z","iopub.execute_input":"2023-10-28T09:42:57.987722Z","iopub.status.idle":"2023-10-28T09:42:58.018345Z","shell.execute_reply.started":"2023-10-28T09:42:57.987687Z","shell.execute_reply":"2023-10-28T09:42:58.016949Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<p style=\"background-color:#efeeee; padding: 15px; font-family:cursive; color:black; font-size:200%; text-align:center; border-radius:200px 200px;\">Check Noise Level</p>","metadata":{}},{"cell_type":"code","source":"def estimate_noise(image_paths):\n    noise_levels = []\n    for image_path in image_paths:\n        image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)  # Load image in grayscale mode\n        laplacian = cv2.Laplacian(image, cv2.CV_64F)\n        std_dev = np.std(laplacian)\n        noise_levels.append(std_dev)\n    mean_noise_level = np.mean(noise_levels)\n    return mean_noise_level\n\nmean_noise_levels = {}\nfor class_label, image_paths in dataset.items():\n    mean_noise_levels[class_label] = estimate_noise(image_paths)","metadata":{"execution":{"iopub.status.busy":"2023-10-28T09:43:23.498009Z","iopub.execute_input":"2023-10-28T09:43:23.498961Z","iopub.status.idle":"2023-10-28T09:44:18.543188Z","shell.execute_reply.started":"2023-10-28T09:43:23.498914Z","shell.execute_reply":"2023-10-28T09:44:18.541753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plotting\nfig = go.Figure()\nclass_labels = list(mean_noise_levels.keys())\nnoise_levels = list(mean_noise_levels.values())\nfig.add_trace(go.Bar(x=class_labels, y=noise_levels))\nfig.update_layout(\n    title='Estimated Noise Levels per Class',\n    xaxis_title='Class',\n    yaxis_title='Estimated Noise Level',\n    legend_title='Class'\n)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-28T09:45:25.288264Z","iopub.execute_input":"2023-10-28T09:45:25.288835Z","iopub.status.idle":"2023-10-28T09:45:25.312995Z","shell.execute_reply.started":"2023-10-28T09:45:25.288777Z","shell.execute_reply":"2023-10-28T09:45:25.311232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<p style=\"background-color:#efeeee; padding: 15px; font-family:cursive; color:black; font-size:200%; text-align:center; border-radius:200px 200px;\">Spectral Analysis</p>","metadata":{}},{"cell_type":"code","source":"def compute_mean_spectrum(image_paths):\n    spectrum_sum = np.zeros((224, 224))  # Assumes images are 256x256 pixels\n    num_images = len(image_paths)\n    for image_path in image_paths:\n        image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)  # Load image in grayscale mode\n        image = cv2.resize(image, (224, 224))\n        f = np.fft.fft2(image)\n        fshift = np.fft.fftshift(f)\n        magnitude_spectrum = np.log(np.abs(fshift) + 1)\n        spectrum_sum += magnitude_spectrum\n    mean_spectrum = spectrum_sum / num_images\n    return mean_spectrum\n\nmean_spectrums = {}\nfor class_label, image_paths in dataset.items():\n    mean_spectrums[class_label] = compute_mean_spectrum(image_paths)","metadata":{"execution":{"iopub.status.busy":"2023-10-28T09:48:09.462029Z","iopub.execute_input":"2023-10-28T09:48:09.462903Z","iopub.status.idle":"2023-10-28T09:50:03.127406Z","shell.execute_reply.started":"2023-10-28T09:48:09.462858Z","shell.execute_reply":"2023-10-28T09:50:03.125763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plotting\nfig = go.Figure()\n\n# Add one surface trace for each class\nfor class_label, mean_spectrum in mean_spectrums.items():\n    fig.add_trace(go.Surface(z=mean_spectrum, name=class_label, visible=False))\n\n# Make the first class visible\nfig.data[0].visible = True\n\n# Create buttons for dropdown menu\nbuttons = [dict(label=class_label,\n                method=\"update\",\n                args=[{\"visible\": [class_label==label for label in mean_spectrums.keys()]}])\n           for class_label in mean_spectrums.keys()]\n\n# Add dropdown menu\nfig.update_layout(\n    updatemenus=[dict(buttons=buttons, direction=\"down\", showactive=True, x=1.05, xanchor=\"left\", y=1.15, yanchor=\"top\")]\n)\n\nfig.update_layout(\n    title='Mean Spectrum per Class',\n    scene=dict(\n        xaxis_title='Frequency X',\n        yaxis_title='Frequency Y',\n        zaxis_title='Magnitude'\n    )\n)\n\nfig.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-10-28T09:50:56.432445Z","iopub.execute_input":"2023-10-28T09:50:56.434175Z","iopub.status.idle":"2023-10-28T09:50:56.514329Z","shell.execute_reply.started":"2023-10-28T09:50:56.434092Z","shell.execute_reply":"2023-10-28T09:50:56.513153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"prep\"></a>\n# <p style=\"background-color:#afeff9; padding: 15px; font-family:cursive; color:black; font-size:100%; text-align:center; border-radius:200px 200px;\">Phase 4 : Data Preprocessing</p>\n\n⬆️ [Tabel of Contents](#contents_tabel)","metadata":{}},{"cell_type":"markdown","source":"<a id=\"lowsize\"></a>\n## <p style=\"background-color:#23ff16; padding: 15px; font-family:cursive; color:black; font-size:100%; text-align:left; border-radius:200px 200px;\">Phase 4.1 : Delete Very Low Size Images</p>\n\n⬆️ [Tabel of Contents](#contents_tabel)","metadata":{}},{"cell_type":"code","source":"def delete_low_size(image_paths):\n    for image_path in image_paths:\n        image = cv2.imread(image_path)\n        h, w, d = image.shape\n        if h < 224 and w < 224:\n            os.remove(image_path)\n            \nfor class_label, image_paths in dataset.items():\n    delete_low_size(image_paths)","metadata":{"execution":{"iopub.status.busy":"2023-11-01T07:58:33.906585Z","iopub.execute_input":"2023-11-01T07:58:33.907440Z","iopub.status.idle":"2023-11-01T07:59:17.399422Z","shell.execute_reply.started":"2023-11-01T07:58:33.907411Z","shell.execute_reply":"2023-11-01T07:59:17.398303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = {}\nfor class_name in glob.glob('/kaggle/working/train/*'):\n        dataset[class_name.split('/')[-1]] = glob.glob(class_name + '/*')","metadata":{"execution":{"iopub.status.busy":"2023-10-29T07:22:34.170733Z","iopub.execute_input":"2023-10-29T07:22:34.171480Z","iopub.status.idle":"2023-10-29T07:22:34.262020Z","shell.execute_reply.started":"2023-10-29T07:22:34.171441Z","shell.execute_reply":"2023-10-29T07:22:34.261235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = dataset.keys()\ny = [len(number) for number in dataset.values()]\nprint(y)\nprint(x)\n\nplt.bar(x, y)","metadata":{"execution":{"iopub.status.busy":"2023-10-28T10:03:21.357132Z","iopub.execute_input":"2023-10-28T10:03:21.357547Z","iopub.status.idle":"2023-10-28T10:03:21.589114Z","shell.execute_reply.started":"2023-10-28T10:03:21.357516Z","shell.execute_reply":"2023-10-28T10:03:21.587650Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"inc\"></a>\n## <p style=\"background-color:#23ff16; padding: 15px; font-family:cursive; color:black; font-size:100%; text-align:left; border-radius:200px 200px;\">Phase 4.2 : Increase Data Quality</p>\n\n⬆️ [Tabel of Contents](#contents_tabel)","metadata":{}},{"cell_type":"code","source":"def save_clahe_img(image_paths):\n    for image_path in image_paths:        \n            # Load image\n            image = cv2.imread(image_path)\n            \n            # Split the image into its color channels\n            b, g, r = cv2.split(image)\n            \n            # CLAHE\n            clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))\n            \n            # Apply CLAHE to each channel separately\n            clahe_b = clahe.apply(b)\n            clahe_g = clahe.apply(g)\n            clahe_r = clahe.apply(r)\n            clahe_image = cv2.merge((clahe_b, clahe_g, clahe_r))\n            \n            # Save image\n            cv2.imwrite(image_path.rsplit('/', 1)[0] + '/' + image_path.split('/')[-1][:-4] + '_clahe.jpg', clahe_image)\n            os.remove(image_path)\n\nfor class_label, image_paths in dataset.items():\n    save_clahe_img(image_paths)","metadata":{"execution":{"iopub.status.busy":"2023-10-28T16:26:25.533917Z","iopub.execute_input":"2023-10-28T16:26:25.534590Z","iopub.status.idle":"2023-10-28T16:30:17.138689Z","shell.execute_reply.started":"2023-10-28T16:26:25.534556Z","shell.execute_reply":"2023-10-28T16:30:17.137607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = {}\nfor class_name in glob.glob('/kaggle/working/train/*'):\n        dataset[class_name.split('/')[-1]] = glob.glob(class_name + '/*')","metadata":{"execution":{"iopub.status.busy":"2023-11-01T08:03:44.282352Z","iopub.execute_input":"2023-11-01T08:03:44.283140Z","iopub.status.idle":"2023-11-01T08:03:44.374795Z","shell.execute_reply.started":"2023-11-01T08:03:44.283085Z","shell.execute_reply":"2023-11-01T08:03:44.373989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mean_histograms_per_class = {}\nfor class_label, image_paths in dataset.items():\n    mean_histograms_per_class[class_label] = compute_mean_histogram(image_paths)","metadata":{"execution":{"iopub.status.busy":"2023-10-28T16:31:05.205111Z","iopub.execute_input":"2023-10-28T16:31:05.205468Z","iopub.status.idle":"2023-10-28T16:32:17.418706Z","shell.execute_reply.started":"2023-10-28T16:31:05.205439Z","shell.execute_reply":"2023-10-28T16:32:17.417555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plotting\nfig = go.Figure()\nfor class_label, mean_histograms in mean_histograms_per_class.items():\n    for color, mean_histogram in mean_histograms.items():\n        fig.add_trace(go.Scatter(x=np.arange(256), y=mean_histogram.flatten(), mode='lines', name=f'{class_label} - {color}'))\n\nfig.update_layout(\n    title='Mean Histograms per Class per Channel',\n    xaxis_title='Pixel Value',\n    yaxis_title='Frequency',\n    legend_title='Class - Channel'\n)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-28T16:32:22.360532Z","iopub.execute_input":"2023-10-28T16:32:22.361360Z","iopub.status.idle":"2023-10-28T16:32:22.857437Z","shell.execute_reply.started":"2023-10-28T16:32:22.361324Z","shell.execute_reply":"2023-10-28T16:32:22.856582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"increaseimages\"></a>\n## <p style=\"background-color:#23ff16; padding: 15px; font-family:cursive; color:black; font-size:100%; text-align:left; border-radius:200px 200px;\">Phase 4.3 : Increase Images</p>\n\n⬆️ [Tabel of Contents](#contents_tabel)","metadata":{}},{"cell_type":"code","source":"seq = iaa.Sequential([\n    iaa.SomeOf((1, 3), [  # apply one to three of the augmentations below\n        iaa.blur.MotionBlur(k=15),\n        iaa.weather.Rain(speed=(0.1, 0.3)),\n        iaa.weather.Fog()\n    ])\n])","metadata":{"execution":{"iopub.status.busy":"2023-11-01T08:00:24.661169Z","iopub.execute_input":"2023-11-01T08:00:24.661534Z","iopub.status.idle":"2023-11-01T08:00:24.672993Z","shell.execute_reply.started":"2023-11-01T08:00:24.661506Z","shell.execute_reply":"2023-11-01T08:00:24.672021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for animal_class, image_paths in dataset.items():\n    # Shuffle the list of image paths and select 50% of them for augmentation\n    random.shuffle(image_paths)\n    augment_paths = image_paths[:len(image_paths)//2]\n\n    for img_path in augment_paths:\n        # Load the image\n        img = cv2.imread(img_path)\n\n        # Apply the augmentations\n        augmented_imgs = seq.augment_images([img])\n\n        # Get the original file path, name and extension\n        original_path = Path(img_path)\n        file_stem = original_path.stem\n        file_ext = original_path.suffix\n\n        # Define new file name for augmented image\n        new_file_name = f\"{file_stem}_aug{file_ext}\"\n        new_file_path = original_path.parent / new_file_name\n\n        # Save the augmented image\n        cv2.imwrite(str(new_file_path), augmented_imgs[0])\n\n        # Delete the original image\n        os.remove(img_path)","metadata":{"execution":{"iopub.status.busy":"2023-11-01T08:03:50.650201Z","iopub.execute_input":"2023-11-01T08:03:50.650522Z","iopub.status.idle":"2023-11-01T08:16:34.237493Z","shell.execute_reply.started":"2023-11-01T08:03:50.650499Z","shell.execute_reply":"2023-11-01T08:16:34.236469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"ttsplit\"></a>\n## <p style=\"background-color:#23ff16; padding: 15px; font-family:cursive; color:black; font-size:100%; text-align:left; border-radius:200px 200px;\">Phase 4.3 : Train, Test, Validation Split</p>\n\n⬆️ [Tabel of Contents](#contents_tabel)","metadata":{}},{"cell_type":"code","source":"directory_path = '/kaggle/working/train'\nimage_size = (224, 224)\nbatch_size = 256  \n\n# Create the training and validation datasets\ntrain_ds = tf.keras.utils.image_dataset_from_directory(\n    directory=directory_path,\n    validation_split=0.3,\n    subset=\"training\",\n    seed=1337,\n    image_size=image_size,\n    batch_size=batch_size\n)\n\nval_ds = tf.keras.utils.image_dataset_from_directory(\n    directory=directory_path,\n    validation_split=0.2,\n    subset=\"validation\",\n    seed=1337,\n    image_size=image_size,\n    batch_size=batch_size\n)","metadata":{"execution":{"iopub.status.busy":"2023-10-29T07:37:11.403157Z","iopub.execute_input":"2023-10-29T07:37:11.403726Z","iopub.status.idle":"2023-10-29T07:37:18.962316Z","shell.execute_reply.started":"2023-10-29T07:37:11.403678Z","shell.execute_reply":"2023-10-29T07:37:18.961254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Get the number of batches in the validation dataset\nval_batches = tf.data.experimental.cardinality(val_ds)\n\n# Split the validation dataset into validation and test datasets\ntest_ds = val_ds.take(val_batches // 2)\nval_ds = val_ds.skip(val_batches // 2)\n","metadata":{"execution":{"iopub.status.busy":"2023-10-29T07:37:22.660275Z","iopub.execute_input":"2023-10-29T07:37:22.661009Z","iopub.status.idle":"2023-10-29T07:37:22.680738Z","shell.execute_reply.started":"2023-10-29T07:37:22.660974Z","shell.execute_reply":"2023-10-29T07:37:22.679850Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"aug\"></a>\n## <p style=\"background-color:#23ff16; padding: 15px; font-family:cursive; color:black; font-size:100%; text-align:left; border-radius:200px 200px;\">Phase 4.4 : Data Augmentation</p>\n\n⬆️ [Tabel of Contents](#contents_tabel)","metadata":{}},{"cell_type":"code","source":"data_augmentation = tf.keras.Sequential([    \n    tf.keras.layers.experimental.preprocessing.RandomFlip('horizontal'),\n    tf.keras.layers.experimental.preprocessing.RandomFlip('vertical'),\n    tf.keras.layers.experimental.preprocessing.RandomRotation(0.1),\n    tf.keras.layers.experimental.preprocessing.RandomRotation(0.2)\n])","metadata":{"execution":{"iopub.status.busy":"2023-10-29T07:37:30.791522Z","iopub.execute_input":"2023-10-29T07:37:30.792367Z","iopub.status.idle":"2023-10-29T07:37:30.815326Z","shell.execute_reply.started":"2023-10-29T07:37:30.792331Z","shell.execute_reply":"2023-10-29T07:37:30.814415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Apply other augmentations only to the training dataset\naugmented_train_ds = train_ds.map(\n    lambda x, y: (data_augmentation(x, training=True), y)\n)","metadata":{"execution":{"iopub.status.busy":"2023-10-29T07:37:34.174571Z","iopub.execute_input":"2023-10-29T07:37:34.174976Z","iopub.status.idle":"2023-10-29T07:37:34.633434Z","shell.execute_reply.started":"2023-10-29T07:37:34.174942Z","shell.execute_reply":"2023-10-29T07:37:34.632200Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def one_hot_encode(image, label):\n    return image, tf.one_hot(label, depth=2)  # Assuming 6 classes\n\ntrain_ds_one_hot = augmented_train_ds.map(one_hot_encode)\nval_ds_one_hot   = val_ds.map(one_hot_encode)\ntest_ds_one_hot  = test_ds.map(one_hot_encode)","metadata":{"execution":{"iopub.status.busy":"2023-10-29T07:37:40.189972Z","iopub.execute_input":"2023-10-29T07:37:40.190845Z","iopub.status.idle":"2023-10-29T07:37:40.258093Z","shell.execute_reply.started":"2023-10-29T07:37:40.190804Z","shell.execute_reply":"2023-10-29T07:37:40.257324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"m1\"></a>\n# <p style=\"background-color:#affff9; padding: 15px; font-family:cursive; color:black; font-size:100%; text-align:center; border-radius:200px 200px;\">Method 1</p>\n\n⬆️ [Tabel of Contents](#contents_tabel)","metadata":{}},{"cell_type":"markdown","source":"<a id=\"net\"></a>\n# <p style=\"background-color:#afeff9; padding: 15px; font-family:cursive; color:black; font-size:100%; text-align:center; border-radius:200px 200px;\">Phase 5 : Prepare Network</p>\n\n⬆️ [Tabel of Contents](#contents_tabel)","metadata":{}},{"cell_type":"code","source":"base_model = InceptionV3(weights='imagenet',\n                         include_top=False,\n                         input_shape=(224, 224, 3))","metadata":{"execution":{"iopub.status.busy":"2023-10-29T08:30:26.829660Z","iopub.execute_input":"2023-10-29T08:30:26.830506Z","iopub.status.idle":"2023-10-29T08:30:29.199117Z","shell.execute_reply.started":"2023-10-29T08:30:26.830471Z","shell.execute_reply":"2023-10-29T08:30:29.198196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_model.summary()","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2023-10-29T08:28:09.025464Z","iopub.execute_input":"2023-10-29T08:28:09.026401Z","iopub.status.idle":"2023-10-29T08:28:09.834530Z","shell.execute_reply.started":"2023-10-29T08:28:09.026354Z","shell.execute_reply":"2023-10-29T08:28:09.826967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_model.trainable = False\n\nmodel = Sequential([\n    tf.keras.layers.Resizing(224, 224),\n    tf.keras.layers.Rescaling(1./255),    \n    base_model,\n    tf.keras.layers.GlobalAveragePooling2D(),\n    tf.keras.layers.Dense(1024, activation='relu'),\n    tf.keras.layers.Dropout(0.2),\n    tf.keras.layers.Dense(128, activation='relu'),\n    tf.keras.layers.Dropout(0.2),\n    tf.keras.layers.Dense(64, activation='relu'),\n    tf.keras.layers.Dropout(0.2),\n    tf.keras.layers.Dense(32, activation='relu'),\n    tf.keras.layers.Dropout(0.2),\n    tf.keras.layers.Dense(16, activation='relu'),\n    tf.keras.layers.Dropout(0.2),    \n    tf.keras.layers.Dense(2, activation='softmax'),\n])","metadata":{"execution":{"iopub.status.busy":"2023-10-29T08:30:33.557888Z","iopub.execute_input":"2023-10-29T08:30:33.558743Z","iopub.status.idle":"2023-10-29T08:30:33.638743Z","shell.execute_reply.started":"2023-10-29T08:30:33.558710Z","shell.execute_reply":"2023-10-29T08:30:33.637835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.build((None, 224, 224, 3))","metadata":{"execution":{"iopub.status.busy":"2023-10-29T08:30:41.984397Z","iopub.execute_input":"2023-10-29T08:30:41.985232Z","iopub.status.idle":"2023-10-29T08:30:42.723077Z","shell.execute_reply.started":"2023-10-29T08:30:41.985198Z","shell.execute_reply":"2023-10-29T08:30:42.722242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-10-29T08:29:38.643864Z","iopub.execute_input":"2023-10-29T08:29:38.644248Z","iopub.status.idle":"2023-10-29T08:29:38.702474Z","shell.execute_reply.started":"2023-10-29T08:29:38.644202Z","shell.execute_reply":"2023-10-29T08:29:38.701565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"trainobj\"></a>\n# <p style=\"background-color:#afeff9; padding: 15px; font-family:cursive; color:black; font-size:100%; text-align:center; border-radius:200px 200px;\">Phase 6 : Prepare Train Objects</p>\n\n⬆️ [Tabel of Contents](#contents_tabel)","metadata":{}},{"cell_type":"code","source":"OPTIMIZER  = tf.keras.optimizers.Adam(learning_rate=0.015,\n                                      amsgrad=True,\n                                      weight_decay=0.05)\nLOSS       = tf.keras.losses.CategoricalCrossentropy()\nMETRICS    = [tf.keras.metrics.CategoricalAccuracy(),\n              tf.keras.metrics.Recall(name='recall'),\n              tf.keras.metrics.Precision(name='precision')]\n\nCHECKPOINT = ModelCheckpoint('cat&dog_classification.h5',\n                             monitor='val_categorical_accuracy',\n                             mode='max',\n                             save_best_only=True)\n\nmodel.compile(optimizer=OPTIMIZER,\n              loss=LOSS,\n              metrics=METRICS)","metadata":{"execution":{"iopub.status.busy":"2023-10-29T08:30:48.916717Z","iopub.execute_input":"2023-10-29T08:30:48.917657Z","iopub.status.idle":"2023-10-29T08:30:48.946441Z","shell.execute_reply.started":"2023-10-29T08:30:48.917622Z","shell.execute_reply":"2023-10-29T08:30:48.945520Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"train\"></a>\n# <p style=\"background-color:#afeff9; padding: 15px; font-family:cursive; color:black; font-size:100%; text-align:center; border-radius:200px 200px;\">Phase 7 : Train</p>\n\n⬆️ [Tabel of Contents](#contents_tabel)","metadata":{}},{"cell_type":"code","source":"history = model.fit(train_ds_one_hot,\n                    callbacks=[CHECKPOINT],\n                    epochs=100,\n                    validation_data=val_ds_one_hot,\n                    workers=2)","metadata":{"execution":{"iopub.status.busy":"2023-10-29T08:35:10.559560Z","iopub.execute_input":"2023-10-29T08:35:10.560420Z","iopub.status.idle":"2023-10-29T14:47:29.678397Z","shell.execute_reply.started":"2023-10-29T08:35:10.560385Z","shell.execute_reply":"2023-10-29T14:47:29.677448Z"},"scrolled":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"eval\"></a>\n# <p style=\"background-color:#afeff9; padding: 15px; font-family:cursive; color:black; font-size:100%; text-align:center; border-radius:200px 200px;\">Phase 8 : Evaluate</p>\n\n⬆️ [Tabel of Contents](#contents_tabel)","metadata":{}},{"cell_type":"code","source":"# Access the accuracy and loss values over epochs\naccuracy = model.history.history['categorical_accuracy']  # Replace 'accuracy' with 'acc' for older versions of Keras\nval_accuracy = model.history.history['val_categorical_accuracy']  # Replace 'val_accuracy' with 'val_acc' for older versions of Keras\nloss = model.history.history['loss']\nval_loss = model.history.history['val_loss']\n\n# If you have precision and recall metrics, you can access them as well\nprecision = model.history.history['precision']\nval_precision = model.history.history['val_precision']\nrecall = model.history.history['recall']\nval_recall = model.history.history['val_recall']\n\n# Create subplots for accuracy and loss\nplt.figure(figsize=(12, 4))\nplt.subplot(1, 2, 1)\nplt.plot(accuracy, label='Training Accuracy')\nplt.plot(val_accuracy, label='Validation Accuracy')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.title('Training and Validation Accuracy')\nplt.legend()\n\nplt.subplot(1, 2, 2)\nplt.plot(loss, label='Training Loss')\nplt.plot(val_loss, label='Validation Loss')\nplt.xlabel('Epoch')\nplt.ylabel('Loss')\nplt.title('Training and Validation Loss')\nplt.legend()\n\nplt.tight_layout()\n\n# If precision and recall metrics are available, create additional plots\nif 'precision' in model.history.history:\n    plt.figure(figsize=(12, 4))\n    plt.subplot(1, 2, 1)\n    plt.plot(precision, label='Training Precision')\n    plt.plot(val_precision, label='Validation Precision')\n    plt.xlabel('Epoch')\n    plt.ylabel('Precision')\n    plt.title('Training and Validation Precision')\n    plt.legend()\n\nif 'recall' in model.history.history:\n    plt.subplot(1, 2, 2)\n    plt.plot(recall, label='Training Recall')\n    plt.plot(val_recall, label='Validation Recall')\n    plt.xlabel('Epoch')\n    plt.ylabel('Recall')\n    plt.title('Training and Validation Recall')\n    plt.legend()\n\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-29T14:47:38.737392Z","iopub.execute_input":"2023-10-29T14:47:38.738112Z","iopub.status.idle":"2023-10-29T14:47:40.096042Z","shell.execute_reply.started":"2023-10-29T14:47:38.738077Z","shell.execute_reply":"2023-10-29T14:47:40.095038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"test\"></a>\n# <p style=\"background-color:#afeff9; padding: 15px; font-family:cursive; color:black; font-size:100%; text-align:center; border-radius:200px 200px;\">Phase 9 : Test</p>\n\n⬆️ [Tabel of Contents](#contents_tabel)","metadata":{}},{"cell_type":"code","source":"model_new = tf.keras.models.load_model('/kaggle/working/cat&dog_classification.h5')","metadata":{"execution":{"iopub.status.busy":"2023-10-29T14:47:50.375290Z","iopub.execute_input":"2023-10-29T14:47:50.375630Z","iopub.status.idle":"2023-10-29T14:47:53.807991Z","shell.execute_reply.started":"2023-10-29T14:47:50.375604Z","shell.execute_reply":"2023-10-29T14:47:53.806993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_new.evaluate(test_ds_one_hot)","metadata":{"execution":{"iopub.status.busy":"2023-10-29T14:47:57.672241Z","iopub.execute_input":"2023-10-29T14:47:57.673319Z","iopub.status.idle":"2023-10-29T14:48:05.452845Z","shell.execute_reply.started":"2023-10-29T14:47:57.673271Z","shell.execute_reply":"2023-10-29T14:48:05.451836Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result = pd.read_csv('/kaggle/input/dogs-vs-cats/sampleSubmission.csv')","metadata":{"execution":{"iopub.status.busy":"2023-10-29T14:49:34.194953Z","iopub.execute_input":"2023-10-29T14:49:34.195306Z","iopub.status.idle":"2023-10-29T14:49:34.242835Z","shell.execute_reply.started":"2023-10-29T14:49:34.195281Z","shell.execute_reply":"2023-10-29T14:49:34.241869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result","metadata":{"execution":{"iopub.status.busy":"2023-10-29T14:49:47.774956Z","iopub.execute_input":"2023-10-29T14:49:47.775686Z","iopub.status.idle":"2023-10-29T14:49:47.806464Z","shell.execute_reply.started":"2023-10-29T14:49:47.775654Z","shell.execute_reply":"2023-10-29T14:49:47.805669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in glob.glob('/kaggle/working/test1/*'):\n    image = cv2.imread(i)\n    if image is None:\n        print(f\"Failed to load {i}\")\n        continue\n    \n    # Convert the image from BGR to RGB as cv2 loads images in BGR format\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n    \n    # Resize the image to the desired dimensions\n    image = cv2.resize(image, (224, 224))\n    \n    # Expand dimensions to match the model's input shape\n    image = np.expand_dims(image, axis=0)\n    \n    # Normalize the image if your model expects normalized inputs\n    # image = image / 255.0  # Uncomment this line if necessary\n    \n    out = model_new.predict(image)\n    out = np.argmax(out, axis=1)[0]\n    result[int(i.split('/')[-1].split('.')[0])] = out","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2023-10-29T15:18:33.502471Z","iopub.execute_input":"2023-10-29T15:18:33.503376Z","iopub.status.idle":"2023-10-29T15:34:36.471911Z","shell.execute_reply.started":"2023-10-29T15:18:33.503340Z","shell.execute_reply":"2023-10-29T15:34:36.471042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ds.class_names","metadata":{"execution":{"iopub.status.busy":"2023-10-29T15:03:00.346278Z","iopub.execute_input":"2023-10-29T15:03:00.346673Z","iopub.status.idle":"2023-10-29T15:03:00.353362Z","shell.execute_reply.started":"2023-10-29T15:03:00.346642Z","shell.execute_reply":"2023-10-29T15:03:00.352323Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result.to_csv('/kaggle/working/result.csv')","metadata":{"execution":{"iopub.status.busy":"2023-10-29T16:07:05.270438Z","iopub.execute_input":"2023-10-29T16:07:05.270871Z","iopub.status.idle":"2023-10-29T16:20:24.586086Z","shell.execute_reply.started":"2023-10-29T16:07:05.270833Z","shell.execute_reply":"2023-10-29T16:20:24.585174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd /kaggle/working/","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from IPython.display import FileLink\na = r'result.csv'\nFileLink(a)","metadata":{"execution":{"iopub.status.busy":"2023-10-29T16:54:35.515349Z","iopub.execute_input":"2023-10-29T16:54:35.515729Z","iopub.status.idle":"2023-10-29T16:54:35.522058Z","shell.execute_reply.started":"2023-10-29T16:54:35.515698Z","shell.execute_reply":"2023-10-29T16:54:35.521074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"m2\"></a>\n# <p style=\"background-color:#affff9; padding: 15px; font-family:cursive; color:black; font-size:100%; text-align:center; border-radius:200px 200px;\">Method 2</p>\n\n⬆️ [Tabel of Contents](#contents_tabel)","metadata":{}},{"cell_type":"markdown","source":"<a id=\"fe\"></a>\n# <p style=\"background-color:#afeff9; padding: 15px; font-family:cursive; color:black; font-size:100%; text-align:center; border-radius:200px 200px;\">Phase 5 : Feature Extraction</p>\n\n⬆️ [Tabel of Contents](#contents_tabel)","metadata":{}},{"cell_type":"code","source":"dataset = {}\nfor class_name in glob.glob('/kaggle/working/train/*'):\n        dataset[class_name.split('/')[-1]] = glob.glob(class_name + '/*')","metadata":{"execution":{"iopub.status.busy":"2023-11-01T11:16:34.619689Z","iopub.execute_input":"2023-11-01T11:16:34.620355Z","iopub.status.idle":"2023-11-01T11:16:34.713044Z","shell.execute_reply.started":"2023-11-01T11:16:34.620316Z","shell.execute_reply":"2023-11-01T11:16:34.712017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"moganet_model = moganet.MogaNetSmall(input_shape=(224, 224, 3), pretrained=\"imagenet\")","metadata":{"execution":{"iopub.status.busy":"2023-11-02T06:01:49.229683Z","iopub.execute_input":"2023-11-02T06:01:49.230341Z","iopub.status.idle":"2023-11-02T06:01:56.143312Z","shell.execute_reply.started":"2023-11-02T06:01:49.230309Z","shell.execute_reply":"2023-11-02T06:01:56.142563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"moganet_model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-11-01T08:22:59.588826Z","iopub.execute_input":"2023-11-01T08:22:59.589176Z","iopub.status.idle":"2023-11-01T08:23:01.968580Z","shell.execute_reply.started":"2023-11-01T08:22:59.589148Z","shell.execute_reply":"2023-11-01T08:23:01.967654Z"},"scrolled":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_model(moganet_model)","metadata":{"execution":{"iopub.status.busy":"2023-11-01T08:41:10.430564Z","iopub.execute_input":"2023-11-01T08:41:10.431485Z","iopub.status.idle":"2023-11-01T08:41:14.101177Z","shell.execute_reply.started":"2023-11-01T08:41:10.431444Z","shell.execute_reply":"2023-11-01T08:41:14.100035Z"},"scrolled":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"feature_extractor = keras.Model(\n   inputs=moganet_model.inputs,\n   outputs=moganet_model.get_layer(name=\"avg_pool\").output,\n)","metadata":{"execution":{"iopub.status.busy":"2023-11-02T06:02:06.694852Z","iopub.execute_input":"2023-11-02T06:02:06.695175Z","iopub.status.idle":"2023-11-02T06:02:06.739557Z","shell.execute_reply.started":"2023-11-02T06:02:06.695152Z","shell.execute_reply":"2023-11-02T06:02:06.738792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def Feature_Extractor(image_path, feature_extractor):    \n\n    # read Image\n    image = cv2.imread(image_path)\n            \n    # Preprocess Image\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n    image = image/255\n    image = cv2.resize(image, (224, 224))\n    image = np.expand_dims(image, axis=0)    \n    \n    # Extract Features\n    return feature_extractor.predict(image)\n\n\ndata_list = []\n\nfor class_label, image_paths in dataset.items():\n    for image_path in image_paths:\n        # Extract features\n        features = Feature_Extractor(image_path, feature_extractor)\n        # Ensure features is a flat array of 512 elements        \n        features = features.flatten()[:512]                \n        # Create a dictionary with class name and features\n        data_dict = {\"class_name\": class_label}\n        for i, feature in enumerate(features):\n            data_dict[f'feature_{i}'] = feature\n        # Append the data_dict to data_list\n        data_list.append(data_dict)\n\n# Create the DataFrame from the list of dictionaries\ndf = pd.DataFrame(data_list)\n\n# Optionally reorder the columns if needed\ndf = df[['class_name'] + [f'feature_{i}' for i in range(512)]]","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2023-11-01T11:16:53.209258Z","iopub.execute_input":"2023-11-01T11:16:53.209659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2023-11-01T12:06:50.812366Z","iopub.execute_input":"2023-11-01T12:06:50.813213Z","iopub.status.idle":"2023-11-01T12:06:50.820265Z","shell.execute_reply.started":"2023-11-01T12:06:50.813175Z","shell.execute_reply":"2023-11-01T12:06:50.819373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df","metadata":{"execution":{"iopub.status.busy":"2023-11-01T12:06:59.774931Z","iopub.execute_input":"2023-11-01T12:06:59.775305Z","iopub.status.idle":"2023-11-01T12:06:59.815530Z","shell.execute_reply.started":"2023-11-01T12:06:59.775276Z","shell.execute_reply":"2023-11-01T12:06:59.814629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.to_csv('/kaggle/working/dataset_cat&dog.csv')","metadata":{"execution":{"iopub.status.busy":"2023-11-01T12:07:45.457570Z","iopub.execute_input":"2023-11-01T12:07:45.458398Z","iopub.status.idle":"2023-11-01T12:08:00.917277Z","shell.execute_reply.started":"2023-11-01T12:07:45.458365Z","shell.execute_reply":"2023-11-01T12:08:00.916148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd /kaggle/working/","metadata":{"execution":{"iopub.status.busy":"2023-11-01T12:08:30.677919Z","iopub.execute_input":"2023-11-01T12:08:30.678325Z","iopub.status.idle":"2023-11-01T12:08:30.685457Z","shell.execute_reply.started":"2023-11-01T12:08:30.678292Z","shell.execute_reply":"2023-11-01T12:08:30.684261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from IPython.display import FileLink\n\na = r'dataset_cat&dog.csv'\nFileLink(a)","metadata":{"execution":{"iopub.status.busy":"2023-11-01T12:08:43.274926Z","iopub.execute_input":"2023-11-01T12:08:43.275706Z","iopub.status.idle":"2023-11-01T12:08:43.282244Z","shell.execute_reply.started":"2023-11-01T12:08:43.275674Z","shell.execute_reply":"2023-11-01T12:08:43.281303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"le = LabelEncoder()\ndf['class_name'] = le.fit_transform(df['class_name'])","metadata":{"execution":{"iopub.status.busy":"2023-11-01T12:22:51.247008Z","iopub.execute_input":"2023-11-01T12:22:51.247365Z","iopub.status.idle":"2023-11-01T12:22:51.259393Z","shell.execute_reply.started":"2023-11-01T12:22:51.247339Z","shell.execute_reply":"2023-11-01T12:22:51.258424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"le.classes_","metadata":{"execution":{"iopub.status.busy":"2023-11-01T12:23:12.946702Z","iopub.execute_input":"2023-11-01T12:23:12.947100Z","iopub.status.idle":"2023-11-01T12:23:12.953529Z","shell.execute_reply.started":"2023-11-01T12:23:12.947069Z","shell.execute_reply":"2023-11-01T12:23:12.952641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pickle.dump(le, open('class_decode.pkl', 'wb'))","metadata":{"execution":{"iopub.status.busy":"2023-11-01T12:49:22.893445Z","iopub.execute_input":"2023-11-01T12:49:22.894381Z","iopub.status.idle":"2023-11-01T12:49:22.899274Z","shell.execute_reply.started":"2023-11-01T12:49:22.894346Z","shell.execute_reply":"2023-11-01T12:49:22.898284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = df.iloc[:, 1:].values\ny = df.iloc[:, 0].values","metadata":{"execution":{"iopub.status.busy":"2023-11-01T12:25:54.539912Z","iopub.execute_input":"2023-11-01T12:25:54.540322Z","iopub.status.idle":"2023-11-01T12:25:54.561457Z","shell.execute_reply.started":"2023-11-01T12:25:54.540292Z","shell.execute_reply":"2023-11-01T12:25:54.559932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(X,\n                                                    y,\n                                                    test_size=0.3,\n                                                    random_state=42)","metadata":{"execution":{"iopub.status.busy":"2023-11-01T12:27:14.109916Z","iopub.execute_input":"2023-11-01T12:27:14.110796Z","iopub.status.idle":"2023-11-01T12:27:14.180804Z","shell.execute_reply.started":"2023-11-01T12:27:14.110761Z","shell.execute_reply":"2023-11-01T12:27:14.179740Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"ml\"></a>\n# <p style=\"background-color:#afeff9; padding: 15px; font-family:cursive; color:black; font-size:100%; text-align:center; border-radius:200px 200px;\">Phase 6 : Make ML Model</p>\n\n⬆️ [Tabel of Contents](#contents_tabel)","metadata":{}},{"cell_type":"code","source":"svc = SVC()\nparam_gs_svc = [{'C':[0.1, 0.2],\n                 'kernel':['linear'],\n                 'gamma':[0.8, 0.9]}]\ngs_svc = GridSearchCV(estimator=svc,\n                      param_grid=param_gs_svc,\n                      scoring='accuracy',\n                      cv=3)","metadata":{"execution":{"iopub.status.busy":"2023-11-01T12:29:49.634872Z","iopub.execute_input":"2023-11-01T12:29:49.635802Z","iopub.status.idle":"2023-11-01T12:30:28.647295Z","shell.execute_reply.started":"2023-11-01T12:29:49.635766Z","shell.execute_reply":"2023-11-01T12:30:28.646125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"train_ml\"></a>\n# <p style=\"background-color:#afeff9; padding: 15px; font-family:cursive; color:black; font-size:100%; text-align:center; border-radius:200px 200px;\">Phase 7 : Train</p>\n\n⬆️ [Tabel of Contents](#contents_tabel)","metadata":{}},{"cell_type":"code","source":"gs_SVC = gs_svc.fit(X_train, y_train)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Best score SVC method : {}'.format(gs_SVC.best_score_))\nprint('Best parameters SVC method : {}'.format(gs_SVC.best_params_))","metadata":{"execution":{"iopub.status.busy":"2023-11-01T12:31:40.983034Z","iopub.execute_input":"2023-11-01T12:31:40.983590Z","iopub.status.idle":"2023-11-01T12:31:40.989115Z","shell.execute_reply.started":"2023-11-01T12:31:40.983555Z","shell.execute_reply":"2023-11-01T12:31:40.988224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"eval_ml\"></a>\n# <p style=\"background-color:#afeff9; padding: 15px; font-family:cursive; color:black; font-size:100%; text-align:center; border-radius:200px 200px;\">Phase 8 : Evaluate</p>\n\n⬆️ [Tabel of Contents](#contents_tabel)","metadata":{}},{"cell_type":"code","source":"best_SVC = gs_SVC.best_estimator_\nY_test_pred  = best_SVC.predict(X_test)\nY_train_pred = best_SVC.predict(X_train)\n\nprint('Train report : ')\nprint(classification_report(Y_train_pred, y_train, target_names=['cat', 'dog']))\n\nprint('Test report : ')\nprint(classification_report(Y_test_pred, y_test, target_names=['cat', 'dog']))","metadata":{"execution":{"iopub.status.busy":"2023-11-01T12:32:47.868126Z","iopub.execute_input":"2023-11-01T12:32:47.868517Z","iopub.status.idle":"2023-11-01T12:32:50.864765Z","shell.execute_reply.started":"2023-11-01T12:32:47.868487Z","shell.execute_reply":"2023-11-01T12:32:50.863664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"test_ml\"></a>\n# <p style=\"background-color:#afeff9; padding: 15px; font-family:cursive; color:black; font-size:100%; text-align:center; border-radius:200px 200px;\">Phase 9 : Test</p>\n\n⬆️ [Tabel of Contents](#contents_tabel)","metadata":{}},{"cell_type":"code","source":"pickle.dump(best_SVC, open('svm.pkl', 'wb'))","metadata":{"execution":{"iopub.status.busy":"2023-11-01T12:34:39.120295Z","iopub.execute_input":"2023-11-01T12:34:39.120756Z","iopub.status.idle":"2023-11-01T12:34:39.131067Z","shell.execute_reply.started":"2023-11-01T12:34:39.120722Z","shell.execute_reply":"2023-11-01T12:34:39.130179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"save_model(feature_extractor, \"feature_extractor.h5\")","metadata":{"execution":{"iopub.status.busy":"2023-11-02T06:04:07.118162Z","iopub.execute_input":"2023-11-02T06:04:07.118465Z","iopub.status.idle":"2023-11-02T06:04:08.037672Z","shell.execute_reply.started":"2023-11-02T06:04:07.118444Z","shell.execute_reply":"2023-11-02T06:04:08.036745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd /kaggle/working/","metadata":{"execution":{"iopub.status.busy":"2023-11-02T06:05:31.486480Z","iopub.execute_input":"2023-11-02T06:05:31.486898Z","iopub.status.idle":"2023-11-02T06:05:31.492755Z","shell.execute_reply.started":"2023-11-02T06:05:31.486869Z","shell.execute_reply":"2023-11-02T06:05:31.492075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from IPython.display import FileLink\n\na = r'feature_extractor.h5'\nFileLink(a)","metadata":{"execution":{"iopub.status.busy":"2023-11-02T06:06:03.930536Z","iopub.execute_input":"2023-11-02T06:06:03.930861Z","iopub.status.idle":"2023-11-02T06:06:03.936780Z","shell.execute_reply.started":"2023-11-02T06:06:03.930839Z","shell.execute_reply":"2023-11-02T06:06:03.935967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def predict(path, FE_path, CLS_path, DC_path):\n    # read Image\n    image = cv2.imread(image_path)\n            \n    # Preprocess Image\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n    image = image/255\n    image = cv2.resize(image, (224, 224))\n    image = np.expand_dims(image, axis=0)\n    \n    # Feature Extraction\n    model_fe = load_model(FE_path)\n    fe = model_fe.predict(image)\n    \n    # Classification\n    model_cls = pickle.load(open(CLS_path, 'rb'))\n    out = model_cls.predict(fe)\n    \n    # Result\n    model_decode = pickle.load(open(DC_path, 'rb'))\n    \n    return model_decode.inverse_transform([out])[0]","metadata":{"execution":{"iopub.status.busy":"2023-11-01T13:05:03.910620Z","iopub.execute_input":"2023-11-01T13:05:03.911465Z","iopub.status.idle":"2023-11-01T13:05:03.918947Z","shell.execute_reply.started":"2023-11-01T13:05:03.911429Z","shell.execute_reply":"2023-11-01T13:05:03.917729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict('/kaggle/working/train/dog/dog.8293.jpg',\n        '/kaggle/working/feature_extractor.h5',\n        '/kaggle/working/svm.pkl',\n        '/kaggle/working/class_decode.pkl')","metadata":{"execution":{"iopub.status.busy":"2023-11-01T13:05:09.241247Z","iopub.execute_input":"2023-11-01T13:05:09.241934Z","iopub.status.idle":"2023-11-01T13:05:20.673682Z","shell.execute_reply.started":"2023-11-01T13:05:09.241900Z","shell.execute_reply":"2023-11-01T13:05:20.672655Z"},"trusted":true},"execution_count":null,"outputs":[]}]}