{"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\"))\nimport cv2\nimport matplotlib.pyplot as plt\n%matplotlib inline\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#creating training data\nfiles = os.listdir(\"../input/train\")\nimg_size=80\ntraining_data =[]\n\nfor i in files:\n    path=os.path.join(\"../input/train\",i)\n    \n    #for label as cat or dog\n    name=i.split(\".\")[0]\n    if name==\"cat\":\n        label=0\n    else:\n        label=1\n    #reading images and resize using imread and resize and gray scale is forblack n white images   \n    images=cv2.resize(cv2.imread(path,cv2.IMREAD_GRAYSCALE),(img_size,img_size)) #resize the image\n    training_data.append([np.array(images),np.array(label)])\n    np.random.shuffle(training_data)\n    np.save(\"cat_dog_traing_data.npy\",training_data)\n        ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"training_data=np.array(training_data)\ntraining_data.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#dividing the data into images and label for the neural network\nX= np.array([i[0] for i in training_data])\ny=np.array([i[1] for i in training_data])\n#scale the data\nX=X/255\nX=X.reshape(-1,80,80,1)\nX.shape,y.shape","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"CNN code started"},{"metadata":{"trusted":true},"cell_type":"code","source":"import tensorflow as tf\nfrom keras.models import Sequential \nfrom keras import layers \nfrom keras.layers import Conv2D,MaxPool2D,Dense,Softmax,Flatten","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.layers import Dropout\nfrom keras.optimizers import Adam\nopt=Adam(lr=.0001)\nmodel=Sequential()\nmodel.add(Dense(16,activation=\"relu\",input_shape=(80,80,1)))\nmodel.add(Conv2D(32,(3,3)))#image size with color/bw\nmodel.add(MaxPool2D(2,2))\n\nmodel.add(Conv2D(32,(3,3)))#image size with color/bw\nmodel.add(MaxPool2D(2,2))\n\n\nmodel.add(Flatten())\nmodel.add(Dense(50,activation=\"relu\"))\nmodel.add(Dropout(0.25))\nmodel.add(Dense(50,activation=\"relu\"))\nmodel.add(Dropout(0.25))\nmodel.add(Dense(1,activation=\"sigmoid\"))\n\nmodel.compile(optimizer=opt,loss=\"binary_crossentropy\",metrics=[\"accuracy\"])\nmodel.summary()\n                 ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.fit(X,y,epochs=30,validation_split=0.30)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Testing data"},{"metadata":{"trusted":true},"cell_type":"code","source":"#creating testing data\ntesting_data =[]\nfor imgs in os.listdir(\"../input/test\"):\n        path=os.path.join(\"../input/test\",imgs)\n        #for label as cat or dog\n        name=i.split(\".\")[0]\n        if name==\"cat\":\n            label=0\n        else:\n            label=1\n    \n        images=cv2.resize(cv2.imread(path,cv2.IMREAD_GRAYSCALE),(img_size,img_size))\n        testing_data.append([np.array(images),np.array(label)])\n        np.random.shuffle(testing_data)\n        np.save(\"cat_dog_testing_data.npy\",testing_data)\n        ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#dividing the data into images and label for the neural network\nX_test= np.array([i[0] for i in testing_data]).reshape(-1,80,80,1)\ny_test=np.array([i[1] for i in testing_data])\nX_test=X_test/255","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_test.shape,y_test.shape","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Prediction "},{"metadata":{"trusted":true},"cell_type":"code","source":"y_pred=model.predict(X_test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.save(\"Imageclassfication.h5py\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_image_11=X_test[11].reshape(80,80)\nX_image_11.shape\nplt.imshow(X_image_11)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_test[11]  ","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":" it's as dog"}],"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}