{"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":"markdown","source":"<a href=\"https://colab.research.google.com/github/concaption/cats-vs-dogs/blob/main/cats_vs_dogs.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>","metadata":{"id":"view-in-github"}},{"cell_type":"markdown","source":"## Load data","metadata":{"id":"WU9_ph160caj"}},{"cell_type":"code","source":"import os\nimport shutil\nimport random\nimport numpy as np\nimport pandas as pd\nfrom zipfile import ZipFile as unzip\nimport matplotlib.pyplot as plt\n!pip install gdown\nimport gdown","metadata":{"id":"s3imhxv99eXj","execution":{"iopub.status.busy":"2022-01-12T08:46:06.911703Z","iopub.execute_input":"2022-01-12T08:46:06.912346Z","iopub.status.idle":"2022-01-12T08:46:27.642826Z","shell.execute_reply.started":"2022-01-12T08:46:06.912309Z","shell.execute_reply":"2022-01-12T08:46:27.641068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras.layers import Flatten, Dense, Conv2D, MaxPooling2D, Dropout\nfrom tensorflow.keras.optimizers import RMSprop\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom keras.preprocessing import image\nfrom tensorflow.keras.preprocessing.image import img_to_array, load_img","metadata":{"id":"1LERCYC4DEN9","execution":{"iopub.status.busy":"2022-01-12T08:46:27.647671Z","iopub.execute_input":"2022-01-12T08:46:27.648547Z","iopub.status.idle":"2022-01-12T08:46:32.757913Z","shell.execute_reply.started":"2022-01-12T08:46:27.648505Z","shell.execute_reply":"2022-01-12T08:46:32.757187Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input_dir = '../input/dogs-vs-cats/'\nfor _ , _  , filenames in os.walk(input_dir):\n    for filename in filenames:\n        if filename[-3:]=='zip':\n            file = unzip(os.path.join(input_dir,filename),'r')\n            file.extractall()\n            file.close()","metadata":{"id":"FttXUwzd9Z-Z","execution":{"iopub.status.busy":"2022-01-12T08:46:32.759288Z","iopub.execute_input":"2022-01-12T08:46:32.759542Z","iopub.status.idle":"2022-01-12T08:46:49.966758Z","shell.execute_reply.started":"2022-01-12T08:46:32.75951Z","shell.execute_reply":"2022-01-12T08:46:49.965981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Number of train images: \" , len(os.listdir('./train')) )\nprint(\"Number of test images: \"  , len(os.listdir('./test1')) )","metadata":{"id":"KEX4YDnHDekq","outputId":"e607743b-bc84-4934-f7f2-e09f0c965078","execution":{"iopub.status.busy":"2022-01-12T08:46:49.968702Z","iopub.execute_input":"2022-01-12T08:46:49.96896Z","iopub.status.idle":"2022-01-12T08:46:49.995991Z","shell.execute_reply.started":"2022-01-12T08:46:49.968926Z","shell.execute_reply":"2022-01-12T08:46:49.994947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"no_of_cats = len([files for files in os.listdir('./train') if 'cat' in files])\nno_of_dogs = len([files for files in os.listdir('./train') if 'dog' in files])\nprint(\"Cat images in train dataset: \",  no_of_cats)\nprint(\"Dog images in train dataset: \", no_of_dogs)\nif no_of_cats==no_of_dogs:\n  total_images = no_of_cats\n  print('Balanced dataset')","metadata":{"id":"aWl4bG7LDsFz","outputId":"84947a85-6f6a-4233-dccb-b0cfba807cca","execution":{"iopub.status.busy":"2022-01-12T08:46:49.997167Z","iopub.execute_input":"2022-01-12T08:46:49.997393Z","iopub.status.idle":"2022-01-12T08:46:50.033623Z","shell.execute_reply.started":"2022-01-12T08:46:49.997361Z","shell.execute_reply":"2022-01-12T08:46:50.032845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_dir = 'data/'\ndirs = ['train','val','test']\nsub_dirs = ['cats','dogs']\nfor dir in dirs:\n  dir =  os.path.join(base_dir, dir)\n  os.makedirs(dir,exist_ok=True)\n  for sub_dir in sub_dirs:\n    sub_dir = os.path.join(dir, sub_dir)\n    os.makedirs(sub_dir,exist_ok=True)","metadata":{"id":"Qi4itbV3Jh9z","execution":{"iopub.status.busy":"2022-01-12T08:47:46.28242Z","iopub.execute_input":"2022-01-12T08:47:46.282691Z","iopub.status.idle":"2022-01-12T08:47:46.289973Z","shell.execute_reply.started":"2022-01-12T08:47:46.282662Z","shell.execute_reply":"2022-01-12T08:47:46.289194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"split = {'train': 0.7,\n         'val': 0.3}\n\nfor dir in dirs[:2]:\n    print(int(split[dir]*total_images))","metadata":{"id":"nlfmQCGHpVyx","outputId":"a32afff0-f716-4393-8ed4-c8aa4025ff6b","execution":{"iopub.status.busy":"2022-01-12T08:47:48.180082Z","iopub.execute_input":"2022-01-12T08:47:48.180763Z","iopub.status.idle":"2022-01-12T08:47:48.187219Z","shell.execute_reply.started":"2022-01-12T08:47:48.180728Z","shell.execute_reply":"2022-01-12T08:47:48.185163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"split_size = 0.7\n\nsplit = {'train': (0,int(split_size*total_images)),\n         'val': (int(split_size*total_images),total_images)}\n\nfor obj in sub_dirs:\n  for dir in dirs[:2]:\n    files = ['{}.{}.jpg'.format(obj[:-1],i) for i in range(*split[dir])]\n    for filename in files:\n      src = os.path.join('./train', filename)\n      dst = os.path.join(base_dir,dir,obj,filename)\n      shutil.copyfile(src,dst)\nprint(\"Train validation split completed.\")","metadata":{"id":"S3oiYk_-eJlI","outputId":"5f3f2b4f-32d0-47d0-f86e-0775e0f26017","execution":{"iopub.status.busy":"2022-01-12T08:47:49.288337Z","iopub.execute_input":"2022-01-12T08:47:49.28882Z","iopub.status.idle":"2022-01-12T08:47:51.668991Z","shell.execute_reply.started":"2022-01-12T08:47:49.288784Z","shell.execute_reply":"2022-01-12T08:47:51.668267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#os.makedirs(os.path.join(base_dir,'test','test_images',),exist_ok=True)\n#for filename in ['{}.jpg'.format(i) for i in range(1,1+len(os.listdir('test1')))]:\n#    src = os.path.join('./test1', filename)\n#    dst = os.path.join(base_dir,'test','test_images',filename)\n#    shutil.copyfile(src,dst)\n#print(\"Test files moved.\")","metadata":{"execution":{"iopub.status.busy":"2022-01-12T08:47:51.670594Z","iopub.execute_input":"2022-01-12T08:47:51.671013Z","iopub.status.idle":"2022-01-12T08:47:51.674906Z","shell.execute_reply.started":"2022-01-12T08:47:51.670975Z","shell.execute_reply":"2022-01-12T08:47:51.673836Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#shutil.rmtree('train')\n#shutil.rmtree('test1')","metadata":{"execution":{"iopub.status.busy":"2022-01-12T08:47:53.740868Z","iopub.execute_input":"2022-01-12T08:47:53.741763Z","iopub.status.idle":"2022-01-12T08:47:53.7454Z","shell.execute_reply.started":"2022-01-12T08:47:53.741716Z","shell.execute_reply":"2022-01-12T08:47:53.744467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir = os.path.join(base_dir, 'train')\nvalidation_dir = os.path.join(base_dir, 'val')\n\n# Directory with our training cat/dog pictures\ntrain_cats_dir = os.path.join(train_dir, 'cats')\ntrain_dogs_dir = os.path.join(train_dir, 'dogs')\n\n# Directory with our validation cat/dog pictures\nvalidation_cats_dir = os.path.join(validation_dir, 'cats')\nvalidation_dogs_dir = os.path.join(validation_dir, 'dogs')","metadata":{"id":"slK7PnJrvFHv","execution":{"iopub.status.busy":"2022-01-12T08:47:55.30841Z","iopub.execute_input":"2022-01-12T08:47:55.308925Z","iopub.status.idle":"2022-01-12T08:47:55.314237Z","shell.execute_reply.started":"2022-01-12T08:47:55.308889Z","shell.execute_reply":"2022-01-12T08:47:55.313256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_cat_fnames = os.listdir( train_cats_dir )\ntrain_dog_fnames = os.listdir( train_dogs_dir )\n\nprint(train_cat_fnames[:10])\nprint(train_dog_fnames[:10])","metadata":{"id":"dl-918bjCCqp","outputId":"f5ee24be-bfa0-4c7b-b034-557431f9324d","execution":{"iopub.status.busy":"2022-01-12T08:47:57.320745Z","iopub.execute_input":"2022-01-12T08:47:57.321479Z","iopub.status.idle":"2022-01-12T08:47:57.338555Z","shell.execute_reply.started":"2022-01-12T08:47:57.321443Z","shell.execute_reply":"2022-01-12T08:47:57.337785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('total training cat images :', len(os.listdir(      train_cats_dir ) ))\nprint('total training dog images :', len(os.listdir(      train_dogs_dir ) ))\n\nprint('total validation cat images :', len(os.listdir( validation_cats_dir ) ))\nprint('total validation dog images :', len(os.listdir( validation_dogs_dir ) ))","metadata":{"id":"jm9ZelGjChv2","outputId":"8c00f8a1-6950-4f00-b4ea-d2632e6afad3","execution":{"iopub.status.busy":"2022-01-12T08:47:58.766221Z","iopub.execute_input":"2022-01-12T08:47:58.766931Z","iopub.status.idle":"2022-01-12T08:47:58.790646Z","shell.execute_reply.started":"2022-01-12T08:47:58.766884Z","shell.execute_reply":"2022-01-12T08:47:58.789956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%matplotlib inline\n\nimport matplotlib.image as mpimg\nimport matplotlib.pyplot as plt\n\n# Parameters for our graph; we'll output images in a 4x4 configuration\nnrows = 4\nncols = 4\n\npic_index = 0 # Index for iterating over images","metadata":{"id":"CfhZ-3-MCrVY","execution":{"iopub.status.busy":"2022-01-12T08:48:10.441808Z","iopub.execute_input":"2022-01-12T08:48:10.442521Z","iopub.status.idle":"2022-01-12T08:48:10.448754Z","shell.execute_reply.started":"2022-01-12T08:48:10.442473Z","shell.execute_reply":"2022-01-12T08:48:10.447761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Set up matplotlib fig, and size it to fit 4x4 pics\nfig = plt.gcf()\nfig.set_size_inches(ncols*4, nrows*4)\n\npic_index+=8\n\nnext_cat_pix = [os.path.join(train_cats_dir, fname) \n                for fname in train_cat_fnames[ pic_index-8:pic_index] \n               ]\n\nnext_dog_pix = [os.path.join(train_dogs_dir, fname) \n                for fname in train_dog_fnames[ pic_index-8:pic_index]\n               ]\n\nfor i, img_path in enumerate(next_cat_pix+next_dog_pix):\n  # Set up subplot; subplot indices start at 1\n  sp = plt.subplot(nrows, ncols, i + 1)\n  sp.axis('Off') # Don't show axes (or gridlines)\n\n  img = mpimg.imread(img_path)\n  plt.imshow(img)\n\nplt.show()","metadata":{"id":"2MvHXFz2CukY","outputId":"8cfc455a-0529-4bc7-99a6-dcf6108f786d","execution":{"iopub.status.busy":"2022-01-12T08:48:11.102659Z","iopub.execute_input":"2022-01-12T08:48:11.103289Z","iopub.status.idle":"2022-01-12T08:48:12.274054Z","shell.execute_reply.started":"2022-01-12T08:48:11.103248Z","shell.execute_reply":"2022-01-12T08:48:12.273294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tf.keras.models.Sequential([\n        Conv2D(128,kernel_size=(3,3),activation='relu',input_shape=(150,150,3)),\n        Conv2D(64,kernel_size=(3,3),activation='relu'),\n        MaxPooling2D(4,4),\n\n        Conv2D(128,kernel_size=(3,3),activation='relu'),    \n        Conv2D(64,kernel_size=(3,3),activation='relu'),\n        MaxPooling2D(4,4),\n    \n        Flatten(),    \n        Dense(512,activation='relu') ,\n        Dropout(rate=0.3),\n        Dense(1, activation='sigmoid'),    \n        ])","metadata":{"id":"uo9Hpc7NCxiQ","execution":{"iopub.status.busy":"2022-01-12T08:48:12.275576Z","iopub.execute_input":"2022-01-12T08:48:12.275941Z","iopub.status.idle":"2022-01-12T08:48:14.750148Z","shell.execute_reply.started":"2022-01-12T08:48:12.275906Z","shell.execute_reply":"2022-01-12T08:48:14.749413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"id":"FEiznamrDB-v","outputId":"aead0eeb-8663-47e2-93b4-816d766f5cb4","execution":{"iopub.status.busy":"2022-01-12T08:48:14.751505Z","iopub.execute_input":"2022-01-12T08:48:14.751922Z","iopub.status.idle":"2022-01-12T08:48:14.765407Z","shell.execute_reply.started":"2022-01-12T08:48:14.751885Z","shell.execute_reply":"2022-01-12T08:48:14.764687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer=RMSprop(learning_rate=0.001),\n              loss='binary_crossentropy',\n              metrics = ['accuracy'])","metadata":{"id":"J2dmQ7YPGvXX","execution":{"iopub.status.busy":"2022-01-12T08:48:14.768239Z","iopub.execute_input":"2022-01-12T08:48:14.768425Z","iopub.status.idle":"2022-01-12T08:48:14.780937Z","shell.execute_reply.started":"2022-01-12T08:48:14.768401Z","shell.execute_reply":"2022-01-12T08:48:14.780182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen = ImageDataGenerator(rescale = 1.0/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\ntrain_generator = datagen.flow_from_directory(train_dir,\n                                              batch_size=20,\n                                              class_mode ='binary',\n                                              target_size=(150,150))\nvalidation_generator = datagen.flow_from_directory(validation_dir,\n                                                   batch_size=20,\n                                                   class_mode='binary',\n                                                   target_size = (150,150))","metadata":{"id":"y4MAfCXGHNf0","outputId":"c4839577-8195-4ce7-92bf-298e9160b2dd","execution":{"iopub.status.busy":"2022-01-12T08:48:15.26847Z","iopub.execute_input":"2022-01-12T08:48:15.269044Z","iopub.status.idle":"2022-01-12T08:48:16.496851Z","shell.execute_reply.started":"2022-01-12T08:48:15.269015Z","shell.execute_reply":"2022-01-12T08:48:16.495307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for _ in range(5):\n    img, label = train_generator.next()\n    print(img.shape)   #  (1,256,256,3)\n    print(label[0])\n    plt.imshow(img[0])\n    plt.show()","metadata":{"id":"ugSXK4hSQ7sD","outputId":"2eee2bfd-fd03-40b0-fe6a-5110b8094c1f","execution":{"iopub.status.busy":"2022-01-12T08:48:18.77575Z","iopub.execute_input":"2022-01-12T08:48:18.776298Z","iopub.status.idle":"2022-01-12T08:48:20.223585Z","shell.execute_reply.started":"2022-01-12T08:48:18.776261Z","shell.execute_reply":"2022-01-12T08:48:20.221988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_callbacks = [\n    tf.keras.callbacks.ModelCheckpoint(filepath='saved/model.{epoch:02d}-{val_loss:.2f}.h5',save_weights_only=False),\n    tf.keras.callbacks.TensorBoard(log_dir='saved/logs'),\n]","metadata":{"execution":{"iopub.status.busy":"2022-01-12T08:48:20.225043Z","iopub.execute_input":"2022-01-12T08:48:20.225867Z","iopub.status.idle":"2022-01-12T08:48:20.566454Z","shell.execute_reply.started":"2022-01-12T08:48:20.225831Z","shell.execute_reply":"2022-01-12T08:48:20.56535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"histroy = model.fit(train_generator,\n                    validation_data = validation_generator,\n                    steps_per_epoch=100,\n                    epochs=50,\n                    validation_steps = 50,\n                    verbose=2,\n                   callbacks=[my_callbacks])","metadata":{"id":"UfNL8bw4Pv3v","outputId":"0ca19ca2-5841-4741-a423-5bf8fd09103e","execution":{"iopub.status.busy":"2022-01-12T08:48:29.968707Z","iopub.execute_input":"2022-01-12T08:48:29.96934Z","iopub.status.idle":"2022-01-12T08:48:46.845088Z","shell.execute_reply.started":"2022-01-12T08:48:29.9693Z","shell.execute_reply":"2022-01-12T08:48:46.844204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# save weights\nmodel.save('/saved/dogs_vs_cats_model.h5')","metadata":{"id":"gusSuU9NicNd","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"successive_outputs = [layer.output for layer in model.layers[1:]]\n\n#visualization_model = Model(img_input, successive_outputs)\nvisualization_model = tf.keras.models.Model(inputs = model.input, outputs = successive_outputs)\n\n# Let's prepare a random input image of a cat or dog from the training set.\ncat_img_files = [os.path.join(train_cats_dir, f) for f in train_cat_fnames]\ndog_img_files = [os.path.join(train_dogs_dir, f) for f in train_dog_fnames]\n\nimg_path = random.choice(cat_img_files + dog_img_files)\nimg = load_img(img_path, target_size=(150, 150))  # this is a PIL image\n\nx   = img_to_array(img)                           # Numpy array with shape (150, 150, 3)\nx   = x.reshape((1,) + x.shape)                   # Numpy array with shape (1, 150, 150, 3)\n\n# Rescale by 1/255\nx /= 255.0\n\n# Let's run our image through our network, thus obtaining all\n# intermediate representations for this image.\nsuccessive_feature_maps = visualization_model.predict(x)\n\n# These are the names of the layers, so can have them as part of our plot\nlayer_names = [layer.name for layer in model.layers]\n\n# -----------------------------------------------------------------------\n# Now let's display our representations\n# -----------------------------------------------------------------------\nfor layer_name, feature_map in zip(layer_names, successive_feature_maps):\n  \n  if len(feature_map.shape) == 4:\n    \n    #-------------------------------------------\n    # Just do this for the conv / maxpool layers, not the fully-connected layers\n    #-------------------------------------------\n    n_features = feature_map.shape[-1]  # number of features in the feature map\n    size       = feature_map.shape[ 1]  # feature map shape (1, size, size, n_features)\n    \n    # We will tile our images in this matrix\n    display_grid = np.zeros((size, size * n_features))\n    \n    #-------------------------------------------------\n    # Postprocess the feature to be visually palatable\n    #-------------------------------------------------\n    for i in range(n_features):\n      x  = feature_map[0, :, :, i]\n      x -= x.mean()\n      x /= x.std ()\n      x *=  64\n      x += 128\n      x  = np.clip(x, 0, 255).astype('uint8')\n      display_grid[:, i * size : (i + 1) * size] = x # Tile each filter into a horizontal grid\n\n    #-----------------\n    # Display the grid\n    #-----------------\n\n    scale = 20. / n_features\n    plt.figure( figsize=(scale * n_features, scale) )\n    plt.title ( layer_name )\n    plt.grid  ( False )\n    plt.imshow( display_grid, aspect='auto', cmap='viridis' )","metadata":{"id":"0z-kUXfnasjY","outputId":"ecca7a8b-6445-4f14-ae8b-a681c7873512","execution":{"iopub.status.busy":"2022-01-12T08:48:52.547254Z","iopub.execute_input":"2022-01-12T08:48:52.547667Z","iopub.status.idle":"2022-01-12T08:48:53.720858Z","shell.execute_reply.started":"2022-01-12T08:48:52.547632Z","shell.execute_reply":"2022-01-12T08:48:53.720068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#-----------------------------------------------------------\n# Retrieve a list of list results on training and test data\n# sets for each training epoch\n#-----------------------------------------------------------\nacc      = model.history.history[     'accuracy' ]\nval_acc  = model.history.history[ 'val_accuracy' ]\nloss     = model.history.history[    'loss' ]\nval_loss = model.history.history['val_loss' ]\n\nepochs   = range(len(acc)) # Get number of epochs\n\n#------------------------------------------------\n# Plot training and validation accuracy per epoch\n#------------------------------------------------\nplt.plot  ( epochs,     acc )\nplt.plot  ( epochs, val_acc )\nplt.title ('Training and validation accuracy')\nplt.figure()\n\n#------------------------------------------------\n# Plot training and validation loss per epoch\n#------------------------------------------------\nplt.plot  ( epochs,     loss )\nplt.plot  ( epochs, val_loss )\nplt.title ('Training and validation loss'   )","metadata":{"id":"iA7bo2noVmO9","outputId":"d7996dd0-04f2-4bd1-e83d-bf13d33efd54","execution":{"iopub.status.busy":"2022-01-12T08:48:53.841714Z","iopub.execute_input":"2022-01-12T08:48:53.84235Z","iopub.status.idle":"2022-01-12T08:48:54.173775Z","shell.execute_reply.started":"2022-01-12T08:48:53.842317Z","shell.execute_reply":"2022-01-12T08:48:54.173135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"saved_model = tf.keras.models.load_model('/saved/dogs_vs_cats_model.h5')","metadata":{"id":"v_0T_sVtinSK","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# for filename in ['{}.jpg'.format(i) for i in range(1,1+len(os.listdir('test1')))]:\n#    path = os.path.join('test1',filename)\n#   img=image.load_img(path, target_size=(150, 150))\n#   x=image.img_to_array(img)\n#   x=np.expand_dims(x, axis=0)\n#   images = np.vstack([x])\n#   classes = model.predict(images, batch_size=10)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# test_generator = datagen.flow_from_directory('./data/test',\n#                                               batch_size=20,\n#                                               class_mode =None,\n#                                               target_size=(150,150))\n# predictions = model.predict(test_generator)\n# predictions[:10]\n# mot going use this because the predcitions are random. Pictures are shuffled.\n# A better way is to first load the test file into a dataframe and then use flow_from_dataframe method.\n# Need tto implement flow_from_dataframe.","metadata":{"execution":{"iopub.status.busy":"2022-01-12T07:38:18.559225Z","iopub.execute_input":"2022-01-12T07:38:18.559418Z","iopub.status.idle":"2022-01-12T07:38:48.911433Z","shell.execute_reply.started":"2022-01-12T07:38:18.559393Z","shell.execute_reply":"2022-01-12T07:38:48.91073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = []\nfor filename in ['{}.jpg'.format(i) for i in range(1,1+len(os.listdir('test1')))]:\n  img = load_img(os.path.join('test1',filename), target_size=(150, 150))\n  x = img_to_array(img)                         # Numpy array with shape (150, 150, 3)\n  x = x.reshape((1,) + x.shape)                 # Numpy array with shape (1, 150, 150, 3)\n  x /= 255.0\n  y_hat= model.predict(x)\n  predictions.append(y_hat)","metadata":{"id":"I26nfTlsjPB1","execution":{"iopub.status.busy":"2022-01-12T08:49:13.41906Z","iopub.execute_input":"2022-01-12T08:49:13.419611Z","iopub.status.idle":"2022-01-12T08:49:24.625599Z","shell.execute_reply.started":"2022-01-12T08:49:13.419576Z","shell.execute_reply":"2022-01-12T08:49:24.624067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred=np.asarray(predictions)\npred=np.squeeze(pred)\nfor i, y_hat in enumerate(pred):\n    pred[i] = 1 if y_hat>0.5 else 0","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"index_values = [i+1 for i in range(len(pred))]\ncolumn_label = ['label']\n\n# creating the dataframe\ndf = pd.DataFrame(data = pred, \n                  index = index_values, \n                  columns = column_label)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.to_csv('submission.csv', index_label='id')","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}