diff --git a/cat_vs_noncat/cat_simple_nn.ipynb b/cat_vs_noncat/cat_simple_nn.ipynb new file mode 100644 index 0000000..e5f99ec --- /dev/null +++ b/cat_vs_noncat/cat_simple_nn.ipynb @@ -0,0 +1,286 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Cat classification with sigmoid function" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Importing python libraries" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np\n", + "import matplotlib.pyplot as plt\n", + "import h5py\n", + "import scipy\n", + "from PIL import Image\n", + "from scipy import ndimage\n", + "from random import shuffle\n", + "from tqdm import tqdm\n", + "import os\n", + "import cv2\n", + "from sklearn.metrics import accuracy_score" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": {}, + "outputs": [], + "source": [ + "training_data = np.load('C:/Users/Rohit/Music/machine_learning_projects/cat_vs_noncat/cat_vs_noncat_10000.npy')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Preprocessing step:" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [], + "source": [ + "labels = training_data[0, 1]\n", + "for i in range(1, training_data.shape[0]):\n", + "\tlabels = np.concatenate((labels, training_data[i, 1]), axis = 0)\n", + "labels = labels.reshape(labels.shape[0], 1)" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [], + "source": [ + "train_data = training_data[0, 0]\n", + "for i in range(1, training_data.shape[0]):\n", + "\ttrain_data = np.concatenate((train_data, training_data[i, 0]), axis = 1)" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [], + "source": [ + "train_data = train_data/255" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(3072, 19000)\n" + ] + } + ], + "source": [ + "X = train_data[:,:19000]\n", + "Y = labels[:19000,:]\n", + "X_test = train_data[:, 19000:]\n", + "Y_test = labels[19000:, :]\n", + "print(X.shape)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Initialization of parameters. " + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [], + "source": [ + "w = np.zeros((train_data.shape[0], 1))\n", + "b = 0" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": {}, + "outputs": [], + "source": [ + "def sigmoid(z):\n", + "\n", + "\ts = 1/(1 + np.exp(-z.astype(float)))\n", + "\n", + "\treturn s" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Gradient calculation using Forward and Backward Propagation" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": {}, + "outputs": [], + "source": [ + "def propagate(w, b, X, Y):\n", + "\tz = np.dot(X.T, w) + b\n", + "\ta = sigmoid(z)\n", + "\tdz = a - Y\n", + "\tdw = np.dot(X, dz)/X.shape[1]\n", + "\tdb = np.sum(dz)/X.shape[1]\n", + "\t#print(db)\n", + "\tgrads = {\"dw\": dw,\n", + "\t\t\t\"db\": db}\n", + "\treturn grads" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Optimization function" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [], + "source": [ + "def optimize(w, b, X, Y, num_iterations, learning_rate):\n", + "\n", + "\tfor i in range(num_iterations):\n", + "\n", + "\t\tgrads = propagate(w, b, X, Y)\n", + "\t\tdw = grads[\"dw\"]\n", + "\t\tdb = grads[\"db\"]\n", + "\t\tw = w - learning_rate*dw\n", + "\t\tb = b - learning_rate*db\n", + "\tparams = {\"w\": w,\n", + "\t\t\t\t\"b\": b}\n", + "\tgrads = {\"dw\": dw,\n", + "\t\t\t\t\"db\":db}\n", + "\treturn params, grads" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": {}, + "outputs": [], + "source": [ + "params, grads = optimize(w, b, X, Y, num_iterations= 100, learning_rate = 0.009)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Prediction on test data" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [], + "source": [ + "def predict(w, b, X, Y):\n", + "\tz = np.dot(X.T, w) + b\n", + "\ta = sigmoid(z)\n", + "\tfor i in range(a.shape[0]):\n", + "\t\tif a[i, 0] < 0.5:\n", + "\t\t\ta[i, 0] = np.array([0])\n", + "\t\telse:\n", + "\t\t\ta[i, 0] = np.array([1])\n", + "\treturn a\n" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": {}, + "outputs": [], + "source": [ + "w = params[\"w\"]\n", + "b = params[\"b\"]\n", + "pred_Y = predict(w, b, X_test, Y_test)" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Accuracy: 0.631\n" + ] + } + ], + "source": [ + "print(\"Accuracy: \",accuracy_score(Y_test, pred_Y))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "