示例#1
0
 def to_ms(data):
     ms = MeanShift()
     ms.set_data(data, 'df')
     ms.set_kernel(kernel)
     ms.set_spatial('kd_tree')
     ms.quality = 1.0
     return ms
示例#2
0
def ms_by_conc(power, code=''):
    ms = MeanShift()
    ms.quality = 0.5
    ms.set_data(numpy.array([1, 0, 0], dtype=numpy.float32), 'f')
    ms.set_kernel('fisher(%.1f%s)' % (2**power, code))
    ms.set_spatial('kd_tree')

    return ms
示例#3
0
def resample(kde):
    data = kde.draws(samples)

    ret = MeanShift()
    ret.set_data(data, 'df')
    ret.set_kernel('triangular')
    ret.set_spatial('kd_tree')
    ret.scale_loo_nll()

    return ret
示例#4
0
def to_kde(grid):
    data = numpy.empty((7, 5), dtype=numpy.float32)
    for y in xrange(7):
        for x in xrange(5):
            data[y, x] = 1.0 if grid[y][x] != ' ' else blank_weight

    ret = MeanShift()
    ret.set_data(data, 'bb', 2)
    #ret.set_kernel('triangular')
    #ret.set_spatial('kd_tree')
    ret.scale_loo_nll()

    return ret
示例#5
0
# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

import random
import numpy
import numpy.random

from ms import MeanShift

# Create a simple data set...
a = numpy.random.normal(3.0, 1.0, 100)
b = numpy.random.normal(5.0, 0.5, 50)

data = numpy.concatenate((a, b))

# Setup the mean shift object...
ms = MeanShift()
ms.set_data(data, 'd')

ms.set_kernel(random.choice(filter(lambda s: s != 'fisher', ms.kernels())))
ms.set_spatial(random.choice(ms.spatials()))

# Print out basic stats...
print 'kernel = %s; spatial = %s' % (ms.get_kernel(), ms.get_spatial())
print 'exemplars = %i; features = %i' % (ms.exemplars(), ms.features())
print 'quality = %.3f; epsilon = %.3f; iter_cap = %i' % (
    ms.quality, ms.epsilon, ms.iter_cap)
print

# Query the mode of various points...
for x in numpy.arange(0.0, 7.0, 0.4):
    mode = ms.mode(numpy.array([x]))
示例#6
0
samples = 1024 * 16
samples_dir = 1024 * 2
scale = 16.0

dimensions = [1, 2, 3]
dir_dimensions = [2, 3, 4]
dir_conc = [2.0, 16.0, 128.0, 1024.0]

# Do the 'simple' kernels...
for kernel in [
        'uniform', 'triangular', 'epanechnikov', 'cosine', 'gaussian',
        'cauchy', 'logistic'
]:
    for dim in dimensions:
        # Create a mean shift object with a single sample of the provided kernel type...
        ms = MeanShift()
        ms.set_data(numpy.array([0.0] * dim, dtype=numpy.float32), 'f')
        ms.set_kernel(kernel)
        ms.quality = 1.0

        # Draw lots of samples from it...
        sample = ms.draws(samples)

        # Get the probability of each...
        p1 = ms.probs(sample)

        # Throw away samples where p1 is 0 - they are a result of the range optimisation, and break the below...
        keep = p1 > 1e-6
        sample = sample[keep, :]
        p1 = p1[keep]

# Simply creates two identical Fisher distributions, one using the approximation, the other the correct value, and then runs some tests to verify that they are the same.



# Parameters...
dims = 4
kernel = 'fisher'
conc = 256.0
samples = 1024



# Create the two distributions - don't need to be complicated...
correct = MeanShift()
correct.set_data(numpy.array([1.0] + [0.0]*(dims-1), dtype=numpy.float32), 'f')
correct.set_kernel('%s(%.1fc)' % (kernel, conc))
correct.quality = 1.0

approximate = MeanShift()
approximate.set_data(numpy.array([1.0] + [0.0]*(dims-1), dtype=numpy.float32), 'f')
approximate.set_kernel('%s(%.1fa)' % (kernel, conc))
approximate.quality = 1.0



# Draw a bunch of samples and compare the probabilities in both to check they are basically the same...
sample = correct.draws(samples)

cp = correct.probs(sample)
示例#8
0
#   http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

import random
import numpy

from ms import MeanShift

# Create some data (!)...
data = ([-2] * 3) + ([0] * 8) + ([1] * 4) + ([2] * 5)
data = numpy.array(data, dtype=numpy.int32)

# Setup the mean shift object...
ms = MeanShift()
ms.set_data(data, 'd')
ms.set_kernel('discrete')
ms.set_spatial('kd_tree')

# Iterate and calculate the probability at a bunch of points, then plot...
sam = numpy.arange(-2.5, 2.5, 0.1)
prob = numpy.array(map(lambda v: ms.prob(numpy.array([v])), sam))

print 'Distribution:'
for threshold in numpy.arange(prob.max(), 0.0, -prob.max() / 10.0):
    print ''.join(map(lambda p: '|' if p > threshold else ' ', prob))

# Draw a bunch of points, make a new ms and then plot, again...
ms2 = MeanShift()
ms2.set_data(ms.draws(50), 'df')
示例#9
0
for i in xrange(samples):
  data_b[i,3:] = to_origin(data_b[i,:3])



# Add a bit of noise...
data_a += 0.1 * numpy.random.normal(size=(samples,6))
data_b += 0.1 * numpy.random.normal(size=(samples,6))



# Create two distributions...
spatial_scale = 8.0
scale = numpy.array([spatial_scale, spatial_scale, spatial_scale, 1.0, 1.0, 1.0, 1.0])

mult_a = MeanShift()
mult_a.set_data(data_a, 'df', None, '...V')
mult_a.set_kernel('composite(3:gaussian,4:mirror_fisher(512.0))')
mult_a.set_spatial('kd_tree')
mult_a.set_scale(scale)

mult_b = MeanShift()
mult_b.set_data(data_b, 'df', None, '...V')
mult_b.copy_all(mult_a)
mult_b.set_scale(scale)



# A function for converting a distribution into a ply file...
def to_ply(fn, samples):
  # Open and header...
示例#10
0
import numpy.random
from scipy.misc import imsave

from utils.prog_bar import ProgBar

from ms import MeanShift

# Multiplies the three great bells (Gaussian, Cauchy and Logistic - not their actual name, but it should be:-P) such that only their tails overlap, drawing a lot of samples then visualising a density estimate of the resulting shape - a rather roundabout demonstration of how they are different...

great_bells = [('gaussian', 4.0), ('cauchy', 6.0), ('logistic', 8.0)]

for bell, gap in great_bells:
    print '%s:' % bell

    # Setup two single sample models...
    a = MeanShift()
    a.set_data(numpy.array([-0.5 * gap], dtype=numpy.float32), 'f')
    a.set_kernel(bell)
    a.quality = 1.0

    b = MeanShift()
    b.set_data(numpy.array([0.5 * gap], dtype=numpy.float32), 'f')
    b.set_kernel(bell)
    b.quality = 1.0

    # Multiply them and generate new distribution...
    draw = numpy.empty((1024, 1), dtype=numpy.float32)
    MeanShift.mult([a, b], draw)

    ab = MeanShift()
    ab.set_data(draw, 'df')
示例#11
0
    sin_theta = numpy.sin(theta)

    which = [deflection, cos_theta, sin_theta]
    chunk = numpy.concatenate((which[ex_dim].reshape(
        (-1, 1)), which[(ex_dim + 1) % 3].reshape(
            (-1, 1)), which[(ex_dim + 2) % 3].reshape((-1, 1))),
                              axis=1)

    data.append(chunk)

data = numpy.concatenate(data, axis=0)
data /= numpy.sqrt(numpy.square(data).sum(axis=1)).reshape((-1, 1))

# Setup mean shift...
print 'Fisher:'
ms = MeanShift()
ms.set_data(data, 'df')

ms.set_kernel('fisher(4096.0)')
ms.set_spatial('kd_tree')

# Visualise the samples on a mercator projection...
print '  Samples...'

image = numpy.zeros((height, width, 3), dtype=numpy.float32)
for vec in data:
    x = numpy.arctan2(vec[1], vec[0])
    if x < 0.0: x += 2.0 * numpy.pi
    x = x * width / (2.0 * numpy.pi)
    if x >= width: x -= width
示例#12
0
data = []

for camera in cameras:
    direction = numpy.random.vonmises(camera[3], conc, size=camera[0])
    x = numpy.random.normal(camera[1], sd_x, size=camera[0])
    y = numpy.random.normal(camera[2], sd_y, size=camera[0])

    block = numpy.concatenate((x.reshape((-1, 1)), y.reshape(
        (-1, 1)), numpy.cos(direction).reshape(
            (-1, 1)), numpy.sin(direction).reshape((-1, 1))),
                              axis=1)
    data.append(block)
data = numpy.concatenate(data, axis=0)

# Construct the mean shift object from it, including a composite kernel...
ms = MeanShift()
ms.set_data(data, 'df')
ms.set_kernel('composite(2:gaussian,2:fisher(32.0))')
ms.set_spatial('kd_tree')
ms.set_scale(numpy.array([10.0, 5.0, 1.0, 1.0]))
ms.merge_range = 0.05

# Print out information in a convoluted way to test some convoluted features!..
ms2 = MeanShift()
ms2.copy_kernel(ms)
print 'Kernel:', ms2.get_kernel()
del ms2

# For our first trick visualise the data set...
img = numpy.zeros((size, size, 3), dtype=numpy.float32)
示例#13
0
from utils.cvarray import *
from utils.prog_bar import ProgBar

from ms import MeanShift

# Test the mirrored version of the von_mises Fisher distribution, this time in 5D...

# Create a dataset - just a bunch of points in one direction, so we can test the mirroring effect (Abuse MeanShift object to do this)...
print 'Mirrored draws:'

vec = numpy.array([1.0, 0.5, 0.0, -0.5, -1.0])
vec /= numpy.sqrt(numpy.square(vec).sum())

print 'Base dir =', vec

draw = MeanShift()
draw.set_data(vec, 'f')
draw.set_kernel('fisher(256.0)')

data = draw.draws(32)

#print 'Input:'
#print data

# Create a mean shift object from the draws, but this time with a mirror_fisher kernel...
mirror = MeanShift()
mirror.set_data(data, 'df')
mirror.set_kernel('mirror_fisher(64.0)')

resample = mirror.draws(16)
示例#14
0
# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

import numpy
import numpy.random
from scipy.misc import imsave

from utils.prog_bar import ProgBar

from ms import MeanShift

# Visualises each kernel...
for kernel in MeanShift.kernels():
    if MeanShift.info_config(kernel) != None:
        continue

    a = MeanShift()
    a.set_data(numpy.array([0.0], dtype=numpy.float32), 'f')
    a.set_kernel(kernel)
    a.quality = 1.0

    image = numpy.ones((384, 1024), dtype=numpy.float32)
    x = numpy.linspace(-4, 4, image.shape[1])
    iy, ix = numpy.meshgrid(numpy.arange(image.shape[0]),
                            numpy.arange(image.shape[1]),
                            indexing='ij')

    ya = a.probs(x[:, None])
    ya = (image.shape[0] - 1 - (image.shape[0] - 1) * (ya / ya.max())).astype(
        numpy.int32)

    image[iy >= ya[None, :]] = 0.0
示例#15
0
# Parameters...
samples = 1024 * 1024
dimensions = [1, 2, 3]
dir_dimensions = [2, 3, 4]
dir_area = [numpy.pi * 2.0, numpy.pi * 4.0, 2.0 * numpy.pi**2]
dir_conc = [2.0, 16.0, 128.0, 1024.0]

# Do the 'simple' kernels...
for kernel in [
        'uniform', 'triangular', 'epanechnikov', 'cosine', 'gaussian',
        'cauchy', 'logistic'
]:
    for dim in dimensions:
        # Create a mean shift object with a single sample of the provided kernel type...
        ms = MeanShift()
        ms.set_data(numpy.array([0.0] * dim, dtype=numpy.float32), 'f')
        ms.set_kernel(kernel)
        ms.quality = 1.0

        # Create a uniform sample over a suitably large region (Yes I am assuming I got the uniform kernel right!)...
        uniform = MeanShift()
        uniform.set_data(numpy.array([0.0] * dim, dtype=numpy.float32), 'f')
        uniform.set_kernel('uniform')
        uniform.set_scale(numpy.ones(dim) / ms.get_range())
        sample = uniform.draws(samples)
        sp = uniform.prob(sample[0, :])

        # Evaluate the probabilities of the uniform set...
        p = ms.probs(sample)
示例#16
0
#   http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

import random
import numpy

from ms import MeanShift

# Create a dataset - draws from a Gaussian...
data = numpy.array(map(lambda _: random.normalvariate(0.0, 2.0), xrange(1000)))

# Setup three mean shift objects with the same data set and draw from them to demonstrate that you get the exact same output...
print 'Should all be the same:'
ms = map(lambda _: MeanShift(), xrange(3))
for i in xrange(len(ms)):
    ms[i].set_data(data, 'd')
    ms[i].set_kernel('gaussian')
    ms[i].set_spatial('kd_tree')

    print 'From', i, '|', ms[i].draw()
print

# Link the second to the first and draw again - first two should be different, third the same as the first...
ms[1].link_rng(ms[0])

print '#2 different:'
for i in xrange(len(ms)):
    print 'From', i, '|', ms[i].draw()
print