{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"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\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 read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\nimport zipfile\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with zipfile.ZipFile('/kaggle/input/dogs-vs-cats/train.zip',mode='r') as z:\n    z.extractall(\".\")\nwith zipfile.ZipFile('/kaggle/input/dogs-vs-cats/test1.zip',mode='r') as z:\n    z.extractall(\".\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nfrom tensorflow.keras.models import Sequential \nfrom tensorflow.keras.layers import Dense,Conv2D,MaxPooling2D,Activation,Flatten,Dropout","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"file_store='/kaggle/working/train'","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from matplotlib.image import imread\nfor i in range(9):\n    plt.subplot(330+1+i)\n    filename=file_store+'/dog.'+str(i)+'.jpg'\n    image=imread(filename)\n    plt.imshow(image)\nplt.show()    ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from matplotlib.image import imread\nfor i in range(9):\n    plt.subplot(330+1+i)\n    filename=file_store+'/cat.'+str(i)+'.jpg'\n    image=imread(filename)\n    plt.imshow(image)\nplt.show()   ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for p in os.listdir(file_store):\n    category=p.split('.')[0]\n    img_array=cv2.imread(os.path.join(file_store,p),cv2.IMREAD_GRAYSCALE)\n    new_img_array=cv2.resize(img_array,dsize=(120,120))\n    plt.imshow(new_img_array,cmap='gray')\n    break","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X=[]\nY=[]\nconvert=lambda category : int(category=='dog')\ndef create_test_data(file_store):\n    for p in os.listdir(file_store):\n        category=p.split('.')[0]\n        category=convert(category)\n        img_array=cv2.imread(os.path.join(file_store,p),cv2.IMREAD_GRAYSCALE)\n        new_img_array=cv2.resize(img_array,dsize=(120,120))\n        X.append(new_img_array)\n        Y.append(category)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"file_store_test='/kaggle/working/test1/'\nX_test=[]\nid_line=[]\ndef create_test1_data(file_store_test):\n    for p in os.listdir(file_store_test):\n        id_line.append(p.split('.')[0])\n        img_array=cv2.imread(os.path.join(file_store_test,p),cv2.IMREAD_GRAYSCALE)\n        new_img_array=cv2.resize(img_array,dsize=(120,120))\n        X_test.append(new_img_array)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Training Data","metadata":{}},{"cell_type":"code","source":"create_test_data(file_store)\nX=np.array(X).reshape(-1,120,120,1)\nY=np.array(Y)\nX=X/255.0\nX.shape[1:]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Testing Data","metadata":{}},{"cell_type":"code","source":"create_test1_data(file_store_test)\nX_test = np.array(X_test).reshape(-1,120,120,1)\nX_test = X_test/255","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model=Sequential()\nmodel.add(Conv2D(64,(3,3),activation='relu',input_shape=X.shape[1:]))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\n\nmodel.add(Conv2D(64,(3,3),activation='relu'))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\n\nmodel.add(Conv2D(64,(3,3),activation='relu'))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\n\nmodel.add(Flatten())\n\nmodel.add(Dense(64,activation='relu'))\nmodel.add(Dense(1,activation='sigmoid'))\n\nmodel.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(X,Y,batch_size=32,epochs=10,validation_split=0.2)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictinos=model.predict(X_test)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_val = [int(round(p[0])) for p in predictinos]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df = pd.DataFrame({'id':id_line, 'label':predicted_val})","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.to_csv(\"submission.csv\", index=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}