Example #1
0
def main():
    print('Getting paths and labels for all train and test data')
    train_image_paths, test_image_paths, train_labels, test_labels = \
        get_image_paths(data_path, categories, num_train_per_cat)
    """ Step 1: Represent each image with the appropriate feature
    Each function to construct features should return and N x d matrix, where
    N is the number of paths passed to the function and d is the dimensionality
    of each image representation.
    """
    print('Using [%s] representation for image' % representation)

    if feature == 'HoG':
        if not os.path.exists('vocab_hog.npy'):
            print('No existing visual word vocabulary found. '
                  'Computing one from training images')

            vocab = build_vocabulary(train_image_paths, vocab_size, feature)
            np.save('vocab_hog', vocab)
    elif feature == 'SIFT':
        if not os.path.exists('vocab_sift.npy'):
            print('No existing visual word vocabulary found. '
                  'Computing one from training images')

            vocab = build_vocabulary(train_image_paths, vocab_size, feature)
            np.save('vocab_sift', vocab)

    pca_visualize(pca_out_dim, feature, vocab_size)

    if representation == 'bag of words':
        # Build vocabulary and save it as a file 'vocab.mat' for reuse.
        train_image_feats = get_bags_of_words(train_image_paths, feature)
        test_image_feats = get_bags_of_words(test_image_paths, feature)
    elif representation == 'spatial_pyramid_feats':
        train_image_feats = get_spatial_pyramid_feats(train_image_paths,
                                                      max_level, feature)
        test_image_feats = get_spatial_pyramid_feats(test_image_paths,
                                                     max_level, feature)
    else:
        raise KeyError('No such representation %s is defined' % representation)
    """ Step 2: Classify each test image by training and using the appropriate classifier
    Each function to classify test features will return an N x 1 array, where
    N is the number of test cases and each entry is string indicating the predicted
    category for each test image. Each entry in 'predicted_categories' must be one of
    the 15 string in 'categories', 'train_labels', and 'test_labels'.
    """
    print('Using [%s] classifier to predict test set categories' % classifier)

    if classifier == 'SVM':
        predicted_categories = svm_classify(train_image_feats, train_labels,
                                            test_image_feats, kernel_type)
    else:
        raise KeyError('No such classifier %s is defined' % classifier)
    """ Step 3: Build a confusion matrix and score the recongnition system
    You do not need to code anything in this section.
    """
    create_results_webpage(train_image_paths, test_image_paths, train_labels,
                           test_labels, categories, abbr_categories,
                           predicted_categories)
Example #2
0
def main():
    print("Getting paths and labels for all train and test data")
    train_image_paths, test_image_paths, train_labels, test_labels = \
        get_image_paths(DATA_PATH, CATEGORIES, NUM_TRAIN_PER_CAT)

    if FEATURE == 'tiny_image':
        train_image_feats = get_tiny_images(train_image_paths)
        test_image_feats = get_tiny_images(test_image_paths)

    elif FEATURE == 'bag_of_sift':
        if os.path.isfile('vocab.pkl') is False:
            print('No existing visual word vocabulary found. Computing one from training images\n')
            vocab_size = 400
            vocab = build_vocabulary(train_image_paths, vocab_size)
            with open('vocab.pkl', 'wb') as handle:
                pickle.dump(vocab, handle, protocol=pickle.HIGHEST_PROTOCOL)

        if os.path.isfile('train_image_feats.pkl') is False:
            train_image_feats = get_bags_of_sifts(train_image_paths);
            with open('train_image_feats.pkl', 'wb') as handle:
                pickle.dump(train_image_feats, handle, protocol=pickle.HIGHEST_PROTOCOL)
        else:
            with open('train_image_feats.pkl', 'rb') as handle:
                train_image_feats = pickle.load(handle)

        if os.path.isfile('test_image_feats.pkl') is False:
            test_image_feats  = get_bags_of_sifts(test_image_paths);
            with open('test_image_feats.pkl', 'wb') as handle:
                pickle.dump(test_image_feats, handle, protocol=pickle.HIGHEST_PROTOCOL)
        else:
            with open('test_image_feats.pkl', 'rb') as handle:
                test_image_feats = pickle.load(handle)
    else:
        raise NameError('Unknown feature type')


    if CLASSIFIER == 'nearest_neighbor':
        predicted_categories = nearest_neighbor_classify(train_image_feats, train_labels, test_image_feats)

    elif CLASSIFIER == 'support_vector_machine':
        predicted_categories = svm_classify(train_image_feats, train_labels, test_image_feats)

    else:
        raise NameError('Unknown classifier type')

    accuracy = float(len([x for x in zip(test_labels,predicted_categories) if x[0]== x[1]]))/float(len(test_labels))
    print("Accuracy = ", accuracy)
    test_labels_ids = [CATE2ID[x] for x in test_labels]
    predicted_categories_ids = [CATE2ID[x] for x in predicted_categories]
    train_labels_ids = [CATE2ID[x] for x in train_labels]

    build_confusion_mtx(test_labels_ids, predicted_categories_ids, ABBR_CATEGORIES)
    visualize(CATEGORIES, test_image_paths, test_labels_ids, predicted_categories_ids, train_image_paths, train_labels_ids)
Example #3
0
def main():
    #This function returns arrays containing the file path for each train
    #and test image, as well as arrays with the label of each train and
    #test image. By default all four of these arrays will be 1500 where each
    #entry is a string.
    print("Getting paths and labels for all train and test data")
    train_image_paths, test_image_paths, train_labels, test_labels = \
        get_image_paths(DATA_PATH, CATEGORIES, NUM_TRAIN_PER_CAT)

    # TODO Step 1:
    # Represent each image with the appropriate feature
    # Each function to construct features should return an N x d matrix, where
    # N is the number of paths passed to the function and d is the
    # dimensionality of each image representation. See the starter code for
    # each function for more details.

    if FEATURE == 'tiny_image':
        # YOU CODE get_tiny_images.py
        train_image_feats = get_tiny_images(train_image_paths)
        test_image_feats = get_tiny_images(test_image_paths)

    elif FEATURE == 'bag_of_sift':
        # YOU CODE build_vocabulary.py
        if os.path.isfile('vocab.pkl') is False:
            print(
                'No existing visual word vocabulary found. Computing one from training images\n'
            )
            vocab_size = 1000  ### Vocab_size is up to you. Larger values will work better (to a point) but be slower to comput.
            vocab = build_vocabulary(train_image_paths, vocab_size)
            with open('vocab.pkl', 'wb') as handle:
                pickle.dump(vocab, handle, protocol=pickle.HIGHEST_PROTOCOL)

        if os.path.isfile('train_image_feats.pkl') is False:
            # YOU CODE get_bags_of_sifts.py
            train_image_feats = get_bags_of_sifts(train_image_paths)
            with open('train_image_feats.pkl', 'wb') as handle:
                pickle.dump(train_image_feats,
                            handle,
                            protocol=pickle.HIGHEST_PROTOCOL)
        else:
            with open('train_image_feats.pkl', 'rb') as handle:
                train_image_feats = pickle.load(handle)

        if os.path.isfile('test_image_feats.pkl') is False:
            test_image_feats = get_bags_of_sifts(test_image_paths)
            with open('test_image_feats.pkl', 'wb') as handle:
                pickle.dump(test_image_feats,
                            handle,
                            protocol=pickle.HIGHEST_PROTOCOL)
        else:
            with open('test_image_feats.pkl', 'rb') as handle:
                test_image_feats = pickle.load(handle)
    elif FEATURE == 'dumy_feature':
        train_image_feats = []
        test_image_feats = []
    else:
        raise NameError('Unknown feature type')

    # TODO Step 2:
    # Classify each test image by training and using the appropriate classifier
    # Each function to classify test features will return an N x 1 array,
    # where N is the number of test cases and each entry is a string indicating
    # the predicted category for each test image. Each entry in
    # 'predicted_categories' must be one of the 15 strings in 'categories',
    # 'train_labels', and 'test_labels.

    if CLASSIFIER == 'nearest_neighbor':
        # YOU CODE nearest_neighbor_classify.py
        predicted_categories = nearest_neighbor_classify(
            train_image_feats, train_labels, test_image_feats)

    elif CLASSIFIER == 'support_vector_machine':
        # YOU CODE svm_classify.py
        predicted_categories = svm_classify(train_image_feats, train_labels,
                                            test_image_feats)

    elif CLASSIFIER == 'dumy_classifier':
        # The dummy classifier simply predicts a random category for
        # every test case
        predicted_categories = test_labels[:]
        shuffle(predicted_categories)
    else:
        raise NameError('Unknown classifier type')

    accuracy = float(
        len([
            x for x in zip(test_labels, predicted_categories) if x[0] == x[1]
        ])) / float(len(test_labels))
    print("Accuracy = ", accuracy)
    test_labels_ids = [CATE2ID[x] for x in test_labels]
    predicted_categories_ids = [CATE2ID[x] for x in predicted_categories]
    train_labels_ids = [CATE2ID[x] for x in train_labels]

    # Step 3: Build a confusion matrix and score the recognition system
    # You do not need to code anything in this section.

    build_confusion_mtx(test_labels_ids, predicted_categories_ids,
                        ABBR_CATEGORIES)
    visualize(CATEGORIES, test_image_paths, test_labels_ids,
              predicted_categories_ids, train_image_paths, train_labels_ids)
Example #4
0
# number of training examples per category to use. Max is 100. For
# simplicity, we assume this is the number of test cases per category, as
# well.

num_train_per_cat = 100  # 100

# This function returns cell arrays containing the file path for each train
# and test image, as well as cell arrays with the label of each train and
# test image. By default all four of these arrays will be 1500x1 where each
# entry is a char array (or string).

print('Getting paths and labels for all train and test data\n')

train_image_paths, test_image_paths, train_labels, test_labels = \
    get_image_paths(data_path, categories, num_train_per_cat)

#   train_image_paths  1500x1   cell
#   test_image_paths   1500x1   cell
#   train_labels       1500x1   cell
#   test_labels        1500x1   cell

## Step 1: Represent each image with the appropriate feature
# Each function to construct features should return an N x d matrix, where
# N is the number of paths passed to the function and d is the
# dimensionality of each image representation. See the starter code for
# each function for more details.

print('Using %s representation for images\n' % FEATURE)

if FEATURE.lower() == "tiny image":
Example #5
0
def main():
    #This function returns arrays containing the file path for each train
    #and test image, as well as arrays with the label of each train and
    #test image. By default all four of these arrays will be 1500 where each
    #entry is a string.
    print("Getting paths and labels for all train and test data")
    train_image_paths, test_image_paths, train_labels, test_labels = \
        get_image_paths(DATA_PATH, CATEGORIES, NUM_TRAIN_PER_CAT)

    # TODO Step 1:
    # Represent each image with the appropriate feature
    # Each function to construct features should return an N x d matrix, where
    # N is the number of paths passed to the function and d is the 
    # dimensionality of each image representation. See the starter code for
    # each function for more details.

    if FEATURE == 'tiny_image':
        # YOU CODE get_tiny_images.py
        # print('in tiny image')
        if os.path.isfile('tiny_test.pkl') is False:
            print('false QQ')
            train_image_feats = get_tiny_images(train_image_paths)
            with open('test_image_feats.pkl', 'wb') as handle:
                    pickle.dump(train_image_feats, handle, protocol=pickle.HIGHEST_PROTOCOL)
            test_image_feats = get_tiny_images(test_image_paths)
            with open('test_image_feats.pkl', 'wb') as handle:
                    pickle.dump(test_image_feats, handle, protocol=pickle.HIGHEST_PROTOCOL)
        else:
            with open('tiny_train.pkl', 'rb') as handle:
                # print('tiny_train exists')
                train_image_feats = pickle.load(handle)
                # print(type(train_image_feats),len(train_image_feats))
            with open('tiny_test.pkl', 'rb') as handle:
                # print('test_image_feats exists')
                test_image_feats = pickle.load(handle)
                # print(type(test_image_feats),len(test_image_feats))

    elif FEATURE == 'bag_of_sift':
        # YOU CODE build_vocabulary.py
        if os.path.isfile('vocab.pkl') is False:
            print('No existing visual word vocabulary found. Computing one from training images\n')
            vocab_size = 400   ### Vocab_size is up to you. Larger values will work better (to a point) but be slower to comput.
            vocab = build_vocabulary(train_image_paths, vocab_size)
            with open('vocab.pkl', 'wb') as handle:
                pickle.dump(vocab, handle, protocol=pickle.HIGHEST_PROTOCOL)

        if os.path.isfile('train_image_feats.pkl') is False:
            # YOU CODE get_bags_of_sifts.py
            train_image_feats = get_bags_of_sifts(train_image_paths);
            with open('train_image_feats.pkl', 'wb') as handle:
                pickle.dump(train_image_feats, handle, protocol=pickle.HIGHEST_PROTOCOL)
        else:
            with open('train_image_feats.pkl', 'rb') as handle:
                train_image_feats = pickle.load(handle)

        if os.path.isfile('test_image_feats.pkl') is False:
            test_image_feats  = get_bags_of_sifts(test_image_paths);
            with open('test_image_feats.pkl', 'wb') as handle:
                pickle.dump(test_image_feats, handle, protocol=pickle.HIGHEST_PROTOCOL)
        else:
            with open('test_image_feats.pkl', 'rb') as handle:
                test_image_feats = pickle.load(handle)
    elif FEATURE == 'dumy_feature':
        train_image_feats = []
        test_image_feats = []
    else:
        raise NameError('Unknown feature type')
    #####################initialize the possibility
    possibility=[float(1/num_all)]*num_all
    sum_p=[]
    for i in range(1,num_all):
        sum_p.append(float(i/num_all))
    testing=[]
    for i in range(num_trial):
        training_id=[]
        #choose 100 examples 
        training_id=choose_training_id(sum_p)
        #training
        predicted_categories = svm_classify([train_image_feats[idx] for idx in training_id], [train_labels[idx] for idx in training_id],train_image_feats)
        temp=[]
        temp=svm_classify([train_image_feats[idx] for idx in training_id], [train_labels[idx] for idx in training_id],test_image_feats)
        testing.append(temp)
        #update possibility
        sum_p=[]
        possibility,sum_p=update_possibility(possibility,predicted_categories,train_labels) 
    accuracy=cal_result(testing,test_labels)
    # TODO Step 2: 
    # Classify each test image by training and using the appropriate classifier
    # Each function to classify test features will return an N x 1 array,
    # where N is the number of test cases and each entry is a string indicating
    # the predicted category for each test image. Each entry in
    # 'predicted_categories' must be one of the 15 strings in 'categories',
    # 'train_labels', and 'test_labels.

    # if CLASSIFIER == 'nearest_neighbor':
    #     # YOU CODE nearest_neighbor_classify.py
    #     predicted_categories = nearest_neighbor_classify(train_image_feats, train_labels, test_image_feats)

    # elif CLASSIFIER == 'support_vector_machine':
    #     # YOU CODE svm_classify.py
    #     print('training')
    #     predicted_categories = svm_classify(train_image_feats, train_labels, test_image_feats)

    # elif CLASSIFIER == 'dumy_classifier':
    #     # The dummy classifier simply predicts a random category for
    #     # every test case
    #     predicted_categories = test_labels[:]
    #     shuffle(predicted_categories)
    # else:
    #     raise NameError('Unknown classifier type')

    # accuracy = float(len([x for x in zip(test_labels,predicted_categories) if x[0]== x[1]]))/float(len(test_labels))
    print("Accuracy = ", accuracy)
    '''