{"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\nfrom tqdm import tqdm\nimport h5py\npath=\"../input/train/train\"\nnew_path_train=\"../input/train/train_new_resized_96\"\nlabel=[]\ndata1=[]\ncounter=0","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":true,"_uuid":"4748fd86549e0ce454b2fbee444c2989611260d0"},"cell_type":"code","source":"for file in os.listdir(path):\n    image_data=cv2.imread(os.path.join(path,file), cv2.IMREAD_GRAYSCALE)\n    image_data=cv2.resize(image_data,(96,96))\n    if file.startswith(\"cat\"):\n        label.append(0)\n    elif file.startswith(\"dog\"):\n        label.append(1)\n    try:\n        data1.append(image_data/255)\n    except:\n        label=label[:len(label)-1]\n    counter+=1\n    if counter%1000==0:\n        print (counter,\" image data retreived\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d12f7adacb8c02ad6be51f3f45dcbfbe19264205"},"cell_type":"code","source":"from keras import Sequential\nfrom keras.layers import Dense,MaxPooling2D,Conv2D,Flatten,Dropout","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0a3be3c49cf57fa01140530665da92292fadc49e"},"cell_type":"code","source":"model=Sequential()\nmodel.add(Conv2D(kernel_size=(3,3),filters=3,input_shape=(96,96,1),activation=\"relu\"))\nmodel.add(Conv2D(kernel_size=(3,3),filters=10,activation=\"relu\",padding=\"same\"))\nmodel.add(MaxPooling2D(pool_size=(2,2),strides=(2,2)))\nmodel.add(Conv2D(kernel_size=(3,3),filters=3,activation=\"relu\"))\nmodel.add(Conv2D(kernel_size=(5,5),filters=5,activation=\"relu\"))\nmodel.add(MaxPooling2D(pool_size=(3,3),strides=(2,2)))\nmodel.add(Conv2D(kernel_size=(2,2),strides=(2,2),filters=10))\nmodel.add(Flatten())\nmodel.add(Dropout(0.3))\nmodel.add(Dense(100,activation=\"sigmoid\"))\nmodel.add(Dense(1,activation=\"sigmoid\"))\nmodel.summary()\nmodel.compile(optimizer=\"adadelta\",loss=\"binary_crossentropy\",metrics=[\"accuracy\"])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"aa0fe6f15694e79c04ebed85fd954ae727802b93"},"cell_type":"code","source":"import numpy as np\n\ndata1=np.array(data1)\nprint (data1.shape)\ndata1=data1.reshape((data1.shape)[0],(data1.shape)[1],(data1.shape)[2],1)\n#data1=data1/255\nlabels=np.array(label)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7ad8fb0e1e103fb7f9d21a7611a3a5ac4115945a"},"cell_type":"code","source":"print (data1.shape)\nprint (labels.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ad30690020baf1cf5d9847a80cbf3f7c14d148fe"},"cell_type":"code","source":"model.fit(data1,labels,validation_split=0.25,epochs=100,batch_size=10)\nmodel.save_weights(\"model.h5\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"275ee24fffaa7152e4ddb9f66b8bcbedd40a8617","_kg_hide-output":true},"cell_type":"code","source":"test_data=[]\nid=[]\ncounter=0\nfor file in os.listdir(\"../input/test1/test1\"):\n    image_data=cv2.imread(os.path.join(\"../input/test1/test1\",file), cv2.IMREAD_GRAYSCALE)\n    try:\n        image_data=cv2.resize(image_data,(96,96))\n        test_data.append(image_data/255)\n        id.append((file.split(\".\"))[0])\n    except:\n        print (\"ek gaya\")\n    counter+=1\n    if counter%1000==0:\n        print (counter,\" image data retreived\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fe7d4d14690cac7a9006ddb5165e90cb5b5fbf4f"},"cell_type":"code","source":"test_data1=np.array(test_data)\nprint (test_data1.shape)\ntest_data1=test_data1.reshape((test_data1.shape)[0],(test_data1.shape)[1],(test_data1.shape)[2],1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1fdcc6e095e21358e92618f2d0d905fe7c8c7f09"},"cell_type":"code","source":"dataframe_output=pd.DataFrame({\"id\":id})","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8d1e188744e90d0cdad53469322375153e75e040"},"cell_type":"code","source":"predicted_labels=model.predict(test_data1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8c0613c78e3ad256d4aed2959a104532abd19ded"},"cell_type":"code","source":"predicted_labels=np.round(predicted_labels,decimals=2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0e16e9d4d95cb189642b092703fc882595933328"},"cell_type":"code","source":"labels=[1 if value>0.5 else 0 for value in predicted_labels]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d072c5357840cca7b4846dd2786da2f751766b85"},"cell_type":"code","source":"dataframe_output[\"label\"]=labels","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5be17382af31459070c165811e1e54f87675d18f"},"cell_type":"code","source":"dataframe_output.to_csv(\"submission.csv\",index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8da53e7b53ca3298c1355eb2a1ffae83fd3554b9"},"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}