{"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":"import pandas as pd\nimport numpy as np\nimport os\nimport zipfile\nimport random\nimport tensorflow as tf\nfrom tensorflow.keras.optimizers import RMSprop\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom shutil import copyfile, move, rmtree","metadata":{"execution":{"iopub.status.busy":"2021-08-24T11:27:53.944162Z","iopub.execute_input":"2021-08-24T11:27:53.944591Z","iopub.status.idle":"2021-08-24T11:27:58.029508Z","shell.execute_reply.started":"2021-08-24T11:27:53.944507Z","shell.execute_reply":"2021-08-24T11:27:58.028661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Data Preparation","metadata":{}},{"cell_type":"code","source":"!unzip /kaggle/input/dogs-vs-cats/train.zip","metadata":{"execution":{"iopub.status.busy":"2021-08-24T11:27:58.030852Z","iopub.execute_input":"2021-08-24T11:27:58.031164Z","iopub.status.idle":"2021-08-24T11:28:09.879402Z","shell.execute_reply.started":"2021-08-24T11:27:58.031133Z","shell.execute_reply":"2021-08-24T11:28:09.878506Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(os.listdir('/kaggle/working/train'))","metadata":{"execution":{"iopub.status.busy":"2021-08-24T11:28:15.123861Z","iopub.execute_input":"2021-08-24T11:28:15.124199Z","iopub.status.idle":"2021-08-24T11:28:15.147608Z","shell.execute_reply.started":"2021-08-24T11:28:15.124163Z","shell.execute_reply":"2021-08-24T11:28:15.146628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir('/kaggle/working/train')[:5]","metadata":{"execution":{"iopub.status.busy":"2021-08-24T11:28:16.110485Z","iopub.execute_input":"2021-08-24T11:28:16.110819Z","iopub.status.idle":"2021-08-24T11:28:16.132364Z","shell.execute_reply.started":"2021-08-24T11:28:16.110791Z","shell.execute_reply":"2021-08-24T11:28:16.131343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_directory = '/kaggle/working/pet-images'\ncat_directory = os.path.join(base_directory, 'cats')\ndog_directory = os.path.join(base_directory, 'dogs')\n\ntry:\n    os.mkdir(base_directory)\n    os.mkdir(cat_directory)\n    os.mkdir(dog_directory)\nexcept:\n    pass","metadata":{"execution":{"iopub.status.busy":"2021-08-24T11:28:17.240664Z","iopub.execute_input":"2021-08-24T11:28:17.240993Z","iopub.status.idle":"2021-08-24T11:28:17.246179Z","shell.execute_reply.started":"2021-08-24T11:28:17.240963Z","shell.execute_reply":"2021-08-24T11:28:17.245156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir('/kaggle/working/pet-images')","metadata":{"execution":{"iopub.status.busy":"2021-08-24T11:28:18.826375Z","iopub.execute_input":"2021-08-24T11:28:18.826699Z","iopub.status.idle":"2021-08-24T11:28:18.833324Z","shell.execute_reply.started":"2021-08-24T11:28:18.826667Z","shell.execute_reply":"2021-08-24T11:28:18.83239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"source = '/kaggle/working/train'\n\nfor image in os.listdir(source):\n    if image.split('.')[0] == 'cat':\n        move(os.path.join(source, image), os.path.join(cat_directory, image))\n    else:\n        move(os.path.join(source, image), os.path.join(dog_directory, image))","metadata":{"execution":{"iopub.status.busy":"2021-08-24T11:28:19.790917Z","iopub.execute_input":"2021-08-24T11:28:19.791273Z","iopub.status.idle":"2021-08-24T11:28:20.580333Z","shell.execute_reply.started":"2021-08-24T11:28:19.791223Z","shell.execute_reply":"2021-08-24T11:28:20.5792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(os.listdir('/kaggle/working/train'))","metadata":{"execution":{"iopub.status.busy":"2021-08-24T11:28:22.171073Z","iopub.execute_input":"2021-08-24T11:28:22.171427Z","iopub.status.idle":"2021-08-24T11:28:22.177046Z","shell.execute_reply.started":"2021-08-24T11:28:22.171396Z","shell.execute_reply":"2021-08-24T11:28:22.176216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f'# of cat images: {len(os.listdir(cat_directory))}')\nprint(f'# of dog images: {len(os.listdir(dog_directory))}')","metadata":{"execution":{"iopub.status.busy":"2021-08-24T11:28:23.13885Z","iopub.execute_input":"2021-08-24T11:28:23.139293Z","iopub.status.idle":"2021-08-24T11:28:23.170081Z","shell.execute_reply.started":"2021-08-24T11:28:23.139216Z","shell.execute_reply":"2021-08-24T11:28:23.169331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"try:\n    os.mkdir(\"/kaggle/working/cats-vs-dogs/\")\n    os.mkdir(\"/kaggle/working/cats-vs-dogs/training/\")\n    os.mkdir(\"/kaggle/working/cats-vs-dogs/training/cats/\")\n    os.mkdir(\"/kaggle/working/cats-vs-dogs/training/dogs/\")\n    os.mkdir(\"/kaggle/working/cats-vs-dogs/validation/\")\n    os.mkdir(\"/kaggle/working/cats-vs-dogs/validation/cats/\")\n    os.mkdir(\"/kaggle/working/cats-vs-dogs/validation/dogs\")\nexcept OSError:\n    pass","metadata":{"execution":{"iopub.status.busy":"2021-08-24T11:28:23.937656Z","iopub.execute_input":"2021-08-24T11:28:23.938078Z","iopub.status.idle":"2021-08-24T11:28:23.943577Z","shell.execute_reply.started":"2021-08-24T11:28:23.938038Z","shell.execute_reply":"2021-08-24T11:28:23.942409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(os.listdir('/kaggle/working/cats-vs-dogs'))\nprint(os.listdir('/kaggle/working/cats-vs-dogs/training'))\nprint(os.listdir('/kaggle/working/cats-vs-dogs/validation'))","metadata":{"execution":{"iopub.status.busy":"2021-08-24T11:28:24.962436Z","iopub.execute_input":"2021-08-24T11:28:24.962753Z","iopub.status.idle":"2021-08-24T11:28:24.967932Z","shell.execute_reply.started":"2021-08-24T11:28:24.962725Z","shell.execute_reply":"2021-08-24T11:28:24.967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def split_data(SOURCE, TRAINING, VALIDATION, SPLIT_SIZE):\n    source_files = os.listdir(SOURCE)\n    randomized_source_files = random.sample(source_files, len(source_files))\n\n    train_split = randomized_source_files[ :int(SPLIT_SIZE * len(source_files))]\n    valid_split = randomized_source_files[int(SPLIT_SIZE * len(source_files)): ]\n\n    for file in train_split:\n        if os.path.getsize(os.path.join(SOURCE, file)) != 0:\n            copyfile(os.path.join(SOURCE, file), os.path.join(TRAINING, file))\n        else:\n            print(f\"{file} has size 0, so ignoring\")\n    \n    for file in valid_split:\n        if os.path.getsize(os.path.join(SOURCE, file)) != 0:\n            copyfile(os.path.join(SOURCE, file), os.path.join(VALIDATION, file))\n        else:\n            print(f\"{file} has size 0, so ignoring\")","metadata":{"execution":{"iopub.status.busy":"2021-08-24T11:28:25.849017Z","iopub.execute_input":"2021-08-24T11:28:25.84939Z","iopub.status.idle":"2021-08-24T11:28:25.858062Z","shell.execute_reply.started":"2021-08-24T11:28:25.849355Z","shell.execute_reply":"2021-08-24T11:28:25.856684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"CAT_SOURCE_DIR = cat_directory\nTRAINING_CATS_DIR = \"/kaggle/working/cats-vs-dogs/training/cats/\"\nVALIDATION_CATS_DIR = \"/kaggle/working/cats-vs-dogs/validation/cats/\"\n\nDOG_SOURCE_DIR = dog_directory\nTRAINING_DOGS_DIR = \"/kaggle/working/cats-vs-dogs/training/dogs/\"\nVALIDATION_DOGS_DIR = \"/kaggle/working/cats-vs-dogs/validation/dogs/\"\n\nsplit_size = .9\n\nsplit_data(CAT_SOURCE_DIR, TRAINING_CATS_DIR, VALIDATION_CATS_DIR, split_size)\nsplit_data(DOG_SOURCE_DIR, TRAINING_DOGS_DIR, VALIDATION_DOGS_DIR, split_size)","metadata":{"execution":{"iopub.status.busy":"2021-08-24T11:28:27.621757Z","iopub.execute_input":"2021-08-24T11:28:27.622103Z","iopub.status.idle":"2021-08-24T11:28:30.406803Z","shell.execute_reply.started":"2021-08-24T11:28:27.622071Z","shell.execute_reply":"2021-08-24T11:28:30.405918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"# of cats in train directory\", len(os.listdir(TRAINING_CATS_DIR)))\nprint(\"# of cats in validation directory\", len(os.listdir(VALIDATION_CATS_DIR)))\nprint(\"# of dogs in train directory\", len(os.listdir(TRAINING_DOGS_DIR)))\nprint(\"# of dogs in validation directory\", len(os.listdir(VALIDATION_DOGS_DIR)))","metadata":{"execution":{"iopub.status.busy":"2021-08-24T11:28:30.994626Z","iopub.execute_input":"2021-08-24T11:28:30.994941Z","iopub.status.idle":"2021-08-24T11:28:31.015694Z","shell.execute_reply.started":"2021-08-24T11:28:30.994913Z","shell.execute_reply":"2021-08-24T11:28:31.014758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Preparing the Data Generators","metadata":{}},{"cell_type":"code","source":"TRAINING_DIR = \"/kaggle/working/cats-vs-dogs/training/\"\n\ntrain_datagen = ImageDataGenerator(\n    rescale = 1./255,\n    rotation_range = 40,\n    width_shift_range = 0.2,\n    height_shift_range = 0.2,\n    shear_range = 0.2,\n    zoom_range = 0.2,\n    horizontal_flip = True,\n    fill_mode = 'nearest'\n)\n\ntrain_generator = train_datagen.flow_from_directory(\n    TRAINING_DIR,\n    target_size = (150,150),\n    batch_size = 128,\n    class_mode = 'binary'\n)\n\nVALIDATION_DIR = \"/kaggle/working/cats-vs-dogs/validation/\"\n\nvalidation_datagen = ImageDataGenerator(rescale = 1./255)\n\nvalidation_generator = validation_datagen.flow_from_directory(\n    VALIDATION_DIR,\n    target_size = (150,150),\n    batch_size = 128,\n    class_mode = 'binary'\n)","metadata":{"execution":{"iopub.status.busy":"2021-08-24T11:28:35.317796Z","iopub.execute_input":"2021-08-24T11:28:35.318116Z","iopub.status.idle":"2021-08-24T11:28:35.980915Z","shell.execute_reply.started":"2021-08-24T11:28:35.318086Z","shell.execute_reply":"2021-08-24T11:28:35.980146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Building Model from Scratch","metadata":{}},{"cell_type":"code","source":"model = tf.keras.models.Sequential([\n  tf.keras.layers.Conv2D(16, (3,3), activation='relu', input_shape=(150,150,3)),\n  tf.keras.layers.MaxPooling2D(2,2),\n  tf.keras.layers.Conv2D(32, (3,3), activation='relu'),\n  tf.keras.layers.MaxPooling2D(2,2),\n  tf.keras.layers.Conv2D(64, (3,3), activation='relu'),\n  tf.keras.layers.MaxPooling2D(2,2),\n  tf.keras.layers.Flatten(),\n  tf.keras.layers.Dense(512, activation='relu'),\n  tf.keras.layers.Dense(1, activation='sigmoid')\n])\n\nmodel.compile(optimizer=RMSprop(lr=0.001), loss='binary_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-08-24T11:28:38.908441Z","iopub.execute_input":"2021-08-24T11:28:38.908751Z","iopub.status.idle":"2021-08-24T11:28:40.768357Z","shell.execute_reply.started":"2021-08-24T11:28:38.908723Z","shell.execute_reply":"2021-08-24T11:28:40.767446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-08-24T11:28:40.827189Z","iopub.execute_input":"2021-08-24T11:28:40.827512Z","iopub.status.idle":"2021-08-24T11:28:40.836545Z","shell.execute_reply.started":"2021-08-24T11:28:40.827485Z","shell.execute_reply":"2021-08-24T11:28:40.83565Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class myCallback(tf.keras.callbacks.Callback):\n    def on_epoch_end(self,epoch,logs=None):\n        if (logs.get('val_accuracy') > 0.96):\n            print('validation Accuracy reached 96% , so Stop now')\n            self.model.stop_training = True","metadata":{"execution":{"iopub.status.busy":"2021-08-24T11:28:45.275472Z","iopub.execute_input":"2021-08-24T11:28:45.275884Z","iopub.status.idle":"2021-08-24T11:28:45.287136Z","shell.execute_reply.started":"2021-08-24T11:28:45.275851Z","shell.execute_reply":"2021-08-24T11:28:45.286379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(\n            train_generator,\n            epochs=5,\n            verbose=1,\n            validation_data=validation_generator,\n            callbacks=[myCallback()])","metadata":{"execution":{"iopub.status.busy":"2021-08-24T11:28:47.669418Z","iopub.execute_input":"2021-08-24T11:28:47.669745Z","iopub.status.idle":"2021-08-24T11:43:10.372208Z","shell.execute_reply.started":"2021-08-24T11:28:47.669717Z","shell.execute_reply":"2021-08-24T11:43:10.371347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%matplotlib inline\n\nimport matplotlib.image  as mpimg\nimport matplotlib.pyplot as plt\n\nacc=history.history['accuracy']\nval_acc=history.history['val_accuracy']\nloss=history.history['loss']\nval_loss=history.history['val_loss']\n\nepochs=range(len(acc)) # Get number of epochs\n\nplt.plot(epochs, acc, 'r', \"Training Accuracy\")\nplt.plot(epochs, val_acc, 'b', \"Validation Accuracy\")\nplt.title('Training and validation accuracy')\nplt.figure()\n\nplt.plot(epochs, loss, 'r', \"Training Loss\")\nplt.plot(epochs, val_loss, 'b', \"Validation Loss\")\n\n\nplt.title('Training and validation loss')","metadata":{"execution":{"iopub.status.busy":"2021-08-24T11:44:13.183791Z","iopub.execute_input":"2021-08-24T11:44:13.184127Z","iopub.status.idle":"2021-08-24T11:44:13.215523Z","shell.execute_reply.started":"2021-08-24T11:44:13.184094Z","shell.execute_reply":"2021-08-24T11:44:13.214036Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Using Transfer Learning","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.applications.inception_v3 import InceptionV3","metadata":{"execution":{"iopub.status.busy":"2021-08-24T11:46:24.589136Z","iopub.execute_input":"2021-08-24T11:46:24.589503Z","iopub.status.idle":"2021-08-24T11:46:24.59309Z","shell.execute_reply.started":"2021-08-24T11:46:24.589469Z","shell.execute_reply":"2021-08-24T11:46:24.592301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pre_trained_model = InceptionV3(input_shape=(150,150,3),\n                               include_top=False,\n                               weights='imagenet')\n\nfor layer in pre_trained_model.layers:\n    layer.trainable = False","metadata":{"execution":{"iopub.status.busy":"2021-08-24T11:46:45.086075Z","iopub.execute_input":"2021-08-24T11:46:45.086447Z","iopub.status.idle":"2021-08-24T11:46:47.627958Z","shell.execute_reply.started":"2021-08-24T11:46:45.086417Z","shell.execute_reply":"2021-08-24T11:46:47.627115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pre_trained_model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-08-24T11:47:07.171852Z","iopub.execute_input":"2021-08-24T11:47:07.172184Z","iopub.status.idle":"2021-08-24T11:47:07.308787Z","shell.execute_reply.started":"2021-08-24T11:47:07.172143Z","shell.execute_reply":"2021-08-24T11:47:07.303912Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"last_layer = pre_trained_model.get_layer('mixed7')\nlast_output = last_layer.output","metadata":{"execution":{"iopub.status.busy":"2021-08-24T11:47:51.223177Z","iopub.execute_input":"2021-08-24T11:47:51.22356Z","iopub.status.idle":"2021-08-24T11:47:51.228206Z","shell.execute_reply.started":"2021-08-24T11:47:51.223526Z","shell.execute_reply":"2021-08-24T11:47:51.22682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = tf.keras.layers.Flatten()(last_output)\nx = tf.keras.layers.Dense(256,activation='relu')(x)\nx = tf.keras.layers.Dropout(0.2)(x)\nx = tf.keras.layers.Dense(1,activation='sigmoid')(x)\n\nmodel2 = tf.keras.Model(pre_trained_model.input,x)\nmodel2.compile(optimizer=RMSprop(lr=0.001), loss='binary_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-08-24T11:51:15.065267Z","iopub.execute_input":"2021-08-24T11:51:15.065592Z","iopub.status.idle":"2021-08-24T11:51:15.112222Z","shell.execute_reply.started":"2021-08-24T11:51:15.065564Z","shell.execute_reply":"2021-08-24T11:51:15.111403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model2.summary()","metadata":{"execution":{"iopub.status.busy":"2021-08-24T11:51:18.294592Z","iopub.execute_input":"2021-08-24T11:51:18.294917Z","iopub.status.idle":"2021-08-24T11:51:18.389669Z","shell.execute_reply.started":"2021-08-24T11:51:18.294886Z","shell.execute_reply":"2021-08-24T11:51:18.388877Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history2 = model2.fit(\n            train_generator,\n            epochs=5,\n            verbose=1,\n            validation_data=validation_generator,\n            callbacks=[myCallback()])","metadata":{"execution":{"iopub.status.busy":"2021-08-24T11:52:13.118448Z","iopub.execute_input":"2021-08-24T11:52:13.118946Z","iopub.status.idle":"2021-08-24T11:55:12.955821Z","shell.execute_reply.started":"2021-08-24T11:52:13.118896Z","shell.execute_reply":"2021-08-24T11:55:12.955004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pre_trained_model.trainable = True\n\nmodel2.compile(optimizer = RMSprop(learning_rate=0.0001), loss = 'binary_crossentropy', metrics = ['accuracy'])\n\nmodel2.summary()","metadata":{"execution":{"iopub.status.busy":"2021-08-24T12:03:21.35253Z","iopub.execute_input":"2021-08-24T12:03:21.352862Z","iopub.status.idle":"2021-08-24T12:03:21.475981Z","shell.execute_reply.started":"2021-08-24T12:03:21.352832Z","shell.execute_reply":"2021-08-24T12:03:21.475184Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history2 = model2.fit(\n            train_generator,\n            epochs=5,\n            verbose=1,\n            validation_data=validation_generator,\n            callbacks=[myCallback()])","metadata":{"execution":{"iopub.status.busy":"2021-08-24T12:04:24.441211Z","iopub.execute_input":"2021-08-24T12:04:24.441605Z","iopub.status.idle":"2021-08-24T12:07:36.029889Z","shell.execute_reply.started":"2021-08-24T12:04:24.441574Z","shell.execute_reply":"2021-08-24T12:07:36.029149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Testing","metadata":{}},{"cell_type":"code","source":"import PIL\nfrom PIL import Image","metadata":{"execution":{"iopub.status.busy":"2021-08-24T12:10:38.030609Z","iopub.execute_input":"2021-08-24T12:10:38.030939Z","iopub.status.idle":"2021-08-24T12:10:38.035205Z","shell.execute_reply.started":"2021-08-24T12:10:38.030909Z","shell.execute_reply":"2021-08-24T12:10:38.034163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cat1 = Image.open('/kaggle/input/pettest/leia.jpg')\ncat1 = cat1.resize((150, 150))\ncat2 = Image.open('/kaggle/input/pettest/luke.jpg')\ncat2 = cat2.resize((150, 150))\ndog1 = Image.open('/kaggle/input/pettest/marley.jpg')\ndog1 = dog1.resize((150, 150))","metadata":{"execution":{"iopub.status.busy":"2021-08-24T12:20:12.858578Z","iopub.execute_input":"2021-08-24T12:20:12.858915Z","iopub.status.idle":"2021-08-24T12:20:12.983217Z","shell.execute_reply.started":"2021-08-24T12:20:12.858883Z","shell.execute_reply":"2021-08-24T12:20:12.982375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"source = '/kaggle/input/pettest'\ndestination = '/kaggle/working/test/'\ncat_destination = '/kaggle/working/test/cat'\ndog_destination = '/kaggle/working/test/dog'\n\ntry:\n    os.mkdir(destination)\n    os.mkdir(cat_destination)\n    os.mkdir(dog_destination)\nexcept:\n    pass\n\nfor image in os.listdir(source):\n    if image.split('.')[0] == 'marley':\n        copyfile(os.path.join(source, image), os.path.join(dog_destination, image))\n    else:\n        copyfile(os.path.join(source, image), os.path.join(cat_destination, image))","metadata":{"execution":{"iopub.status.busy":"2021-08-24T12:38:50.767209Z","iopub.execute_input":"2021-08-24T12:38:50.76756Z","iopub.status.idle":"2021-08-24T12:38:50.787132Z","shell.execute_reply.started":"2021-08-24T12:38:50.76753Z","shell.execute_reply":"2021-08-24T12:38:50.786405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_datagen = ImageDataGenerator(rescale = 1./255)\n\ndestination = '/kaggle/working/test'\n\ntest_generator = test_datagen.flow_from_directory(destination,\n                                                  target_size=(150,150),\n                                                  batch_size=3,\n                                                  class_mode='binary')\nx, y = next(test_generator)","metadata":{"execution":{"iopub.status.busy":"2021-08-24T12:46:50.08552Z","iopub.execute_input":"2021-08-24T12:46:50.085898Z","iopub.status.idle":"2021-08-24T12:46:50.275943Z","shell.execute_reply.started":"2021-08-24T12:46:50.085864Z","shell.execute_reply":"2021-08-24T12:46:50.274978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model2.predict(x)\npredicted_val = [int(round(p[0])) for p in predictions]","metadata":{"execution":{"iopub.status.busy":"2021-08-24T12:53:06.427096Z","iopub.execute_input":"2021-08-24T12:53:06.427447Z","iopub.status.idle":"2021-08-24T12:53:07.23217Z","shell.execute_reply.started":"2021-08-24T12:53:06.427416Z","shell.execute_reply":"2021-08-24T12:53:07.231311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(3):\n    image = x[i]\n    plt.imshow(image)\n    print(\"Cat\") if predicted_val[i] == 0 else print(\"Dog\")\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-08-24T12:53:08.834076Z","iopub.execute_input":"2021-08-24T12:53:08.834463Z","iopub.status.idle":"2021-08-24T12:53:09.345508Z","shell.execute_reply.started":"2021-08-24T12:53:08.834428Z","shell.execute_reply":"2021-08-24T12:53:09.344719Z"},"trusted":true},"execution_count":null,"outputs":[]}]}