{"cells":[{"metadata":{},"cell_type":"markdown","source":"Full Credits to Farhan for the vast majority of the pre-processing/actual machine learning code."},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"!unzip ../input/dogs-vs-cats-redux-kernels-edition/train.zip -d train","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras.layers import Dense, Dropout, Activation, Flatten, Conv2D, MaxPooling2D\nfrom tensorflow.keras.models import Sequential\nimport matplotlib.pyplot as plt\nimport cv2\nimport pickle\nfrom sklearn.model_selection import train_test_split\nimport os, os.path, shutil\nimport numpy as np\n\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"### Sorting into different folders\npath = '../working/train/train'\nimages = [f for f in os.listdir(path) if os.path.isfile(os.path.join(path, f))]\n\nfor image in images:\n    folder_name = image.split('.')[0]\n\n    new_path = os.path.join(path, folder_name)\n    if not os.path.exists(new_path):\n        os.makedirs(new_path)\n\n    old_image_path = os.path.join(path, image)\n    new_image_path = os.path.join(new_path, image)\n    shutil.move(old_image_path, new_image_path)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"path = '/kaggle/working/train/train'\ncatagories = os.listdir(path)\nprint(catagories)\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\nX =[]\ny = []\n\ndef create_data(img_size):\n    for c in catagories:\n        curr_path = os.path.join(path, c)\n        classification = catagories.index(c)\n        print(classification)\n        for img in os.listdir(curr_path):\n            try:\n                img_array = cv2.imread(os.path.join(curr_path, img), cv2.IMREAD_GRAYSCALE)\n                new_array = cv2.resize(img_array, (img_size, img_size))\n                X.append(new_array)\n                y.append(classification)\n            except Exception as e:\n                pass\n            \n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"create_data(50)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X = np.array(X).reshape(-1,50,50,1)\nX = tf.keras.utils.normalize(X, axis = 1)\ny = np.array(y)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"unique, counts = np.unique(y, return_counts=True)\ndict(zip(unique, counts))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"xTrain, xTest, yTrain, yTest = train_test_split(X, y, test_size = 0.1, random_state = 0)\nunique, counts = np.unique(yTrain, return_counts=True)\ndict(zip(unique, counts))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(xTrain.shape[1:])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"yTest = tf.keras.utils.to_categorical(yTest, 2)\nyTrain = tf.keras.utils.to_categorical(yTrain, 2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = Sequential()\n\nmodel.add(Conv2D(64, (3,3),input_shape = xTrain.shape[1:], activation = 'relu'))\n\nmodel.add(MaxPooling2D(pool_size = (2,2)))\n\nmodel.add(Flatten())\nmodel.add(Dense(128, activation = 'sigmoid'))\n\nmodel.add(Dense(2, activation=\"sigmoid\"))\n\nmodel.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy'])\n\nmodel.fit(xTrain, yTrain, batch_size = 32, epochs = 10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":" model.evaluate(xTest, yTest)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import sklearn.metrics as metrics\n\n\nprobs = model.predict_proba(xTest)\nprint(probs)\n\nfpr, tpr, threshold = metrics.roc_curve(y_true = np.ravel(yTest), y_score = np.ravel(probs), pos_label = 1)\nroc_auc = metrics.auc(fpr, tpr)\n\nplt.title('Receiver Operating Characteristic')\nplt.plot(fpr, tpr, 'b', label = 'AUC = %0.2f' % roc_auc)\nplt.legend(loc = 'lower right')\nplt.plot([0, 1], [0, 1],'r--')\nplt.xlim([0, 1])\nplt.ylim([0, 1])\nplt.ylabel('True Positive Rate')\nplt.xlabel('False Positive Rate')\nplt.show()\n","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}