<file_sep>import matplotlib.pyplot as plt
import numpy as np

labels = ["Poly"]

values = np.linspace(.001,3,5)

results = [0.0098, 0.0098, 0.26961, 0.08987, 0.04052]
std = [0.0, 0.0, 0.01274, 0.00594, 0.00455]

plt.style.use("ggplot")

fig = plt.figure(figsize=(14,9))

ax = fig.add_subplot(111)
ax.errorbar(values, results, yerr=std, label=labels[0])
ax.legend()

plt.title("Varying Degree Parameter in the Poly Kernel")
plt.xlabel("Degree")
plt.ylabel("Accuracy")

text = "Feature Extractor: SURF\nNumber of Clusters: 800\n(Best from Graph #1)\nC: 1\nGamma: Auto"
ax.text(-.1,0,text,verticalalignment='bottom', horizontalalignment='left')

plt.show()
<file_sep>import csv
import matplotlib.pyplot as plt
import numpy as np

path = "./graph1_accuracy.csv"

x_axis = []
y_axis = []

with open(path, "rb") as file:
    reader = csv.reader(file)
    for row in reader:
        x_axis.append(row[1])
        y_axis.append(row[2])

plt.plot(x_axis, y_axis)
plt.locator_params(numticks=4)
plt.show()
<file_sep>import matplotlib.pyplot as plt
import numpy as np

labels = ["Ball Tree", "KD Tree", "Brute"]

values = range(1,31,1)

ball_results = [0.17516, 0.17516, 0.17941, 0.17712, 0.17974, 0.17876, 0.1768, 0.17484, 0.1719, 0.17288, 0.1719, 0.17059, 0.16993, 0.17059, 0.17157, 0.1683, 0.16503, 0.16242, 0.16209, 0.15882, 0.15915, 0.15817, 0.15784, 0.15817, 0.1585, 0.15686, 0.15817, 0.15686, 0.15588, 0.15621]
ball_std = [0.01229, 0.01229, 0.01488, 0.01087, 0.00895, 0.00989, 0.00829, 0.00941, 0.0064, 0.00443, 0.00823, 0.0069, 0.00752, 0.0065, 0.00547, 0.00547, 0.00547, 0.00824, 0.00696, 0.00855, 0.00939, 0.01255, 0.01072, 0.01071, 0.00846, 0.0067, 0.00903, 0.00766, 0.00658, 0.00811]

kd_results = [0.17516, 0.17516, 0.17941, 0.17712, 0.17974, 0.17876, 0.1768, 0.17484, 0.1719, 0.17288, 0.1719, 0.17059, 0.16993, 0.17059, 0.17157, 0.1683, 0.16503, 0.16242, 0.16209, 0.15882, 0.15915, 0.15817, 0.15784, 0.15817, 0.1585, 0.15686, 0.15817, 0.15686, 0.15588, 0.15621]
kd_std = [0.01229, 0.01229, 0.01488, 0.01087, 0.00895, 0.00989, 0.00829, 0.00941, 0.0064, 0.00443, 0.00823, 0.0069, 0.00752, 0.0065, 0.00547, 0.00547, 0.00547, 0.00824, 0.00696, 0.00855, 0.00939, 0.01255, 0.01072, 0.01071, 0.00846, 0.0067, 0.00903, 0.00766, 0.00658, 0.00811]

brute_results = [0.17516, 0.17516, 0.17941, 0.17712, 0.17974, 0.17876, 0.1768, 0.17484, 0.1719, 0.17288, 0.1719, 0.17059, 0.16993, 0.17059, 0.17157, 0.1683, 0.16503, 0.16242, 0.16209, 0.15882, 0.15915, 0.15817, 0.15784, 0.15817, 0.1585, 0.15686, 0.15817, 0.15686, 0.15588, 0.15621]
brute_std = [0.01229, 0.01229, 0.01488, 0.01087, 0.00895, 0.00989, 0.00829, 0.00941, 0.0064, 0.00443, 0.00823, 0.0069, 0.00752, 0.0065, 0.00547, 0.00547, 0.00547, 0.00824, 0.00696, 0.00855, 0.00939, 0.01255, 0.01072, 0.01071, 0.00846, 0.0067, 0.00903, 0.00766, 0.00658, 0.00811]

plt.style.use("ggplot")

fig = plt.figure(figsize=(14,9))

ax = fig.add_subplot(111)
ax.errorbar(values, ball_results, yerr=ball_std, label=labels[0])
ax.legend()

ax = fig.add_subplot(111)
ax.errorbar(values, kd_results, yerr=kd_std, label=labels[1])
ax.legend()

ax = fig.add_subplot(111)
ax.errorbar(values, brute_results, yerr=brute_std, label=labels[2])
ax.legend()

plt.title("Varying Number of Neighbors with Different Algorithms")
plt.xlabel("Number of Neighbors")
plt.ylabel("Accuracy")

text = "(All of the results were exactly the same)\n\nFeature Extractor: SURF\nNumber of Clusters: 400\n(Best from Graph #1)\nWeight: Distance\nP: 2"

ax.text(0,.1435,text,verticalalignment='bottom', horizontalalignment='left')

plt.show()
<file_sep># need
import cv2
import glob
import numpy as np
import pickle
import os
from sklearn.utils import shuffle
import math
import random
from itertools import compress
from shutil import copy

def saveImagesToPickle(path, color, out_filename):
    """ Reads all images from a certain path. The images will be saved to a .pkl file as
    a dictionary. The keys are the name of the categories (which are to be the folder
    names).

    Params:
        path: The location of the images
        color: Boolean of whether to encode the images in color or not
        out_filename: Name of the pickle file to be saved

    Returns:
        None (The images are saved to the disk as a .pkl file)
    """
    all_images = {}

    for folder in glob.glob(path + "*"):

        # Set color flags
        if color:
            color_flag = cv2.IMREAD_COLOR
        else:
            color_flag = cv2.IMREAD_GRAYSCALE

        # Get name of category which is the folder name
        category = folder.split("\\")[-1]

        all_images[category] = []

        # Loop through all images and read them
        for image in glob.glob(path + category + "/*"):
            if color:
                image = cv2.imread(image, color_flag)
            else:
                image = cv2.imread(image, color_flag)

            all_images[category].append(image)

        # Save all images to disk
    pickle.dump(all_images, open("../tmp/objects/" + out_filename + ".pkl", "wb"))

def loadImagesRandomly(file, n_category_train):
    """ Loads the images from a .pkl file. This assumes the .pkl file contains a
    dictionary with the keys being a string of the category name and the values
    to be a list of images. The images that are selected are chosen at random.

    Params:
        file: The location of the .pkl file
        n_category_train: The number of images to randomly select
                            from each category for the training set

    Returns:
        images: A list containing all the iamges. The length will be
                num_categories * num_per_category
        target: A list of labels that match up with the images respectively
    """
    all_images = pickle.load(file)
    X_train = []
    y_train = []
    X_test = []
    y_test = []

    # Loop through each category and pull out n_category_train images
    # for the training set and use the rest of the dataset as the testing
    # set
    for category in all_images:
        category_length = list(range(len(all_images[category])))
        rand_indexes = random.sample(category_length, k=n_category_train)
        complement = []
        for i in category_length:
            if i not in rand_indexes:
                complement.append(i)

        # Append images and labels to train list
        for i in rand_indexes:
            X_train.append(all_images[category][i])
            y_train.append(category)

        # Append images and labels to test list
        for i in complement:
            X_test.append(all_images[category][i])
            y_test.append(category)

    return X_train, X_test, y_train, y_test

def cropImages(images, size):
    """ Loops through and crops images to a certain size
    Params:
    images: A list of the images to crop
    size: A list where the first two elements are taken for the new dimensions. [width, height]
    Returns: A list of the resized images
    """
    cropped_images = []
    for image in images:
        cropped_images.append(cv2.resize(image, (size[0], size[1])))

    cropped_images = np.asarray(cropped_images)
    return cropped_images

def divideImages(data_path, train, test, n_category_train):
    base_path = os.getcwd()
    data_path = os.path.join(base_path, data_path)
    categories = os.listdir(data_path)
    training_path = os.path.join(base_path, train)
    testing_path = os.path.join(base_path, test)

    # Loop through folders
    for cat in categories:

        image_files = os.listdir(os.path.join(data_path, cat))
        category_len_list = list(range(len(image_files)))

        # Get indexes of n_category_train number of images
        rand_indexes = random.sample(category_len_list, k=n_category_train)

        # Get the rest of the images
        complement = []
        for i in category_len_list:
            if i not in rand_indexes:
                complement.append(i)

        # Create lists of image paths using respective indexes
        training_files = []
        testing_files = []

        for i in rand_indexes:
            training_files.append(image_files[i])

        for i in complement:
            testing_files.append(image_files[i])

        # Copy training files
        for image in training_files:
            origin_path = os.path.join(data_path, cat, image)
            dest_dir = os.path.join(training_path, cat)
            dest_path = os.path.join(training_path, cat, image)
            if not os.path.isdir(dest_dir):
                os.mkdir(dest_dir)
            copy(origin_path, dest_path)

        # Copy testing files
        for image in testing_files:
            origin_path = os.path.join(data_path, cat, image)
            dest_dir = os.path.join(testing_path, cat)
            dest_path = os.path.join(testing_path, cat, image)
            if not os.path.isdir(dest_dir):
                os.mkdir(dest_dir)
            copy(origin_path, dest_path)

def divideImagePaths(path, n_category_train):

    filenames = {}
    training_files = []
    testing_files = []

    for folder in glob.glob(path + "*"):
        category = folder.split("\\")[-1]
        filenames[category] = []
        for image in glob.glob(folder + "/*"):
            filenames[category].append(image)

    for cat in filenames:
        image_files = os.listdir(os.path.join(path, cat))
        category_len_list = list(range(len(image_files)))
        rand_indexes = random.sample(category_len_list, k=n_category_train)

        complement = []
        for i in category_len_list:
            if i not in rand_indexes:
                complement.append(i)

        for i in rand_indexes:
            training_files.append(path + image_files[i])

        for i in complement:
            testing_files.append(path + image_files[i])

    training_files = np.asarray(training_files)
    testing_files = np.asarray(testing_files)

    np.save("training_files.npy", training_files)
    np.save("testing_files.npy", testing_files)<file_sep>import keras
from keras.preprocessing.image import ImageDataGenerator
from models import marconet
from keras.optimizers import sgd


##########################
# Constants
##########################

batch_size = 64
epochs = 10

image_input_shape = (224, 224, 3)
num_classes = 101
dropout = 0.5
weight_decay = .01


##########################
# Pre-Processing
##########################

train_data_path = "./data/train/"
test_data_path = "./data/test/"


train_data_generator = ImageDataGenerator(
    # rotation_range=20,
    # horizontal_flip=True,
    # vertical_flip=True,
    # zoom_range=0.2,
    # width_shift_range=0.2,
    # height_shift_range=0.2,
    # fill_mode="wrap"
)

test_data_generator = ImageDataGenerator(
    #
)

train_generator = train_data_generator.flow_from_directory(
    train_data_path,
    batch_size=batch_size,
    shuffle=True,
    target_size=(224, 224)
)

test_generator = test_data_generator.flow_from_directory(
    test_data_path,
    batch_size=batch_size,
    shuffle=False,
    target_size=(224, 224)
)

##########################
# Model
##########################

# import matplotlib.pyplot as plt
# from PIL import Image
# test = train_generator[0][0]
# for i in range(10):
#     img = train_generator[0][0][i]
#     plt.imshow(img/255.0)
#     plt.show()

# model = marconet.build_marconet()
model = keras.models.load_model("model_marconet_snapshot.h5")
# model = keras.applications.VGG16(weights=None, classes=102)

sgd = sgd(lr=.05, decay=5e-4, momentum=.9, nesterov=True)
# adam = optimizers.adam()
# opt = keras.optimizers.rmsprop(lr=0.0001, decay=1e-6)
model.compile(optimizer=sgd, loss='categorical_crossentropy', metrics=['acc'])

##########################
# Fit
##########################

# model.fit_generator(
#     train_generator,
#     epochs=epochs,
#     validation_data=(test_generator),
#     shuffle=True,
#     verbose=1
# )

##########################
# Predict
##########################

import matplotlib.pyplot as plt
import numpy as np

# predictions = model.predict_generator(test_generator, verbose=1)
# np.save("./predictions.npy", predictions)

predictions = np.load(open("predictions.npy", "rb"))
results = []
class_percentage = {}
percentages = []
key_to_label = {v: k for k, v in test_generator.class_indices.items()}

for category in test_generator.class_indices:
    class_percentage[category] = [0, 0]

for prediction in predictions:
    results.append(np.argmax(prediction))

for r, a in zip(results, test_generator.classes):
    class_name = key_to_label[a]
    class_percentage[class_name][0] += 1

    if r == a:
        class_percentage[class_name][1] += 1

for category in class_percentage:
    total = class_percentage[category][0]
    correct = class_percentage[category][1]
    percentage = correct / total
    percentages.append(percentage)
    class_percentage[category] = percentage
    print("Category: {} - Accuracy: {}".format(category, percentage))

print("Mean class average: {}".format(np.mean(percentages)))

stop

# for i in range(test_generator.samples):
#     if i % 10:
#         img = test_generator[0][0][i, :, :, :]
#         plt.imshow(img / 255.0)
#         predictions = np.argmax(model.predict(np.expand_dims(img, axis=0)))
#         key_to_label = {v: k for k, v in test_generator.class_indices.items()}
#         # plt.title(key_to_label[predictions])
#         plt.title(key_to_label[test_generator.classes[i]])
#         plt.show()



# model.save('model_marconet_snapshot_2.h5')
<file_sep>

class SVM(Classifer):
    """ This class allows the user to classify images using support vector machine.

    Params:
        svm_params: A dictionary of all the Classifiers parameters
    """

    def __init__(self, images, labels, detector, n_clusters, bow="True", **svm_params):
        Classifer.__init__(images, labels, detector, n_clusters, bow)
        self.model = SVC(**svm_params)
        self.scale = None



    def predict(self, images):


        features = self._get_features(images, self.detector)

        if self.bow:
            histogram = self._make_histograms(features)

        return self.model.predict(histogram)<file_sep>from sklearn import cluster
from sklearn.preprocessing import StandardScaler
import cv2
from sklearn.svm import SVC
from sklearn.neighbors import KNeighborsClassifier
import numpy as np

class Classifer:
    """ This class builds a classifier using visual bag of words

     Params:
        classifier: The classifier you wish to use
        images: The training images
        labels: The image labels
        detector: Which feature detector to use. Either "sift" or "surf"
        n_clusters: The number of clusters to use for KMeans. This only applied if using bow.
        bow: Whether or not to use bow (Currently no other option. Must use bow)
        **params: A dictionary of the parameters for the classifier
     """

    def __init__(self, classifier, images, labels, detector, n_clusters, bow=True, **params):
        self.images = images
        self.labels = labels
        self.detector = detector
        self.n_clusters = n_clusters
        self.bow = bow
        self.classifier = classifier
        self.scale = None

        if self.classifier == "svm":
            self.model = SVC(**params)
        elif self.classifier == "knn":
            self.model = KNeighborsClassifier(**params)

        self.features = self._get_features(images, detector)

        if self.bow:
            self.kmeans = cluster.MiniBatchKMeans(self.n_clusters)
            self.vocabulary = self._make_histograms(self.features)
            self.model.fit(self.vocabulary, self.labels)

    def _get_features(self, images, detector):
        """ Gets features from numpy array of images.

        Params:
            images: A list of numpy images
            detector: A string of which feature detector to use.

        Returns:
            Returns a list of numpy arrays that contain the features of each image
        """

        if detector == "sift":
            sift = cv2.xfeatures2d.SIFT_create()
        elif detector == "surf":
            surf = cv2.xfeatures2d.SURF_create()

        features = []

        for image in images:
            if detector == "sift":
                kp, feats = sift.detectAndCompute(image, None)
                features.append(feats)
            if detector == "surf":
                kp, feats = surf.detectAndCompute(image, None)
                features.append(feats)

        return features

    def _make_histograms(self, features):
        """ Creates a numpy array whose shape is (Frequency, Number of clusters)

        Params:
            features: A list containing separate numpy arrays of features for each image.
                Each element in the list is a numpy array of the features for an image.

        Returns: The vocabulary that is a numpy array with the shape
            (number of images, number of clusters).
         """

        # Move all the features together into one list and create a separate list
        # containing an image label for that feature
        all_features = []
        all_features_labels = []

        label = 0
        for image in features:
            for feature in image:
                all_features.append(feature)
                all_features_labels.append(label)
            label += 1

        # Cluster all features using Kmeans
        clustered_features = self.kmeans.fit_predict(all_features)
        img_count = len(features)
        vocabulary = np.array([np.zeros(self.n_clusters) for i in range(img_count)])

        for i in range(len(clustered_features)):
            image_id = all_features_labels[i]
            visual_word_id = clustered_features[i]
            vocabulary[image_id][visual_word_id] += 1

        # Scale the data
        if self.scale is None:
            self.scale = StandardScaler().fit(vocabulary)

        vocabulary = self.scale.transform(vocabulary)

        return vocabulary

    def predict(self, images):
        """ Predicts the categories of a list of numpy images.

        Params:
            images: A list of numpy images

        Returns:
            A list of predictions
         """

        features = self._get_features(images, self.detector)

        if self.bow:
            histogram = self._make_histograms(features)

            return self.model.predict(histogram)<file_sep>import cv2

class Sift():
    def __init__(self):
        self.sift = cv2.xfeatures2d.SIFT_create()

    def getImageFeatures(self, image):
        keypoints, features = self.sift.detectAndCompute(image, None)
        return features

    def getDataFeatures(self, images):

        data_features = []

        for image in images:
            data_features.append(self.getImageFeatures(image))

        return data_features

class Surf():
    def __init__(self):
        self.surf = cv2.xfeatures2d.SURF_create()

    def getImageFeatures(self, image):
        keypoints, features = self.surf.detectAndCompute(image, None)
        return features

    def getDataFeatures(self, images):

        data_features = []

        for image in images:
            data_features.append(self.getImageFeatures(image))

        return data_features

<file_sep>import time
from FeatureExtraction import *
from Classifiers import *
from ImageProcessing import *
from BagOfWords import *

class Testing:

    def testSVM(self, K, CV, n_categories, n_train_imgs, C, kernel, gamma, dfs, path, feature_extractor, value):

        print("Processing value: {}".format(value))
        start = time.time()

        # image processing
        print("Processing images for value {}".format(value))
        ip = ImageProcessing()
        svm = SVM(C, kernel, gamma, dfs)
        category_limit = n_train_imgs/n_categories
        images, target, img_count = ip.readImages(path, category_limit)

        data_features = feature_extractor.getDataFeatures(images)

        # data representation
        print("Processing data representation for value {}".format(value))
        bow = BagOfWords(K)
        data = bow.makeVocabulary(data_features, img_count)

        # classification
        print("Processing classification for value {}".format(value))
        scores = svm.cross_validate(data, target, CV)
        end = time.time()
        print("Finished processing value: {} Time elapsed: {}".format(value, end-start))
        print("Value {}'s scores: {}".format(value, scores))

        return scores

    def testKNN(self, K, CV, n_categories, n_train_imgs, path, feature_extractor, n_neighbors, weights, value):

        print("Processing value: {}".format(value))
        start = time.time()

        # image processing
        print("Processing images for value {}".format(value))
        ip = ImageProcessing()
        knn = KNN(n_neighbors, weights)
        category_limit = n_train_imgs/n_categories
        images, target, img_count = ip.readImages(path, category_limit)

        data_features = feature_extractor.getDataFeatures(images)

        # data representation
        print("Processing data representation for value {}".format(value))
        bow = BagOfWords(K)
        data = bow.makeVocabulary(data_features, img_count)

        # classification
        print("Processing classification for value {}".format(value))
        scores = knn.cross_validate(data, target, CV)
        end = time.time()
        print("Finished processing value: {} Time elapsed: {}".format(value, end-start))
        print("Value {}'s scores: {}".format(value, scores))

        return scores

    def testTrainingData(self, K, n_categories, n_train_imgs, C, kernel, gamma, dfs, path, feature_extractor, value):
        print("Processing value: {}".format(value))
        start = time.time()

        # image processing
        print("Processing images for value {}".format(value))
        ip = ImageProcessing()
        svm = SVM(C, kernel, gamma, dfs)
        category_limit = n_train_imgs / n_categories
        images, target, img_count = ip.readImages(path, category_limit)

        data_features = feature_extractor.getDataFeatures(images)

        # data representation
        print("Processing data representation for value {}".format(value))
        bow = BagOfWords(K)
        data = bow.makeVocabulary(data_features, img_count)

        # classification
        print("Processing classification for value {}".format(value))
        score = svm.training_set_accuracy(data, target, .30)
        end = time.time()
        print("Finished processing value: {} Time elapsed: {}".format(value, end - start))
        print("Value {}'s scores: {}".format(value, score))

        return score


<file_sep>https://docs.google.com/presentation/d/12qr0uVl_mSnhTXN7G3i20MDHA2w7djscZ1KrPDVKr-o/edit?usp=sharing
<file_sep>import tensorflow as tf
import numpy as np
from tensorflow.examples.tutorials.mnist import input_data
from ImageProcessing import *
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split, KFold
from sklearn.preprocessing import LabelBinarizer

DATA_DIR = "/tmp/data"
NUM_STEPS = 95
NUM_MINI_BATCHES = 20

# Pre processing

ip = ImageProcessing()

images, target, img_count = ip.readImages("./images/",30)

cropped_images = []
for image in images:
    cropped_images.append(cv2.resize(image,(100,100)))


cropped_images = np.asarray(cropped_images)
cropped_images_split = np.split(cropped_images, NUM_MINI_BATCHES)

lb = LabelBinarizer()
target = lb.fit_transform(target)

target_split = np.split(target, NUM_MINI_BATCHES)

# CNN

# Species the weights for either fully connected or convolutional layers of the network
def weight_variable(shape):
    initial = tf.truncated_normal(shape, stddev=0.1)
    return tf.Variable(initial)

def bias_vaiable(shape):
    initial = tf.constant(0.1, shape=shape)
    return tf.Variable(initial)

def conv2d(x, W, stride):
    return tf.nn.conv2d(x, W, strides=stride, padding="SAME")

def max_pool_2x2(x):
    return tf.nn.max_pool(x, ksize=[1,2,2,1],
                          strides=[1,2,2,1], padding="SAME")

def max_pool_3x3(x):
    return tf.nn.max_pool(x, ksize=[1,3,3,1],
                          strides=[1,2,2,1], padding="SAME")

def conv_layer(input, shape, stride):
    W = weight_variable(shape)
    b = bias_vaiable([shape[3]])
    return tf.nn.relu(conv2d(input, W, stride) + b)

def full_layer(input, size):
    in_size = int(input.get_shape()[1])
    W = weight_variable([in_size, size])
    b = bias_vaiable([size])
    return tf.matmul(input, W) + b


x = tf.placeholder(tf.float32, shape=[None, 100,100])
y_ = tf.placeholder(tf.float32, shape=[None, 102])

x_image = tf.reshape(x, [-1, 100, 100, 1]) # reshaped images
conv1 = conv_layer(x_image, shape=[11, 11, 1, 96], stride=[1,4,4,1]) # first convolution 25x25x96
conv1_pool = max_pool_2x2(conv1) # first pooling 14x14x32

conv2 = conv_layer(conv1_pool, shape=[5, 5, 96, 256], stride=[1,1,1,1]) # second convolution 25x25x64
conv2_pool = max_pool_2x2(conv2) # second pooling 7x7x64

conv3 = conv_layer(conv2_pool, shape=[3, 3, 256, 384], stride=[1,1,1,1])

conv4 = conv_layer(conv3, shape=[3, 3, 384, 384], stride=[1,1,1,1])

conv5 = conv_layer(conv4, shape=[3, 3, 384, 256], stride=[1,1,1,1])
conv5_pool = max_pool_2x2(conv5)
conv5_flat = tf.reshape(conv5_pool, [-1,])



conv2_flat = tf.reshape(conv2_pool, [-1, 25*25*64]) # flatten previous output to 1x1x40000
full_1 = tf.nn.relu(full_layer(conv2_flat, 1024)) # makes fully connected layer and performs relu

keep_prob = tf.placeholder(tf.float32)
full1_drop = tf.nn.dropout(full_1, keep_prob=keep_prob) # drops random

y_conv = full_layer(full1_drop, 102)

cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=y_conv, labels=y_))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1)) # list of true and false values
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) # percentage

writer = tf.summary.FileWriter("./tmp/events/")
writer.add_graph(tf.get_default_graph())
writer.close()

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())

    test_X_fold = cropped_images_split[0]
    train_X_folds = cropped_images_split[1:]

    test_y_fold = target_split[0]
    train_y_folds = target_split[1:]

    print(len(train_X_folds))
    print(train_X_folds[0].shape)

    for i in range(NUM_STEPS):

        print("Performing step: {}".format(i))

        # periodic logging to let us know what is going
        if i % 10 == 0:
            train_accuracy = sess.run(accuracy, feed_dict={x: train_X_folds[i%19], y_: train_y_folds[i%19],
                                                           keep_prob: 1.0})
            print("Step: {} - Training Accuracy: {}".format(i, train_accuracy))

        sess.run(train_step, feed_dict={x: train_X_folds[i%19], y_: train_y_folds[i%19],
                                        keep_prob: .5})

    test_accuracy = np.mean(sess.run(accuracy, feed_dict={x: test_X_fold, y_: test_y_fold, keep_prob: 1.0}))

    print("Test Accuracy: {}".format(np.mean(test_accuracy)))


<file_sep>from keras import Sequential

from keras.layers import (
    Conv2D,
    MaxPooling2D,
    Activation,
    Flatten,
    Dense
)

from keras.layers.convolutional import ZeroPadding2D
from keras.layers.normalization import BatchNormalization
from keras.regularizers import l2


def build_marconet():

    # Constants
    weight_reg = 5e-4

    model = Sequential()

    model.add(Conv2D(filters=128, kernel_size=(5, 5), strides=(2, 2), kernel_initializer="he_normal",
                     data_format="channels_last", kernel_regularizer=l2(weight_reg), input_shape=(224, 224, 3)))

    model.add(BatchNormalization())

    model.add(Activation("relu"))

    model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))

    model.add(Conv2D(filters=256, kernel_size=(3, 3), strides=(1, 1), kernel_initializer="he_normal",
                     data_format="channels_last", kernel_regularizer=l2(weight_reg)))

    model.add(BatchNormalization())

    model.add(Activation("relu"))

    model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))

    model.add(ZeroPadding2D(padding=(1, 1)))

    model.add(Conv2D(filters=512, kernel_size=(3, 3), strides=(1, 1), kernel_initializer="he_normal",
                     data_format="channels_last", kernel_regularizer=l2(weight_reg)))

    model.add(BatchNormalization())

    model.add(Activation("relu"))

    model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))

    model.add(Flatten())

    model.add(Dense(1024, kernel_initializer="he_normal", kernel_regularizer=l2(weight_reg)))

    model.add(BatchNormalization())

    model.add(Activation("relu"))

    model.add(Dense(101, kernel_initializer="he_normal", kernel_regularizer=l2(weight_reg)))

    model.add(Activation("softmax"))

    return model<file_sep># need
from sklearn import cluster
from sklearn.preprocessing import StandardScaler
import numpy as np
import time

#test
from sklearn.decomposition import PCA

class BagOfWords:
    def __init__(self, n_clusters):
        self.n_clusters = n_clusters
        self.kmeans_obj = cluster.MiniBatchKMeans(n_clusters)
        self.scale = None

    def combineFeatures(self, data_features):
        features = [] # features from all images
        feature_img_ids = [] # identifies what image the feature belongs to

        img_id = 0
        for image in data_features:
            for feature in image:
                features.append(feature)
                feature_img_ids.append(img_id)
            img_id += 1

        print("Length of feature list: {}".format(len(features)))
        return features, feature_img_ids

    # Poorly named. This is really making the training data.
    def makeVocabulary(self, data_features, img_count):
        features, feature_img_ids = self.combineFeatures(data_features)
        # this line does two things: it clusters all the features getting the cluster centers,
        # but then it goes back through each feature and returns an array of what cluster center
        # each feature was closest to
        # Because I am using cross validation in this project, if I wanted to bring in outside images
        # I would have to make sure the outside image is just using predict so it will compare it to the
        # cluster centers from the training data
        start = time.time()
        print("Started kmeans")
        features = self.kmeans_obj.fit_predict(features)
        end = time.time()
        print("Finished kmeans - Time elapsed: {}".format(end-start))
        vocabulary = np.array([np.zeros(self.n_clusters) for i in range(img_count)])
        for i in range(len(features)):
            image_id = feature_img_ids[i]
            visual_word_id = features[i]
            vocabulary[image_id][visual_word_id] += 1

        if self.scale is None:
            self.scale = StandardScaler().fit(vocabulary)

        vocabulary = self.scale.transform(vocabulary)

        return vocabulary












<file_sep>import matplotlib.pyplot as plt
import numpy as np

labels = ["RBF"]

values = np.linspace(2**-15,2**3)

results = [0.14575, 0.07222, 0.11144, 0.10948, 0.10425, 0.08824, 0.07712, 0.07157, 0.05948, 0.05261, 0.04706, 0.04118, 0.03725, 0.03562, 0.03366, 0.03235, 0.02974, 0.02745, 0.02582, 0.02353, 0.02288, 0.0219, 0.02059, 0.01993, 0.01928, 0.0183, 0.01797, 0.01765, 0.01765, 0.01601, 0.01536, 0.01471, 0.01471, 0.01471, 0.01373, 0.01373, 0.0134, 0.01209, 0.01209, 0.01144, 0.01144, 0.01144, 0.01144, 0.01144, 0.01144, 0.01176, 0.01176, 0.01176, 0.01176, 0.01176]
std = [0.01124, 0.01284, 0.01477, 0.01529, 0.01162, 0.00953, 0.00873, 0.01015, 0.0069, 0.00551, 0.00489, 0.00606, 0.00769, 0.00673, 0.00581, 0.00623, 0.00681, 0.00734, 0.00597, 0.00553, 0.00517, 0.00502, 0.00434, 0.00419, 0.00419, 0.00419, 0.004, 0.00392, 0.00364, 0.00217, 0.00196, 0.00273, 0.00273, 0.00273, 0.00336, 0.00336, 0.00378, 0.00303, 0.00303, 0.00231, 0.00231, 0.00231, 0.00231, 0.00231, 0.00231, 0.00191, 0.00191, 0.00191, 0.00191, 0.00191]

plt.style.use("ggplot")

fig = plt.figure(figsize=(14,9))

ax = fig.add_subplot(111)
ax.errorbar(values, results, yerr=std, label=labels[0])
ax.legend()

plt.title("Varying Gamma on RBF Kernel")
plt.xlabel("Gamma")
plt.ylabel("Accuracy")

text = "Feature Extractor: SURF\nNumber of Clusters: 800\n(Best from Graph #1)\nC: 1"
ax.text(0,.005,text,verticalalignment='bottom', horizontalalignment='left')

plt.show()
<file_sep>
# This is to get rid of the deprecation warning
def warn(*args, **kwargs):
    pass
import warnings
warnings.warn = warn

from ImageProcessing import *
from FeatureExtraction import *
from BagOfWords import *
from Classifiers import *
from Testing import *
import time
from sklearn.grid_search import GridSearchCV
from sklearn.svm import SVC
from sklearn.neighbors import KNeighborsClassifier
from sklearn.externals import joblib

if __name__ == "__main__":

    # Classifier
    svm = SVC()

    # Classifiers params
    kernel_list = ["rbf"]
    # c_list = np.linspace(2 ** -5, 2 ** 15)
    c_list = [1]
    gamma_list = ["auto"]

    # Classifiers grid
    param_grid = dict(kernel=kernel_list,
                      C=c_list,
                      gamma=gamma_list
                      )
    grid = GridSearchCV(svm, param_grid, cv=5, scoring='accuracy', n_jobs=-1, verbose=1)

    # data = joblib.load("../objects/102/bow/surf/102_bow_800_surf_.pkl")
    # target = joblib.load("../objects/102/102_target_surf.pkl")

    bow = BagOfWords(800)
    surf = Surf()
    training_files = np.load("../training_files.npy")
    testing_files = np.load("../testing_files.npy")



    # Grid search
    grid.fit(data, target)
    print(grid.grid_scores_)
    print(grid.best_score_)
    print(grid.best_params_)

    # def bowScores(clf, CV, n_clusters, data_features, img_count, target, saveToDisk):
    #     bow = BagOfWords(n_clusters)
    #     data = bow.makeVocabulary(data_features, img_count)
    #     scores = cross_val_score(clf, data, target, cv=CV)
    #     if saveToDisk:
    #         filename = "./objects/bow/102_bow_" + str(n_clusters) + "_surf_.pkl"
    #         joblib.dump(data,filename)
    #     print("Scores: {}".format(scores))
    #     print("Mean: {}".format(np.mean(scores)))
    #     print("STD: {}".format(np.std(scores)))
    #     results.append(np.mean(scores))
    #     std.append(np.std(scores))
    #
    # data_features = joblib.load("./objects/102_data_features_surf.pkl")
    # img_count = joblib.load("./objects/102_img_count_surf.pkl")
    # target = joblib.load("./objects/102_target_surf.pkl")
    #
    # svm = SVC()
    #
    # values = range(2000,50000,5000)
    # for value in values:
    #     print("Computing value: {}".format(value))
    #     start = time.time()
    #     bowScores(svm,5,value,data_features,img_count,target, True)
    #     end = time.time()
    #     print("Finished value: {} - Time elapsed: {}".format(value, end-start))
    #
    # print("Results:")
    # print(results)
    # print("STD:")
    # print(std)

    # knn = KNeighborsClassifier()
    # target = joblib.load("./objects/10_target_surf.pkl")
    #
    # for value in values:
    #     filename_data = "./objects/bow/10_bow_" + str(value) + "_surf_.pkl"
    #     data = joblib.load(filename_data)
    #     scores = cross_val_score(knn, data, target, cv=5)
    #     print("Scores: {}".format(scores))
    #     print("Mean: {}".format(np.mean(scores)))
    #     print("STD: {}".format(np.std(scores)))
    #     results.append(np.mean(scores))
    #     std.append(np.std(scores))
    #
    # print(results)
    # print(std)
    #

<file_sep>import matplotlib.pyplot as plt
import numpy as np

labels = ["P=1","P=2"]

values = range(1,31,1)

p1_results = [0.13758, 0.11928, 0.11863, 0.11928, 0.12026, 0.12026, 0.12124, 0.11895, 0.11895, 0.11667, 0.11503, 0.11405, 0.11471, 0.11569, 0.11536, 0.11438, 0.11438, 0.11373, 0.11405, 0.11209, 0.11078, 0.11144, 0.11209, 0.11242, 0.11307, 0.11242, 0.11176, 0.11111, 0.10784, 0.10882]
p1_std = [0.01545, 0.00685, 0.00824, 0.008, 0.00843, 0.00837, 0.01124, 0.01225, 0.01129, 0.00899, 0.01087, 0.0079, 0.01071, 0.00977, 0.01163, 0.01584, 0.0165, 0.01251, 0.01353, 0.01144, 0.01216, 0.01365, 0.01412, 0.01369, 0.01203, 0.01305, 0.01427, 0.01554, 0.01303, 0.01478]

p2_results = [0.17516, 0.15523, 0.14542, 0.14771, 0.15327, 0.14935, 0.14902, 0.15229, 0.15425, 0.15229, 0.15163, 0.15163, 0.14967, 0.14902, 0.15065, 0.15163, 0.15, 0.14673, 0.14706, 0.14608, 0.14902, 0.14706, 0.14771, 0.14902, 0.14804, 0.14641, 0.14706, 0.14608, 0.14673, 0.14641]
p2_std = [0.01229, 0.00969, 0.01447, 0.01125, 0.00836, 0.0069, 0.00521, 0.00521, 0.00749, 0.00783, 0.00649, 0.0079, 0.00469, 0.00478, 0.00455, 0.00455, 0.00711, 0.00349, 0.00462, 0.00336, 0.003, 0.00231, 0.0032, 0.00521, 0.0059, 0.00303, 0.00537, 0.00658, 0.00836, 0.00735]

plt.style.use("ggplot")

fig = plt.figure(figsize=(14,9))

ax = fig.add_subplot(111)
ax.errorbar(values, p1_results, yerr=p1_std, label=labels[0])
ax.legend()

ax = fig.add_subplot(111)
ax.errorbar(values, p2_results, yerr=p2_std, label=labels[1])
ax.legend()

plt.title("Different Values for P over Various Number of Neighbors")
plt.xlabel("Number of Neighbors")
plt.ylabel("Accuracy")

text = "Feature Extractor: SURF\nNumber of Clusters: 400\n(Best from Graph #1)\nWeight: Uniform"

ax.text(0.1,.09,text,verticalalignment='bottom', horizontalalignment='left')

plt.show()
<file_sep>import glob
path = "./images/"
count = {}
for folder in glob.glob(path+"*"):
    category = folder.split("\\")[-1]
    count[category] = 0
    for image in glob.glob(folder+"/*"):
        count[category] += 1

import pprint
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(count)
small = min(count, key=count.get)
large = max(count, key=count.get)
print(count[small])
print(count[large])
<file_sep>import cv2
import numpy as np
import glob
from Classifiers import Classifer

# Read two images for debugging
images = []
labels = ["desert", "jellyfish"]
for image in glob.glob("./test_images/*"):
    image = cv2.imread(image)
    images.append(image)

# Test the package
svm_params = dict(n_neighbors=2)
svm = Classifer(classifier="knn", images=images, labels=labels, detector="surf",
                n_clusters=50, bow=True, **svm_params)
predictions = svm.predict(images)
stop<file_sep>import matplotlib.pyplot as plt
import numpy as np

labels = ["Uniform","Distance"]

values = range(1,31,1)

uniform_results = [0.17516, 0.15523, 0.14542, 0.14771, 0.15327, 0.14935, 0.14902, 0.15229, 0.15425, 0.15229, 0.15163, 0.15163, 0.14967, 0.14902, 0.15065, 0.15163, 0.15, 0.14673, 0.14706, 0.14608, 0.14902, 0.14706, 0.14771, 0.14902, 0.14804, 0.14641, 0.14706, 0.14608, 0.14673, 0.14641]
uniform_std = [0.01229, 0.00969, 0.01447, 0.01125, 0.00836, 0.0069, 0.00521, 0.00521, 0.00749, 0.00783, 0.00649, 0.0079, 0.00469, 0.00478, 0.00455, 0.00455, 0.00711, 0.00349, 0.00462, 0.00336, 0.003, 0.00231, 0.0032, 0.00521, 0.0059, 0.00303, 0.00537, 0.00658, 0.00836, 0.00735]

distance_results = [0.17516, 0.17516, 0.17941, 0.17712, 0.17974, 0.17876, 0.1768, 0.17484, 0.1719, 0.17288, 0.1719, 0.17059, 0.16993, 0.17059, 0.17157, 0.1683, 0.16503, 0.16242, 0.16209, 0.15882, 0.15915, 0.15817, 0.15784, 0.15817, 0.1585, 0.15686, 0.15817, 0.15686, 0.15588, 0.15621]
distance_std = [0.01229, 0.01229, 0.01488, 0.01087, 0.00895, 0.00989, 0.00829, 0.00941, 0.0064, 0.00443, 0.00823, 0.0069, 0.00752, 0.0065, 0.00547, 0.00547, 0.00547, 0.00824, 0.00696, 0.00855, 0.00939, 0.01255, 0.01072, 0.01071, 0.00846, 0.0067, 0.00903, 0.00766, 0.00658, 0.00811]

plt.style.use("ggplot")

fig = plt.figure(figsize=(14,9))

ax = fig.add_subplot(111)
ax.errorbar(values, uniform_results, yerr=uniform_std, label=labels[0])
ax.legend()

ax = fig.add_subplot(111)
ax.errorbar(values, distance_results, yerr=distance_std, label=labels[1])
ax.legend()

plt.title("Different Weights over Various Number of Neighbors")
plt.xlabel("Number of Neighbors")
plt.ylabel("Accuracy")

text = "Feature Extractor: SURF\nNumber of Clusters: 400\n(Best from Graph #1)\nP: 2"

ax.text(0.1,.129,text,verticalalignment='bottom', horizontalalignment='left')

plt.show()
<file_sep>from Classifiers.Classifier import Classifer