{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"scrolled":true},"cell_type":"code","source":"# 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. https://github.com/jrosebr1/imutils\nimport cv2 # opencv library\nimport os\nprint(\"Imports done\")","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()\nprint(\"Function defined\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9510d8bc95e6f34446413b9a40471caa20c8f15c"},"cell_type":"code","source":"dataset = \"../input/train/train/\"\nprint(\"Dataset variable created\")","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)))\n        \nprint(\"All images processed\")","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":"bUseCompleteDataset = False\n\n# partition the data into training and testing splits, using 75%\n# of the data for training and the remaining 25% for testing\nif bUseCompleteDataset:\n    (trainRI, testRI, trainRL, testRL) = train_test_split(\n        rawImages, labels, test_size=0.25, random_state=42)\n    print(\"Complete train and test sets created\")\nelse:\n    # Select a subset of the entire dataset \n    rawImages_subset = rawImages[:2000]\n    labels_subset= labels[:2000]\n    (trainRI, testRI, trainRL, testRL) = train_test_split(\n        rawImages_subset, labels_subset, test_size=0.25, random_state=42)\n    print(\"Small train and test sets created\")","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"46f0f84e46199a9a03cd11ce94697eb5a449de7d"},"cell_type":"markdown","source":"## K Nearest Neighbors Classifier"},{"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(\"Evaluation in progress\")\nfor k in neighbors:\n    knn_model = KNeighborsClassifier(n_neighbors= k)\n    knn_model.fit(trainRI, trainRL)\n    acc = knn_model.score(testRI, testRL)\n    print(\"[INFO] Raw pixel accuracy ({} neighbors): {:.2f}%\".format(k, acc * 100))\nprint(\"Evaluation completed\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"68b277b0331fdacd19be062e8539b6280222dae8"},"cell_type":"markdown","source":"## Neural Network Classifier"},{"metadata":{"trusted":true,"_uuid":"936022ed1e4d65447c704dcec0145ef7510c4379"},"cell_type":"code","source":"bClassifierTesting_neural = False\nif bClassifierTesting_neural:\n    layer_sizes = [100, 300, 500]\n    activation_types = ['relu', 'identity', 'logistic', 'tanh']\n    rates = ['constant', 'invscaling', 'adaptive']\nelse:\n    layer_sizes = [300]\n    activation_types = ['relu']\n    rates = ['adaptive']\n\nprint(\"Evaluation in progress\")\nfor size in layer_sizes:\n    for act in activation_types:\n        for rate in rates:\n            mlp_model = MLPClassifier(hidden_layer_sizes=size, activation=act, learning_rate=rate)\n            mlp_model.fit(trainRI, trainRL)\n            acc = mlp_model.score(testRI, testRL)\n            print(\"[INFO] Raw pixel accuracy ({}, {}, {}): {:.2f}%\".format(size, act, rate, acc * 100))     \nprint(\"Evaluation completed\")","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"3926b4a17d1a31fbf6c3dff71ce1d6ab043bd5b5"},"cell_type":"markdown","source":"## Support Vector Machine Classifier"},{"metadata":{"trusted":true,"_uuid":"bc7c4a645a8fae367fee23db2eb642cb4d1c1ea5"},"cell_type":"code","source":"bClassifierTesting_support = False\nif bClassifierTesting_support:\n    c_values = [1.0, 5.0, 10.0]\n    kernel_types = [ 'rbf', 'linear', 'poly','sigmoid']\nelse:\n    c_values = [1.0]\n    kernel_types = ['linear']\n\nprint(\"Evaluation in progress\")\nfor c_val in c_values:\n        for k in kernel_types:\n            svc_model = SVC(C=c_val, kernel=k, gamma='scale')\n            svc_model.fit(trainRI, trainRL)\n            acc = svc_model.score(testRI, testRL)\n            print(\"[INFO] Raw pixel accuracy ({}, {}): {:.2f}%\".format(c_val, k, acc * 100))     \nprint(\"Evaluation completed\")","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}