Example #1
0
 def __getitem__(self, item):
     # Get parameter
     gp = GlobalProperties()
     camMat = gp.getCamMat()
     # Sampling for reprojection error image
     sampling = cnn.stochasticSubSample(self.imgBGR,
                                        targetsize=self.objInputSize,
                                        patchsize=self.rgbInputSize)
     estObj = cnn.getCoordImg(self.imgBGR, sampling, self.rgbInputSize,
                              self.model)
     # Produce GroundTruth Label
     poseGT = Hypothesis()
     poseGT.Info(self.info)
     driftLevel = np.random.randint(0, 3)
     if not driftLevel:
         poseNoise = poseGT * getRandHyp(2, 2)
     else:
         poseNoise = poseGT * getRandHyp(10, 100)
     data = cnn.getDiffMap(
         TYPE.our2cv([poseNoise.getRotation(),
                      poseNoise.getTranslation()]), estObj, sampling,
         camMat)
     data = self.transform(data)
     label = -1 * self.temperature * max(
         poseGT.calcAngularDistance(poseNoise),
         np.linalg.norm(poseGT.getTranslation() -
                        poseNoise.getTranslation()) / 10.0)
     return data, label
Example #2
0
    def __init__(self, value=None, N=1, proposal=numpy.eye(1)):
        #print numpy.array([0.0]*N), [prposal
        if value is None:
            value = numpy.random.multivariate_normal(numpy.array([0.0] * N),
                                                     proposal)
        Hypothesis.__init__(self, value=value)

        self.__dict__.update(locals())
Example #3
0
 def __init__(self, value=None, n=1, proposal=None):
     if proposal is None:
         proposal = numpy.eye(n)
     if value is None:
         value = numpy.random.multivariate_normal(numpy.array([0.0]*n), proposal)
     Hypothesis.__init__(self, value=value)
     self.n = n
     self.proposal = proposal
     self.__dict__.update(locals())
Example #4
0
	def __init__(self, value=None, f=None, args=['x']):
		"""
			value - the value of this hypothesis
			f - defaultly None, in which case this uses self.value2function
			args - the argumetns to the function
		"""
		self.args = args # must come first since below calls value2function
		Hypothesis.__init__(self,value) # this initializes prior and likleihood variables, so keep it here!
		self.set_value(value,f)
Example #5
0
	def set_value(self, value, f=None):
		"""
		The the value. You optionally can send f, and not write (this is for some speed considerations) but you better be sure f is correct
		since an error will not be caught!
		"""
		
		Hypothesis.set_value(self,value)
		
		if f is not None:     self.fvalue = f
		elif value is None:   self.fvalue = None
		else:                 self.fvalue = self.value2function(value)
Example #6
0
    def set_value(self, value, f=None):
        """
                Sets the value for the hypothesis.
                Another option: send f, and not write (this is for some speed considerations) but you better be sure f is correct
                since an error will not be caught!
        """

        Hypothesis.set_value(self, value)

        if f is not None: self.fvalue = f
        elif value is None: self.fvalue = None
        else: self.fvalue = self.value2function(value)
Example #7
0
 def __init__(self, value=None, n=1, proposal=None, propose_scale=1.0, propose_n=1):
     self.n = n
     self.propose_n = propose_n
     if value is None:
         value = np.random.multivariate_normal(np.array([0.0] * n), proposal)
     if proposal is None:
         proposal = np.eye(n) * propose_scale
     propose_mask = self.get_propose_mask()
     proposal = proposal * propose_mask
     self.proposal = proposal
     Hypothesis.__init__(self, value=value)
     self_update(self, locals())
Example #8
0
    def __init__(self, value=None, f=None, args=['x'], **kwargs):
        """
                *value* - the value of this hypothesis

                *f* - defaultly None, in which case this uses self.value2function

                *args* - the arguments to the function
        """
        self.args = args  # must come first since below calls value2function
        Hypothesis.__init__(
            self, value, **kwargs
        )  # this initializes prior and likleihood variables, so keep it here!
        self.set_value(value, f)
Example #9
0
    def __init__(self, value=None, f=None, display="lambda x: %s", **kwargs):
        """
                *value* - the value of this hypothesis

                *f* - defaultly None, in which case this uses self.value2function

                *args* - the arguments to the function
        """
        # this initializes prior and likleihood variables, so keep it here!
        # However, don't give it value, since then it calls set_value with no f argument!
        Hypothesis.__init__(self, None, display=display, **kwargs)

        # And set our value
        self.set_value(value, f=f)
Example #10
0
    def set_value(self, value, f=None):
        """
                Sets the value for the hypothesis.
                Another option: send f if speed is necessary
        """

        Hypothesis.set_value(self, value)

        if f is not None:
            self.fvalue = f
        elif value is None:
            self.fvalue = None
        else:
            self.fvalue = self.compile_function()  # now that the value is set
Example #11
0
    def set_value(self, value, f=None):
        """
                Sets the value for the hypothesis.
                Another option: send f if speed is necessary
        """

        Hypothesis.set_value(self, value)

        if f is not None:
            self.fvalue = f
        elif value is None:
            self.fvalue = None
        else:
            self.fvalue =  self.compile_function() # now that the value is set
    def __getitem__(self, item):

        # Get raw data
        imgBGR = self.dataset.rand_getBGR(item)
        info = self.dataset.rand_getInfo(item)
        poseGT = Hypothesis()
        poseGT.Info(info)

        # Sampling for reprojection error image
        sampling = cnn.stochasticSubSample(imgBGR,
                                           targetsize=self.objInputSize,
                                           patchsize=self.rgbInputSize)
        estObj = cnn.getCoordImg1(imgBGR, sampling, self.rgbInputSize,
                                  self.model)

        return imgBGR, poseGT, sampling, estObj
Example #13
0
    def __init__(self, value=None, f=None, args=['x'], **kwargs):
        """
                *value* - the value of this hypothesis

                *f* - defaultly None, in which case this uses self.value2function

                *args* - the arguments to the function
        """
        self.args = args  # must come first since below calls value2function

        # this initializes prior and likleihood variables, so keep it here!
        # However, don't give it value, since then it calls set_value with no f argument!
        Hypothesis.__init__(self, None, **kwargs)

        # And set our value
        self.set_value(value, f=f)
 def test_init_hypothesis(self):
     u0 = 1
     std0 = 1
     hypothesis = Hypothesis.draw_init_hypothesis(self.model, u0, std0)
     self.assertEqual((u0, std0),
                      hypothesis.get_reward_table(self.s1, self.act_a))
     self.assertEqual((u0, std0),
                      hypothesis.get_reward_table(self.s1, self.act_b))
     self.assertEqual((u0, std0),
                      hypothesis.get_reward_table(self.s1, self.act_a))
     self.assertEqual((u0, std0),
                      hypothesis.get_reward_table(self.s2, self.act_b))
     self.assertEqual((u0, std0),
                      hypothesis.get_reward_table(self.s2, self.act_a))
     self.assertEqual((u0, std0),
                      hypothesis.get_reward_table(self.s3, self.act_b))
     self.assertEqual((u0, std0),
                      hypothesis.get_reward_table(self.s3, self.act_a))
     next_states = self.model.get_next_states(self.s1)
     s = 0
     for next_state in next_states:
         p = hypothesis.get_transition(self.s1, self.act_a, next_state)
         s += p
         self.assertGreater(p, 0)
     self.assertAlmostEqual(s, 1)
Example #15
0
    def __init__(self, value=None, f=None, args=['x'], **kwargs):
        """
                *value* - the value of this hypothesis

                *f* - defaultly None, in which case this uses self.value2function

                *args* - the arguments to the function
        """
        self.args = args # must come first since below calls value2function

        # this initializes prior and likleihood variables, so keep it here!
        # However, don't give it value, since then it calls set_value with no f argument!
        Hypothesis.__init__(self, None, **kwargs)

        # And set our value
        self.set_value(value, f=f)
 def constructHypo(self, hypo, score, english, start, end):
     """ Construct a new hypothesis by updating the score, its 
         output and coverage.
     """
     new_score = hypo.score + score
     new_output = hypo.output + " " + english
     new_wordscovered = hypo.wordscovered.copy()
     new_wordscovered.setPos(range(start, end))
     return Hypothesis(new_wordscovered, new_score, new_output)
Example #17
0
 def __init__(self,
              value=None,
              n=1,
              proposal=None,
              propose_scale=1.0,
              propose_n=1):
     self.n = n
     self.propose_n = propose_n
     if value is None:
         value = np.random.multivariate_normal(np.array([0.0] * n),
                                               proposal)
     if proposal is None:
         proposal = np.eye(n) * propose_scale
     propose_mask = self.get_propose_mask()
     proposal = proposal * propose_mask
     self.proposal = proposal
     Hypothesis.__init__(self, value=value)
     self_update(self, locals())
Example #18
0
 def test_draw_hypothesis(self):
     keepr = Keeper()
     for i in range(1000):
         keepr.update_reward_and_transition(self.s1, self.act_a, self.s2, 1.4)        
     hypothesis = Hypothesis.draw_hypothesis(self.model, keepr)
     for next_state in self.model.get_next_states(self.s1):
         self.assertGreater(hypothesis.get_transition(self.s1, self.act_a, next_state), 0)
     places = 1
     self.assertAlmostEqual(hypothesis.get_transition(self.s1, self.act_a, self.s2), 1, places)       
     self.assertAlmostEqual(hypothesis.get_reward(self.s1, self.act_a), 1.4, places)            
Example #19
0
def getRandHyp(gaussRot, gaussTrans):
    trans = np.array([
        np.random.normal(0, gaussTrans),
        np.random.normal(0, gaussTrans),
        np.random.normal(0, gaussTrans)
    ])
    rotAxis = np.array(([np.random.rand(),
                         np.random.rand(),
                         np.random.rand()]))
    rotAxis = rotAxis / np.linalg.norm(rotAxis)
    rotAxis = rotAxis * np.random.normal(0, gaussRot) * math.pi / 180
    # Construct rot vector and translation vector
    RotVec = np.zeros(6)
    RotVec[:3] = rotAxis
    RotVec[3:6] = trans
    # Construct a hypothesis
    h = Hypothesis()
    h.RodvecandTrans(RotVec)
    return h
 def test_draw_hypothesis(self):
     keepr = Keeper()
     for i in range(1000):
         keepr.update_reward_and_transition(self.s1, self.act_a, self.s2,
                                            1.4)
     hypothesis = Hypothesis.draw_hypothesis(self.model, keepr)
     for next_state in self.model.get_next_states(self.s1):
         self.assertGreater(
             hypothesis.get_transition(self.s1, self.act_a, next_state), 0)
     places = 1
     self.assertAlmostEqual(
         hypothesis.get_transition(self.s1, self.act_a, self.s2), 1, places)
     self.assertAlmostEqual(hypothesis.get_reward(self.s1, self.act_a), 1.4,
                            places)
Example #21
0
 def test_init_hypothesis(self):
     u0 = 1
     std0 = 1
     hypothesis = Hypothesis.draw_init_hypothesis(self.model, u0, std0)
     self.assertEqual((u0, std0), hypothesis.get_reward_table(self.s1, self.act_a))
     self.assertEqual((u0, std0), hypothesis.get_reward_table(self.s1, self.act_b))
     self.assertEqual((u0, std0), hypothesis.get_reward_table(self.s1, self.act_a))
     self.assertEqual((u0, std0), hypothesis.get_reward_table(self.s2, self.act_b))
     self.assertEqual((u0, std0), hypothesis.get_reward_table(self.s2, self.act_a))
     self.assertEqual((u0, std0), hypothesis.get_reward_table(self.s3, self.act_b))
     self.assertEqual((u0, std0), hypothesis.get_reward_table(self.s3, self.act_a))
     next_states = self.model.get_next_states(self.s1)
     s = 0
     for next_state in next_states:
         p = hypothesis.get_transition(self.s1, self.act_a, next_state)
         s += p
         self.assertGreater(p, 0)
     self.assertAlmostEqual(s, 1)
Example #22
0
import matplotlib.pyplot as plt
from random import randint
from matplotlib import cm
import numpy as np
from numpy import arange
from mpl_toolkits.mplot3d import Axes3D
from TrainingSet import TrainingSet
from Vector import Vector
from Hypothesis import Hypothesis
from TrainingExample import TrainingExample

# initial learning stuff
coeff = Vector(randint(-5, 5), randint(-5, 5))
ts = TrainingSet(Hypothesis(coeff, Hypothesis.LOGISTIC), Vector.fill(-1, 2),
                 Vector.fill(1, 2), 0, 100)

# data to plot surface
surface_0 = np.arange(-5, 5, 0.25)
surface_1 = np.arange(-5, 5, 0.25)
err = []
surface_0, surface_1 = np.meshgrid(surface_0, surface_1)

gradient_u = []
gradient_v = []
gradient_w = []

for s0, s1 in zip(surface_0, surface_1):
    err.append(ts.error(Hypothesis(Vector(s0, s1), Hypothesis.LOGISTIC)))
    grad = ts.mean_gradient(Hypothesis(Vector(s0, s1),
                                       Hypothesis.LOGISTIC)).unit() * .5
    gradient_u.append(grad.x)
 def decode(self):
     """ Fill the stacks by generating translations left-to-right. """
     self.stacks[0].add(Hypothesis(Wordscovered()))  # add empty hypothesis
     for stack in self.stacks:
         for hypo in stack:
             self.expand(hypo)
Example #24
0
    def __init__(self,value=None, N=1, proposal=numpy.eye(1)):
        #print numpy.array([0.0]*N), [prposal
        if value is None: value = numpy.random.multivariate_normal(numpy.array([0.0]*N), proposal)
        Hypothesis.__init__(self, value=value)

        self.__dict__.update(locals())