{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","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":30746,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"#Extracting the compressed file\nfrom zipfile import ZipFile\ndataset = '/kaggle/input/dogs-vs-cats/train.zip'\nwith ZipFile(dataset,'r') as zip:\n    zip.extractall()\n    print('the Dataset is extracted')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-08-14T15:33:26.547766Z","iopub.execute_input":"2024-08-14T15:33:26.548296Z","iopub.status.idle":"2024-08-14T15:33:42.227725Z","shell.execute_reply.started":"2024-08-14T15:33:26.548252Z","shell.execute_reply":"2024-08-14T15:33:42.225987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\n\n#Counting number of images il train folder\npath , dirs , files = next(os.walk('/kaggle/working/train'))\nfile_count = len(files)\nprint('Number of images in training Dataset is: ',file_count)","metadata":{"execution":{"iopub.status.busy":"2024-08-14T15:33:44.999567Z","iopub.execute_input":"2024-08-14T15:33:45.000109Z","iopub.status.idle":"2024-08-14T15:33:45.046712Z","shell.execute_reply.started":"2024-08-14T15:33:45.000062Z","shell.execute_reply":"2024-08-14T15:33:45.045022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Importing the Needed libraries\nimport numpy as np\nfrom PIL import Image\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nfrom sklearn.model_selection import train_test_split\n","metadata":{"execution":{"iopub.status.busy":"2024-08-14T15:33:45.280105Z","iopub.execute_input":"2024-08-14T15:33:45.280615Z","iopub.status.idle":"2024-08-14T15:33:46.092012Z","shell.execute_reply.started":"2024-08-14T15:33:45.28058Z","shell.execute_reply":"2024-08-14T15:33:46.090283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Displaying an example of cat image\nimg = mpimg.imread('/kaggle/working/train/cat.10000.jpg')\nplt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2024-08-14T15:33:46.095939Z","iopub.execute_input":"2024-08-14T15:33:46.096658Z","iopub.status.idle":"2024-08-14T15:33:46.645979Z","shell.execute_reply.started":"2024-08-14T15:33:46.096599Z","shell.execute_reply":"2024-08-14T15:33:46.644106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Displaying an example of dog image\nimg = mpimg.imread('/kaggle/working/train/dog.10000.jpg')\nplt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2024-08-14T15:33:48.74299Z","iopub.execute_input":"2024-08-14T15:33:48.743588Z","iopub.status.idle":"2024-08-14T15:33:49.069458Z","shell.execute_reply.started":"2024-08-14T15:33:48.743543Z","shell.execute_reply":"2024-08-14T15:33:49.06745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"file_names = os.listdir('/kaggle/working/train')\nnumber_of_cat = 0\nnumber_of_dog = 0\nfor image_file in file_names:\n    image = image_file[0:3]\n    if image == 'cat':\n        number_of_cat+=1\n    else:\n        number_of_dog+=1\nprint('number of cat images:',number_of_cat)\nprint('number of dog images:',number_of_dog)","metadata":{"execution":{"iopub.status.busy":"2024-08-14T15:33:49.493955Z","iopub.execute_input":"2024-08-14T15:33:49.494459Z","iopub.status.idle":"2024-08-14T15:33:49.53599Z","shell.execute_reply.started":"2024-08-14T15:33:49.494409Z","shell.execute_reply":"2024-08-14T15:33:49.534697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Note that cat example image was in size of (400,350) and dog example image was (200,400) so the images need to be at the same size\n\n#Resizing the images\n\n#creating a directory for resized images\n#os.mkdir('/kaggle/working/image_resized')\n\noriginal_folder = '/kaggle/working/train/'\nresized_folder = '/kaggle/working/image_resized/'\n\n#let's resize the first 2000 images only\nfor i in range(2000):\n    filename = os.listdir(original_folder)[i]\n    img_path = original_folder+filename\n    img = Image.open(img_path)\n    img = img.resize((224,224))\n    img = img.convert('RGB')\n    new_image_path = resized_folder+filename\n    img.save(new_image_path)","metadata":{"execution":{"iopub.status.busy":"2024-08-14T15:33:50.431417Z","iopub.execute_input":"2024-08-14T15:33:50.431969Z","iopub.status.idle":"2024-08-14T15:34:34.071833Z","shell.execute_reply.started":"2024-08-14T15:33:50.431926Z","shell.execute_reply":"2024-08-14T15:34:34.070245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"file_names = os.listdir('/kaggle/working/image_resized')\nfor i in range(5):\n    image=file_names[i]\n    print(image)\n  ","metadata":{"execution":{"iopub.status.busy":"2024-08-14T15:34:34.074396Z","iopub.execute_input":"2024-08-14T15:34:34.074959Z","iopub.status.idle":"2024-08-14T15:34:34.086478Z","shell.execute_reply.started":"2024-08-14T15:34:34.074912Z","shell.execute_reply":"2024-08-14T15:34:34.085107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Displaying an example of resized cat image\nimg = mpimg.imread('/kaggle/working/image_resized/cat.9549.jpg')\nplt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2024-08-14T15:34:34.08853Z","iopub.execute_input":"2024-08-14T15:34:34.08953Z","iopub.status.idle":"2024-08-14T15:34:34.546763Z","shell.execute_reply.started":"2024-08-14T15:34:34.089484Z","shell.execute_reply":"2024-08-14T15:34:34.544955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Displaying an example of resized dog image\nimg = mpimg.imread('/kaggle/working/image_resized/dog.2950.jpg')\nplt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2024-08-14T15:34:34.550533Z","iopub.execute_input":"2024-08-14T15:34:34.551002Z","iopub.status.idle":"2024-08-14T15:34:34.983756Z","shell.execute_reply.started":"2024-08-14T15:34:34.550959Z","shell.execute_reply":"2024-08-14T15:34:34.981945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Creating labels for resized images '1 for dog , 0 for cat'\n\nfile_names = os.listdir('/kaggle/working/image_resized/')\nlabels = []\n\nfor i in range(2000):\n    filename=file_names[i]\n    label = filename[0:3]\n    if label == 'dog':\n        labels.append(1)\n    else:\n        labels.append(0)\n        ","metadata":{"execution":{"iopub.status.busy":"2024-08-14T15:34:34.985621Z","iopub.execute_input":"2024-08-14T15:34:34.986057Z","iopub.status.idle":"2024-08-14T15:34:34.999897Z","shell.execute_reply.started":"2024-08-14T15:34:34.986022Z","shell.execute_reply":"2024-08-14T15:34:34.997847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(file_names[0:5])\nprint(labels[0:5])","metadata":{"execution":{"iopub.status.busy":"2024-08-14T15:34:35.003459Z","iopub.execute_input":"2024-08-14T15:34:35.004013Z","iopub.status.idle":"2024-08-14T15:34:35.014106Z","shell.execute_reply.started":"2024-08-14T15:34:35.003965Z","shell.execute_reply":"2024-08-14T15:34:35.012538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Counting the number of cat & dog images in these 2000 resized images\ndog = 0\ncat = 0\nfor label in labels:\n    if label == 0 :\n        cat+=1\n    else:\n        dog+=1\nprint('count of dog images: ',dog)\nprint('count of cat images: ',cat)\n","metadata":{"execution":{"iopub.status.busy":"2024-08-14T15:34:35.016285Z","iopub.execute_input":"2024-08-14T15:34:35.016887Z","iopub.status.idle":"2024-08-14T15:34:35.032201Z","shell.execute_reply.started":"2024-08-14T15:34:35.016837Z","shell.execute_reply":"2024-08-14T15:34:35.03005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Converting resized images into numpy arrays\nimport cv2\nimport glob","metadata":{"execution":{"iopub.status.busy":"2024-08-14T15:34:35.034147Z","iopub.execute_input":"2024-08-14T15:34:35.034693Z","iopub.status.idle":"2024-08-14T15:34:35.200197Z","shell.execute_reply.started":"2024-08-14T15:34:35.034656Z","shell.execute_reply":"2024-08-14T15:34:35.198469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images_dir = '/kaggle/working/image_resized/'\nimage_extension = ['jpg','png']\n\nfiles = []\n\n[files.extend(glob.glob(images_dir+'*.'+e)) for e in image_extension]\n\ndog_cat_images = np.asarray([cv2.imread(file) for file in files])","metadata":{"execution":{"iopub.status.busy":"2024-08-14T15:34:35.202047Z","iopub.execute_input":"2024-08-14T15:34:35.202503Z","iopub.status.idle":"2024-08-14T15:34:36.674485Z","shell.execute_reply.started":"2024-08-14T15:34:35.202463Z","shell.execute_reply":"2024-08-14T15:34:36.673167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type(dog_cat_images)","metadata":{"execution":{"iopub.status.busy":"2024-08-14T15:34:36.725068Z","iopub.execute_input":"2024-08-14T15:34:36.725583Z","iopub.status.idle":"2024-08-14T15:34:36.7352Z","shell.execute_reply.started":"2024-08-14T15:34:36.725547Z","shell.execute_reply":"2024-08-14T15:34:36.733578Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(dog_cat_images.shape)","metadata":{"execution":{"iopub.status.busy":"2024-08-14T15:34:37.411723Z","iopub.execute_input":"2024-08-14T15:34:37.412261Z","iopub.status.idle":"2024-08-14T15:34:37.419849Z","shell.execute_reply.started":"2024-08-14T15:34:37.412222Z","shell.execute_reply":"2024-08-14T15:34:37.418088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Split the data into features and label\nx = dog_cat_images\ny = np.asarray(labels)\n","metadata":{"execution":{"iopub.status.busy":"2024-08-14T15:34:37.947502Z","iopub.execute_input":"2024-08-14T15:34:37.947972Z","iopub.status.idle":"2024-08-14T15:34:37.955181Z","shell.execute_reply.started":"2024-08-14T15:34:37.947938Z","shell.execute_reply":"2024-08-14T15:34:37.953402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Split the data into training and testing\nx_train , x_test , y_train , y_test = train_test_split(x,y,test_size=0.2,random_state=2)\n","metadata":{"execution":{"iopub.status.busy":"2024-08-14T15:34:38.395243Z","iopub.execute_input":"2024-08-14T15:34:38.395799Z","iopub.status.idle":"2024-08-14T15:34:38.68993Z","shell.execute_reply.started":"2024-08-14T15:34:38.395762Z","shell.execute_reply":"2024-08-14T15:34:38.688276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Scaling the data\nx_train_scaled = x_train/255\nx_test_scaled = x_test /255\n","metadata":{"execution":{"iopub.status.busy":"2024-08-14T15:34:38.835524Z","iopub.execute_input":"2024-08-14T15:34:38.836052Z","iopub.status.idle":"2024-08-14T15:34:39.836622Z","shell.execute_reply.started":"2024-08-14T15:34:38.836017Z","shell.execute_reply":"2024-08-14T15:34:39.834939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Building the Neural Network\nimport tensorflow as tf\n","metadata":{"execution":{"iopub.status.busy":"2024-08-14T15:36:09.27368Z","iopub.execute_input":"2024-08-14T15:36:09.274915Z","iopub.status.idle":"2024-08-14T15:36:09.283905Z","shell.execute_reply.started":"2024-08-14T15:36:09.274856Z","shell.execute_reply":"2024-08-14T15:36:09.281685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow_hub as hub\n","metadata":{"execution":{"iopub.status.busy":"2024-08-14T15:36:10.24664Z","iopub.execute_input":"2024-08-14T15:36:10.247169Z","iopub.status.idle":"2024-08-14T15:36:10.255122Z","shell.execute_reply.started":"2024-08-14T15:36:10.247133Z","shell.execute_reply":"2024-08-14T15:36:10.253105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mobilenet_model = 'https://tfhub.dev/google/tf2-preview/mobilenet_v2/feature_vector/4'","metadata":{"execution":{"iopub.status.busy":"2024-08-14T15:36:22.57308Z","iopub.execute_input":"2024-08-14T15:36:22.573695Z","iopub.status.idle":"2024-08-14T15:36:22.582188Z","shell.execute_reply.started":"2024-08-14T15:36:22.57365Z","shell.execute_reply":"2024-08-14T15:36:22.579993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pretrained_model = hub.KerasLayer(mobilenet_model, input_shape=(224,224,3),trainable=False)","metadata":{"execution":{"iopub.status.busy":"2024-08-14T15:36:29.82973Z","iopub.execute_input":"2024-08-14T15:36:29.830382Z","iopub.status.idle":"2024-08-14T15:36:33.459087Z","shell.execute_reply.started":"2024-08-14T15:36:29.830341Z","shell.execute_reply":"2024-08-14T15:36:33.457223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_of_classes=2","metadata":{"execution":{"iopub.status.busy":"2024-08-14T15:36:59.93981Z","iopub.execute_input":"2024-08-14T15:36:59.940391Z","iopub.status.idle":"2024-08-14T15:36:59.94812Z","shell.execute_reply.started":"2024-08-14T15:36:59.940351Z","shell.execute_reply":"2024-08-14T15:36:59.946261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tf.keras.Sequential([\n    pretrained_model,\n    tf.keras.layers.Dense(num_of_classes)\n    \n    \n])\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2024-08-14T15:38:52.074207Z","iopub.execute_input":"2024-08-14T15:38:52.074706Z","iopub.status.idle":"2024-08-14T15:38:53.191099Z","shell.execute_reply.started":"2024-08-14T15:38:52.074668Z","shell.execute_reply":"2024-08-14T15:38:53.189231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(\n\noptimizer='adam',\nloss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),\nmetrics = ['acc']\n\n)","metadata":{"execution":{"iopub.status.busy":"2024-08-14T15:41:28.359293Z","iopub.execute_input":"2024-08-14T15:41:28.36019Z","iopub.status.idle":"2024-08-14T15:41:28.417508Z","shell.execute_reply.started":"2024-08-14T15:41:28.360123Z","shell.execute_reply":"2024-08-14T15:41:28.412771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(x_train_scaled,y_train,epochs=10)","metadata":{"execution":{"iopub.status.busy":"2024-08-14T15:42:08.723405Z","iopub.execute_input":"2024-08-14T15:42:08.725554Z","iopub.status.idle":"2024-08-14T15:48:23.289212Z","shell.execute_reply.started":"2024-08-14T15:42:08.725465Z","shell.execute_reply":"2024-08-14T15:48:23.287004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"score, acc = model.evaluate(x_test_scaled, y_test)\nprint('Test Loss =', score)\nprint('Test Accuracy =', acc)","metadata":{"execution":{"iopub.status.busy":"2024-08-14T15:49:14.381608Z","iopub.execute_input":"2024-08-14T15:49:14.38345Z","iopub.status.idle":"2024-08-14T15:49:24.913358Z","shell.execute_reply.started":"2024-08-14T15:49:14.383358Z","shell.execute_reply":"2024-08-14T15:49:24.911251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}