{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport cv2 # image and video processing library to be used for reading and resizing our images\n\nimport matplotlib.pyplot as plt\n%matplotlib inline \n\nimport random # to split and shuffle our dataset\nimport gc # garbage collector for cleaning deleted data from memory\n\n# Input data files are available in the \"../input/\" directory.\n\nimport os\n'''for dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))'''\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"# create a file path to our train and test data\ntrain_dir = '../input/dogs-vs-cats-redux-kernels-edition/train'\ntest_dir = '../input/dogs-vs-cats-redux-kernels-edition/test'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# list comprehension to get all the images in the train data zip file\n# and retrieve all images with dog/cat in their name\ntrain_dogs = ['../input/dogs-vs-cats-redux-kernels-edition/train/{}'.format(i) for i in os.listdir(train_dir) if 'dog' in i] # get dog images\ntrain_cats = ['../input/dogs-vs-cats-redux-kernels-edition/train/{}'.format(i) for i in os.listdir(train_dir) if 'cat' in i] # get cat images\n\ntest_imgs = ['../input/dogs-vs-cats-redux-kernels-edition/test/{}'.format(i) for i in os.listdir(test_dir)] # get test images","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_imgs = train_dogs[:2000] + train_cats[:2000] # grab the images from train_dogs and train_cats and concatenate them","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"random.shuffle(train_imgs) #shuffle the training images so they aren't ordered by first half dogs and second half cats","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# now that we have train_imgs, we don't need train_dogs and train_cats so we get rid of them so we don't run out of \n# memory when training our model\ndel train_dogs\ndel train_cats\ngc.collect()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# lets view some images in train_imgs\nimport matplotlib.image as mpimg #import an image plotting module from matplotlib\nfor ima in train_imgs[0:3]: # run a for loop to plot the first three images in train_imgs\n    img = mpimg.imread(ima)\n    imgplot = plt.imshow(img)\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"The images above are not the same dimensions so we need to resize them to all the same. \nLets declare the new dimensions to be 150 x 150 for height and width and 3 channels (for colour)."},{"metadata":{"trusted":true},"cell_type":"code","source":"# resize the images using the cv2 module\nnrows = 150\nncolumns = 150\nchannels = 3","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# function to read and resize the images\ndef read_and_process_image(list_of_images):\n    '''\n    Returns two arrays:\n    X is an array of resized images\n    y is an array of labels\n    '''\n    X = [] # images\n    y = [] # labels\n    \n    for image in list_of_images:\n        # read the image\n        X.append(cv2.resize(cv2.imread(image, cv2.IMREAD_COLOR), (nrows,ncolumns), interpolation=cv2.INTER_CUBIC))\n\n        # get the labels\n        if 'dog' in image[-13:-1]:\n            y.append(1)\n        elif 'cat' in image[-13:-1]:\n            y.append(0)\n        \n    return X, y","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# call the function\nX, y = read_and_process_image(train_imgs)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"X is now an array of image pixel values and y is a list of labels. "},{"metadata":{"trusted":true},"cell_type":"code","source":"# preview of the first image and label\nX[0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y[0:5]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(20,10))\ncolumns = 5\nfor i in range(columns):\n    plt.subplot(5/columns + 1, columns, i+1)\n    plt.imshow(X[i])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import seaborn as sns\ndel train_imgs # delete train_imgs since it has already been converted to an array and saved in X\ngc.collect()\n\n# convert list to numpy array to use in our model as X and y are currently a python array list\nX = np.array(X)\ny = np.array(y)\n\n# lets make sure our labels contain the correct number of images\nsns.countplot(y)\nplt.title('Labels for Cats and Dogs')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# check shape of data\nprint('Shape of train images is:', X.shape)\nprint('Shape of labels is:', y.shape)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"This matches our previous heght, width, and channels."},{"metadata":{},"cell_type":"markdown","source":"## Train Test Split"},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=2) \n# test_size =0.2 means we set 20% of the data to be assigned to the validation set, \n# and 80% to the train set","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print('Shape of train images is:', X_train.shape)\nprint('Shape of test images is:', X_val.shape)\nprint('Shape of labels is:', y_train.shape)\nprint('Shape of labels is:', y_val.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"del X\ndel y\ngc.collect()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# get the length of the train and validation data\nntrain = len(X_train)\nntest = len(X_val)\n\nbatch_size = 32","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Create Model"},{"metadata":{},"cell_type":"markdown","source":"Using convolutional neual network"},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras import layers\nfrom keras import models\nfrom keras import optimizers\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.preprocessing.image import img_to_array, load_img","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Network Architecture"},{"metadata":{},"cell_type":"markdown","source":"Using VGGnet to arrange our convolution layers"},{"metadata":{"trusted":true},"cell_type":"code","source":"model = models.Sequential() \nmodel.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(150, 150, 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)))\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'))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.summary()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Compile The Model"},{"metadata":{"trusted":true},"cell_type":"code","source":"# we'll use the RMSprop optimizer with a learning rate of 0.0001\n# we'll use binary_crossentropy loss because its a binary classification\nmodel.compile(loss='binary_crossentropy', optimizer=optimizers.RMSprop(lr=1e-4), metrics=['acc'])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Image Data Generator"},{"metadata":{"trusted":true},"cell_type":"code","source":"train_datagen = ImageDataGenerator(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)\nval_datagen = ImageDataGenerator(rescale=1./255)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# with the ImageDataGenerator complete, pass the train and validation set\ntrain_generator = train_datagen.flow(X_train, y_train, batch_size=batch_size)\nval_generator = val_datagen.flow(X_val, y_val, batch_size=batch_size)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# training the model\nhistory = model.fit_generator(train_generator,\n                             steps_per_epoch=ntrain // batch_size,\n                             epochs = 64,\n                             validation_data=val_generator,\n                             validation_steps=ntest // batch_size)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"After 64 epochs, the accuracy is about 83%"},{"metadata":{"trusted":true},"cell_type":"code","source":"# save the model for use next time without training again\nmodel.save_weights('model_weights.h5')\nmodel.save('model_keras.h5')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Now lets test our model on the test images"},{"metadata":{"trusted":true},"cell_type":"code","source":"# prediction on the first ten images of the test dataset\nX_test, y_test = read_and_process_image(test_imgs[0:10])\nx = np.array(X_test)\ntest_datagen = ImageDataGenerator(rescale=1./255)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"i = 0\ntext_labels = [] # create a list to hold the labels we will generate\nplt.figure(figsize=(30,20)) # figure size of the images we will plot\nfor batch in test_datagen.flow(x, batch_size=1):\n    pred = model.predict(batch) # make a prediction on the image \n    if pred > 0.5: # if prediction is > 0.5 it should be a dog, so append 'dog'\n        text_labels.append('dog')\n    else: # otherwise, it should be a cat\n        text_labels.append('cat')\n    plt.subplot(5 / columns + 1, columns, i + 1) # subplot to plot multiple images\n    plt.title('This is a ' + text_labels[i])\n    imgplot = plt.imshow(batch[0])\n    i+=1\n    if i % 10 == 0:\n        break\nplt.show()","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":1}