{"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":"# Read DataSet","metadata":{}},{"cell_type":"code","source":"pip install plotly","metadata":{"execution":{"iopub.status.busy":"2023-11-08T13:51:15.913656Z","iopub.execute_input":"2023-11-08T13:51:15.914373Z","iopub.status.idle":"2023-11-08T13:51:28.032471Z","shell.execute_reply.started":"2023-11-08T13:51:15.914340Z","shell.execute_reply":"2023-11-08T13:51:28.031355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport shutil\nimport pandas as pd\nimport glob\nimport cv2\nimport plotly.express as px\nimport matplotlib.pyplot as plt\nimport tensorflow as tf","metadata":{"execution":{"iopub.status.busy":"2023-11-13T07:28:18.897973Z","iopub.execute_input":"2023-11-13T07:28:18.898780Z","iopub.status.idle":"2023-11-13T07:28:27.474521Z","shell.execute_reply.started":"2023-11-13T07:28:18.898751Z","shell.execute_reply":"2023-11-13T07:28:27.473708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!unzip /kaggle/input/dogs-vs-cats/train.zip\n!unzip /kaggle/input/dogs-vs-cats/test1.zip","metadata":{"execution":{"iopub.status.busy":"2023-11-13T07:29:26.406930Z","iopub.execute_input":"2023-11-13T07:29:26.408057Z","iopub.status.idle":"2023-11-13T07:29:47.356218Z","shell.execute_reply.started":"2023-11-13T07:29:26.408021Z","shell.execute_reply":"2023-11-13T07:29:47.355154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Shape and Count Each Class","metadata":{}},{"cell_type":"code","source":"# Count the number of train sample\ntrain_files = glob.glob('/kaggle/working/train/*')\nlen_train = len(train_files)\nlen_train","metadata":{"execution":{"iopub.status.busy":"2023-11-13T07:30:36.274374Z","iopub.execute_input":"2023-11-13T07:30:36.274747Z","iopub.status.idle":"2023-11-13T07:30:36.371081Z","shell.execute_reply.started":"2023-11-13T07:30:36.274719Z","shell.execute_reply":"2023-11-13T07:30:36.370281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# another way to acheive count of train data\npath , dirs , files = next(os.walk('/kaggle/working/train'))\nlen_train = len(files)\nlen_train","metadata":{"execution":{"iopub.status.busy":"2023-11-13T07:30:38.745838Z","iopub.execute_input":"2023-11-13T07:30:38.746206Z","iopub.status.idle":"2023-11-13T07:30:38.782610Z","shell.execute_reply.started":"2023-11-13T07:30:38.746178Z","shell.execute_reply":"2023-11-13T07:30:38.781741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dirs","metadata":{"execution":{"iopub.status.busy":"2023-11-08T13:51:53.261987Z","iopub.execute_input":"2023-11-08T13:51:53.262323Z","iopub.status.idle":"2023-11-08T13:51:53.267871Z","shell.execute_reply.started":"2023-11-08T13:51:53.262291Z","shell.execute_reply":"2023-11-08T13:51:53.266950Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path","metadata":{"execution":{"iopub.status.busy":"2023-11-08T13:51:53.269186Z","iopub.execute_input":"2023-11-08T13:51:53.269775Z","iopub.status.idle":"2023-11-08T13:51:53.276902Z","shell.execute_reply.started":"2023-11-08T13:51:53.269750Z","shell.execute_reply":"2023-11-08T13:51:53.276072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"files","metadata":{"execution":{"iopub.status.busy":"2023-11-08T13:51:53.277971Z","iopub.execute_input":"2023-11-08T13:51:53.278279Z","iopub.status.idle":"2023-11-08T13:51:53.303254Z","shell.execute_reply.started":"2023-11-08T13:51:53.278250Z","shell.execute_reply":"2023-11-08T13:51:53.302192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Count the number of test sample\ntest_files = glob.glob('/kaggle/working/test1/*')\nlen_test = len(test_files)\nlen_test","metadata":{"execution":{"iopub.status.busy":"2023-11-13T07:30:43.619160Z","iopub.execute_input":"2023-11-13T07:30:43.619960Z","iopub.status.idle":"2023-11-13T07:30:43.669655Z","shell.execute_reply.started":"2023-11-13T07:30:43.619926Z","shell.execute_reply":"2023-11-13T07:30:43.668788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Count of each class in train data\ndata_train = {}\n\ndata_train['cat'] = glob.glob('/kaggle/working/train/cat*')\ndata_train['dog'] = glob.glob('/kaggle/working/train/dog*')\n\nprint(f\"count of cat in train data :  {len(data_train['cat'])}\")\nprint(f\"count of dog in train data :  {len(data_train['dog'])}\")","metadata":{"execution":{"iopub.status.busy":"2023-11-13T07:30:45.833393Z","iopub.execute_input":"2023-11-13T07:30:45.834050Z","iopub.status.idle":"2023-11-13T07:30:45.988383Z","shell.execute_reply.started":"2023-11-13T07:30:45.834016Z","shell.execute_reply":"2023-11-13T07:30:45.987476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_train","metadata":{"execution":{"iopub.status.busy":"2023-11-08T13:51:53.485436Z","iopub.execute_input":"2023-11-08T13:51:53.485699Z","iopub.status.idle":"2023-11-08T13:51:53.542237Z","shell.execute_reply.started":"2023-11-08T13:51:53.485675Z","shell.execute_reply":"2023-11-08T13:51:53.541389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # Count of each class in test data\n# data_test = {}\n\n# data_test['cat'] = glob.glob('/kaggle/working/test1/cat*')\n# data_test['dog'] = glob.glob('/kaggle/working/test1/dog*')\n\n# print(f\"count of cat in test data :  {len(data_test['cat'])}\")\n# print(f\"count of dog in test data :  {len(data_test['dog'])}\")","metadata":{"execution":{"iopub.status.busy":"2023-11-08T13:51:53.543471Z","iopub.execute_input":"2023-11-08T13:51:53.543769Z","iopub.status.idle":"2023-11-08T13:51:53.549410Z","shell.execute_reply.started":"2023-11-08T13:51:53.543742Z","shell.execute_reply":"2023-11-08T13:51:53.548477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = data_train.keys()\ny = [len(count) for count in data_train.values()]\n\nplt.bar(x,y);","metadata":{"execution":{"iopub.status.busy":"2023-11-13T07:30:49.809895Z","iopub.execute_input":"2023-11-13T07:30:49.810874Z","iopub.status.idle":"2023-11-13T07:30:50.051759Z","shell.execute_reply.started":"2023-11-13T07:30:49.810832Z","shell.execute_reply":"2023-11-13T07:30:50.050835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"data in each class are balanced","metadata":{}},{"cell_type":"code","source":"#Get the dimensions of each photo\ndef get_dims (img):\n    image = cv2.imread(img)\n    h, w, d = image.shape\n    return h, w , d","metadata":{"execution":{"iopub.status.busy":"2023-11-13T07:30:54.155300Z","iopub.execute_input":"2023-11-13T07:30:54.155777Z","iopub.status.idle":"2023-11-13T07:30:54.160876Z","shell.execute_reply.started":"2023-11-13T07:30:54.155744Z","shell.execute_reply":"2023-11-13T07:30:54.159849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame(columns = ['Height',\n                            'Width',\n                            'Channels',\n                            'Class'])\nfor img in files:\n    h, w, d = get_dims(path+'/'+img)\n    df.loc[len(df)] = [h , w , d, img.split('.')[0]]\n    \ndf\n","metadata":{"execution":{"iopub.status.busy":"2023-11-13T07:30:59.866078Z","iopub.execute_input":"2023-11-13T07:30:59.866434Z","iopub.status.idle":"2023-11-13T07:31:09.537575Z","shell.execute_reply.started":"2023-11-13T07:30:59.866405Z","shell.execute_reply":"2023-11-13T07:31:09.536279Z"},"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-11-13T07:31:09.538235Z","iopub.status.idle":"2023-11-13T07:31:09.538587Z","shell.execute_reply.started":"2023-11-13T07:31:09.538408Z","shell.execute_reply":"2023-11-13T07:31:09.538424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# EDA(Exploratory Data Analysis)","metadata":{}},{"cell_type":"code","source":"### show some data in each class\ncount = 0\nplt.figure(figsize= (15,15))\nfor label in data_train.keys():\n    for path in data_train[label]:\n        sp = plt.subplot(2 , 4 , count+1)\n        sp.set_title(label)\n        image = cv2.imread(path)\n        plt.axis('off')\n        plt.imshow(image)\n        count+=1\n        if count % 4 == 0:\n            break\nplt.show()\n    ","metadata":{"execution":{"iopub.status.busy":"2023-11-13T07:31:15.251674Z","iopub.execute_input":"2023-11-13T07:31:15.252490Z","iopub.status.idle":"2023-11-13T07:31:16.618504Z","shell.execute_reply.started":"2023-11-13T07:31:15.252456Z","shell.execute_reply":"2023-11-13T07:31:16.617377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Split Train , Test , Validation","metadata":{}},{"cell_type":"code","source":"\n# # Path to train, test, and validation folders\n# train_dir = '/kaggle/working/train_data'\n# test_dir = '/kaggle/working/test_data'\n# validation_dir = '/kaggle/working/validation_data'\n\n# Dictionary data_train containing the addresses of the images in each class\n\n\n# # Create the folders\n# os.makedirs(train_dir, exist_ok=True)\n# os.makedirs(test_dir, exist_ok=True)\n# os.makedirs(validation_dir, exist_ok=True)\n\n# # Number of training, test, and validation data\n# num_train = 10000\n# num_test_validation = 2500\n\n\n# # Copy dog images to the train , validation and test folder\n\n  \n# for i in range(len(data_train['dog'])+1):\n#     if i < num_train:\n#         shutil.copy(data_train['dog'][i], os.path.join(train_dir))\n#     elif i < num_train + num_test_validation/2:\n#         shutil.copy(data_train['dog'][i], os.path.join(validation_dir))\n#     elif i < num_train + num_test_validation:\n#         shutil.copy(data_train['dog'][i], os.path.join(test_dir))\n\n# # Copy cat images to the train , validation and test folder\n# for i in range(len(data_train['cat'])+1):\n#     if i < num_train:\n#         shutil.copy(data_train['cat'][i], os.path.join(train_dir))\n#     elif i < num_train + num_test_validation/2:\n#         shutil.copy(data_train['cat'][i], os.path.join(validation_dir))\n#     elif i < num_train + num_test_validation:\n#         shutil.copy(data_train['cat'][i], os.path.join(test_dir))","metadata":{"execution":{"iopub.status.busy":"2023-11-08T13:53:14.559016Z","iopub.execute_input":"2023-11-08T13:53:14.559294Z","iopub.status.idle":"2023-11-08T13:53:14.564564Z","shell.execute_reply.started":"2023-11-08T13:53:14.559270Z","shell.execute_reply":"2023-11-08T13:53:14.563621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_dir = '/kaggle/working/dogs_vs_cats'\nos.mkdir(base_dir)\n\ntrain_dir = os.path.join(base_dir , 'train')\nos.mkdir(train_dir) #make a new train directory inside my base directory\n\nvalid_dir= os.path.join(base_dir , 'validation')\nos.mkdir(valid_dir) #make a new validation directory inside my base directory\n\ntest_dir = os.path.join(base_dir , 'test')\nos.mkdir(test_dir) #make a new test directory inside my base directory\n\ntrain_cats_dir = os.path.join(train_dir , 'cats')\nos.mkdir(train_cats_dir) #make a new cats directory inside my train directory\n\ntrain_dogs_dir = os.path.join(train_dir , 'dogs')\nos.mkdir(train_dogs_dir) #make a new dogs directory inside my train directory\n\nvalid_cats_dir = os.path.join(valid_dir, 'cats')\nos.mkdir(valid_cats_dir) #make a new cats directory inside my validation directory\n\nvalid_dogs_dir = os.path.join(valid_dir , 'dogs')\nos.mkdir(valid_dogs_dir) #make a new dogs directory inside my validation directory\n\ntest_cats_dir = os.path.join(test_dir , 'cats')\nos.mkdir(test_cats_dir) #make a new cats directory inside my test directory\n\ntest_dogs_dir = os.path.join(test_dir , 'dogs')\nos.mkdir(test_dogs_dir) #make a new dogs directory inside my test directory","metadata":{"execution":{"iopub.status.busy":"2023-11-13T07:31:43.461903Z","iopub.execute_input":"2023-11-13T07:31:43.462248Z","iopub.status.idle":"2023-11-13T07:31:43.471984Z","shell.execute_reply.started":"2023-11-13T07:31:43.462222Z","shell.execute_reply":"2023-11-13T07:31:43.471085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"original_dataset_dir = '/kaggle/working/train'\n\nfnames = ['cat.{}.jpg'.format(i) for i in range(10000)] #Move 10,000 cat images from the original dataset to the train cat directory\nfor fname in fnames:\n  src = os.path.join(original_dataset_dir , fname)\n  dst = os.path.join(train_cats_dir , fname)\n  shutil.move(src , dst)\n\nfnames = ['cat.{}.jpg'.format(i) for i in range(10000,11250)] #Move 1250 cat images from the original dataset to the validation cat directory\nfor fname in fnames:\n  src = os.path.join(original_dataset_dir , fname)\n  dst = os.path.join(valid_cats_dir , fname)\n  shutil.move(src,dst)\n\nfnames = ['cat.{}.jpg'.format(i) for i in range(11250,12500)] #Move 1250 cat images from the original dataset to the test cat directory\nfor fname in fnames:\n  src= os.path.join(original_dataset_dir , fname)\n  dst= os.path.join(test_cats_dir, fname)\n  shutil.move(src,dst)\n\nfnames = ['dog.{}.jpg'.format(i) for i in range(10000)] #Move 10,000 dog images from the original dataset to the train cat directory\nfor fname in fnames:\n  src = os.path.join(original_dataset_dir , fname)\n  dst = os.path.join(train_dogs_dir ,fname)\n  shutil.move(src,dst)\n\nfnames = ['dog.{}.jpg'.format(i) for i in range(10000,11250)] #Move 1250 dog images from the original dataset to the validation cat directory\nfor fname in fnames:\n  src = os.path.join(original_dataset_dir,fname)\n  dst = os.path.join(valid_dogs_dir , fname)\n  shutil.move(src,dst)\n\nfnames = ['dog.{}.jpg'.format(i) for i in range(11250,12500)]#Move 1250 dog images from the original dataset to the test cat directory\nfor fname in fnames:\n  src= os.path.join(original_dataset_dir, fname)\n  dst = os.path.join(test_dogs_dir , fname)\n  shutil.move(src, dst)","metadata":{"execution":{"iopub.status.busy":"2023-11-13T07:32:39.494912Z","iopub.execute_input":"2023-11-13T07:32:39.495295Z","iopub.status.idle":"2023-11-13T07:32:41.135449Z","shell.execute_reply.started":"2023-11-13T07:32:39.495267Z","shell.execute_reply":"2023-11-13T07:32:41.134410Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f'Total training cat images {len(os.listdir(train_cats_dir))}')\nprint(f'Total validation cat images {len(os.listdir(valid_cats_dir))}')\nprint(f'Total test cat images {len(os.listdir(test_cats_dir))}')\nprint('\\n')\nprint(f'Total training dog images {len(os.listdir(train_dogs_dir))}')\nprint(f'Total validation cat images {len(os.listdir(valid_dogs_dir))}')\nprint(f'Total test cat images {len(os.listdir(test_dogs_dir))}')","metadata":{"execution":{"iopub.status.busy":"2023-11-08T13:53:17.112659Z","iopub.execute_input":"2023-11-08T13:53:17.112974Z","iopub.status.idle":"2023-11-08T13:53:17.134130Z","shell.execute_reply.started":"2023-11-08T13:53:17.112948Z","shell.execute_reply":"2023-11-08T13:53:17.133263Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Augmentation using Filter","metadata":{}},{"cell_type":"code","source":"import random\nfrom PIL import Image, ImageFilter, ImageDraw\nimport os\n\n# Function to apply random filters to an image\ndef apply_random_filter(image):\n    filter_type = random.choice(['Fog', 'Rain', 'Snow'])\n    if filter_type == 'Fog':\n        # Apply fog filter\n        image = image.filter(ImageFilter.GaussianBlur(radius=5))\n    elif filter_type == 'Rain':\n        # Apply rain filter\n        width, height = image.size\n        draw = ImageDraw.Draw(image)\n        raindrop_count = int(width * height * 0.01)  # Adjust the raindrop density as desired\n\n        for _ in range(raindrop_count):\n            x = random.randint(0, width)\n            y = random.randint(0, height)\n            length = random.randint(5, 15)\n            draw.line([(x, y), (x, y + length)], fill=(200, 200, 200), width=1)\n\n    elif filter_type == 'Snow':\n        # Apply snow filter\n        image = image.convert(\"RGBA\")\n        width, height = image.size\n        snowflake_count = int(width * height * 0.01)  # Adjust the snowflake density as desired\n\n        snowflake = Image.new('RGBA', (4, 4), (255, 255, 255, 255))\n\n        for _ in range(snowflake_count):\n            x = random.randint(0, width)\n            y = random.randint(0, height)\n            image.alpha_composite(snowflake, dest=(x, y))\n\n    return image\n","metadata":{"execution":{"iopub.status.busy":"2023-11-08T13:53:17.135290Z","iopub.execute_input":"2023-11-08T13:53:17.135619Z","iopub.status.idle":"2023-11-08T13:53:17.147195Z","shell.execute_reply.started":"2023-11-08T13:53:17.135587Z","shell.execute_reply":"2023-11-08T13:53:17.146444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# apply filter on one sample\nimage = Image.open('/kaggle/working/dogs_vs_cats/train/dogs/dog.890.jpg')\n\n# Apply the random filter\nfiltered_image = apply_random_filter(image)\n\n\nplt.figure()\nplt.subplot(1, 2, 1)\nplt.imshow(image)\nplt.title('Original Image')\nplt.axis('off')\n\nplt.subplot(1, 2, 2)\nplt.imshow(filtered_image)\nplt.title('Filtered Image')\nplt.axis('off')\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-08T13:53:17.148276Z","iopub.execute_input":"2023-11-08T13:53:17.148565Z","iopub.status.idle":"2023-11-08T13:53:17.394687Z","shell.execute_reply.started":"2023-11-08T13:53:17.148541Z","shell.execute_reply":"2023-11-08T13:53:17.393792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Directory paths for cats and dogs images\ntrain_cats_dir = '/kaggle/working/dogs_vs_cats/train/cats'\ntrain_dogs_dir = '/kaggle/working/dogs_vs_cats/train/dogs'\n\n# Number of images to apply filters and delete\nnum_images_to_filter = 5000\n\n# Loop through the images in the train_cats_dir\ncat_image_files = os.listdir(train_cats_dir)\nrandom_cat_images = random.sample(cat_image_files, num_images_to_filter)\n\nfor fname in random_cat_images:\n    image_path = os.path.join(train_cats_dir, fname)\n    image = Image.open(image_path)\n    image = apply_random_filter(image)\n    image = image.convert(\"RGB\")  # Convert the image to RGB mode\n    image.save(image_path)\n\n\n    \n# Loop through the images in the train_dogs_dir\ndog_image_files = os.listdir(train_dogs_dir)\nrandom_dog_images = random.sample(dog_image_files, num_images_to_filter)\n\nfor fname in random_dog_images:\n    image_path = os.path.join(train_dogs_dir, fname)\n    image = Image.open(image_path)\n    image = apply_random_filter(image)\n    image = image.convert(\"RGB\")  # Convert the image to RGB mode\n    image.save(image_path)\n","metadata":{"execution":{"iopub.status.busy":"2023-11-08T13:53:17.395838Z","iopub.execute_input":"2023-11-08T13:53:17.396129Z","iopub.status.idle":"2023-11-08T13:56:30.101526Z","shell.execute_reply.started":"2023-11-08T13:53:17.396105Z","shell.execute_reply":"2023-11-08T13:56:30.100136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_cats_dir = '/kaggle/working/dogs_vs_cats/train/cats'\ncat_image_files = os.listdir(train_cats_dir)\nlen(cat_image_files)","metadata":{"execution":{"iopub.status.busy":"2023-11-08T13:56:30.103147Z","iopub.execute_input":"2023-11-08T13:56:30.104137Z","iopub.status.idle":"2023-11-08T13:56:30.118893Z","shell.execute_reply.started":"2023-11-08T13:56:30.104100Z","shell.execute_reply":"2023-11-08T13:56:30.117917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model","metadata":{}},{"cell_type":"code","source":"import os\nfrom tensorflow.keras.applications.resnet import ResNet152\nfrom tensorflow.keras.applications.resnet import preprocess_input, decode_predictions\nfrom tensorflow.keras.preprocessing import image\nimport numpy as np\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n","metadata":{"execution":{"iopub.status.busy":"2023-11-08T13:56:30.120271Z","iopub.execute_input":"2023-11-08T13:56:30.120541Z","iopub.status.idle":"2023-11-08T13:56:30.131835Z","shell.execute_reply.started":"2023-11-08T13:56:30.120518Z","shell.execute_reply":"2023-11-08T13:56:30.129410Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.applications.inception_v3 import InceptionV3","metadata":{"execution":{"iopub.status.busy":"2023-11-08T13:56:30.133227Z","iopub.execute_input":"2023-11-08T13:56:30.133584Z","iopub.status.idle":"2023-11-08T13:56:30.139150Z","shell.execute_reply.started":"2023-11-08T13:56:30.133550Z","shell.execute_reply":"2023-11-08T13:56:30.137999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_conv = InceptionV3(weights='imagenet', input_shape=(250, 250, 3), include_top=False)","metadata":{"execution":{"iopub.status.busy":"2023-11-08T13:56:30.146676Z","iopub.execute_input":"2023-11-08T13:56:30.146995Z","iopub.status.idle":"2023-11-08T13:56:39.940658Z","shell.execute_reply.started":"2023-11-08T13:56:30.146969Z","shell.execute_reply":"2023-11-08T13:56:39.939754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_conv.summary()","metadata":{"execution":{"iopub.status.busy":"2023-11-08T13:56:39.941979Z","iopub.execute_input":"2023-11-08T13:56:39.942299Z","iopub.status.idle":"2023-11-08T13:56:40.657651Z","shell.execute_reply.started":"2023-11-08T13:56:39.942274Z","shell.execute_reply":"2023-11-08T13:56:40.656735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_generator = ImageDataGenerator(\n    rescale=1.0/255.0,\n    shear_range=0.1,\n    zoom_range=0.3,\n)\ngenerator = data_generator.flow_from_directory(\n    '/kaggle/working/dogs_vs_cats/train',\n    target_size=(250, 250),\n    batch_size=20,\n    class_mode='binary'\n)","metadata":{"execution":{"iopub.status.busy":"2023-11-08T13:56:40.658948Z","iopub.execute_input":"2023-11-08T13:56:40.659306Z","iopub.status.idle":"2023-11-08T13:56:41.208353Z","shell.execute_reply.started":"2023-11-08T13:56:40.659272Z","shell.execute_reply":"2023-11-08T13:56:41.207558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_generator_test = ImageDataGenerator(\n    rescale=1.0/255.0,\n)\ngenerator_valid = data_generator_test.flow_from_directory(\n    '/kaggle/working/dogs_vs_cats/validation',\n    target_size=(250, 250),\n    batch_size=10,\n    class_mode='binary'\n)","metadata":{"execution":{"iopub.status.busy":"2023-11-08T13:56:41.209454Z","iopub.execute_input":"2023-11-08T13:56:41.209752Z","iopub.status.idle":"2023-11-08T13:56:41.284576Z","shell.execute_reply.started":"2023-11-08T13:56:41.209726Z","shell.execute_reply":"2023-11-08T13:56:41.283875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"generator_test = data_generator_test.flow_from_directory(\n    '/kaggle/working/dogs_vs_cats/test',\n    target_size=(250, 250),\n    batch_size=10,\n    class_mode='binary'\n)","metadata":{"execution":{"iopub.status.busy":"2023-11-08T13:56:41.285759Z","iopub.execute_input":"2023-11-08T13:56:41.286106Z","iopub.status.idle":"2023-11-08T13:56:41.365096Z","shell.execute_reply.started":"2023-11-08T13:56:41.286075Z","shell.execute_reply":"2023-11-08T13:56:41.364260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras import models, layers, optimizers","metadata":{"execution":{"iopub.status.busy":"2023-11-08T13:56:41.367571Z","iopub.execute_input":"2023-11-08T13:56:41.367836Z","iopub.status.idle":"2023-11-08T13:56:41.371957Z","shell.execute_reply.started":"2023-11-08T13:56:41.367812Z","shell.execute_reply":"2023-11-08T13:56:41.370975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_conv.trainable = False","metadata":{"execution":{"iopub.status.busy":"2023-11-08T13:56:41.373106Z","iopub.execute_input":"2023-11-08T13:56:41.373398Z","iopub.status.idle":"2023-11-08T13:56:41.392286Z","shell.execute_reply.started":"2023-11-08T13:56:41.373374Z","shell.execute_reply":"2023-11-08T13:56:41.391424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = models.Sequential()\nmodel.add(model_conv)\nmodel.add(layers.Flatten())\nmodel.add(layers.Dense(256, activation='relu'))\nmodel.add(layers.Dropout(0.5))\nmodel.add(layers.Dense(1, activation='sigmoid'))\nmodel.compile(optimizer='Adam', loss='binary_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-11-08T13:56:41.393268Z","iopub.execute_input":"2023-11-08T13:56:41.393580Z","iopub.status.idle":"2023-11-08T13:56:42.319967Z","shell.execute_reply.started":"2023-11-08T13:56:41.393549Z","shell.execute_reply":"2023-11-08T13:56:42.319126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-11-08T13:56:42.321057Z","iopub.execute_input":"2023-11-08T13:56:42.321329Z","iopub.status.idle":"2023-11-08T13:56:42.360413Z","shell.execute_reply.started":"2023-11-08T13:56:42.321306Z","shell.execute_reply":"2023-11-08T13:56:42.359555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau, ModelCheckpoint\n\nes = EarlyStopping(monitor='val_loss', mode='min', patience=7)\nrlrop = ReduceLROnPlateau(monitor='val_loss', patience=2, factor=0.2, min_lr=0.001)\nmch = ModelCheckpoint('model.h5', monitor='val_loss', mode='min', save_best_only=True)","metadata":{"execution":{"iopub.status.busy":"2023-11-08T13:56:42.361565Z","iopub.execute_input":"2023-11-08T13:56:42.362622Z","iopub.status.idle":"2023-11-08T13:56:42.369601Z","shell.execute_reply.started":"2023-11-08T13:56:42.362589Z","shell.execute_reply":"2023-11-08T13:56:42.368616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train","metadata":{}},{"cell_type":"code","source":"history = model.fit(generator, steps_per_epoch=len(generator), epochs=500, validation_data=generator_valid, validation_steps=len(generator_valid), callbacks=[es, rlrop, mch])","metadata":{"execution":{"iopub.status.busy":"2023-11-08T14:25:34.954676Z","iopub.execute_input":"2023-11-08T14:25:34.955189Z","iopub.status.idle":"2023-11-08T15:47:14.787779Z","shell.execute_reply.started":"2023-11-08T14:25:34.955156Z","shell.execute_reply":"2023-11-08T15:47:14.786675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Evaluate","metadata":{}},{"cell_type":"code","source":"os.environ['KMP_DUPLICATE_LIB_OK']='True'\nplt.plot(history.history['accuracy'], color='blue', label = 'Training Accuracy')\nplt.plot(history.history['val_accuracy'], color='green' , label = 'Validation Accuracy')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.legend()\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"best_val_accuracy = max(history.history['val_accuracy'])\nprint(\"Best Validation Accuracy:\", best_val_accuracy)","metadata":{"execution":{"iopub.status.busy":"2023-11-08T14:24:36.571535Z","iopub.status.idle":"2023-11-08T14:24:36.571900Z","shell.execute_reply.started":"2023-11-08T14:24:36.571713Z","shell.execute_reply":"2023-11-08T14:24:36.571729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Predict","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.models import load_model\nmodel = load_model('/kaggle/working/model.h5')\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-11-08T14:24:36.572941Z","iopub.status.idle":"2023-11-08T14:24:36.573270Z","shell.execute_reply.started":"2023-11-08T14:24:36.573108Z","shell.execute_reply":"2023-11-08T14:24:36.573124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#predict one sample\nimport tensorflow.keras.utils as utils\nimg = utils.load_img(\n    '/kaggle/working/dogs_vs_cats/test/dogs/dog.11369.jpg',\n    grayscale=False,\n    color_mode='rgb',\n    target_size=(250, 250),\n    interpolation='nearest',\n    keep_aspect_ratio=False\n)\nimg","metadata":{"execution":{"iopub.status.busy":"2023-11-13T07:38:19.359824Z","iopub.execute_input":"2023-11-13T07:38:19.360591Z","iopub.status.idle":"2023-11-13T07:38:19.389928Z","shell.execute_reply.started":"2023-11-13T07:38:19.360537Z","shell.execute_reply":"2023-11-13T07:38:19.388980Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = utils.img_to_array(img)\nimg.shape","metadata":{"execution":{"iopub.status.busy":"2023-11-08T14:24:36.576366Z","iopub.status.idle":"2023-11-08T14:24:36.576669Z","shell.execute_reply.started":"2023-11-08T14:24:36.576516Z","shell.execute_reply":"2023-11-08T14:24:36.576530Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result = model.predict(tf.expand_dims(img, axis=0))\nresult","metadata":{"execution":{"iopub.status.busy":"2023-11-08T14:24:36.577550Z","iopub.status.idle":"2023-11-08T14:24:36.577875Z","shell.execute_reply.started":"2023-11-08T14:24:36.577715Z","shell.execute_reply":"2023-11-08T14:24:36.577730Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if float(result) < 0.5:\n    print('Cat')\nelse:\n    print('Dog')","metadata":{"execution":{"iopub.status.busy":"2023-11-08T14:24:36.578808Z","iopub.status.idle":"2023-11-08T14:24:36.579142Z","shell.execute_reply.started":"2023-11-08T14:24:36.578981Z","shell.execute_reply":"2023-11-08T14:24:36.578997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Test","metadata":{}},{"cell_type":"code","source":"model = load_model('/kaggle/working/model.h5')","metadata":{"execution":{"iopub.status.busy":"2023-11-08T14:24:36.580341Z","iopub.status.idle":"2023-11-08T14:24:36.580652Z","shell.execute_reply.started":"2023-11-08T14:24:36.580501Z","shell.execute_reply":"2023-11-08T14:24:36.580516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.evaluate(generator_test)","metadata":{"execution":{"iopub.status.busy":"2023-11-08T14:24:36.581874Z","iopub.status.idle":"2023-11-08T14:24:36.582207Z","shell.execute_reply.started":"2023-11-08T14:24:36.582053Z","shell.execute_reply":"2023-11-08T14:24:36.582068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Download Model","metadata":{}},{"cell_type":"code","source":"# import os\n# import subprocess\n# from IPython.display import FileLink, display\n\n# def download_file(path, download_file_name):\n#     os.chdir('/kaggle/working/')\n#     zip_name = f\"/kaggle/working/{download_file_name}.zip\"\n#     command = f\"zip {zip_name} {path} -r\"\n#     result = subprocess.run(command, shell=True, capture_output=True, text=True)\n#     if result.returncode != 0:\n#         print(\"Unable to run zip command!\")\n#         print(result.stderr)\n#         return\n#     display(FileLink(f'{download_file_name}.zip'))\n\n# download_file('/kaggle/working/model.h5', 'out')","metadata":{"execution":{"iopub.status.busy":"2023-11-08T14:24:36.583461Z","iopub.status.idle":"2023-11-08T14:24:36.583765Z","shell.execute_reply.started":"2023-11-08T14:24:36.583614Z","shell.execute_reply":"2023-11-08T14:24:36.583628Z"},"trusted":true},"execution_count":null,"outputs":[]}]}