{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"collapsed":true},"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport os\nprint(os.listdir(\"../input\"))","execution_count":193,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","collapsed":true,"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"import cv2                 \nimport numpy as np         \nimport os                  \nfrom random import shuffle\nfrom tqdm import tqdm      \n\ntrain_dir = '../input/train'\ntest_dir = '../input/test'","execution_count":194,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"c80cd80be9d850003f68e59057d5ba9a6d52b43a"},"cell_type":"code","source":"def get_label(img):\n    label = img.split('.')[0]\n    if label == 'cat': \n        return [1,0]\n    elif label == 'dog': \n        return [0,1]","execution_count":102,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"f0f1ada45edbc50ee8a715361b535e76dc8731ec"},"cell_type":"code","source":"def making_train_data():\n    training_data = []\n    \n    for img in tqdm(os.listdir(train_dir)):\n        label = get_label(img)\n        path = os.path.join(train_dir,img)\n        img = cv2.imread(path,cv2.IMREAD_GRAYSCALE)\n        img = cv2.resize(img, (50,50))\n        training_data.append([np.array(img),np.array(label)])\n        \n    shuffle(training_data)\n    np.save('train_data.npy', training_data)\n    return training_data","execution_count":103,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"e75ed4e473a4291280a90dfcd7e5bfb21457e954"},"cell_type":"code","source":"def making_test_data():\n    testing_data = []\n    \n    for img in tqdm(os.listdir(test_dir)):\n        path = os.path.join(test_dir , img)\n        img_num = img.split('.')[0]\n        img = cv2.imread(path , cv2.IMREAD_GRAYSCALE)\n        img = cv2.resize(img , (50,50))\n        testing_data.append([np.array(img), img_num])\n        \n    shuffle(testing_data)\n    np.save('test_data.npy', testing_data)\n    return testing_data","execution_count":127,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2c3359f6cfe6e81a34e28831a9c6f48e5dbc5588","collapsed":true},"cell_type":"code","source":"train_data = making_train_data()","execution_count":105,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b9f6580cdf4598e86c4db3fe262aff82aa7c6120","collapsed":true},"cell_type":"code","source":"import numpy\nfrom keras.datasets import cifar10\nfrom keras.models import Sequential\nfrom keras.layers import Dense\nfrom keras.layers import Dropout\nfrom keras.layers import Flatten\nfrom keras.constraints import maxnorm\nfrom keras.optimizers import SGD\nfrom keras.layers.convolutional import Conv2D\nfrom keras.layers.convolutional import MaxPooling2D\nfrom keras.utils import np_utils\nfrom keras import backend as K\nK.set_image_dim_ordering('th')","execution_count":106,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"57e5d2067ad790344118caa9c3a480499a1fb513","collapsed":true},"cell_type":"code","source":"train = train_data[0:20000]\ntest = train_data[20000:25000]","execution_count":107,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c77070b911daae6f0be5d8556270966c7f639978","collapsed":true},"cell_type":"code","source":"print(len(train) , len(test))","execution_count":108,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a5f2755c2a3df145464b27cb69166750a7a6f5e3","collapsed":true},"cell_type":"code","source":"X = np.array([i[0] for i in train]).reshape(-1,1,50,50)\nY = [i[1] for i in train]\n\ntest_x = np.array([i[0] for i in test]).reshape(-1,1,50,50)\ntest_y = [i[1] for i in test]","execution_count":109,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"49511c921e399fd0d7a8c53133d5421ea45a2eb6","collapsed":true},"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator\n\ndatagen = ImageDataGenerator(\n        featurewise_center=False,  \n        samplewise_center=False,  \n        featurewise_std_normalization=False,  \n        samplewise_std_normalization=False,  \n        zca_whitening=False,  \n        rotation_range=10,  \n        zoom_range = 0.0,  \n        width_shift_range=0.1,  \n        height_shift_range=0.1,  \n        horizontal_flip=False, \n        vertical_flip=False)  \n\ndatagen.fit(X)","execution_count":110,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"88c97ab677d33df3c5da5c58509688778fd9dbef","collapsed":true},"cell_type":"code","source":"from keras.callbacks import ReduceLROnPlateau\nlr_reduce = ReduceLROnPlateau(monitor='val_acc', factor=0.1, epsilon=0.0001, patience=1, verbose=1)","execution_count":111,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"283a16994d0b7f9d2415faf3c3128a73afd86f1a","collapsed":true},"cell_type":"code","source":"Y = np.asarray(Y)\nY.reshape(len(Y) , 2)","execution_count":112,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"719b0f7eea83f3911e12505913568b302f878cc2","collapsed":true},"cell_type":"code","source":"test_y = np.asarray(test_y)\ntest_y.reshape(len(test_y) , 2)","execution_count":113,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9295d203164fc199cd4784db0d49c175450508a7","collapsed":true},"cell_type":"code","source":"test_x = test_x.reshape(-1, 1, 50, 50)","execution_count":114,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"577c6251872bde2b13b54be8edea2095e1fd0405","collapsed":true},"cell_type":"code","source":"test_x = test_x / 255\nX = X / 255","execution_count":115,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"af74e6007ef14fbfa487c90c417a2a6d509b8b9b","collapsed":true},"cell_type":"code","source":"X.shape","execution_count":116,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8c6a06cbab7c8fcd7ec4aec37dbe0eb5a9ab2c6f","collapsed":true},"cell_type":"code","source":"from keras.layers import Dense , Activation\nfrom keras.layers import Dropout\nfrom keras.layers import Flatten\nfrom keras.constraints import maxnorm\nfrom keras.optimizers import SGD\nfrom keras.layers import Conv2D , BatchNormalization\nfrom keras.layers import MaxPooling2D\nfrom keras.utils import np_utils\nfrom keras import backend as K\nK.set_image_dim_ordering('th')","execution_count":117,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"da9272b73522ea4388780ba10b622aa11d7ee3f9","collapsed":true},"cell_type":"code","source":"def swish_activation(x):\n    '''\n    Keras implementation of swish https://arxiv.org/abs/1710.05941\n    '''\n    return (K.sigmoid(x) * x)\n\nmodel = Sequential()\n\nmodel.add(Conv2D(32, (3, 3), activation='relu', padding=\"same\", input_shape=(1,50,50)))\nmodel.add(Conv2D(32, (3, 3), padding=\"same\", activation='relu'))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\n\nmodel.add(Conv2D(64, (3, 3), activation='relu', padding=\"same\"))\nmodel.add(Conv2D(64, (3, 3), padding=\"same\", activation='relu'))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\n\nmodel.add(Conv2D(96, (3, 3), dilation_rate=(2, 2), activation='relu', padding=\"same\"))\nmodel.add(Conv2D(96, (3, 3), padding=\"valid\", activation='relu'))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\n\nmodel.add(Conv2D(128, (3, 3), dilation_rate=(2, 2), activation='relu', padding=\"same\"))\nmodel.add(Conv2D(128, (3, 3), padding=\"valid\", activation='relu'))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\n\nmodel.add(Flatten())\n\nmodel.add(Dense(64, activation=swish_activation))\nmodel.add(Dropout(0.4))\nmodel.add(Dense(2 , activation='sigmoid'))\n\nmodel.compile(loss='binary_crossentropy',\n              optimizer='adam',\n              metrics=['accuracy'])\n\nprint(model.summary())","execution_count":119,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"671e43a7e0f1866e6dc751157f6d39aa96755d35","collapsed":true},"cell_type":"code","source":"batch_size = 128\nepochs = 20\n\n\nmodel.compile(loss='binary_crossentropy', optimizer='adam' , metrics=['accuracy'])\nsteps_per_epoch = len(train_data) // batch_size\nvalidation_steps = len((test_x, test_y)) // batch_size","execution_count":120,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9ca97682b7bedf82496cb760f10b8872ae6dd567","collapsed":true},"cell_type":"code","source":"history = model.fit_generator(datagen.flow(X, Y, batch_size=batch_size),\n                    steps_per_epoch=X.shape[0] // batch_size,\n                    callbacks=[lr_reduce],\n                    validation_data=(test_x, test_y),\n                    epochs = epochs, verbose = 2)","execution_count":121,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f41c9194c281f9e2432b7ad32a9ca37f8f2d0291","collapsed":true},"cell_type":"code","source":"score = model.evaluate(test_x, test_y, verbose=0)\nprint('valid loss:', score[0])\nprint('valid accuracy:', score[1])","execution_count":122,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"49d0ac796fdfec9113829eeada58d20b28bb0de7","collapsed":true},"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', 'test'], 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', 'test'], loc='upper left')\nplt.show()","execution_count":123,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2dd16d81767f4493072e8bb13eeda453c1a119da","collapsed":true},"cell_type":"code","source":"test_data = making_test_data()","execution_count":128,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"391070080e653d1a5ac0999b8f546c4106fbcdb9","collapsed":true},"cell_type":"code","source":"temp = np.array(test_data)","execution_count":138,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a3e4bf413b43ee08c94db52ccb22f301bdd08008","collapsed":true},"cell_type":"code","source":"with open('final.csv','w') as f:\n    f.write('id,label\\n')\n            \nwith open('final.csv','a') as f:\n    for data in tqdm(test_data):\n        img_num = data[1]\n        img_data = data[0]\n        orig = img_data\n        data = img_data.reshape(1,1,50,50)\n        model_out = model.predict([data])[0]\n        f.write('{},{}\\n'.format(img_num,model_out[1]))","execution_count":150,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"33a256a0014c8565e88c5cab0c6f55c3462bc017"},"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.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}