示例#1
0
def _form_nndata(images, attributes, load_from_cache=False, save_to_cache=False):
    print "Forming NN-data ..."
    print "==================="

    if load_from_cache:
        with open(_cached_nndata, 'rb') as f:
            nndata = cPickle.load(f)
    else:
        nndata = Datasets(images, attributes)
        nndata.split(0.8, 0.1)

    if save_to_cache:
        with open(_cached_nndata, 'wb') as f:
            cPickle.dump(nndata, f, protocol=cPickle.HIGHEST_PROTOCOL)

    return nndata
示例#2
0
def _form_nndata(images,
                 attributes,
                 load_from_cache=False,
                 save_to_cache=False):
    print "Forming NN-data ..."
    print "==================="

    if load_from_cache:
        with open(_cached_nndata, 'rb') as f:
            nndata = cPickle.load(f)
    else:
        nndata = Datasets(images, attributes)
        nndata.split(0.8, 0.1)

    if save_to_cache:
        with open(_cached_nndata, 'wb') as f:
            cPickle.dump(nndata, f, protocol=cPickle.HIGHEST_PROTOCOL)

    return nndata
示例#3
0
def _prepare_data(load_from_cache=False, save_to_cache=False):
    if load_from_cache:
        with open(_cached_datasets, 'rb') as f:
            datasets = cPickle.load(f)
    else:
        # Setup data files
        input_data = DataLoader(
            '../data/parse/cuhk_large_labeled_subsampled.mat',
            verbose=True)

        target_data = DataLoader(
            '../data/parse/cuhk_large_labeled_subsampled_parse.mat',
            verbose=True)

        input_images = input_data.get_all_images()
        target_images = target_data.get_all_images()

        # Pre-processing
        print "Pre-processing ..."

        inputs = [_input_preproc(image) for image in input_images]
        inputs = imageproc.images2mat(inputs)

        targets = [_target_preproc(image) for image in target_images]
        targets = imageproc.images2mat(targets)

        # Prepare the datasets
        print "Prepare the datasets ..."

        datasets = Datasets(inputs, targets)
        datasets.split(train_ratio=0.5, valid_ratio=0.3)

    if save_to_cache:
        with open(_cached_datasets, 'wb') as f:
            cPickle.dump(datasets, f, protocol=cPickle.HIGHEST_PROTOCOL)

    return datasets
示例#4
0
def _prepare_data(load_from_cache=False, save_to_cache=False):
    if load_from_cache:
        with open(_cached_datasets, 'rb') as f:
            datasets = cPickle.load(f)
    else:
        # Setup data files
        input_data = DataLoader(
            '../data/parse/cuhk_large_labeled_subsampled.mat', verbose=True)

        target_data = DataLoader(
            '../data/parse/cuhk_large_labeled_subsampled_parse.mat',
            verbose=True)

        input_images = input_data.get_all_images()
        target_images = target_data.get_all_images()

        # Pre-processing
        print "Pre-processing ..."

        inputs = [_input_preproc(image) for image in input_images]
        inputs = imageproc.images2mat(inputs)

        targets = [_target_preproc(image) for image in target_images]
        targets = imageproc.images2mat(targets)

        # Prepare the datasets
        print "Prepare the datasets ..."

        datasets = Datasets(inputs, targets)
        datasets.split(train_ratio=0.5, valid_ratio=0.3)

    if save_to_cache:
        with open(_cached_datasets, 'wb') as f:
            cPickle.dump(datasets, f, protocol=cPickle.HIGHEST_PROTOCOL)

    return datasets
示例#5
0
def _prepare_data(load_from_cache=False, save_to_cache=False):
    if load_from_cache:
        with open(_cached_datasets, 'rb') as f:
            views_data, datasets = cPickle.load(f)
    else:
        image_data = DataLoader('../data/cuhk_small_masked.mat', verbose=True)

        # Prepare the view-first order data representation
        print "Preparing the view-first order data ..."

        n_pedes, n_views = [], []
        for gid in xrange(image_data.get_n_groups()):
            m, v = image_data.get_n_pedes_views(gid)
            n_pedes.append(m)
            n_views.append(v)

        assert min(n_views) == max(n_views), \
            "The number of views in each group should be equal"

        v = n_views[0]

        views_data = [[] for __ in xrange(v)]
        for gid in xrange(image_data.get_n_groups()):
            bias = sum(n_pedes[0:gid])
            group_data = data_manager.view_repr(image_data.get_pedes(gid))

            for vid in xrange(v):
                view_data = group_data[vid]
                view_data = [(pid+bias, image) for pid, image in view_data]
                views_data[vid].extend(view_data)

        # Prepare the datasets
        print "Prepare the datasets ..."

        X, Y = [], []
        for gid in xrange(image_data.get_n_groups()):
            m, v = image_data.get_n_pedes_views(gid)

            for pid in xrange(m):
                n_images = image_data.get_n_images(gid, pid)

                for vi in xrange(v):
                    for vj in xrange(vi+1, v):
                        for i in xrange(n_images[vi]):
                            for j in xrange(n_images[vj]):
                                X.append(_preproc(
                                    image_data.get_image(gid, pid, vi, i)))
                                Y.append(_preproc(
                                    image_data.get_image(gid, pid, vj, j)))

        X = imageproc.images2mat(X).astype(theano.config.floatX)
        Y = imageproc.images2mat(Y).astype(theano.config.floatX)

        datasets = Datasets(X, Y)
        datasets.split(train_ratio=0.8, valid_ratio=0.1)

    if save_to_cache:
        with open(_cached_datasets, 'wb') as f:
            cPickle.dump((views_data, datasets), f,
                protocol=cPickle.HIGHEST_PROTOCOL)

    return (views_data, datasets)
示例#6
0
from reid.models import cost_functions as costfuncs
from reid.models import active_functions as actfuncs
from reid.models.layers import FullConnLayer, ConvPoolLayer
from reid.models.neural_net import NeuralNet
from reid.models.evaluate import Evaluator

# Load the MNIST Dataset
with open(os.path.join('..', 'data', 'mnist', 'mnist.pkl'), 'rb') as f:
    train_set, valid_set, test_set = cPickle.load(f)

train_set = (train_set[0], train_set[1].reshape(train_set[1].shape[0], 1))
valid_set = (valid_set[0], valid_set[1].reshape(valid_set[1].shape[0], 1))
test_set = (test_set[0], test_set[1].reshape(test_set[1].shape[0], 1))

datasets = Datasets(train_set=train_set,
                    valid_set=valid_set,
                    test_set=test_set)

# Build up the model and evaluator
layers = [
    ConvPoolLayer((20, 1, 5, 5), (2, 2), (1, 28, 28), actfuncs.tanh, False),
    ConvPoolLayer((50, 20, 5, 5), (2, 2), None, actfuncs.tanh, True),
    FullConnLayer(800, 500, actfuncs.tanh),
    FullConnLayer(500, 10, actfuncs.softmax)
]

model = NeuralNet(layers)

evaluator = Evaluator(model, costfuncs.mean_negative_loglikelihood,
                      costfuncs.mean_number_misclassified)
示例#7
0
def _prepare_data(load_from_cache=False, save_to_cache=False):
    if load_from_cache:
        with open(_cached_datasets, 'rb') as f:
            views_data, datasets = cPickle.load(f)
    else:
        image_data = DataLoader('../data/cuhk_small_masked.mat', verbose=True)

        # Prepare the view-first order data representation
        print "Preparing the view-first order data ..."

        n_pedes, n_views = [], []
        for gid in xrange(image_data.get_n_groups()):
            m, v = image_data.get_n_pedes_views(gid)
            n_pedes.append(m)
            n_views.append(v)

        assert min(n_views) == max(n_views), \
            "The number of views in each group should be equal"

        v = n_views[0]

        views_data = [[] for __ in xrange(v)]
        for gid in xrange(image_data.get_n_groups()):
            bias = sum(n_pedes[0:gid])
            group_data = data_manager.view_repr(image_data.get_pedes(gid))

            for vid in xrange(v):
                view_data = group_data[vid]
                view_data = [(pid + bias, image) for pid, image in view_data]
                views_data[vid].extend(view_data)

        # Prepare the datasets
        print "Prepare the datasets ..."

        X, Y = [], []
        for gid in xrange(image_data.get_n_groups()):
            m, v = image_data.get_n_pedes_views(gid)

            for pid in xrange(m):
                n_images = image_data.get_n_images(gid, pid)

                for vi in xrange(v):
                    for vj in xrange(vi + 1, v):
                        for i in xrange(n_images[vi]):
                            for j in xrange(n_images[vj]):
                                X.append(
                                    _preproc(
                                        image_data.get_image(gid, pid, vi, i)))
                                Y.append(
                                    _preproc(
                                        image_data.get_image(gid, pid, vj, j)))

        X = imageproc.images2mat(X).astype(theano.config.floatX)
        Y = imageproc.images2mat(Y).astype(theano.config.floatX)

        datasets = Datasets(X, Y)
        datasets.split(train_ratio=0.8, valid_ratio=0.1)

    if save_to_cache:
        with open(_cached_datasets, 'wb') as f:
            cPickle.dump((views_data, datasets),
                         f,
                         protocol=cPickle.HIGHEST_PROTOCOL)

    return (views_data, datasets)