{"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 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\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"#importing the needed libraries\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport os\nimport cv2","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":" # loading the\nTRAIN_DATA = '/kaggle/input/dogs-vs-cats/train/train'\n\n# go through all the examples of the images\nexamples = { 'dog': [], 'cat': [] }\n\nfor dirname,_, filenames in os.walk(TRAIN_DATA):\n    for filename in filenames:\n        if 'dog' in filename:\n            examples['dog'].append(cv2.imread(os.path.join(dirname, filename), 0))\n        else:\n            examples['cat'].append(cv2.imread(os.path.join(dirname, filename), 0))\nprint(len(examples['dog']))\nprint(len(examples['cat']))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sample_cat = examples['cat'][0]\nplt.imshow(sample_cat, cmap='gray')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"IMG_SIZE = 50\nplt.imshow(cv2.resize(sample_cat, (IMG_SIZE, IMG_SIZE)), cmap='gray')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def create_training_data():\n    TRAIN_DATA = '/kaggle/input/dogs-vs-cats/train/train'\n    # array for the sampel data\n    training_data = []\n    # walk through all the images and laod the training data\n    for dirname,_, filenames in os.walk(TRAIN_DATA):\n        for filename in filenames:\n            category = 0 if 'dog' in filename else 1 \n            img = cv2.imread(os.path.join(dirname, filename), 0)\n            resized_img = cv2.resize(img, (IMG_SIZE, IMG_SIZE))\n            training_data.append([resized_img, category])\n    return training_data","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"training_data = create_training_data()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X = []\ny = []\nfor feature, label in training_data:\n    X.append(feature)\n    y.append(label)\nX = np.array(X).reshape(-1, IMG_SIZE,IMG_SIZE, 1)\nprint(len(X))\nprint(len(y))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import pickle\n\npickle_out = open('X.pickle', 'wb')\npickle.dump(X, pickle_out)\npickle_out.close()\n\n\npickle_out = open('y.pickle', 'wb')\npickle.dump(y, pickle_out)\npickle_out.close()\n\npickle_in = open('X.pickle', 'rb')\nsome_X = pickle.load(pickle_in)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# loading some values here","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Dropout, Activation, Flatten, Conv2D, MaxPooling2D ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_x = X / 255.0","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = Sequential()\n\nmodel.add(Conv2D(64, (3,3), input_shape = X.shape[1:]))\nmodel.add(Activation('relu'))\nmodel.add(MaxPooling2D(pool_size = (2,2)))\n\nmodel.add(Conv2D(64, (3,3)))\nmodel.add(Activation('relu'))\nmodel.add(MaxPooling2D(pool_size = (2,2)))\n\nmodel.add(Flatten())\nmodel.add(Dense(64))\n\nmodel.add(Dense(1))\nmodel.add(Activation('sigmoid'))\n          \n          \nmodel.compile(loss='binary_crossentropy',\n             optimizer = 'adam',\n             metrics = ['accuracy'])\n          \nmodel.fit(train_x, y, batch_size = 32, epochs = 3, validation_split = 0.1)","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":1}