{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport os,cv2,random\nfrom keras.utils import np_utils\nfrom keras.models import Sequential\nfrom keras.layers import Dense,Convolution2D,Flatten,Activation,MaxPooling2D\nfrom keras.utils import np_utils\nfrom keras.callbacks import EarlyStopping\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"TRAIN_DIR = '../input/train/'\nTEST_DIR = '../input/test/'\n\nROWS = 64\nCOLS = 64\nCHANNELS = 3\n\ndog_train_images = [TRAIN_DIR+i for i in os.listdir(TRAIN_DIR) if 'dog' in i]\ncat_train_images = [TRAIN_DIR+i for i in os.listdir(TRAIN_DIR) if 'cat' in i]\ntest_images = [TEST_DIR+i for i in os.listdir(TEST_DIR)]\n\ntrain_images = dog_train_images[:1000] + cat_train_images[:1000]\nrandom.shuffle(train_images)\ntest_images = test_images[:25]\n\ndef read_images(path):\n    img = cv2.imread(path,cv2.IMREAD_COLOR)\n    return cv2.resize(img,(ROWS,COLS),interpolation=cv2.INTER_CUBIC)\n\ndef prep_data(images):\n    size = len(images)\n    data = np.ndarray((size,ROWS,COLS,CHANNELS),np.uint8)\n    \n    for i,image_file in enumerate(images):\n        image = read_images(image_file)\n        data[i] = image\n    return data\n\ntrain = prep_data(train_images)\ntest = prep_data(test_images)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cd9d411dce1e02eacef2265d79e6b281891b7814"},"cell_type":"code","source":"label = []\ndef labeling(images):\n    for i in images:\n        if 'dog' in i:\n            label.append(0)\n        else :\n            label.append(1)\n\nlabeling(train_images)\nsns.countplot(label)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5ff18aed2eed49b854957ef9910d54e29c490352"},"cell_type":"code","source":"def show_images(i):\n    img1 = read_images(cat_train_images[i])\n    img2 = read_images(dog_train_images[i])\n    \n    pair = np.concatenate((img1,img2),axis=1)\n    plt.figure()\n    plt.imshow(pair)\n    plt.show()\n\nfor i in range (40,44):\n    show_images(i)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"414db7b84d99da51001fc3dc5ef0a153641c50c9"},"cell_type":"code","source":"optimizer = 'adam'\nloss = 'binary_crossentropy'\n\nmodel = Sequential()\nmodel.add(Convolution2D(32,3,3,border_mode='same',input_shape = (ROWS,COLS,CHANNELS),activation='relu'))\nmodel.add(Convolution2D(32,3,3,border_mode='same',activation='relu'))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\n\nmodel.add(Convolution2D(64,3,3,border_mode='same',activation='relu'))\nmodel.add(Convolution2D(64,3,3,border_mode='same',activation='relu'))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\n          \nmodel.add(Convolution2D(128,3,3,border_mode='same',activation='relu'))\nmodel.add(Convolution2D(128,3,3,border_mode='same',activation='relu'))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\n          \nmodel.add(Convolution2D(256,3,3,border_mode='same',activation='relu'))\nmodel.add(Convolution2D(256,3,3,border_mode='same',activation='relu'))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\n          \nmodel.add(Flatten())\nmodel.add(Dense(256,activation='relu'))\nmodel.add(Dense(256,activation='relu'))\nmodel.add(Dense(1,activation='sigmoid'))\n          \nmodel.compile(loss=loss,metrics = ['accuracy'],optimizer=optimizer)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"34c7d43fde660e24b7f9a85e480577d093e8292f"},"cell_type":"code","source":"early_stopping = EarlyStopping(patience=2,monitor='val_loss')\nmodel.fit(train,label,epochs=3,callbacks=[early_stopping],batch_size=32)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1cb4bac8ab77075a886c1c25b66ff66ee7161d88"},"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}