{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input/dogs-vs-cats'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-07-18T04:58:52.753811Z","iopub.execute_input":"2024-07-18T04:58:52.754654Z","iopub.status.idle":"2024-07-18T04:58:53.718750Z","shell.execute_reply.started":"2024-07-18T04:58:52.754619Z","shell.execute_reply":"2024-07-18T04:58:53.717534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nprint(os.listdir(\"../input\"))\n\nimport zipfile\n\nwith zipfile.ZipFile(\"/kaggle/input/dogs-vs-cats/test1.zip\",\"r\") as z:\n    z.extractall(\".\")\n    \nwith zipfile.ZipFile(\"/kaggle/input/dogs-vs-cats/train.zip\",\"r\") as z:\n    z.extractall(\".\")","metadata":{"execution":{"iopub.status.busy":"2024-07-18T05:00:23.449262Z","iopub.execute_input":"2024-07-18T05:00:23.449767Z","iopub.status.idle":"2024-07-18T05:00:39.067912Z","shell.execute_reply.started":"2024-07-18T05:00:23.449737Z","shell.execute_reply":"2024-07-18T05:00:39.066984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plot dog photos from the dogs vs cats dataset\nfrom matplotlib import pyplot\nfrom matplotlib.image import imread\ntrain_folder = '/kaggle/working/train/'\n\n# define location of dataset\nfolder = train_folder\n# plot first few images\nfor i in range(9):\n\t# define subplot\n\tpyplot.subplot(330 + 1 + i)\n\t# define filename\n\tfilename = folder + 'dog.' + str(i) + '.jpg'\n\t# load image pixels\n\timage = imread(filename)\n\t# plot raw pixel data\n\tpyplot.imshow(image)\n# show the figure\npyplot.show()","metadata":{"execution":{"iopub.status.busy":"2024-07-18T05:00:42.488999Z","iopub.execute_input":"2024-07-18T05:00:42.489345Z","iopub.status.idle":"2024-07-18T05:00:43.735848Z","shell.execute_reply.started":"2024-07-18T05:00:42.489319Z","shell.execute_reply":"2024-07-18T05:00:43.734717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plot first few images\nfor i in range(9):\n\t# define subplot\n\tpyplot.subplot(330 + 1 + i)\n\t# define filename\n\tfilename = folder + 'cat.' + str(i) + '.jpg'\n\t# load image pixels\n\timage = imread(filename)\n\t# plot raw pixel data\n\tpyplot.imshow(image)\n# show the figure\npyplot.show()","metadata":{"execution":{"iopub.status.busy":"2024-07-18T05:01:04.684246Z","iopub.execute_input":"2024-07-18T05:01:04.685171Z","iopub.status.idle":"2024-07-18T05:01:05.767524Z","shell.execute_reply.started":"2024-07-18T05:01:04.685134Z","shell.execute_reply":"2024-07-18T05:01:05.766478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# load dogs vs cats dataset, reshape and save to a new file\nfrom os import listdir\nfrom numpy import asarray\nfrom numpy import save\nfrom keras.preprocessing.image import load_img\nfrom keras.preprocessing.image import img_to_array","metadata":{"execution":{"iopub.status.busy":"2024-07-18T05:07:46.837737Z","iopub.execute_input":"2024-07-18T05:07:46.838246Z","iopub.status.idle":"2024-07-18T05:07:46.844474Z","shell.execute_reply.started":"2024-07-18T05:07:46.838198Z","shell.execute_reply":"2024-07-18T05:07:46.843446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# define location of dataset\nfolder = 'train/'\nphotos, labels = list(), list()\n# enumerate files in the directory\nfor file in listdir(folder):\n#for file in listdir(folder[:10_000]): # select 10_000 images\n\t# determine class\n\toutput = 0.0\n\tif file.startswith('dog'):\n\t\toutput = 1.0\n\t# load image\n\tphoto = load_img(folder + file, target_size=(200, 200))\n\t# convert to numpy array\n\tphoto = img_to_array(photo)\n\t# store\n\tphotos.append(photo)\n\tlabels.append(output)","metadata":{"execution":{"iopub.status.busy":"2024-07-18T05:02:17.534111Z","iopub.execute_input":"2024-07-18T05:02:17.534776Z","iopub.status.idle":"2024-07-18T05:02:59.739137Z","shell.execute_reply.started":"2024-07-18T05:02:17.534743Z","shell.execute_reply":"2024-07-18T05:02:59.738016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls -l","metadata":{"execution":{"iopub.status.busy":"2024-07-18T05:02:59.740930Z","iopub.execute_input":"2024-07-18T05:02:59.741345Z","iopub.status.idle":"2024-07-18T05:03:01.037444Z","shell.execute_reply.started":"2024-07-18T05:02:59.741306Z","shell.execute_reply":"2024-07-18T05:03:01.036041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# load and confirm the shape\nfrom numpy import load photos = load('dogs_vs_cats_photos.npy') labels = load('dogs_vs_cats_labels.npy') print(photos.shape, labels.shape)","metadata":{}},{"cell_type":"code","source":"# create directories\nimport os\ndataset_home = 'dataset_dogs_vs_cats/'\nsubdirs = ['train/', 'test/']\nfor subdir in subdirs:\n    # create label subdirectories\n    labeldirs = ['dogs/', 'cats/']\n    for labldir in labeldirs:\n        newdir = dataset_home + subdir + labldir\n        os.makedirs(newdir, exist_ok=True)","metadata":{"execution":{"iopub.status.busy":"2024-07-18T05:03:39.234906Z","iopub.execute_input":"2024-07-18T05:03:39.235367Z","iopub.status.idle":"2024-07-18T05:03:39.242750Z","shell.execute_reply.started":"2024-07-18T05:03:39.235329Z","shell.execute_reply":"2024-07-18T05:03:39.241539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# organize dataset into a useful structure\nfrom os import makedirs\nfrom os import listdir\nfrom shutil import copyfile\nfrom random import seed\nfrom random import random\n# create directories\ndataset_home = 'dataset_dogs_vs_cats/'\nsubdirs = ['train/', 'test/']\nfor subdir in subdirs:\n\t# create label subdirectories\n\tlabeldirs = ['dogs/', 'cats/']\n\tfor labldir in labeldirs:\n\t\tnewdir = dataset_home + subdir + labldir\n\t\tmakedirs(newdir, exist_ok=True)\n# seed random number generator\nseed(1)\n# define ratio of pictures to use for validation\nval_ratio = 0.25\n# copy training dataset images into subdirectories\nsrc_directory = 'train/'\nfor file in listdir(src_directory):\n\tsrc = src_directory + '/' + file\n\tdst_dir = 'train/'\n\tif random() < val_ratio:\n\t\tdst_dir = 'test/'\n\tif file.startswith('cat'):\n\t\tdst = dataset_home + dst_dir + 'cats/'  + file\n\t\tcopyfile(src, dst)\n\telif file.startswith('dog'):\n\t\tdst = dataset_home + dst_dir + 'dogs/'  + file\n\t\tcopyfile(src, dst)","metadata":{"execution":{"iopub.status.busy":"2024-07-18T05:03:52.654116Z","iopub.execute_input":"2024-07-18T05:03:52.654935Z","iopub.status.idle":"2024-07-18T05:03:55.247143Z","shell.execute_reply.started":"2024-07-18T05:03:52.654899Z","shell.execute_reply":"2024-07-18T05:03:55.246009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls -l test1 | wc","metadata":{"execution":{"iopub.status.busy":"2024-07-18T05:04:04.629146Z","iopub.execute_input":"2024-07-18T05:04:04.629519Z","iopub.status.idle":"2024-07-18T05:04:06.039143Z","shell.execute_reply.started":"2024-07-18T05:04:04.629490Z","shell.execute_reply":"2024-07-18T05:04:06.037575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}