{"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-03-31T23:12:24.44761Z","iopub.execute_input":"2022-03-31T23:12:24.448507Z","iopub.status.idle":"2022-03-31T23:12:24.478694Z","shell.execute_reply.started":"2022-03-31T23:12:24.448391Z","shell.execute_reply":"2022-03-31T23:12:24.477938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Overview\nThis notebook uses a modified version of Inception V3 to distinguish between photos of dogs and cats.\n\nTraining accuracy after a few epochs is about 99% while validation accuracy is around 96% so there is some overfitting going on.\n\nThis notebook must be run with GPU acceleration because of the inclusion of the large Inception network.\n\nImage preprocessing consists of resizing to 150 x 150 and rescaling the pixel values by dividing by 255.  Resizing happens before training while pixel rescaling happens on-the-fly during training.  Performing resizing during training increases the load on the CPU during training (for every epoch), which is why it is done before training (only once).\n\nIt does not matter much as the above mentioned accuracy figures are already achieved after only a few epochs, which only take a few minutes of training time.\n\nThe CPU still bottlenecks training.  This is likely caused by the repetitive fetching of data and training of the data.","metadata":{}},{"cell_type":"code","source":"%matplotlib inline\n\nimport zipfile\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nimport shutil\n\nimport tensorflow as tf\nfrom tensorflow.keras import utils\nimport keras_preprocessing\nfrom keras_preprocessing import image\nfrom keras_preprocessing.image import ImageDataGenerator\n\nfrom tensorflow.keras.applications.inception_v3 import InceptionV3\nfrom tensorflow.keras import layers\nfrom tensorflow.keras import Model\nfrom tensorflow.keras.optimizers import RMSprop","metadata":{"execution":{"iopub.status.busy":"2022-03-31T23:12:24.480354Z","iopub.execute_input":"2022-03-31T23:12:24.480822Z","iopub.status.idle":"2022-03-31T23:12:29.393547Z","shell.execute_reply.started":"2022-03-31T23:12:24.480785Z","shell.execute_reply":"2022-03-31T23:12:29.392784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Check for availability and use of GPU.","metadata":{}},{"cell_type":"code","source":"!nvidia-smi\nprint(tf.test.is_gpu_available())\nprint(tf.test.gpu_device_name())\nprint(tf.config.experimental.list_physical_devices(device_type='GPU'))","metadata":{"execution":{"iopub.status.busy":"2022-03-31T23:12:29.394974Z","iopub.execute_input":"2022-03-31T23:12:29.395242Z","iopub.status.idle":"2022-03-31T23:12:31.964951Z","shell.execute_reply.started":"2022-03-31T23:12:29.395206Z","shell.execute_reply":"2022-03-31T23:12:31.963759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Empty the /kaggle/working directory so you can run this notebook multiple times.","metadata":{}},{"cell_type":"code","source":"working_dir = \"/kaggle/working/\"\nfor rootdir, dirs, files in os.walk(working_dir):\n    for subdir in dirs:\n        shutil.rmtree(subdir)","metadata":{"execution":{"iopub.status.busy":"2022-03-31T23:12:31.967339Z","iopub.execute_input":"2022-03-31T23:12:31.968117Z","iopub.status.idle":"2022-03-31T23:12:31.972903Z","shell.execute_reply.started":"2022-03-31T23:12:31.968073Z","shell.execute_reply":"2022-03-31T23:12:31.972101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Unzip the training set in the working directory.","metadata":{}},{"cell_type":"code","source":"input_dir = \"/kaggle/input/dogs-vs-cats/\"\n\nlocal_zip = input_dir + 'train.zip'\nzip_ref = zipfile.ZipFile(local_zip, 'r')\nzip_ref.extractall(working_dir)\nzip_ref.close()","metadata":{"execution":{"iopub.status.busy":"2022-03-31T23:12:31.973958Z","iopub.execute_input":"2022-03-31T23:12:31.974738Z","iopub.status.idle":"2022-03-31T23:12:43.09499Z","shell.execute_reply.started":"2022-03-31T23:12:31.97468Z","shell.execute_reply":"2022-03-31T23:12:43.093977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Unzip the test set.","metadata":{}},{"cell_type":"code","source":"local_zip = input_dir + 'test1.zip'\nzip_ref = zipfile.ZipFile(local_zip, 'r')\nzip_ref.extractall(working_dir)\nzip_ref.close()","metadata":{"execution":{"iopub.status.busy":"2022-03-31T23:12:43.099324Z","iopub.execute_input":"2022-03-31T23:12:43.099591Z","iopub.status.idle":"2022-03-31T23:12:48.310825Z","shell.execute_reply.started":"2022-03-31T23:12:43.099556Z","shell.execute_reply":"2022-03-31T23:12:48.3101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Do not try to see the files in the training or test set in the data panel on the right as it will hang the browser.\n\nPrint the names of a few files in the training set.","metadata":{}},{"cell_type":"code","source":"train_dir = working_dir + 'train/'\nfor dirname, _, filenames in os.walk(train_dir):\n    for filename in filenames[:3]:\n        print(os.path.join(dirname, filename))","metadata":{"execution":{"iopub.status.busy":"2022-03-31T23:12:48.311982Z","iopub.execute_input":"2022-03-31T23:12:48.312266Z","iopub.status.idle":"2022-03-31T23:12:48.352496Z","shell.execute_reply.started":"2022-03-31T23:12:48.312231Z","shell.execute_reply":"2022-03-31T23:12:48.351733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Print the names of a few files in the test set.","metadata":{}},{"cell_type":"code","source":"test_dir = working_dir + 'test1/'\nfor dirname, _, filenames in os.walk(test_dir):\n    for filename in filenames[0:3]:\n        print(os.path.join(dirname, filename))","metadata":{"execution":{"iopub.status.busy":"2022-03-31T23:12:48.353893Z","iopub.execute_input":"2022-03-31T23:12:48.354177Z","iopub.status.idle":"2022-03-31T23:12:48.376446Z","shell.execute_reply.started":"2022-03-31T23:12:48.354141Z","shell.execute_reply":"2022-03-31T23:12:48.375742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Plot a few training images.","metadata":{}},{"cell_type":"code","source":"image_files = [\"/kaggle/working/train/dog.918.jpg\",\n               \"/kaggle/working/train/dog.7156.jpg\",\n               \"/kaggle/working/train/dog.10335.jpg\"]\n\nfor i, img_dir in enumerate(image_files):\n  img = mpimg.imread(img_dir)\n  plt.imshow(img)\n  plt.axis('Off')\n  plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-31T23:12:48.377607Z","iopub.execute_input":"2022-03-31T23:12:48.378004Z","iopub.status.idle":"2022-03-31T23:12:48.834766Z","shell.execute_reply.started":"2022-03-31T23:12:48.377966Z","shell.execute_reply":"2022-03-31T23:12:48.833745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Print the number of training and test images.","metadata":{}},{"cell_type":"code","source":"print(len(os.listdir(train_dir)))\nprint(len(os.listdir(test_dir)))","metadata":{"execution":{"iopub.status.busy":"2022-03-31T23:12:48.838555Z","iopub.execute_input":"2022-03-31T23:12:48.838857Z","iopub.status.idle":"2022-03-31T23:12:48.877168Z","shell.execute_reply.started":"2022-03-31T23:12:48.838817Z","shell.execute_reply":"2022-03-31T23:12:48.87579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Separate Resizing\nResize the training images and put the results in a new training directory.","metadata":{}},{"cell_type":"code","source":"destination_dir = working_dir + 'train_resized/'\nif (os.path.isdir(destination_dir) == False):\n    os.mkdir(destination_dir)\n\nfor dirname, _, filenames in os.walk(train_dir):\n    for filename in filenames:\n        input_path = os.path.join(dirname, filename)\n        img = image.load_img(input_path, target_size=(150, 150))\n        x = image.img_to_array(img)\n#         x = np.divide(x, 255.0)\n        output_path = os.path.join(destination_dir, filename)\n        utils.save_img(output_path, x, data_format='channels_last', file_format='jpeg', scale=False)\n        \ntrain_dir = working_dir + 'train_resized/'","metadata":{"execution":{"iopub.status.busy":"2022-03-31T23:12:48.878576Z","iopub.execute_input":"2022-03-31T23:12:48.878825Z","iopub.status.idle":"2022-03-31T23:14:09.94867Z","shell.execute_reply.started":"2022-03-31T23:12:48.878791Z","shell.execute_reply":"2022-03-31T23:14:09.94787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Move the training images to separate subfolders so we can use an ImageDataGenerator and have it automatically create the labels from the subfolder names.","metadata":{}},{"cell_type":"code","source":"dog_files = [name for name in os.listdir(train_dir) if (name.split('.')[0]=='dog')]\nprint(dog_files[:3])\nprint(len(dog_files))\ncat_files = [name for name in os.listdir(train_dir) if (name.split('.')[0]=='cat')]\nprint(cat_files[:3])\nprint(len(cat_files))","metadata":{"execution":{"iopub.status.busy":"2022-03-31T23:14:09.950165Z","iopub.execute_input":"2022-03-31T23:14:09.950408Z","iopub.status.idle":"2022-03-31T23:14:09.997212Z","shell.execute_reply.started":"2022-03-31T23:14:09.950375Z","shell.execute_reply":"2022-03-31T23:14:09.996381Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_subs_dir = working_dir + 'train_subs/'\nif (os.path.isdir(train_subs_dir) == False):\n    os.mkdir(train_subs_dir)\n\ntrain_subs_dog_dir = train_subs_dir + 'dog/'\nif (os.path.isdir(train_subs_dog_dir) == False):\n    os.mkdir(train_subs_dog_dir)\n\ntrain_subs_cat_dir = train_subs_dir + 'cat/'\nif (os.path.isdir(train_subs_cat_dir) == False):\n    os.mkdir(train_subs_cat_dir)\n\nprint(train_subs_dir)\nprint(train_subs_dog_dir)\nprint(train_subs_cat_dir)\n\nfor name in dog_files:\n    shutil.copyfile(train_dir + name, train_subs_dog_dir + name)\n    \nfor name in cat_files:\n    shutil.copyfile(train_dir + name, train_subs_cat_dir + name)","metadata":{"execution":{"iopub.status.busy":"2022-03-31T23:14:09.998815Z","iopub.execute_input":"2022-03-31T23:14:09.9991Z","iopub.status.idle":"2022-03-31T23:14:11.82427Z","shell.execute_reply.started":"2022-03-31T23:14:09.999065Z","shell.execute_reply":"2022-03-31T23:14:11.823399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for dirname, _, filenames in os.walk(train_subs_dog_dir):\n    for filename in filenames[:3]:\n        print(os.path.join(dirname, filename))\n\nfor dirname, _, filenames in os.walk(train_subs_cat_dir):\n    for filename in filenames[:3]:\n        print(os.path.join(dirname, filename))","metadata":{"execution":{"iopub.status.busy":"2022-03-31T23:14:11.825763Z","iopub.execute_input":"2022-03-31T23:14:11.826008Z","iopub.status.idle":"2022-03-31T23:14:11.86557Z","shell.execute_reply.started":"2022-03-31T23:14:11.825973Z","shell.execute_reply":"2022-03-31T23:14:11.864704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(os.listdir(train_subs_dog_dir)))\nprint(len(os.listdir(train_subs_cat_dir)))","metadata":{"execution":{"iopub.status.busy":"2022-03-31T23:14:11.867087Z","iopub.execute_input":"2022-03-31T23:14:11.867344Z","iopub.status.idle":"2022-03-31T23:14:11.887376Z","shell.execute_reply.started":"2022-03-31T23:14:11.86731Z","shell.execute_reply":"2022-03-31T23:14:11.886699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Use the last 2500 dog images and the last 2500 cat images as the validation set.","metadata":{}},{"cell_type":"code","source":"val_subs_dir = working_dir + 'val_subs/'\nif (os.path.isdir(val_subs_dir) == False):\n    os.mkdir(val_subs_dir)\n\nval_subs_dog_dir = val_subs_dir + 'dog/'\nif (os.path.isdir(val_subs_dog_dir) == False):\n    os.mkdir(val_subs_dog_dir)\n\nval_subs_cat_dir = val_subs_dir + 'cat/'\nif (os.path.isdir(val_subs_cat_dir) == False):\n    os.mkdir(val_subs_cat_dir)","metadata":{"execution":{"iopub.status.busy":"2022-03-31T23:14:11.888479Z","iopub.execute_input":"2022-03-31T23:14:11.888783Z","iopub.status.idle":"2022-03-31T23:14:11.894873Z","shell.execute_reply.started":"2022-03-31T23:14:11.888736Z","shell.execute_reply":"2022-03-31T23:14:11.89395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for name in dog_files[:2500]:\n    os.rename(train_subs_dog_dir + name, val_subs_dog_dir + name)\nfor name in cat_files[:2500]:\n    os.rename(train_subs_cat_dir + name, val_subs_cat_dir + name)","metadata":{"execution":{"iopub.status.busy":"2022-03-31T23:14:11.896325Z","iopub.execute_input":"2022-03-31T23:14:11.896772Z","iopub.status.idle":"2022-03-31T23:14:11.992217Z","shell.execute_reply.started":"2022-03-31T23:14:11.896724Z","shell.execute_reply":"2022-03-31T23:14:11.991531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_subs_dog_dir)\nprint(len(os.listdir(train_subs_dog_dir)))\nfor dirname, _, filenames in os.walk(train_subs_dog_dir):\n    for filename in filenames[:3]:\n        print(os.path.join(dirname, filename))\n\nprint(train_subs_cat_dir)\nprint(len(os.listdir(train_subs_cat_dir)))\nfor dirname, _, filenames in os.walk(train_subs_cat_dir):\n    for filename in filenames[:3]:\n        print(os.path.join(dirname, filename))\n\nprint(val_subs_dog_dir)\nprint(len(os.listdir(val_subs_dog_dir)))\nfor dirname, _, filenames in os.walk(val_subs_dog_dir):\n    for filename in filenames[:3]:\n        print(os.path.join(dirname, filename))\n\nprint(val_subs_cat_dir)\nprint(len(os.listdir(val_subs_cat_dir)))\nfor dirname, _, filenames in os.walk(val_subs_cat_dir):\n    for filename in filenames[:3]:\n        print(os.path.join(dirname, filename))","metadata":{"execution":{"iopub.status.busy":"2022-03-31T23:14:11.993591Z","iopub.execute_input":"2022-03-31T23:14:11.993853Z","iopub.status.idle":"2022-03-31T23:14:12.050348Z","shell.execute_reply.started":"2022-03-31T23:14:11.993819Z","shell.execute_reply":"2022-03-31T23:14:12.049529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Create the image data generators.","metadata":{}},{"cell_type":"code","source":"train_subs_dir = working_dir + 'train_subs/'\nval_subs_dir = working_dir + 'val_subs/'\n\ntraining_datagen = ImageDataGenerator(\n      rescale = 1./255,\n#       rotation_range=20,\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 = training_datagen.flow_from_directory(\n    train_subs_dir,\n    target_size=(150,150),\n    class_mode='binary',\n    batch_size=128\n)\n\nvalidation_datagen = ImageDataGenerator(rescale = 1./255)\n\nvalidation_generator = validation_datagen.flow_from_directory(\n    val_subs_dir,\n    target_size=(150,150),\n    class_mode='binary',\n    batch_size=128\n)","metadata":{"execution":{"iopub.status.busy":"2022-03-31T23:14:12.051735Z","iopub.execute_input":"2022-03-31T23:14:12.051995Z","iopub.status.idle":"2022-03-31T23:14:12.827507Z","shell.execute_reply.started":"2022-03-31T23:14:12.051961Z","shell.execute_reply":"2022-03-31T23:14:12.826715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Create the model, starting with Inception V3.","metadata":{}},{"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\n\n# pre_trained_model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-03-31T23:14:12.82859Z","iopub.execute_input":"2022-03-31T23:14:12.828845Z","iopub.status.idle":"2022-03-31T23:14:15.612501Z","shell.execute_reply.started":"2022-03-31T23:14:12.828809Z","shell.execute_reply":"2022-03-31T23:14:15.611727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"last_desired_layer = pre_trained_model.get_layer('mixed7')\nprint('last layer output shape: ', last_desired_layer.output_shape)\nlast_output = last_desired_layer.output\nprint('last layer output: ', last_output)\n\nx = layers.Flatten()(last_output)\nx = layers.Dense(1024, activation='relu')(x)\nx = layers.Dropout(0.2)(x)  \nx = layers.Dense  (1, activation='sigmoid')(x)        \n\nmodel = Model(inputs=pre_trained_model.input, outputs=x)","metadata":{"execution":{"iopub.status.busy":"2022-03-31T23:14:15.613828Z","iopub.execute_input":"2022-03-31T23:14:15.614079Z","iopub.status.idle":"2022-03-31T23:14:15.65761Z","shell.execute_reply.started":"2022-03-31T23:14:15.614027Z","shell.execute_reply":"2022-03-31T23:14:15.656851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Create callbacks.","metadata":{}},{"cell_type":"code","source":"class myCallback(tf.keras.callbacks.Callback):\n  def on_epoch_end(self, epoch, logs={}):\n    if(logs.get('accuracy')>0.99):\n      print(\"\\nReached 99% accuracy, cancelling training\")\n      self.model.stop_training = True","metadata":{"execution":{"iopub.status.busy":"2022-03-31T23:14:15.659515Z","iopub.execute_input":"2022-03-31T23:14:15.659768Z","iopub.status.idle":"2022-03-31T23:14:15.804781Z","shell.execute_reply.started":"2022-03-31T23:14:15.659721Z","shell.execute_reply":"2022-03-31T23:14:15.803912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Compile the model.","metadata":{}},{"cell_type":"code","source":"model.compile(optimizer = RMSprop(learning_rate=0.0001), \n                loss = 'binary_crossentropy',\n                metrics = ['accuracy'])\n\n# model.compile(loss = 'categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-03-31T23:14:15.80814Z","iopub.execute_input":"2022-03-31T23:14:15.808411Z","iopub.status.idle":"2022-03-31T23:14:15.826496Z","shell.execute_reply.started":"2022-03-31T23:14:15.808383Z","shell.execute_reply":"2022-03-31T23:14:15.825734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Train the model.","metadata":{}},{"cell_type":"code","source":"callbacks = myCallback()\nmodel.fit(train_generator,\n          validation_data = validation_generator,\n          epochs = 100,\n          batch_size=128,\n          verbose = 2,\n          callbacks=callbacks)","metadata":{"execution":{"iopub.status.busy":"2022-03-31T23:14:15.828003Z","iopub.execute_input":"2022-03-31T23:14:15.828538Z","iopub.status.idle":"2022-03-31T23:16:37.281792Z","shell.execute_reply.started":"2022-03-31T23:14:15.828498Z","shell.execute_reply":"2022-03-31T23:16:37.281096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Results\nTraining accuracy exceeds the 99% threshold after finishing the 4rd epoch, which is after about 3 minutes.  At that point the validation accuracy is almost 97%.\n\n    training_datagen = ImageDataGenerator(rescale = 1./255)\n\n    Epoch 3/100\n    157/157 - 72s - loss: 0.0245 - accuracy: 0.9915 - val_loss: 0.1301 - val_accuracy: 0.9584\n","metadata":{}},{"cell_type":"code","source":"print(len(os.listdir(test_dir)))\ntest_files = os.listdir(test_dir)\nprint(test_files[:3])\nid_strings = [name.split('.')[0] for name in os.listdir(test_dir)]\ntest_ids = list(map(int, id_strings))\ntest_ids.sort()\n# print(test_ids) # it looks like test_ids - ie. the numbers in the test image file names - is simply a list from 1 thru 12500\nprint(min(test_ids))\nprint(max(test_ids))\nsorted_id_strings = [(test_dir+str(test_id)+'.jpg') for test_id in test_ids]\nprint(sorted_id_strings[:10])","metadata":{"execution":{"iopub.status.busy":"2022-03-31T23:16:37.283008Z","iopub.execute_input":"2022-03-31T23:16:37.283278Z","iopub.status.idle":"2022-03-31T23:16:37.323737Z","shell.execute_reply.started":"2022-03-31T23:16:37.283243Z","shell.execute_reply":"2022-03-31T23:16:37.322914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for name in sorted_id_strings[:10]:\n    img = image.load_img(name, target_size=(150, 150))\n    x = image.img_to_array(img)\n    x = np.divide(x, 255.0)\n    x = np.expand_dims(x, axis=0)\n    \n    images = np.vstack([x])\n    classes = model.predict(images)\n    print(name)\n    print(classes[0][0])\n    if classes[0][0]>0.5:\n      print(\"Dog\")\n    else:\n      print(\"Cat\")\n    plt.imshow(img)\n    plt.axis('Off')\n    plt.show()\n    print()","metadata":{"execution":{"iopub.status.busy":"2022-03-31T23:16:37.325146Z","iopub.execute_input":"2022-03-31T23:16:37.325401Z","iopub.status.idle":"2022-03-31T23:16:40.217471Z","shell.execute_reply.started":"2022-03-31T23:16:37.325366Z","shell.execute_reply":"2022-03-31T23:16:40.216577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = []\n\nfor name in sorted_id_strings:\n    img = image.load_img(name, target_size=(150, 150))\n    x = image.img_to_array(img)\n    x = np.divide(x, 255.0)\n    x = np.expand_dims(x, axis=0)\n    \n    images = np.vstack([x])\n    classes = model.predict(images)\n    if classes[0][0]>0.5:\n      prediction = 1 # Dog\n    else:\n      prediction = 0 # Cat\n    predictions.append(prediction)","metadata":{"execution":{"iopub.status.busy":"2022-03-31T23:16:40.219315Z","iopub.execute_input":"2022-03-31T23:16:40.219618Z","iopub.status.idle":"2022-03-31T23:26:09.886627Z","shell.execute_reply.started":"2022-03-31T23:16:40.219558Z","shell.execute_reply":"2022-03-31T23:26:09.885855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(predictions))\ndf = pd.DataFrame()\ndf['id'] = test_ids\ndf['label'] = predictions\nprint(df)","metadata":{"execution":{"iopub.status.busy":"2022-03-31T23:26:09.890774Z","iopub.execute_input":"2022-03-31T23:26:09.890995Z","iopub.status.idle":"2022-03-31T23:26:09.928065Z","shell.execute_reply.started":"2022-03-31T23:26:09.890967Z","shell.execute_reply":"2022-03-31T23:26:09.92702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-03-31T23:26:09.929439Z","iopub.execute_input":"2022-03-31T23:26:09.930038Z","iopub.status.idle":"2022-03-31T23:26:09.956469Z","shell.execute_reply.started":"2022-03-31T23:26:09.929996Z","shell.execute_reply":"2022-03-31T23:26:09.955767Z"},"trusted":true},"execution_count":null,"outputs":[]}]}