{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":3362,"databundleVersionId":31148,"sourceType":"competition"}],"dockerImageVersionId":30558,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd \nimport matplotlib.pyplot as plt \nfrom tensorflow.keras.applications.resnet import ResNet152, preprocess_input, decode_predictions\nfrom keras.utils import to_categorical ,load_img\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix\nfrom tensorflow.keras.preprocessing import image\nfrom keras.preprocessing.image import ImageDataGenerator\n\n#Reading data\nimport zipfile\nimport random\nimport os  \n\n# EDA\nimport cv2\nimport glob\nfrom sklearn.utils.class_weight import compute_class_weight\nfrom cv2 import imread\n\n#Augmentation\nimport imgaug.augmenters as iaa\nfrom pathlib import Path\nimport random\n\n\n# Visualization\nimport plotly.express as px\nimport plotly.graph_objects as go\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom tensorflow.keras.utils import plot_model , load_img, img_to_array\nfrom PIL import Image\n\n#evaluation\nfrom sklearn.metrics import classification_report\n\n# Preprocessing\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator","metadata":{"execution":{"iopub.status.busy":"2023-11-14T23:31:31.310718Z","iopub.execute_input":"2023-11-14T23:31:31.311116Z","iopub.status.idle":"2023-11-14T23:31:31.321454Z","shell.execute_reply.started":"2023-11-14T23:31:31.311084Z","shell.execute_reply":"2023-11-14T23:31:31.319588Z"},"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;\">Reading data</p>","metadata":{}},{"cell_type":"code","source":"\nif ('train' not in os.listdir('/kaggle/working')):\n    print ('extraction...')\n    with zipfile.ZipFile('/kaggle/input/dogs-vs-cats/train.zip', 'r') as ztrain:\n        ztrain.extractall('/kaggle/working')\nif ('test1' not in os.listdir('/kaggle/working')):\n    print ('extraction...')\n    with zipfile.ZipFile('/kaggle/input/dogs-vs-cats/test1.zip', 'r') as ztest:\n        ztest.extractall('/kaggle/working')\n        \n","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:15:55.386681Z","iopub.execute_input":"2023-11-14T12:15:55.387714Z","iopub.status.idle":"2023-11-14T12:16:09.747690Z","shell.execute_reply.started":"2023-11-14T12:15:55.387675Z","shell.execute_reply":"2023-11-14T12:16:09.746862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#constructing dataframe of pictures' paths and their labels  in traint folder      \nweather={'sunny':0 ,'snowy':1 ,'rainy':2, 'foggy':3}\ncategory={'dog':0 , 'cat':1}\ndef images_in_folder(folder ,weather_stat): \n    data=pd.DataFrame({'path': [], 'root':[], 'picture_name':[] , 'label': [], 'stat':[]}) \n    data = data.set_index('path')\n    root = folder\n    for picture_name in os.listdir(folder):\n        if picture_name.split('.')[0] =='dog':\n            data.loc[folder+picture_name]= [root,picture_name ,0 , weather[weather_stat]]\n        elif picture_name.split('.')[0] =='cat':\n            data.loc[folder+picture_name]= [root,picture_name ,1 , weather[weather_stat]]\n        else:\n            data.loc[folder+picture_name]= [root,picture_name ,None , weather[weather_stat]]  \n    return(data)\n\ndata=images_in_folder('/kaggle/working/train/' ,weather_stat = 'sunny')\ntest_data=images_in_folder('/kaggle/working/test1/' , weather_stat = 'sunny')\ndata.head()","metadata":{"execution":{"iopub.status.busy":"2023-11-14T21:09:31.426523Z","iopub.execute_input":"2023-11-14T21:09:31.426909Z","iopub.status.idle":"2023-11-14T21:12:32.468770Z","shell.execute_reply.started":"2023-11-14T21:09:31.426877Z","shell.execute_reply":"2023-11-14T21:12:32.467133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.head()","metadata":{"execution":{"iopub.status.busy":"2023-11-14T21:13:30.882512Z","iopub.execute_input":"2023-11-14T21:13:30.882945Z","iopub.status.idle":"2023-11-14T21:13:30.898472Z","shell.execute_reply.started":"2023-11-14T21:13:30.882911Z","shell.execute_reply":"2023-11-14T21:13:30.896439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axes = plt.subplots(1, 5, figsize=(12, 3))\nfor i, path in enumerate(data.head(5).index):\n    image = Image.open(path)\n    axes[i].imshow(image)\n    axes[i].axis('off')\n    axes[i].set_title(data.loc[path].label)\nplt.tight_layout()\n\nfig, axes = plt.subplots(1, 5, figsize=(12, 3))\nfor i, path in enumerate(data.tail(5).index):\n    image = Image.open(path)\n    axes[i].imshow(image)\n    axes[i].axis('off')\n    axes[i].set_title(data.loc[path].label) \nplt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:19:07.149442Z","iopub.execute_input":"2023-11-14T12:19:07.149845Z","iopub.status.idle":"2023-11-14T12:19:08.464181Z","shell.execute_reply.started":"2023-11-14T12:19:07.149816Z","shell.execute_reply":"2023-11-14T12:19:08.462879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.label.value_counts().plot(kind='bar')","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:19:08.465314Z","iopub.execute_input":"2023-11-14T12:19:08.466473Z","iopub.status.idle":"2023-11-14T12:19:08.657400Z","shell.execute_reply.started":"2023-11-14T12:19:08.466437Z","shell.execute_reply":"2023-11-14T12:19:08.656237Z"},"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 picture Sizes</p>","metadata":{}},{"cell_type":"code","source":"def get_dims(file): \n    image = cv2.imread(file)    \n    h, w, d = image.shape\n    return [h, w, d] \n\nfor path in data.index: \n    data.loc[path ,['h','w', 'd'] ]= get_dims(path)","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:19:08.658827Z","iopub.execute_input":"2023-11-14T12:19:08.659475Z","iopub.status.idle":"2023-11-14T12:20:23.178638Z","shell.execute_reply.started":"2023-11-14T12:19:08.659438Z","shell.execute_reply":"2023-11-14T12:20:23.176861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:20:23.180007Z","iopub.execute_input":"2023-11-14T12:20:23.180347Z","iopub.status.idle":"2023-11-14T12:20:23.201109Z","shell.execute_reply.started":"2023-11-14T12:20:23.180316Z","shell.execute_reply":"2023-11-14T12:20:23.199642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = px.scatter(data, x='h', y='w', color='label', \n                    labels={'Height':'h', 'Width':'w'},\n                    title=\"Scatter plot of Image Dimensions by Class\")\n\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:20:23.202367Z","iopub.execute_input":"2023-11-14T12:20:23.202669Z","iopub.status.idle":"2023-11-14T12:20:25.452809Z","shell.execute_reply.started":"2023-11-14T12:20:23.202642Z","shell.execute_reply":"2023-11-14T12:20:25.451357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TARGET_SIZE=(224,224)\ndata = data[(data.h>=200)&(data.h>=200)]\nfor path in data[(data.h<200)&(data.h<200) ].index:\n    os.remove(path)","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:20:25.457062Z","iopub.execute_input":"2023-11-14T12:20:25.457801Z","iopub.status.idle":"2023-11-14T12:20:25.469819Z","shell.execute_reply.started":"2023-11-14T12:20:25.457765Z","shell.execute_reply":"2023-11-14T12:20:25.468678Z"},"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":{"execution":{"iopub.status.busy":"2023-11-04T15:42:01.160161Z","iopub.execute_input":"2023-11-04T15:42:01.160471Z","iopub.status.idle":"2023-11-04T15:42:01.165833Z","shell.execute_reply.started":"2023-11-04T15:42:01.160450Z","shell.execute_reply":"2023-11-04T15:42:01.164770Z"}}},{"cell_type":"code","source":"def compute_mean_histogram(paths):\n    histograms = {'red': [], 'green': [], 'blue': []}\n    for path in paths:\n        image = cv2.imread(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 = {}\nmean_histograms_per_class[0] = compute_mean_histogram(list(data.loc[data['label']==0].index ))\nmean_histograms_per_class[1] = compute_mean_histogram(list(data.loc[data['label']==1].index ))\n\n#Plotting the histogram    \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-11-14T12:20:25.471004Z","iopub.execute_input":"2023-11-14T12:20:25.471895Z","iopub.status.idle":"2023-11-14T12:21:25.023587Z","shell.execute_reply.started":"2023-11-14T12:20:25.471869Z","shell.execute_reply":"2023-11-14T12:21:25.021711Z"},"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 And  Noise Level</p>","metadata":{"execution":{"iopub.status.busy":"2023-11-04T15:42:51.387273Z","iopub.execute_input":"2023-11-04T15:42:51.387555Z","iopub.status.idle":"2023-11-04T15:42:51.392967Z","shell.execute_reply.started":"2023-11-04T15:42:51.387535Z","shell.execute_reply":"2023-11-04T15:42:51.391655Z"}}},{"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\n\navg_stds_per_class = {}\navg_stds_per_class [0] = compute_avg_std(list(data.loc[data['label']==0 ].index ))\navg_stds_per_class [1] = compute_avg_std(list(data.loc[data['label']==1].index ))\n ","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:21:25.025408Z","iopub.execute_input":"2023-11-14T12:21:25.026443Z","iopub.status.idle":"2023-11-14T12:22:37.627074Z","shell.execute_reply.started":"2023-11-14T12:21:25.026398Z","shell.execute_reply":"2023-11-14T12:22:37.625675Z"},"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-11-14T12:22:37.628371Z","iopub.execute_input":"2023-11-14T12:22:37.628690Z","iopub.status.idle":"2023-11-14T12:22:37.652849Z","shell.execute_reply.started":"2023-11-14T12:22:37.628661Z","shell.execute_reply":"2023-11-14T12:22:37.651358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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 = {}\nmean_noise_levels [0] = estimate_noise(list(data.loc[data['label']==0 ].index ))\nmean_noise_levels [1] = estimate_noise(list(data.loc[data['label']==1 ].index ))\n ","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:22:37.654960Z","iopub.execute_input":"2023-11-14T12:22:37.655322Z","iopub.status.idle":"2023-11-14T12:23:13.646966Z","shell.execute_reply.started":"2023-11-14T12:22:37.655290Z","shell.execute_reply":"2023-11-14T12:23:13.645356Z"},"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-11-14T12:23:13.648472Z","iopub.execute_input":"2023-11-14T12:23:13.648797Z","iopub.status.idle":"2023-11-14T12:23:13.666661Z","shell.execute_reply.started":"2023-11-14T12:23:13.648771Z","shell.execute_reply":"2023-11-14T12:23:13.664902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<p style=\"background-color:#efeeee; padding: 15px; font-family:cursive; color:black; font-size:400%; text-align:center; border-radius:200px 200px;\">PreProcess and Data Augmentation</p>","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;\"> Data Augmentation</p>","metadata":{}},{"cell_type":"code","source":"\ndef produce_save_augmented_filters(image_paths , plot_some = True ):          \n    for count, path in enumerate(image_paths):\n        #Snow\n        image = cv2.imread(path)\n        image_name = path.rsplit('/', 1)[-1]\n        lbl =data.loc[path , 'label']\n        aug = iaa.FastSnowyLandscape(lightness_threshold=140,lightness_multiplier=2.5)\n        snowy_image = aug.augment_image(image)\n        snow_path =  '/kaggle/working/train/1.'+image_name \n        data.loc[snow_path ,'label'] = lbl\n        data.loc[snow_path , 'picture_name']= \"1.\"+image_name\n        data.loc[snow_path ,'stat'] = 1    # stat=1 means it is snowy\n        cv2.imwrite(snow_path, snowy_image) \n        # Rain\n        aug = aug = iaa.Rain(speed=(0.1, 0.3))\n        rainy_image = aug.augment_image(image)\n        rain_path =  '/kaggle/working/train/2.'+ image_name\n        data.loc[rain_path,'label'] = lbl\n        data.loc[rain_path , 'picture_name']= \"2.\"+image_name\n        data.loc[rain_path ,'stat'] = 2     # stat=2 means it is rainy \n        cv2.imwrite(rain_path, rainy_image)\n        #fog\n        aug = iaa.Fog()\n        foggy_image = aug.augment_image(image)\n        fog_path = '/kaggle/working/train/3.' +image_name\n        data.loc[fog_path,'label'] = lbl\n        data.loc[fog_path , 'picture_name']= \"3.\"+image_name\n        data.loc[fog_path ,'stat'] = 3      # stat=3 means it is foggy\n        cv2.imwrite(fog_path, foggy_image)\n        #Plot some augmented images\n        if plot_some == True: \n            if count<5:\n                count += 1\n                fig, ax = plt.subplots(1,4, figsize=(12, 3))\n                ax[0].imshow(image)\n                ax[0].set_title(\"Intact\")\n                ax[0].axis('off')\n                ax[1].imshow(snowy_image)\n                ax[1].set_title(\"Snowy \")\n                ax[1].axis('off')\n                ax[2].imshow(rainy_image)\n                ax[2].set_title(\"Rainy \")\n                ax[2].axis('off')\n                ax[3].imshow(foggy_image)\n                ax[3].set_title(\"Foggy \")\n                ax[3].axis('off')\n        \n ","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:23:13.668510Z","iopub.execute_input":"2023-11-14T12:23:13.668885Z","iopub.status.idle":"2023-11-14T12:23:13.682665Z","shell.execute_reply.started":"2023-11-14T12:23:13.668854Z","shell.execute_reply":"2023-11-14T12:23:13.680963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"produce_save_augmented_filters(random.choices(list(data.index) ,k=len(data)//100),\n                              plot_some = True)\n             ","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:23:13.684898Z","iopub.execute_input":"2023-11-14T12:23:13.685624Z","iopub.status.idle":"2023-11-14T12:23:36.372688Z","shell.execute_reply.started":"2023-11-14T12:23:13.685588Z","shell.execute_reply":"2023-11-14T12:23:36.370928Z"},"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;\">Size Correction and Contrast Correction Using Clahe</p>","metadata":{}},{"cell_type":"code","source":"from pathlib import Path\n    \ndef save_clahe_img(data , plot_some = True): \n    save_folder = '/kaggle/working/train/'\n     \n    image_paths=list(data.index)\n    for count, image_path in enumerate(image_paths ):\n        image = cv2.imread(image_path)\n        image = cv2.resize(image,TARGET_SIZE) \n        lab = cv2.cvtColor(image, cv2.COLOR_BGR2LAB)\n        # Split Lab\n        lab_planes = list(cv2.split(lab)) \n        # Apply Histogram Equlization\n        clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))\n        lab_planes[0] = clahe.apply(lab_planes[0])\n        lab = cv2.merge(lab_planes)\n        cla = cv2.cvtColor(lab, cv2.COLOR_LAB2BGR) \n        cv2.imwrite(image_path, cla)\n        #plot first 5 images with their corrected images for instance\n        if plot_some == True: \n            if count<5:\n                count += 1\n                fig, ax = plt.subplots(1,2, figsize=(12, 3))\n                ax[0].imshow(image)\n                ax[0].set_title(\"Intact\")\n                ax[0].axis('off')\n                ax[1].imshow(cla)\n                ax[1].set_title(\"Corrected contrast & resized\") \n                ax[1].axis('off') \n            \nsave_clahe_img(data , plot_some = True)\ndata.to_csv('/kaggle/working/data_df.csv')","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:23:36.374633Z","iopub.execute_input":"2023-11-14T12:23:36.375464Z","iopub.status.idle":"2023-11-14T12:25:24.167604Z","shell.execute_reply.started":"2023-11-14T12:23:36.375437Z","shell.execute_reply":"2023-11-14T12:25:24.166120Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<p style=\"background-color:#efeeee; padding: 15px; font-family:cursive; color:black; font-size:400%; text-align:center; border-radius:200px 200px;\">Using Augmented and PreProcessed data</p>","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;\">Train, Validation and Test Data</p>","metadata":{}},{"cell_type":"code","source":"data.head()","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:25:24.168966Z","iopub.execute_input":"2023-11-14T12:25:24.169330Z","iopub.status.idle":"2023-11-14T12:25:24.185442Z","shell.execute_reply.started":"2023-11-14T12:25:24.169296Z","shell.execute_reply":"2023-11-14T12:25:24.184075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data=data.reset_index()\ntest_data=test_data.reset_index()\ndata.label=data.label.astype(str)\ndata.head()","metadata":{"execution":{"iopub.status.busy":"2023-11-14T21:14:59.431998Z","iopub.execute_input":"2023-11-14T21:14:59.432364Z","iopub.status.idle":"2023-11-14T21:14:59.455556Z","shell.execute_reply.started":"2023-11-14T21:14:59.432337Z","shell.execute_reply":"2023-11-14T21:14:59.454108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data, val_data  = train_test_split(data[['path' , 'label']], test_size=0.2, random_state=14) \ntrain_data.label.value_counts().plot(kind='bar')","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:25:24.222457Z","iopub.execute_input":"2023-11-14T12:25:24.222753Z","iopub.status.idle":"2023-11-14T12:25:24.414043Z","shell.execute_reply.started":"2023-11-14T12:25:24.222728Z","shell.execute_reply":"2023-11-14T12:25:24.412409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 16\nsize=224\ntrain_datagen = ImageDataGenerator(rescale=1/255)\ntrain_generator = train_datagen.flow_from_dataframe(\n    dataframe = train_data,\n    x_col='path',\n    y_col='label', \n    batch_size=batch_size,\n    class_mode=\"categorical\"\n    )\nvalid_datagen = ImageDataGenerator(rescale=1/255)\nvalid_generator = train_datagen.flow_from_dataframe(\n    dataframe=val_data,\n    x_col='path',\n    y_col='label', \n    batch_size=batch_size,\n    class_mode=\"categorical\"\n    )\ntest_datagen = ImageDataGenerator(rescale=1./255)\ntest_generator = test_datagen.flow_from_dataframe(\n    dataframe = test_data, \n    x_col='path',\n    y_col=None,\n    class_mode=None,\n    batch_size=batch_size,\n    shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2023-11-14T21:15:16.885384Z","iopub.execute_input":"2023-11-14T21:15:16.885745Z","iopub.status.idle":"2023-11-14T21:15:17.182829Z","shell.execute_reply.started":"2023-11-14T21:15:16.885714Z","shell.execute_reply":"2023-11-14T21:15:17.181056Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<p style=\"background-color:#efeeee; padding: 15px; font-family:cursive; color:black; font-size:300%; text-align:center; border-radius:200px 200px;\">Transfer learning using VGG16 model</p>","metadata":{}},{"cell_type":"code","source":"from keras.models import Sequential\nfrom keras import layers\nfrom keras.layers import Conv2D, MaxPooling2D, Dropout, Flatten, Dense, Activation,GlobalMaxPooling2D\nfrom keras import applications\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras import optimizers\nfrom keras.applications import VGG16\nfrom keras.models import Model\n \ninput_shape = (224, 224, 3)\n\nvgg16_epochs = 5\nbatch_size = 16\n\npre_trained_model = VGG16(input_shape=input_shape, include_top=False, weights=\"imagenet\")\n    \nfor layer in pre_trained_model.layers[:-1]:\n    layer.trainable = False\npre_trained_model.layers[-1].trainable = True\n    \nlast_layer = pre_trained_model.get_layer('block5_pool')\nlast_output = last_layer.output\n    \n    \nx = GlobalMaxPooling2D()(last_output) \nx = Dense(512, activation='relu')(x) \nx = Dropout(0.5)(x)\nx = Dense(128, activation='relu')(x) \nx = Dropout(0.5)(x) \nx = layers.Dense(2, activation='softmax')(x)\n\nmodel = Model(pre_trained_model.input, x)\n\nmodel.compile(loss='categorical_crossentropy',\n              optimizer='adam',\n              #optimizer=optimizers.SGD(lr=1e-4, momentum=0.9),\n              metrics=['accuracy'])\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:25:24.708768Z","iopub.execute_input":"2023-11-14T12:25:24.709347Z","iopub.status.idle":"2023-11-14T12:25:25.889740Z","shell.execute_reply.started":"2023-11-14T12:25:24.709317Z","shell.execute_reply":"2023-11-14T12:25:25.888355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_model(model)","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:25:25.891165Z","iopub.execute_input":"2023-11-14T12:25:25.891596Z","iopub.status.idle":"2023-11-14T12:25:26.226801Z","shell.execute_reply.started":"2023-11-14T12:25:25.891561Z","shell.execute_reply":"2023-11-14T12:25:26.226119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.callbacks import ModelCheckpoint , EarlyStopping\nmch = ModelCheckpoint('model.h5', monitor='val_loss', mode='min', save_best_only=True)\nes = EarlyStopping(monitor='val_loss', mode='min', patience=3)","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:25:26.227774Z","iopub.execute_input":"2023-11-14T12:25:26.228416Z","iopub.status.idle":"2023-11-14T12:25:26.233723Z","shell.execute_reply.started":"2023-11-14T12:25:26.228389Z","shell.execute_reply":"2023-11-14T12:25:26.232756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nhistory = model.fit(\n    train_generator,\n    epochs=4,\n    steps_per_epoch = train_generator.samples // batch_size,\n    validation_data = valid_generator, \n    validation_steps = valid_generator.samples // batch_size, \n    callbacks=[es, mch]\n)\n                 ","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:25:26.235202Z","iopub.execute_input":"2023-11-14T12:25:26.235680Z","iopub.status.idle":"2023-11-14T19:09:17.679762Z","shell.execute_reply.started":"2023-11-14T12:25:26.235653Z","shell.execute_reply":"2023-11-14T19:09:17.678230Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('/kaggle/working/')","metadata":{"execution":{"iopub.status.busy":"2023-11-14T19:09:21.575112Z","iopub.execute_input":"2023-11-14T19:09:21.575415Z","iopub.status.idle":"2023-11-14T19:09:24.437752Z","shell.execute_reply.started":"2023-11-14T19:09:21.575389Z","shell.execute_reply":"2023-11-14T19:09:24.436316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<p style=\"background-color:#efeeee; padding: 15px; font-family:cursive; color:black; font-size:400%; text-align:center; border-radius:200px 200px;\">Model Evaluation</p>","metadata":{}},{"cell_type":"code","source":"def plot_model_history(model_history, param1='accuracy', param2='val_accuracy'):\n    fig, axs = plt.subplots(1,2,figsize=(15,5))\n    axs[0].plot(range(1,len(model_history.history[param1])+1),model_history.history[param1])\n    axs[0].plot(range(1,len(model_history.history[param2])+1),model_history.history[param2])\n    axs[0].set_title('Accuracy with Feature extractor VGG')\n    axs[0].set_xlabel('Epoch') \n    axs[0].legend(['train', 'val'], loc='best')\n    axs[1].plot(range(1,len(model_history.history['loss'])+1),model_history.history['loss'])\n    axs[1].plot(range(1,len(model_history.history['val_loss'])+1),model_history.history['val_loss'])\n    axs[1].set_title('Model Loss with Feature extractor VGG') \n    axs[1].set_xlabel('Epoch') \n    axs[1].legend(['train', 'val'], loc='best')\n    plt.show()\n    \nplot_model_history(history, param1='accuracy', param2='val_accuracy')","metadata":{"execution":{"iopub.status.busy":"2023-11-14T19:09:24.441150Z","iopub.execute_input":"2023-11-14T19:09:24.441560Z","iopub.status.idle":"2023-11-14T19:09:24.933190Z","shell.execute_reply.started":"2023-11-14T19:09:24.441525Z","shell.execute_reply":"2023-11-14T19:09:24.931887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<p style=\"background-color:#efeeee; padding: 15px; font-family:cursive; color:black; font-size:300%; text-align:center; border-radius:200px 200px;\">validation set prediction </p>","metadata":{}},{"cell_type":"code","source":"y_val = pd.to_numeric(val_data['label'] ,downcast=\"integer\")\ny_pred =  model.predict(valid_generator) \ny_pred_val = np.where(y_pred < 0.5, 1,0)\n","metadata":{"execution":{"iopub.status.busy":"2023-11-14T23:31:58.670867Z","iopub.execute_input":"2023-11-14T23:31:58.672106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\nconfusion_mtx = confusion_matrix(y_val, y_pred[:,1])  \nf,ax = plt.subplots(figsize=(8, 8))\nsns.heatmap(confusion_mtx, annot=True,ax=ax)\nplt.xlabel(\"Predicted label\")\nplt.ylabel(\"Real label\")\nplt.title(\"Confusion Matrixwith Feature extractor VGG\")\nplt.show()\n\nprint(classification_report(y_val, y_pred))\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<p style=\"background-color:#efeeee; padding: 15px; font-family:cursive; color:black; font-size:300%; text-align:center; border-radius:200px 200px;\">Test set prediction </p>","metadata":{}},{"cell_type":"code","source":"y_pred_test =  model.predict(test_generator) ","metadata":{"execution":{"iopub.status.busy":"2023-11-14T21:31:37.296852Z","iopub.execute_input":"2023-11-14T21:31:37.297258Z","iopub.status.idle":"2023-11-14T22:25:59.305166Z","shell.execute_reply.started":"2023-11-14T21:31:37.297226Z","shell.execute_reply":"2023-11-14T22:25:59.304035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred__test = np.where(y_pred_test < 0.5, 1,0)\ntest_data['label'] = y_pred__test\n\n","metadata":{"execution":{"iopub.status.busy":"2023-11-14T22:49:27.460491Z","iopub.execute_input":"2023-11-14T22:49:27.460841Z","iopub.status.idle":"2023-11-14T22:49:27.465933Z","shell.execute_reply.started":"2023-11-14T22:49:27.460813Z","shell.execute_reply":"2023-11-14T22:49:27.464877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.to_csv('/kaggle/working/results_test_set.csv')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"category= {0:'dog' , 1:'cat'}\nsample_test = test_data.sample(n=16).reset_index() \nplt.figure(figsize=(12, 12))\nfor index, row in sample_test.iterrows():\n    filename = row['path']\n    label = row['label']\n    img = load_img(filename, target_size=(224, 224))\n    plt.subplot(4, 4, index+1)\n    plt.imshow(img) \n    plt.title(category[label])\n    plt.axis('off')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-14T22:51:53.243420Z","iopub.execute_input":"2023-11-14T22:51:53.243781Z","iopub.status.idle":"2023-11-14T22:51:54.664336Z","shell.execute_reply.started":"2023-11-14T22:51:53.243754Z","shell.execute_reply":"2023-11-14T22:51:54.662256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<p style=\"background-color:#efeeee; padding: 15px; font-family:cursive; color:black; font-size:300%; text-align:center; border-radius:200px 200px;\">Combination of Feature Extractor with ML </p>","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;\"> Feature Extraction </p>","metadata":{}},{"cell_type":"code","source":"train_data=train_data.set_index('path')\ntest_data=test_data.set_index('path')\ntrain_data","metadata":{"execution":{"iopub.status.busy":"2023-11-14T23:01:27.810822Z","iopub.execute_input":"2023-11-14T23:01:27.811187Z","iopub.status.idle":"2023-11-14T23:01:27.827869Z","shell.execute_reply.started":"2023-11-14T23:01:27.811163Z","shell.execute_reply":"2023-11-14T23:01:27.826204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install keras-cv-attention-models","metadata":{"execution":{"iopub.status.busy":"2023-11-14T23:23:40.015734Z","iopub.execute_input":"2023-11-14T23:23:40.016145Z","iopub.status.idle":"2023-11-14T23:23:52.756911Z","shell.execute_reply.started":"2023-11-14T23:23:40.016116Z","shell.execute_reply":"2023-11-14T23:23:52.755592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nfrom keras_cv_attention_models import moganet\nmoganet_model = moganet.MogaNetSmall(input_shape=(224, 224, 3), pretrained=\"imagenet\")\nbase_model =  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-14T23:24:56.554239Z","iopub.execute_input":"2023-11-14T23:24:56.554833Z","iopub.status.idle":"2023-11-14T23:25:02.829343Z","shell.execute_reply.started":"2023-11-14T23:24:56.554797Z","shell.execute_reply":"2023-11-14T23:25:02.828310Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" \ndef extract_features(image_path):\n    img = image.load_img(image_path, target_size=(224, 224))\n    x = image.img_to_array(img)\n    x = np.expand_dims(x, axis=0)\n    x = preprocess_input(x)\n    features = base_model.predict(x)\n    return features.flatten()\n\ntrain_features_df = pd.DataFrame(columns= [*range(512) , 'label'])\nfor image_path in (train_data.index):  \n    train_features_df.loc[image_path] = [*extract_features(image_path) , train_data.loc[image_path,'label'] ]\n     \ntest_features_df = pd.DataFrame(columns= [*range(512) , 'label'])    \nfor image_path in test_data.path:  \n    test_features_df.loc[image_path] = [*extract_features(image_path) ,  test_data.loc[image_path,'label']  ]\n    \ntrain_features_df , test_features_df","metadata":{"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;\"> ML model </p>","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import MinMaxScaler\nfrom sklearn.svm import SVC\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import accuracy_score\n\nscaler = MinMaxScaler()\ntrain_features_df[[0:512]] = scaler.fit_transform(train_features_df[[0:512]] )\nX_train, X_test, y_train, y_test = train_test_split(train_features_df, train_labels, test_size=0.2, random_state=42)\n \nsvm_classifier = SVC()\n \nsvm_classifier.fit(X_train, y_train)\n \ny_pred = svm_classifier.predict(X_test)\n \naccuracy = accuracy_score(y_test, y_pred)\nprint(\"Accuracy:\", accuracy)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(y_test, y_pred))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}