{"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\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\nimport os\n\nfrom tensorflow import keras\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D,Dropout,Dense,Activation,Flatten,MaxPooling2D\nimport pickle\nimport matplotlib.pyplot as plt\nimport cv2\nDATADIR = '../input/cat-and-dog/training_set/training_set'\ncategories = ['dogs','cats']\nfor category in categories:\n    path = os.path.join(DATADIR,category) # puts to cats and dog dir\n    for img in os.listdir(path):\n        img_arry = cv2.imread(os.path.join(path,img),cv2.IMREAD_GRAYSCALE)\n        plt.imshow(img_arry,cmap = 'gray')\n        plt.show()\n        break\n    break\n\n\n\n# You can write up to 5GB 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","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(img_arry.shape)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Normalising the images","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"new_array = cv2.resize(img_arry,(60,60))\n#print(new_array)\nplt.imshow(new_array,cmap ='gray')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"training_data = []\nIMG_SIZE = 60\ndef create_training_data():\n    for category in categories:\n        path = os.path.join(DATADIR,category) # puts to cats and dog dir\n        class_num = categories.index(category)\n        for img in os.listdir(path):\n            try:\n                img_arry = cv2.imread(os.path.join(path,img),cv2.IMREAD_GRAYSCALE)\n                new_array = cv2.resize(img_arry,(IMG_SIZE,IMG_SIZE))\n                training_data.append([new_array,class_num])\n            except Exception as e:  \n                    pass\n               \n            \ncreate_training_data()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(len(training_data))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import random\nrandom.shuffle(training_data)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for sample in training_data:\n    print(sample[1])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X = []\ny = []","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"IMG_SIZE = 60\nfor features,label in training_data:\n    X.append(features)\n    y.append(label)\n    \nX = np.array(X).reshape(-1,IMG_SIZE,IMG_SIZE,1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"## this is for using this dataset sometime else\n## names are given below\n\nimport pickle\npickle_out = open(\"Dogs_and_Cats_X.pickle\",\"wb\")\npickle.dump(X ,pickle_out)\npickle_out.close()\n\n\npickle_out= open(\"Dogs_and_Cats_y\",\"wb\")\npickle.dump(y ,pickle_out)\npickle_out.close()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X = X/255.0","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y = np.array(y)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = Sequential()\nmodel.add(Conv2D(80,(3,3), input_shape =X.shape[1:]))\nmodel.add(Activation('relu'))\nmodel.add(MaxPooling2D(pool_size = (2,2)))\nmodel.add(Dropout(0.2))\n\nmodel.add(Conv2D(80,(3,3)))\nmodel.add(Activation('relu'))\nmodel.add(MaxPooling2D(pool_size = (2,2)))\nmodel.add(Dropout(0.2))\n\nmodel.add(Flatten())\nmodel.add(Dense(80))\nmodel.add(Activation('sigmoid'))\nmodel.add(Dropout(0.2))\n\n\nmodel.add(Dense(1))\nmodel.add(Activation('sigmoid'))\n\nmodel.compile(loss = 'binary_crossentropy',\n             optimizer = 'adam',\n             metrics = ['accuracy'])\n\nmodel.fit(X, y, batch_size=32, epochs=10, validation_split=0.1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pred = model.predict(X[0][0][0][0])\nplt.imshow(pred,cmap ='gray')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}