{"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","execution":{"iopub.status.busy":"2022-12-02T19:47:48.022606Z","iopub.execute_input":"2022-12-02T19:47:48.0231Z","iopub.status.idle":"2022-12-02T19:47:48.035421Z","shell.execute_reply.started":"2022-12-02T19:47:48.023028Z","shell.execute_reply":"2022-12-02T19:47:48.034231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Create folders for Train, Test and Validation","metadata":{}},{"cell_type":"code","source":"pwd","metadata":{"execution":{"iopub.status.busy":"2022-12-02T19:45:08.973005Z","iopub.execute_input":"2022-12-02T19:45:08.973463Z","iopub.status.idle":"2022-12-02T19:45:09.009261Z","shell.execute_reply.started":"2022-12-02T19:45:08.973366Z","shell.execute_reply":"2022-12-02T19:45:09.008197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir ./test_data","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\n\nbase_dir = './cats_and_dogs'\nos.mkdir(base_dir)\n\ntrain_dir = os.path.join(base_dir, 'train')\ntest_dir = os.path.join(base_dir, 'test')\n\ntrain_cats = os.path.join(train_dir, 'cats')\ntest_cats = os.path.join(test_dir, 'cats')\n\ntrain_dogs = os.path.join(train_dir, 'dogs')\ntest_dogs = os.path.join(test_dir, 'dogs')\n\nfor path_ in [train_dir, test_dir,\n             train_cats, test_cats,\n             train_dogs, test_dogs]:\n    os.mkdir(path_)","metadata":{"execution":{"iopub.status.busy":"2022-12-02T04:34:31.204485Z","iopub.execute_input":"2022-12-02T04:34:31.204933Z","iopub.status.idle":"2022-12-02T04:34:31.21534Z","shell.execute_reply.started":"2022-12-02T04:34:31.204896Z","shell.execute_reply":"2022-12-02T04:34:31.214252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import PIL\nPIL.Image.open('.test/1963.jpg')","metadata":{"execution":{"iopub.status.busy":"2022-12-02T04:51:29.499262Z","iopub.execute_input":"2022-12-02T04:51:29.499679Z","iopub.status.idle":"2022-12-02T04:51:29.529907Z","shell.execute_reply.started":"2022-12-02T04:51:29.499647Z","shell.execute_reply":"2022-12-02T04:51:29.528167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import shutil\n\ndef copyfiles(a,b,c,d): \n  fnames = [a.format(i) for i in range(b,c)]\n  for fname in fnames:\n    src = os.path.join('./train',fname)\n    dst = os.path.join(d, fname)\n    shutil.copyfile(src,dst)\n    \ncopyfiles('cat.{}.jpg',0,11000,train_cats)\ncopyfiles('cat.{}.jpg',11000,12500,test_cats)\ncopyfiles('dog.{}.jpg',0,11000,train_dogs)\ncopyfiles('dog.{}.jpg',11000,12500,test_dogs)","metadata":{"execution":{"iopub.status.busy":"2022-12-02T04:34:39.033003Z","iopub.execute_input":"2022-12-02T04:34:39.033462Z","iopub.status.idle":"2022-12-02T04:34:42.413204Z","shell.execute_reply.started":"2022-12-02T04:34:39.033425Z","shell.execute_reply":"2022-12-02T04:34:42.412227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from tensorflow import keras\n# from keras.preprocessing.image import ImageDataGenerator\n\n# train_datagen = ImageDataGenerator(\n#         rescale=1./255,\n#         validation_split=0.2)\n# train_data=train_datagen.flow_from_directory(\n#         train_dir,\n#         target_size=(250, 250),\n#         batch_size=32,\n#         class_mode='binary',\n#         subset='training')\n\n# test_datagen = ImageDataGenerator(rescale=1./255)\n# test_data = test_datagen.flow_from_directory(\n#         test_dir,\n#         target_size=(250, 250),\n#         batch_size=32,\n#         class_mode='binary')","metadata":{"execution":{"iopub.status.busy":"2022-12-02T03:40:50.705806Z","iopub.execute_input":"2022-12-02T03:40:50.706351Z","iopub.status.idle":"2022-12-02T03:40:51.855813Z","shell.execute_reply.started":"2022-12-02T03:40:50.706311Z","shell.execute_reply":"2022-12-02T03:40:51.853756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\ntrain_data = tf.keras.utils.image_dataset_from_directory(\n    train_dir,\n    labels='inferred',\n    label_mode='binary',\n    image_size=(256, 256),\n    seed=5,\n    validation_split=0.2,\n    subset=\"training\"\n)\nvalidation_data = tf.keras.utils.image_dataset_from_directory(\n    train_dir,\n    labels='inferred',\n    label_mode='binary',\n    image_size=(256, 256),\n    seed=5,\n    validation_split=0.2,\n    subset=\"validation\"\n)","metadata":{"execution":{"iopub.status.busy":"2022-12-02T04:44:52.653963Z","iopub.execute_input":"2022-12-02T04:44:52.65524Z","iopub.status.idle":"2022-12-02T04:44:54.587991Z","shell.execute_reply.started":"2022-12-02T04:44:52.655196Z","shell.execute_reply":"2022-12-02T04:44:54.586843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.layers.pooling import MaxPooling2D\nfrom keras import layers\nfrom keras import models\n\nmodel = models.Sequential()\nmodel.add(layers.Conv2D(32, (3,3), activation='relu', input_shape = (256,256,3)))\nmodel.add(layers.MaxPooling2D(2,2))\nmodel.add(layers.Conv2D(64, (3,3), activation='relu'))\nmodel.add(layers.MaxPooling2D(2,2))\nmodel.add(layers.Conv2D(128, (3,3), activation='relu'))\nmodel.add(layers.MaxPooling2D(2,2))\nmodel.add(layers.Conv2D(128, (3,3), activation='relu'))\nmodel.add(layers.MaxPooling2D(2,2))\nmodel.add(layers.Flatten())\nmodel.add(layers.Dropout(0.5))\nmodel.add(layers.Dense(512, activation = 'relu'))\nmodel.add(layers.Dense(1, activation= 'sigmoid'))\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-12-02T04:52:49.372509Z","iopub.execute_input":"2022-12-02T04:52:49.372937Z","iopub.status.idle":"2022-12-02T04:52:49.564641Z","shell.execute_reply.started":"2022-12-02T04:52:49.372904Z","shell.execute_reply":"2022-12-02T04:52:49.563374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='binary_crossentropy', \n              optimizer='adam',\n              metrics=['acc'])\nhist=model.fit(train_data, \n               epochs=20, \n               steps_per_epoch=100,\n               validation_data=validation_data)","metadata":{"execution":{"iopub.status.busy":"2022-12-02T05:38:12.399881Z","iopub.execute_input":"2022-12-02T05:38:12.400324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.DataFrame()\nnum_test_image = len([name for name in os.listdir('./test')])\nsub['id'] = [i+1 for i in range(num_test_image)]","metadata":{"execution":{"iopub.status.busy":"2022-12-02T03:25:49.087453Z","iopub.execute_input":"2022-12-02T03:25:49.087862Z","iopub.status.idle":"2022-12-02T03:25:49.139131Z","shell.execute_reply.started":"2022-12-02T03:25:49.08783Z","shell.execute_reply":"2022-12-02T03:25:49.137917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# get data for final evaluation\n# test0 = test_datagen.flow_from_directory(\n#         './test0',\n#         target_size=(250, 250),\n#         batch_size=32,\n#         class_mode=None,\n# shuffle=False)\ntest0 = tf.keras.utils.image_dataset_from_directory(\n    './test0',\n    labels=None,\n    label_mode=None,\n    image_size=(256, 256),\n    shuffle=False\n)","metadata":{"execution":{"iopub.status.busy":"2022-12-02T04:41:35.534396Z","iopub.execute_input":"2022-12-02T04:41:35.534815Z","iopub.status.idle":"2022-12-02T04:41:35.576399Z","shell.execute_reply.started":"2022-12-02T04:41:35.534784Z","shell.execute_reply":"2022-12-02T04:41:35.574852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"res=model.predict(test0)\n# sub['label']=res\n# sub.to_csv('dc1.csv', index=False)\nres","metadata":{"execution":{"iopub.status.busy":"2022-12-02T05:38:05.944339Z","iopub.execute_input":"2022-12-02T05:38:05.944744Z","iopub.status.idle":"2022-12-02T05:38:06.868387Z","shell.execute_reply.started":"2022-12-02T05:38:05.944713Z","shell.execute_reply":"2022-12-02T05:38:06.86543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub['label']=res\nsub.to_csv('sub_shuffle_false.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-12-02T04:40:14.099275Z","iopub.execute_input":"2022-12-02T04:40:14.100302Z","iopub.status.idle":"2022-12-02T04:40:14.136158Z","shell.execute_reply.started":"2022-12-02T04:40:14.10025Z","shell.execute_reply":"2022-12-02T04:40:14.134877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"res.","metadata":{},"execution_count":null,"outputs":[]}]}