{"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\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras.models import Sequential \nfrom tensorflow.keras.layers import Activation, Dense, Flatten, BatchNormalization, Conv2D, MaxPool2D\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.metrics import categorical_crossentropy\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom sklearn.metrics import confusion_matrix \nimport itertools\nimport os\nimport shutil\nimport random \nimport glob\nimport matplotlib.pyplot as plt \nimport warnings\nwarnings.simplefilter(action = 'ignore', category = FutureWarning)\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":"2021-09-08T14:27:31.647609Z","iopub.execute_input":"2021-09-08T14:27:31.648262Z","iopub.status.idle":"2021-09-08T14:27:38.319315Z","shell.execute_reply.started":"2021-09-08T14:27:31.648174Z","shell.execute_reply":"2021-09-08T14:27:38.318202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n","metadata":{"execution":{"iopub.status.busy":"2021-09-08T14:32:32.715062Z","iopub.execute_input":"2021-09-08T14:32:32.715394Z","iopub.status.idle":"2021-09-08T14:32:32.720378Z","shell.execute_reply.started":"2021-09-08T14:32:32.715364Z","shell.execute_reply":"2021-09-08T14:32:32.719312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pwd","metadata":{"execution":{"iopub.status.busy":"2021-09-08T14:39:26.775163Z","iopub.execute_input":"2021-09-08T14:39:26.775524Z","iopub.status.idle":"2021-09-08T14:39:27.553026Z","shell.execute_reply.started":"2021-09-08T14:39:26.775477Z","shell.execute_reply":"2021-09-08T14:39:27.551759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nimport zipfile\nwith zipfile.ZipFile(\"/kaggle/input/dogs-vs-cats/train.zip\",'r') as zip_ref:\n    zip_ref.extractall(\".\")\n   \n\n# !cp -r original_directory_path ./\n\n","metadata":{"execution":{"iopub.status.busy":"2021-09-08T14:43:20.767814Z","iopub.execute_input":"2021-09-08T14:43:20.768183Z","iopub.status.idle":"2021-09-08T14:43:27.696374Z","shell.execute_reply.started":"2021-09-08T14:43:20.768154Z","shell.execute_reply":"2021-09-08T14:43:27.695613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.chdir(\"data/\")","metadata":{"execution":{"iopub.status.busy":"2021-09-08T14:46:56.446526Z","iopub.execute_input":"2021-09-08T14:46:56.446896Z","iopub.status.idle":"2021-09-08T14:46:56.451427Z","shell.execute_reply.started":"2021-09-08T14:46:56.446861Z","shell.execute_reply":"2021-09-08T14:46:56.450526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pwd","metadata":{"execution":{"iopub.status.busy":"2021-09-08T14:47:03.469784Z","iopub.execute_input":"2021-09-08T14:47:03.470203Z","iopub.status.idle":"2021-09-08T14:47:04.256066Z","shell.execute_reply.started":"2021-09-08T14:47:03.470167Z","shell.execute_reply":"2021-09-08T14:47:04.254816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if os.path.isdir('train/dog') is False: \n  os.makedirs('train/dog')\n  os.makedirs('train/cat')\n  os.makedirs('valid/dog')\n  os.makedirs('valid/cat')\n  os.makedirs('test/dog')\n  os.makedirs('test/cat')\n# train = pd.read_csv('/kaggle/input/dogs-vs-cats/train.zip')","metadata":{"execution":{"iopub.status.busy":"2021-09-08T14:47:08.404759Z","iopub.execute_input":"2021-09-08T14:47:08.405143Z","iopub.status.idle":"2021-09-08T14:47:08.411696Z","shell.execute_reply.started":"2021-09-08T14:47:08.405107Z","shell.execute_reply":"2021-09-08T14:47:08.410898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for c in random.sample(glob.glob('cat*'), 500): \n    shutil.move(c, 'train/cat')\nfor c in random.sample(glob.glob('dog*'), 500): \n    shutil.move(c, 'train/dog')\nfor c in random.sample(glob.glob('cat*'), 100): \n    shutil.move(c, 'valid/cat')\nfor c in random.sample(glob.glob('dog*'), 100): \n    shutil.move(c, 'valid/dog')\nfor c in random.sample(glob.glob('cat*'), 50): \n    shutil.move(c, 'test/cat')\nfor c in random.sample(glob.glob('dog*'), 50): \n    shutil.move(c, 'test/dog')","metadata":{"execution":{"iopub.status.busy":"2021-09-08T14:49:48.716378Z","iopub.execute_input":"2021-09-08T14:49:48.716755Z","iopub.status.idle":"2021-09-08T14:49:48.953039Z","shell.execute_reply.started":"2021-09-08T14:49:48.71672Z","shell.execute_reply":"2021-09-08T14:49:48.952002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pwd\n","metadata":{"execution":{"iopub.status.busy":"2021-09-08T14:50:50.321175Z","iopub.execute_input":"2021-09-08T14:50:50.321572Z","iopub.status.idle":"2021-09-08T14:50:51.102223Z","shell.execute_reply.started":"2021-09-08T14:50:50.321495Z","shell.execute_reply":"2021-09-08T14:50:51.101007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path = 'train/'\ntest_path = 'test/'\nvalid_path =  'valid/'","metadata":{"execution":{"iopub.status.busy":"2021-09-08T14:53:51.751318Z","iopub.execute_input":"2021-09-08T14:53:51.751667Z","iopub.status.idle":"2021-09-08T14:53:51.756295Z","shell.execute_reply.started":"2021-09-08T14:53:51.751638Z","shell.execute_reply":"2021-09-08T14:53:51.754853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_batches = ImageDataGenerator(preprocessing_function=tf.keras.applications.vgg16.preprocess_input).flow_from_directory(directory=train_path, target_size=(224,224), classes=[\"cat\", \"dog\"], batch_size=10)\nvalid_batches  = ImageDataGenerator(preprocessing_function=tf.keras.applications.vgg16.preprocess_input).flow_from_directory(directory=valid_path, target_size=(224,224), classes=[\"cat\", \"dog\"], batch_size=10)\ntest_batches = ImageDataGenerator(preprocessing_function=tf.keras.applications.vgg16.preprocess_input).flow_from_directory(directory=test_path, target_size=(224,224), classes=[\"cat\", \"dog\"], batch_size=10, shuffle = False)","metadata":{"execution":{"iopub.status.busy":"2021-09-08T14:53:54.204676Z","iopub.execute_input":"2021-09-08T14:53:54.205006Z","iopub.status.idle":"2021-09-08T14:53:54.523243Z","shell.execute_reply.started":"2021-09-08T14:53:54.204979Z","shell.execute_reply":"2021-09-08T14:53:54.522103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imgs, labels = next(train_batches)","metadata":{"execution":{"iopub.status.busy":"2021-09-08T14:54:16.919346Z","iopub.execute_input":"2021-09-08T14:54:16.919727Z","iopub.status.idle":"2021-09-08T14:54:16.965241Z","shell.execute_reply.started":"2021-09-08T14:54:16.919691Z","shell.execute_reply":"2021-09-08T14:54:16.964257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# this function will plot images in the form of a grid with 1 row and 10 columns where images are placed\ndef plotImages(images_arr): \n    fig, axes = plt.subplots(1, 10, figsize =  (20, 20))\n    axes = axes.flatten()\n    for img, ax in zip(images_arr, axes ): \n        ax.imshow(img)\n        ax.axis(\"off\")\n    plt.tight_layout()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-09-08T14:54:34.280882Z","iopub.execute_input":"2021-09-08T14:54:34.281225Z","iopub.status.idle":"2021-09-08T14:54:34.286802Z","shell.execute_reply.started":"2021-09-08T14:54:34.281195Z","shell.execute_reply":"2021-09-08T14:54:34.285645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plotImages(imgs)\nprint(labels)\n# this is what a batch of training data looks like ","metadata":{"execution":{"iopub.status.busy":"2021-09-08T14:54:47.97026Z","iopub.execute_input":"2021-09-08T14:54:47.970756Z","iopub.status.idle":"2021-09-08T14:54:48.627188Z","shell.execute_reply.started":"2021-09-08T14:54:47.970725Z","shell.execute_reply":"2021-09-08T14:54:48.626531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential([ \n    Conv2D(filters=32, kernel_size = (3,3), activation=\"relu\", padding=\"same\", input_shape=(224,224,3)),\n    MaxPool2D(pool_size=(2,2), strides = 2), \n    Conv2D(filters=64, kernel_size = (3,3), activation = \"relu\", padding = \"same\"), \n    MaxPool2D(pool_size=(2,2), strides = 2), \n    Flatten(), # flattened to one dimensional tensor\n    Dense(units=2, activation = \"softmax\"), \n])","metadata":{"execution":{"iopub.status.busy":"2021-09-08T14:55:00.343717Z","iopub.execute_input":"2021-09-08T14:55:00.344222Z","iopub.status.idle":"2021-09-08T14:55:00.504994Z","shell.execute_reply.started":"2021-09-08T14:55:00.34419Z","shell.execute_reply":"2021-09-08T14:55:00.504087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-09-08T14:55:11.471976Z","iopub.execute_input":"2021-09-08T14:55:11.472379Z","iopub.status.idle":"2021-09-08T14:55:11.764335Z","shell.execute_reply.started":"2021-09-08T14:55:11.472343Z","shell.execute_reply":"2021-09-08T14:55:11.763431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer = Adam(learning_rate = 0.0001), loss = \"categorical_crossentropy\", metrics=[\"accuracy\"]) ","metadata":{"execution":{"iopub.status.busy":"2021-09-08T14:55:29.536354Z","iopub.execute_input":"2021-09-08T14:55:29.537036Z","iopub.status.idle":"2021-09-08T14:55:29.552991Z","shell.execute_reply.started":"2021-09-08T14:55:29.536979Z","shell.execute_reply":"2021-09-08T14:55:29.551808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(x=train_batches, validation_data=valid_batches, epochs= 10, verbose=2)\n","metadata":{"execution":{"iopub.status.busy":"2021-09-08T14:55:39.77884Z","iopub.execute_input":"2021-09-08T14:55:39.779202Z","iopub.status.idle":"2021-09-08T15:09:27.07505Z","shell.execute_reply.started":"2021-09-08T14:55:39.779172Z","shell.execute_reply":"2021-09-08T15:09:27.074224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_imgs, test_labels = next(test_batches)\nplotImages(test_imgs)\nprint(test_labels) \n# all pics in batch will be same (like all cats or all dogs) because \n# we specified that we do not want to shuffle it earlier ","metadata":{"execution":{"iopub.status.busy":"2021-09-08T15:09:37.121653Z","iopub.execute_input":"2021-09-08T15:09:37.122293Z","iopub.status.idle":"2021-09-08T15:09:37.755053Z","shell.execute_reply.started":"2021-09-08T15:09:37.122254Z","shell.execute_reply":"2021-09-08T15:09:37.754255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_batches.classes","metadata":{"execution":{"iopub.status.busy":"2021-09-08T15:09:41.016835Z","iopub.execute_input":"2021-09-08T15:09:41.017443Z","iopub.status.idle":"2021-09-08T15:09:41.02619Z","shell.execute_reply.started":"2021-09-08T15:09:41.017393Z","shell.execute_reply":"2021-09-08T15:09:41.025154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(x=test_batches, verbose=0)","metadata":{"execution":{"iopub.status.busy":"2021-09-08T15:09:45.735101Z","iopub.execute_input":"2021-09-08T15:09:45.735455Z","iopub.status.idle":"2021-09-08T15:09:48.283117Z","shell.execute_reply.started":"2021-09-08T15:09:45.735424Z","shell.execute_reply":"2021-09-08T15:09:48.282088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.round(predictions)","metadata":{"execution":{"iopub.status.busy":"2021-09-08T15:09:56.217673Z","iopub.execute_input":"2021-09-08T15:09:56.21805Z","iopub.status.idle":"2021-09-08T15:09:56.228976Z","shell.execute_reply.started":"2021-09-08T15:09:56.218018Z","shell.execute_reply":"2021-09-08T15:09:56.22786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# confusion matrix can be created","metadata":{},"execution_count":null,"outputs":[]}]}