{"cells":[{"metadata":{"trusted":true,"_uuid":"b8921714f9d2707effcb74466a433f5a6b229b81"},"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":{"trusted":true,"_uuid":"4ef67c36e505927547d813f1a17be421b13c3dc3"},"cell_type":"code","source":"import cv2                 \nimport os                  \nfrom tqdm import tqdm\nfrom random import shuffle\nfrom random import shuffle\nfrom keras.preprocessing import image\nimport matplotlib.pyplot as plt\nTRAIN_DIR = '../input/train'\nTEST_DIR = '../input/test'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8081c3e78ba95a068d0658016c3afc746a7add70"},"cell_type":"code","source":"IMG_SIZE = 64","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"417ace3c27555b9bc39a99042e7b876ff87801c4"},"cell_type":"code","source":"import glob\ntrain_cats = sorted(glob.glob(os.path.join(TRAIN_DIR, 'cat*.jpg')))\ntrain_dogs = sorted(glob.glob(os.path.join(TRAIN_DIR, 'dog*.jpg')))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d87d2b8a77f7baeba1c991bfa95bed3d980dd025"},"cell_type":"code","source":"len(train_cats), len(train_dogs)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b9ec70c4ae40464d3ff14eb3e5bc69b37b9549c2"},"cell_type":"code","source":"#Creating training and validation splits\ntrain_list = train_cats[:10000] + train_dogs[:10000]\nval_list = train_cats[10000:] + train_dogs[10000:]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"09bfbfcc35ff2e9ce6bc0652a373fa63f304e802"},"cell_type":"code","source":"\nshuffle(train_list)\nshuffle(val_list)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a085f7ea3623290293256678aa3296d855764bce"},"cell_type":"code","source":"len(train_list), len(val_list)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"207221332d9833f302ff46d27effaeec3b6ce3e0"},"cell_type":"code","source":"#Function for defining label\ndef label_img(img):\n    if 'cat' in img: return [0, 1]\n    elif 'dog' in img: return [1, 0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fbebb09b34c6f63b395955123458d0fb64563820"},"cell_type":"code","source":"#Function for resizing images\ndef create_train_data(train_list):\n    training_data = []\n    for img in tqdm(train_list):\n        label = label_img(img)\n        path = img\n        img = cv2.imread(path)\n        img = cv2.resize(img, (IMG_SIZE,IMG_SIZE))\n        training_data.append([np.array(img),label])\n    #np.save('train_data.npy', training_data)\n    return training_data","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"78d4fe645ac1b3737c4ea1b2d1f2784123a6af39"},"cell_type":"code","source":"#Creating array with data\ntrain = create_train_data(train_list)\nval = create_train_data(val_list)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b3bda28d97bbacaf79aa1b588b8c66b85a7b7fa7"},"cell_type":"code","source":"train[0][0].shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8db5a7d6fc9248e26ae6d4acafdae1dceb5c924f"},"cell_type":"code","source":"#Creating training ad Validation arrays\nX = np.array([i[0] for i in train]).reshape(-1,IMG_SIZE,IMG_SIZE, 3)\nY = [i[1] for i in train]\n\nval_X = np.array([i[0] for i in val]).reshape(-1,IMG_SIZE,IMG_SIZE, 3)\nval_Y = [i[1] for i in val]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"360b20850178184cf75c602d7da25fa225799576"},"cell_type":"code","source":"Y = np.asarray(Y)\n#Y.reshape(len(Y) , 2)\n\nval_Y = np.asarray(val_Y)\n#val_Y.reshape(len(val_Y) , 2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"072c1c5616c4b49bee3e4fa361108d046124ef7e"},"cell_type":"code","source":"#Scaling data for neural network\nX = X/float(255)\nval_X = val_X/float(255)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1076c081705b8ec8591969c3de2c455a8eab656f"},"cell_type":"code","source":"X.shape, val_X.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"85bfc027706ad63e0cf550bb6d987e42097ff962"},"cell_type":"code","source":"#Importing keras libraries\nimport keras\nfrom keras.layers import Input, Dense, Flatten, Dropout\nfrom keras.layers import MaxPooling2D, Conv2D\nfrom keras.optimizers import SGD, Adam\nfrom keras.models import Model","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"70df24c2209550fcb5223be249906282ca6c4886"},"cell_type":"code","source":"from keras import backend as K\nK.set_image_data_format('channels_last')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a8e103b49aa8ecf9be04a190cdb1f650c92a7189"},"cell_type":"code","source":"#Network Architecture\nx_input = Input(shape=(IMG_SIZE, IMG_SIZE, 3))\nx = Conv2D(32, (3, 3), padding=\"same\", activation='relu')(x_input)\nx = MaxPooling2D(pool_size=(2, 2))(x)\nx = Conv2D(64, (3, 3), padding=\"same\", activation='relu')(x)\n# x = BatchNormalization()(x)\nx = MaxPooling2D(pool_size=(2, 2))(x)\nx = Conv2D(128, (3, 3), padding=\"same\", activation='relu')(x)\n# x = BatchNormalization()(x)\nx = MaxPooling2D(pool_size=(2, 2))(x)\n\nx = Flatten()(x)\n#Additional Layer\nx = Dense(256, activation=\"relu\")(x)\n\n#x = Dropout(0.3)(x)\n\nx = Dense(16, activation=\"relu\")(x)\nx_out = Dense(2, activation=\"softmax\")(x)\n\n#Specifying input and output\nmodel = Model(inputs=x_input, outputs=x_out)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c886e1e878fafd1ac750ea8f684d177a84508550"},"cell_type":"code","source":"sgd = SGD(lr=0.03, momentum=0.0, decay=0.0, nesterov=False)\n#adam = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False)\n\nmodel.compile(optimizer=sgd, loss = \"binary_crossentropy\", metrics=[\"accuracy\"])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0ff9912e6dec2e097c166369689d801033bb9eff"},"cell_type":"code","source":"model.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"efcc5d7a5066d2ad68d2f482f1c62fe4b964888d"},"cell_type":"code","source":"from keras.callbacks import ModelCheckpoint, ReduceLROnPlateau, EarlyStopping\ncallbacks = [\n    EarlyStopping(monitor = \"val_loss\",\n                 patience = 18,\n                 verbose = 1,\n                 min_delta = 0.001,\n                 mode = \"min\"),\n    ReduceLROnPlateau(monitor = \"val_loss\",\n                     factor = 0.2,\n                     patience = 8,\n                     verbose = 1,\n                     mode = \"min\"),\n    ModelCheckpoint(monitor = \"val_loss\",\n                   filepath = \"Freq_wt.{epoch:02d}-{val_loss:.2f}.hdf5\", \n                   save_best_only=True,\n                   period = 1)]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"aeb4360285de687fda5c77631355d44b97d29460"},"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"62602f12b0d674ebaed9e2189032db7d8dc0b1ff"},"cell_type":"code","source":"history = model.fit(x=X, \n                    y=Y, \n                    validation_data=(val_X, val_Y),\n                    #callbacks = callbacks,\n                    epochs = 40, \n                    batch_size=128, \n                    verbose=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"081ddeadaedb93d312a8ae9d7147e235366d425a"},"cell_type":"code","source":"prediction = model.predict(val_X)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"78dd238c642a857fe5e51048932886ffa8ee6715"},"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nplt.plot(history.history['acc'])\nplt.plot(history.history['val_acc'])\nplt.title('model accuracy')\nplt.ylabel('accuracy')\nplt.xlabel('epoch')\nplt.legend(['train', 'validation'], loc='upper left')\nplt.show()\n# summarize history for loss\nplt.plot(history.history['loss'])\nplt.plot(history.history['val_loss'])\nplt.title('model loss')\nplt.ylabel('loss')\nplt.xlabel('epoch')\nplt.legend(['train', 'validation'], loc='upper left')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"731bc039191920280a33c9e864127a4aaf6c31be"},"cell_type":"code","source":"def print_result(idx):\n    print(\"Probablity of the prediction is \",prediction[idx].tolist())\n    print(\"Actual Label is \",val_Y[idx])\n    (plt.imshow(image.load_img(val_list[idx],target_size=(224,224))))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e06141db59a4f9dc6f8cf4a12ab625794a21bed9"},"cell_type":"code","source":"print_result(5)","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}