{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Cats vs Dogs with Logistic Regression\n\n- I made this test with the whole purpose to test how does LR perform with image classification. \n- In a real setting I will suggest using convolutional neural networks to do this.\n- Anyway, here is wonderwall...","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom zipfile import ZipFile\nfrom keras.preprocessing.image import load_img \nfrom matplotlib import pyplot as plt\nfrom sklearn.model_selection import train_test_split\nimport cv2\nimport os","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Unzip Data into Train/Test set folders","execution_count":null},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# Extract train and test set into folders\n\nfor dirname, _, filenames in os.walk('../input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\ntrain = \"/kaggle/input/dogs-vs-cats/train.zip\"\ntest = \"/kaggle/input/dogs-vs-cats/test1.zip\"\n\n# Unzip files into working directories\n\nif (not os.path.isdir('/train')):\n    with ZipFile(train, 'r') as zip:  \n        zip.extractall('/train')\n        print('Train Files Extracted') \n\nif (not os.path.isdir('/test')):\n    with ZipFile(test,'r') as zip:\n        zip.extractall('/test')\n        print('Test Files Extracted') ","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"# count the number of files in train and test set\n\ntrain_loc = '/train/train'\ntest_loc = '/test/test1'\n\nprint(\"Training Set File Count:\",len(os.listdir(train_loc)))\nprint(\"Training Set File Count:\",len(os.listdir(test_loc)))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Create Train/Test Sets","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"# this is the format of each file [class].[number].[file format] (dog.890.jpg)\n\ntrain_set_files = os.listdir(train_loc)\n\n# get classes and URL for each image in the traininig set.\n\ny_train = [x.split('.')[0] for x in train_set_files]\nX_train_url = [\"/train/train/\" + x  for x in train_set_files]\n\nprint(y_train[1:3],X_train_url[1:3])\n\n# lets create a dataframe to hold the data so we can later use test-train split\n\ndata = pd.DataFrame(columns=['url','y'])\ndata.url = X_train_url\ndata.y = y_train\n\nprint(data.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# lets print the first image to check how does it looks\n\nimg = load_img(X_train_url[0])\nplt.imshow(img)\n\nprint(\"image size:\",img.size)\n\n# image size (target_size x target_size)\ntarget_size = 64\n\n# estimate the number of features of a flattened image\nimg = cv2.imread(X_train_url[0], cv2.COLOR_BGR2RGB) \nimg = cv2.resize(img, (target_size, target_size))\nimg = img.reshape((target_size*target_size*3, 1))\nfeatures_count = len(img)\nprint(\"size of each image (array size):\",features_count)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# lets create a test/train split out of the train set folder for validation (80/20)\n\nX_train_url, X_test_url, y_train, y_test = train_test_split(data.url, data.y, test_size=0.2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# iterate over each image and lets convert it into a numpy array\n# this might take a while....\n\nX_train = np.ones((1,features_count), int)\n\nfor path in X_train_url: \n    #img = cv2.imread(path, cv2.IMREAD_GRAYSCALE) \n    #img = cv2.resize(img, (target_size, target_size)).flatten()\n    img = cv2.imread(path, cv2.COLOR_BGR2RGB) \n    img = cv2.resize(img, (target_size, target_size))\n    img = img.reshape((target_size*target_size*3, 1))\n    X_train = np.append(X_train,img.T, axis = 0)\n    \nprint(\"X_train created\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_test = np.ones((1,features_count), int)\n    \nfor path in X_test_url: \n    #img = cv2.imread(path, cv2.IMREAD_GRAYSCALE) \n    #img = cv2.resize(img, (target_size, target_size)).flatten() \n    img = cv2.imread(path, cv2.COLOR_BGR2RGB) \n    img = cv2.resize(img, (target_size, target_size))\n    img = img.reshape((target_size*target_size*3, 1))\n    X_test = np.append(X_test,img.T, axis = 0)\n    \nprint(\"X_test created\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# remove extra ones\nX_train = np.delete(X_train, 0, 0)\nX_test = np.delete(X_test, 0, 0)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Train Model & Metrics","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.linear_model import LogisticRegression\n\nX_train_scale = X_train/255\nX_test_scale = X_test/255\n\nmodel = LogisticRegression(max_iter=10000).fit(X_train_scale, y_train)\n\nprint(model)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.metrics import accuracy_score\nfrom sklearn.metrics import classification_report\n\ny_test_predict = model.predict(X_test_scale)\n\n# model accuracy\nprint(\"model acc:\",accuracy_score(y_test, y_test_predict))\n\n# prediction report\nprint(classification_report(y_test, y_test_predict))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"seems LR for this dataset is not performing too well...","execution_count":null}],"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}