Example #1
0
def _generate_output(model, data, orig_images):
    import theano
    import theano.tensor as T

    x = T.matrix()
    f = theano.function(inputs=[x], outputs=T.argmax(model.get_output(x), axis=1))

    wrong, right = [], []
    for i in xrange(data.X.shape[0]):
        y = f(data.X[i:(i+1)].astype(theano.config.floatX))
        if y == data.Y[i]: right.append(orig_images[i])
        else: wrong.append(orig_images[i])

    data = DataSaver()

    def add(images):
        m = len(images)
        gid = data.add_group(m, 1)

        for pid in xrange(m):
            data.set_images(gid, pid, 0, [images[pid]])

    add(wrong)
    add(right)

    data.save(_cached_output)
Example #2
0
def _generate_output(model, threshold, datasets, image_shape):
    # For convenience, we will save the result in our data format.
    # Regard train, valid, and test sets as three groups.
    # Each pedestrian only has one view, containing output and target images.

    x = T.matrix('x')
    y = model.get_output(x)
    output_func = theano.function(inputs=[x], outputs=y)

    data = DataSaver()

    def add(X, Y):
        m = X.shape[0]
        gid = data.add_group(m, 1)

        for pid in xrange(m):
            x, target = X[pid:pid+1, :], Y[pid, :] # Ensure x to be a matrix
            y = (output_func(x) >= threshold).astype(numpy.uint8) * 255
            y = y.reshape(image_shape)
            target = numpy.uint8(target * 255).reshape(image_shape)
            data.set_images(gid, pid, 0, [y, target])

    add(datasets.train_x.get_value(borrow=True),
        datasets.train_y.get_value(borrow=True))

    add(datasets.valid_x.get_value(borrow=True),
        datasets.valid_y.get_value(borrow=True))

    add(datasets.test_x.get_value(borrow=True),
        datasets.test_y.get_value(borrow=True))

    data.save(_cached_output)
Example #3
0
def _generate_output(model, data, orig_images):
    import theano
    import theano.tensor as T

    x = T.matrix()
    f = theano.function(inputs=[x],
                        outputs=T.argmax(model.get_output(x), axis=1))

    wrong, right = [], []
    for i in xrange(data.X.shape[0]):
        y = f(data.X[i:(i + 1)].astype(theano.config.floatX))
        if y == data.Y[i]: right.append(orig_images[i])
        else: wrong.append(orig_images[i])

    data = DataSaver()

    def add(images):
        m = len(images)
        gid = data.add_group(m, 1)

        for pid in xrange(m):
            data.set_images(gid, pid, 0, [images[pid]])

    add(wrong)
    add(right)

    data.save(_cached_output)
Example #4
0
def _generate_output(model, threshold, datasets, image_shape):
    # For convenience, we will save the result in our data format.
    # Regard train, valid, and test sets as three groups.
    # Each pedestrian only has one view, containing output and target images.

    x = T.matrix('x')
    y = model.get_output(x)
    output_func = theano.function(inputs=[x], outputs=y)

    data = DataSaver()

    def add(X, Y):
        m = X.shape[0]
        gid = data.add_group(m, 1)

        for pid in xrange(m):
            x, target = X[pid:pid + 1, :], Y[pid, :]  # Ensure x to be a matrix
            y = (output_func(x) >= threshold).astype(numpy.uint8) * 255
            y = y.reshape(image_shape)
            target = numpy.uint8(target * 255).reshape(image_shape)
            data.set_images(gid, pid, 0, [y, target])

    add(datasets.train_x.get_value(borrow=True),
        datasets.train_y.get_value(borrow=True))

    add(datasets.valid_x.get_value(borrow=True),
        datasets.valid_y.get_value(borrow=True))

    add(datasets.test_x.get_value(borrow=True),
        datasets.test_y.get_value(borrow=True))

    data.save(_cached_output)
Example #5
0
def _mask_dataset():
    # Load model and compile function
    with open('../cache/foreground_model.pkl', 'rb') as f:
        model, threshold = cPickle.load(f)

    x = T.matrix('x')
    y = model.get_output(x)
    output_func = theano.function(inputs=[x], outputs=(y >= threshold))

    # Load data
    image_data = DataLoader('../data/cuhk_small.mat', verbose=True)

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

    images = image_data.get_all_images()
    images = [_input_preproc(image) for image in images]
    images = imageproc.images2mat(images).astype(theano.config.floatX)

    # Compute masks
    print "Computing masks ..."

    masks = output_func(images)

    # Save masks
    print "Saving data ..."

    mask_data = DataSaver()

    cur_index = 0
    for gid in xrange(image_data.get_n_groups()):
        m, v = image_data.get_n_pedes_views(gid)
        mask_data.add_group(m, v)

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

            for vid, n in enumerate(n_images):
                view_masks = [0] * n
                for k in xrange(n):
                    mask = masks[cur_index, :]
                    mask = mask.reshape(160, 80, 1)
                    orig_image = image_data.get_image(gid, pid, vid, k)
                    orig_image = imageproc.imresize(orig_image, (160, 80, 3))
                    view_masks[k] = (mask * orig_image).astype(numpy.uint8)
                    cur_index += 1

                mask_data.set_images(gid, pid, vid, view_masks)

    mask_data.save('../data/cuhk_small_masked.mat')
Example #6
0
def _mask_dataset():
    # Load model and compile function
    with open('../cache/foreground_model.pkl', 'rb') as f:
        model, threshold = cPickle.load(f)

    x = T.matrix('x')
    y = model.get_output(x)
    output_func = theano.function(inputs=[x], outputs=(y >= threshold))

    # Load data
    image_data = DataLoader('../data/cuhk_small.mat', verbose=True)

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

    images = image_data.get_all_images()
    images = [_input_preproc(image) for image in images]
    images = imageproc.images2mat(images).astype(theano.config.floatX)

    # Compute masks
    print "Computing masks ..."

    masks = output_func(images)

    # Save masks
    print "Saving data ..."

    mask_data = DataSaver()

    cur_index = 0
    for gid in xrange(image_data.get_n_groups()):
        m, v = image_data.get_n_pedes_views(gid)
        mask_data.add_group(m, v)

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

            for vid, n in enumerate(n_images):
                view_masks = [0] * n
                for k in xrange(n):
                    mask = masks[cur_index, :]
                    mask = mask.reshape(160, 80, 1)
                    orig_image = image_data.get_image(gid, pid, vid, k)
                    orig_image = imageproc.imresize(orig_image, (160, 80, 3))
                    view_masks[k] = (mask * orig_image).astype(numpy.uint8)
                    cur_index += 1

                mask_data.set_images(gid, pid, vid, view_masks)

    mask_data.save('../data/cuhk_small_masked.mat')