{"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":"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'):\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","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! unzip -qq \"../input/dogs-vs-cats/test1.zip\"\n! unzip -qq \"../input/dogs-vs-cats/train.zip\"","metadata":{"execution":{"iopub.status.busy":"2023-06-01T12:25:19.063437Z","iopub.execute_input":"2023-06-01T12:25:19.063949Z","iopub.status.idle":"2023-06-01T12:25:39.771429Z","shell.execute_reply.started":"2023-06-01T12:25:19.063908Z","shell.execute_reply":"2023-06-01T12:25:39.770142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":" **PICTURES**","metadata":{}},{"cell_type":"code","source":"import os\nimport numpy as np\nimport random\nimport matplotlib.pyplot as plt\nfilenames = os.listdir('./train/')\n\nfor i in range(2):\n    picture = np.array(plt.imread(os.path.join('./train',random.choice(filenames))))\n    plt.imshow(picture)\n    plt.show()\n    plt.figure()\n","metadata":{"execution":{"iopub.status.busy":"2023-06-01T12:27:11.362600Z","iopub.execute_input":"2023-06-01T12:27:11.363233Z","iopub.status.idle":"2023-06-01T12:27:12.267625Z","shell.execute_reply.started":"2023-06-01T12:27:11.363175Z","shell.execute_reply":"2023-06-01T12:27:12.266360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**CREATING A DATAFRAME**","metadata":{}},{"cell_type":"code","source":"from glob import glob\nimport pandas as pd\n\nfile_names = glob('./train/*.jpg')\ncategories = [1 if 'dog' in pic else 0 for pic in os.listdir(\"./train\")]\n\ndf = pd.DataFrame({'filename': file_names, 'category':categories})\ndf[\"category\"] = df[\"category\"].replace({0: 'cat', 1: 'dog'})\n\nprint(\"shape:\", df.shape)\ndf.head()\n","metadata":{"execution":{"iopub.status.busy":"2023-06-01T12:27:54.349461Z","iopub.execute_input":"2023-06-01T12:27:54.349972Z","iopub.status.idle":"2023-06-01T12:27:54.539176Z","shell.execute_reply.started":"2023-06-01T12:27:54.349935Z","shell.execute_reply":"2023-06-01T12:27:54.538050Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**AMOUNT OF DOGS AND CATS IN THE DATASET**","metadata":{}},{"cell_type":"code","source":"import seaborn as sns\nprint('Number of dogs in the train dataset:',df.category[df.category=='dog'].count())\nprint('Number of cats in the train dataset:',df.category[df.category=='cat'].count())\nsns.histplot(data=df, x=\"category\", shrink=0.8)","metadata":{"execution":{"iopub.status.busy":"2023-06-01T12:28:54.453540Z","iopub.execute_input":"2023-06-01T12:28:54.454106Z","iopub.status.idle":"2023-06-01T12:28:55.946645Z","shell.execute_reply.started":"2023-06-01T12:28:54.454067Z","shell.execute_reply":"2023-06-01T12:28:55.945410Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**SPLIT DATA TRAIN AND VALIDATE**","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\ntrain_df, validate_df = train_test_split(df, test_size=0.2, random_state=10)\ntrain_df = train_df.reset_index(drop=True)\nvalidate_df = validate_df.reset_index(drop=True)\nvalidate_df.head(10)","metadata":{"execution":{"iopub.status.busy":"2023-06-01T12:29:34.334298Z","iopub.execute_input":"2023-06-01T12:29:34.334705Z","iopub.status.idle":"2023-06-01T12:29:34.578530Z","shell.execute_reply.started":"2023-06-01T12:29:34.334675Z","shell.execute_reply":"2023-06-01T12:29:34.577243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**IMAGE PROCESSING**","metadata":{}},{"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator\ntrain_generator = ImageDataGenerator(\nrescale = 1./255,\nrotation_range = 40,\nshear_range = 0.2,\nzoom_range = 0.2,\nhorizontal_flip = True,\nfill_mode = 'nearest'\n)\n\nval_generator = ImageDataGenerator(rescale = 1./255)\n\ntrain_iterator = train_generator.flow_from_dataframe(\ntrain_df,x_col='filename',\ny_col='category',\ntarget_size=(128,128),\nbatch_size=512,\nclass_mode='binary'\n)\n\nval_iterator = val_generator.flow_from_dataframe(\nvalidate_df,x_col='filename',\ny_col='category',\ntarget_size=(128,128),\nbatch_size=512,\nclass_mode='binary'\n)\n","metadata":{"execution":{"iopub.status.busy":"2023-06-01T12:30:03.559708Z","iopub.execute_input":"2023-06-01T12:30:03.560994Z","iopub.status.idle":"2023-06-01T12:30:13.273769Z","shell.execute_reply.started":"2023-06-01T12:30:03.560946Z","shell.execute_reply":"2023-06-01T12:30:13.272446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**MODEL**","metadata":{}},{"cell_type":"code","source":"from keras import Sequential\nfrom keras.layers import Conv2D, MaxPool2D, Flatten, Dense\n\nmodel = Sequential([\n       Conv2D(16, (3,3), activation='relu', input_shape=(128,128,3)),\n       MaxPool2D((2,2)),\n       Conv2D(32, (3,3), activation='relu'),\n       MaxPool2D((2,2)),\n       Conv2D(64, (3,3), activation='relu'),\n       MaxPool2D((2,2)),\n       Flatten(),\n       Dense(512, activation='relu'),\n       Dense(1, activation='sigmoid')\n])","metadata":{"execution":{"iopub.status.busy":"2023-06-01T12:31:17.130115Z","iopub.execute_input":"2023-06-01T12:31:17.131366Z","iopub.status.idle":"2023-06-01T12:31:17.289382Z","shell.execute_reply.started":"2023-06-01T12:31:17.131313Z","shell.execute_reply":"2023-06-01T12:31:17.288071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-06-01T12:32:07.381285Z","iopub.execute_input":"2023-06-01T12:32:07.381722Z","iopub.status.idle":"2023-06-01T12:32:07.445031Z","shell.execute_reply.started":"2023-06-01T12:32:07.381688Z","shell.execute_reply":"2023-06-01T12:32:07.442541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(train_iterator, epochs=10, validation_data=val_iterator)","metadata":{"execution":{"iopub.status.busy":"2023-06-01T12:32:13.270176Z","iopub.execute_input":"2023-06-01T12:32:13.270703Z","iopub.status.idle":"2023-06-01T13:15:57.140770Z","shell.execute_reply.started":"2023-06-01T12:32:13.270667Z","shell.execute_reply":"2023-06-01T13:15:57.139521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**HIGHEST TRAINING ACCURACY WAS 79.38**","metadata":{}},{"cell_type":"markdown","source":"**HIGHEST VALIDATION ACCURACY WAS 77.54**","metadata":{}},{"cell_type":"markdown","source":"**TEST**","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.utils import load_img\nfilenames = os.listdir('./test1/')\nfor i in filenames[200:226]:\n    image_path = \"test1/\" + i\n    img = load_img(image_path, target_size=(128, 128))\n    img = np.array(img)\n    img = img / 255.0\n    img = img.reshape(1, 128, 128, 3)\n\n    picture = np.array(plt.imread((image_path)))\n    plt.imshow(picture)\n    plt.show()\n    plt.figure()\n\n    pred = model.predict(img)\n    if pred[0] > 0.5:\n        label = 'Dog'\n    else:\n        label = 'Cat'\n    print(label)","metadata":{"execution":{"iopub.status.busy":"2023-06-01T13:17:34.004456Z","iopub.execute_input":"2023-06-01T13:17:34.004994Z","iopub.status.idle":"2023-06-01T13:17:46.863742Z","shell.execute_reply.started":"2023-06-01T13:17:34.004958Z","shell.execute_reply":"2023-06-01T13:17:46.862597Z"},"trusted":true},"execution_count":null,"outputs":[]}]}