{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"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 in \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 \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nimport os\nprint(os.listdir(\"../input\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"import cv2\nimport numpy as np \nimport pandas as pd\nimport matplotlib.pyplot as plt\n%matplotlib inline\nimport random\nimport gc","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"eab48f8eb55add2d2c51b89f93345410c9ff9b08"},"cell_type":"code","source":"train_dir = \"../input/train\"\ntest_dir = \"../input/test\"\n\n\ntrain_dogs = ['../input/train/{}'.format(i) for i in os.listdir(train_dir) if 'dog' in i]\ntrain_cats = ['../input/train/{}'.format(i) for i in os.listdir(train_dir) if 'cat' in i]\n\ntest_img = ['../input/test/{}'.format(i) for i in os.listdir(test_dir)]\n\ntrain_imgs = train_dogs[:2000] + train_cats[:2000]\nrandom.shuffle(train_imgs)\n\ndel train_dogs\ndel train_cats\ngc.collect()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0b8c721f4714db0c7ebd1bc75b6dcfc0e9f73fe6"},"cell_type":"code","source":"train_imgs","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"51cf00fddcbafc1e56440c9640dc563bef43ea6b"},"cell_type":"code","source":"import matplotlib.image as mpimg\nfor ima in train_imgs[6:10]:\n    img = mpimg.imread(ima)\n    imgplot = plt.imshow(img)\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d157e23c2e7bcbc83cc5acc033a6c483d744e28d"},"cell_type":"code","source":"nrows = 150\nncolumns =150\nchannels = 3\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"67fcafda80202a458509fb207d967ecd8b84142e"},"cell_type":"code","source":"def read_and_process_image(list_of_images):\n    \n    x = [] # images\n    y = [] # labels\n    \n    for image in list_of_images:\n        x.append(cv2.resize(cv2.imread(image, cv2.IMREAD_COLOR), (nrows, ncolumns), interpolation=cv2.INTER_CUBIC))\n        if 'dog' in image:\n            y.append(1)\n        elif 'cat' in image:\n            y.append(0)\n            \n            \n    return x, y\n        \n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"de9a8926f399290b67da259be9acf106de232f7e"},"cell_type":"code","source":"x, y = read_and_process_image(train_imgs)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a204df396237a48b7e63d8259a9464dc635d539b"},"cell_type":"code","source":"x[0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ab44aab562abe002246c22dcbf81f0f0b09673b7"},"cell_type":"code","source":"y","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1aa65d6c353d9631f4061c6994d1ff0e896ed6e6"},"cell_type":"code","source":"plt.figure(figsize = (20,10))\ncolumns =5\nfor i in range(columns):\n    plt.subplot(2, columns, i+1)\n    plt.imshow(x[i])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"18b5b28ec99c3f35452ed939486ab8051907dcf9"},"cell_type":"code","source":"import seaborn as sns\ndel train_imgs\ngc.collect()\n\nx = np.array(x)\ny = np.array(y)\n\nsns.countplot(y)\nplt.title('Labels of cats and dogs')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"38779aa056afae3a4f12d5c7dc7691a274e88970"},"cell_type":"code","source":"print('shape of train image is: ', x.shape)\nprint('shape of label is: ', y.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6c820c1115d3cdde8cfebf9482de271cf35ddd8c"},"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\nprint('shape of train image is: ', X_train.shape)\nprint('shape of validation image is: ', X_val.shape)\nprint('shape of labels is: ', y_train.shape)\nprint('shape of label is: ', y_val.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e27a6dd0cb01f72365dd4aa6ea7407bccbafb491"},"cell_type":"code","source":"del x\ndel y\ngc.collect()\n\nntrain = len(X_train)\nnval = len(X_val)\n\nbatch_size = 32","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"37727f83c508ea46b7b105a94df6f9e772130395"},"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\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"352909ddcf0932672ab1ab5bdf15664891bc6fb6"},"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), activation = 'relu'))\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'))\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1d4e8fe69e1d7715e24474bc8c57d002d3b24ec9"},"cell_type":"code","source":"model.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3742693f61ccc1b3649f1340c4fd348d4647ba17"},"cell_type":"code","source":"model.compile(loss='binary_crossentropy', optimizer=optimizers.RMSprop(lr=1e-4), metrics=['acc'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"560746f1bf932ce4bffd8745a483246031ab4774"},"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)\n\nval_datgen = ImageDataGenerator(rescale=1./255)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7a2e65dbd4b77e1315e027a9ea8ad6ff2b41bd30"},"cell_type":"code","source":"train_generator = train_datagen.flow(X_train, y_train, batch_size=batch_size)\nval_generator = val_datgen.flow(X_val, y_val, batch_size=batch_size)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ce1ef78a163c8c486a9b4a4524c327ae9f276055"},"cell_type":"code","source":"history = model.fit_generator(train_generator,\n                             steps_per_epoch=ntrain // batch_size,\n                             epochs=64,\n                             validation_data=val_generator,\n                             validation_steps=nval // batch_size)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"56626b8d228ce3608c2152a8f70a5cc8cd2724d0"},"cell_type":"code","source":"model.save_weights('model_weights.h5')\nmodel.save('model_keras.h5')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b316cd8f9aa4880c65b595a9198c23a84280fbf0"},"cell_type":"code","source":"acc = history.history['acc']\nval_acc = history.history['val_acc']\nloss = history.history['loss']\nval_loss = history.history['val_loss']\n\nepochs = range(1, len(acc) + 1)\n\nplt.plot(epochs, acc, 'b', label='Training accuracy')\nplt.plot(epochs, val_acc, 'r', label='Validation accuracy')\nplt.title('training and validation accuracy')\n\nplt.figure\n\nplt.plot(epochs, loss, 'b', label = 'training loss')\nplt.plot(epochs, val_loss, 'r', label = 'validation loss')\nplt.title('training and validation loss')\nplt.legend()\n\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9e3a8a9411a5ca856a573d9ebf346d6b1d3e1fdd"},"cell_type":"code","source":"X_test, y_test = read_and_process_image(test_img[:10])\nx = np.array(X_test)\ntest_datagen = ImageDataGenerator(rescale = 1./255)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9d850f334e121e15a63ac768a7417a7fa329a7b7"},"cell_type":"code","source":"i = 0\ntext_labels = []\nplt.figure(figsize=(20,10))\nfor batch in test_datagen.flow(x, batch_size = 1):\n    pred = model.predict(batch)\n    if pred > 0.5:\n        text_labels.append('dog')\n    else:\n        text_labels.append('cat')\n    \n    plt.subplot(5/columns + 1, columns, i+1)\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":{"trusted":true,"_uuid":"99808cd2f07f56913a2dee0f4c7a9834843df59d"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}