{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"scrolled":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 the files in the input directory\n\nimport os\nprint(os.listdir(\"../input/train\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9548902205315f78f9d6ea596a7a45d3dd66db4e"},"cell_type":"code","source":"# Adopted (and modified) from https://www.pyimagesearch.com/2016/08/08/k-nn-classifier-for-image-classification/\nfrom sklearn.neighbors import KNeighborsClassifier\nfrom sklearn.neural_network import MLPClassifier\nfrom sklearn.svm import SVC\nfrom sklearn.model_selection import train_test_split\nfrom imutils import paths\nimport numpy as np\nimport imutils # a simple image utility library\nimport cv2 #opencv library\nimport os","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2ba6d6ea34bd8a0f36d2873b6b0960ee8a9b8775"},"cell_type":"code","source":"def image_to_feature_vector(image, size=(32, 32)):\n# resize the image to a fixed size, then flatten the image into\n# a list of raw pixel intensities\n    return cv2.resize(image, size).flatten()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9510d8bc95e6f34446413b9a40471caa20c8f15c"},"cell_type":"code","source":"dataset = \"../input/train/train/\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"970c37bf80b54b5e186061d0fe39c5a34f4c1995"},"cell_type":"code","source":"# grab the list of images that we'll be describing\nprint(\"[INFO] describing images...\")\nimagePaths = list(paths.list_images(dataset))\nprint(len(imagePaths))\nprint(imagePaths[0])\n# initialize the raw pixel intensities matrix, the features matrix,\n# and labels list\nrawImages = []\nlabels = []","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2c3f13675a86fd1a762649ef52114b5151be16e5"},"cell_type":"code","source":"# loop over the input images\nfor (i, imagePath) in enumerate(imagePaths):\n\t# load the image and extract the class label (assuming that our\n\t# path as the format: /path/to/dataset/{class}.{image_num}.jpg\n\timage = cv2.imread(imagePath)\n\tlabel = imagePath.split(os.path.sep)[-1].split(\".\")[0]\n \n\t# extract raw pixel intensity \"features\", followed by a color\n\t# histogram to characterize the color distribution of the pixels\n\t# in the image\n\tpixels = image_to_feature_vector(image)\n\t\n \n\t# update the raw images, features, and labels matricies,\n\t# respectively\n\trawImages.append(pixels)\n\tlabels.append(label)\n \n\t# show an update every 1,000 images\n\tif i > 0 and i % 1000 == 0:\n\t\tprint(\"[INFO] processed {}/{}\".format(i, len(imagePaths)))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e3b2adca6b9b7f83038f0a5d1c3a2118d1afdede"},"cell_type":"code","source":"# show some information on the memory consumed by the raw images\n# matrix and features matrix\nrawImages = np.array(rawImages)\nlabels = np.array(labels)\nprint(\"[INFO] pixels matrix: {:.2f}MB\".format(\n\trawImages.nbytes / (1024 * 1000.0)))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5fab6cded70110b1fc3494799346a6c31793f9c9"},"cell_type":"code","source":"# partition the data into training and testing splits, using 75%\n# of the data for training and the remaining 25% for testing\n(trainRI, testRI, trainRL, testRL) = train_test_split(\n\trawImages, labels, test_size=0.25, random_state=42)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"96f099b5712bfb068bd93211725ff11cfa70b3cf"},"cell_type":"code","source":"# Select a subset of the entire dataset \nrawImages_subset = rawImages[:2000]\nlabels_subset= labels[:2000]\n(trainRI, testRI, trainRL, testRL) = train_test_split(\n\trawImages_subset, labels_subset, test_size=0.25, random_state=42)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"70ccd1c8cae68e8af1692316dbfb8fd62355a476"},"cell_type":"code","source":"# train and evaluate a k-NN classifer on the raw pixel intensities\nprint(\"[INFO] evaluating raw pixel accuracy...\")\nneighbors = [1, 3, 5, 7, 13]\n\nprint(\"Commencing evaulation.\")\nfor k in neighbors:\n    model = KNeighborsClassifier(n_neighbors= k)\n    model.fit(trainRI, trainRL)\n    acc = model.score(testRI, testRL)\n    print(\"[INFO] raw pixel accuracy: {:.2f}%\".format(acc * 100))\nprint(\"Evaulation complete.\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"68b277b0331fdacd19be062e8539b6280222dae8"},"cell_type":"code","source":"# Nerual Network Classifier\n\n# train and evaluate a Neural Network classifier on the raw pixel intensity\nprint(\"[INFO] evaluating raw pixel accuracy...\")\nhidden_layers = [150, 350, 550]\nactivation = ['identity','logistic','tanh','relu']\nlearning_rate = ['constant','invscaling','adaptive']\n\nprint(\"Commencing evaluation.\")\nfor layer in hidden_layers:\n    for act in activation:\n        for rate in learning_rate:\n            mlp = MLPClassifier(hidden_layer_sizes = layer, activation = act, learning_rate = rate)\n            mlp.fit(trainRI, trainRL)\n            accuracy = mlp.score(testRI, testRL)\n            print(\"[INFO] raw pixel accuracy: ({}, {}, {}): {:.2f}%\".format(layer, act, rate, accuracy * 100))\nprint(\"Evaluation complete.\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"11853804ecd72341940f2980cd5ff804e4711e9a"},"cell_type":"code","source":"# Support Vector Machine Classifier\n\nprint(\"[INFO] evaluating raw pixel accuracy...\")\nc_num = [1.0, 3.0, 5.0, 7.0, 9.0]\nkernel_type = ['linear','poly','rbf','sigmoid']\n\nprint(\"Commencing evaluation.\")\nfor c in c_num:\n    for kern in kernel_type:\n        svc = SVC(C=c, kernel = kern, gamma = 'scale')\n        svc.fit(trainRI, trainRL)\n        accuracy = svc.score(testRI, testRL)\n        print(\"[INFO] raw pixel accuracy: ({}, {}): {:.2f}%\".format(c, kern, accuracy * 100))\nprint(\"Evaulation complete.\")","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}