Esempio n. 1
0
 def AddTrainNet(self, net):
     Solver.AddTrainNet(self, net)
     params = self.net_.learnable_params()
     for i in range(len(params)):
         blob = Blob()
         blob.ReshapeLike(params[i])
         self.history_.append(blob)
Esempio n. 2
0
    def __init__(self, pic, tau):
        """
        Constructs a blob finder to find blobs in the picture pic, using
        a luminance threshold tau.
        """

        # Initialize an empty list for the blobs in pic.
        self._blobs = []

        # Create a 2D list of booleans called marked, having the same
        # dimensions as pic.
        x = pic.width()
        y = pic.height()
        marked = stdarray.create2D(x, y, False)

        # Enumerate the pixels of pic, and for each pixel (i, j):
        for i in range(x):
            for j in range(y):
                # 1. Create a Blob object called blob.
                blob = Blob()

                # 2. Call _findBlob() with the right arguments.
                self._findBlob(pic, tau, i, j, marked, blob)

                # 3. Add blob to _blobs if it has a non-zero mass.
                if blob.mass() > 0:
                    self._blobs.append(blob)
Esempio n. 3
0
 def __init__(self, threshold):
     NeuronLayer.__init__(self)
     self.threshold_ = threshold
     self.rand_blob_ = Blob()
     if 1.0 == threshold:
         self.scale_ = 1.0
     else:
         self.scale_ = 1.0 / (1.0 - threshold)
Esempio n. 4
0
 def test_ReshapeLike(self):
     blob = Blob()
     other1 = Blob()
     other2 = Blob()
     blob.set_data(numpy.array(range(30), float))
     blob.set_diff(numpy.array(range(30), float))
     blob.ReshapeLike(other1)
     blob.ReshapeLike(other2)
     blob.ReshapeLike(other1)
Esempio n. 5
0
 def reset(self):
     self.player = Blob(
         *self.np_random.randint(0, self.size, (2, ), dtype=int))
     self.food = Blob(
         *self.np_random.randint(0, self.size, (2, ), dtype=int))
     self.enemy = Blob(
         *self.np_random.randint(0, self.size, (2, ), dtype=int))
     self.state = np.array([self.player.pos, self.food.pos, self.enemy.pos])
     return self.state
Esempio n. 6
0
def main():
    blue_blobs = dict(enumerate([Blob(BLUE, WIDTH, HEIGHT) for i in range(STARTING_BLUE_BLOBS)]))
    red_blobs = dict(enumerate([Blob(RED, WIDTH, HEIGHT) for i in range(STARTING_RED_BLOBS)]))
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
        draw_environment([blue_blobs, red_blobs])
        clock.tick(60)
Esempio n. 7
0
def encrypt(input):
    """Encrypts the given string following the same syscalls as done by
    ConvertFrom-SecureString.

    Arguments:
    input -- an input string.

    Returns:
    output -- string containing the output of the encryption in hexadecimal.
    """
    # CryptProtectData takes UTF-16; so we must convert the data here:
    encoded = input.encode("utf-16")
    data = create_string_buffer(encoded, len(encoded))

    # create our various Blobs:
    input_blob = Blob(len(encoded), data)
    output_blob = Blob()
    flag = 0x01

    # call CryptProtectData:
    res = protect_data(byref(input_blob), u"", byref(Blob()), None,
                       None, flag, byref(output_blob))
    input_blob.free_blob()

    # check return code:
    if res == 0:
        output_blob.free_blob()
        raise Exception("Failed to encrypt: %s" % input)
    else:
        raw = output_blob.get_data()
        output_blob.free_blob()

        # encode the resulting bytes into hexadecimal before returning:
        hex = encode(raw, "hex")
        return decode(hex, "utf-8").upper()
Esempio n. 8
0
def decrypt(input):
    """Decrypts the given hexadecimally-encoded string in conformity
    with CryptUnprotectData.

    Arguments:
    input -- the encrypted input string in hexadecimal format.

    Returns:
    output -- string containing the output of decryption.
    """
    # de-hex the input:
    rawinput = decode(input, "hex")
    data = create_string_buffer(rawinput, len(rawinput))

    # create out various Blobs:
    input_blob = Blob(len(rawinput), data)
    output_blob = Blob()
    dwflags = 0x01

    # call CryptUnprotectData:
    res = unprotect_data(byref(input_blob), u"", byref(Blob()), None,
                         None, dwflags, byref(output_blob))
    input_blob.free_blob()

    # check return code:
    if res == 0:
        output_blob.free_blob()
        raise Exception("Failed to decrypt: %s" + input)
    else:
        raw = output_blob.get_data()
        output_blob.free_blob()

        # decode the resulting data from UTF-16:
        return decode(raw, "utf-16")
Esempio n. 9
0
class Env:
    LOSE_PENALTY = -25
    BALL_REWARD = 25
    OBSERVATION_SPACE_VALUES = 3
    ACTION_SPACE_SIZE = 3

    def reset(self):
        self.blob = Blob(20, windowHeight - 110)
        self.ball = Ball(20, windowHeight // 2)
        self.wall = Wall(windowWidth // 2 - 20, windowHeight - 250)
        self.enemyBlob = Blob(windowWidth - 60, windowHeight - 110)

        self.episode_step = 0

        observation = [
            self.ball.x, self.ball.y, self.blob.x, self.ball.velocity
        ]
        return observation

    def step(self, action):
        self.episode_step += 1
        self.blob.move(action, 0, windowWidth // 2)

        self.enemyBlob.follow(self.ball, windowWidth // 2, True)

        self.ball.move()
        self.ball.gravity(windowHeight)
        self.ball.collide(self.blob)
        self.ball.collide(self.enemyBlob)

        new_observation = [
            self.ball.x, self.ball.y, self.blob.x, self.ball.velocity
        ]

        if (self.ball.y >= windowHeight - self.ball.height - 10
                and self.ball.x < windowWidth // 2 - 30):
            reward = self.LOSE_PENALTY
        elif (self.ball.y >= windowHeight - self.ball.height - 10
              and self.ball.x >= windowWidth // 2 - 30
              and self.ball.x < windowWidth):
            reward = self.BALL_REWARD
        elif (self.ball.x <= 10):
            reward = self.LOSE_PENALTY
        elif (self.ball.x >= windowWidth - 10):
            reward = self.BALL_REWARD
        elif (abs(self.ball.x - self.blob.x) > (windowWidth // 2 % 10)):
            reward = 5
        else:
            reward = 1

        done = False
        if reward == self.BALL_REWARD or reward == self.LOSE_PENALTY or self.episode_step >= 200:
            done = True

        return new_observation, reward, done
Esempio n. 10
0
    def isolate(self,frame):
        blobs = []
        grayscale  = cv2.cvtColor( frame.data, cv2.COLOR_BGR2GRAY )
        rectangles = self.cascade.detectMultiScale( grayscale, scaleFactor=1.2, minNeighbors=3, minSize=self.min_size)
        
        for r in rectangles:
            b = Blob( self.get_estimation_id() ) 
            b.x, b.y = r[0:2]
            b.w, b.h = r[2:4]
            blobs.append(b)

        return blobs
Esempio n. 11
0
 def __init__(self):
     self.player_color = (255, 0, 0)
     self.enemy_color = (0, 0, 255)
     self.food_color = (0, 255, 0)
     self.board = np.zeros((self.SIZE, self.SIZE, 3), dtype=np.uint8)
     self.player = Blob(self.SIZE)
     self.board[self.player.y, self.player.x] = self.player_color
     self.enemy = self.get_enemy_blob()
     self.board[self.enemy.y, self.enemy.x] = self.enemy_color
     self.food = self.get_food_blob()
     self.board[self.food.y, self.food.x] = self.food_color
     self.target_update_counter = 0
Esempio n. 12
0
    def reset(self):
        self.blob = Blob(20, windowHeight - 110)
        self.ball = Ball(20, windowHeight // 2)
        self.wall = Wall(windowWidth // 2 - 20, windowHeight - 250)
        self.enemyBlob = Blob(windowWidth - 60, windowHeight - 110)

        self.episode_step = 0

        observation = [
            self.ball.x, self.ball.y, self.blob.x, self.ball.velocity
        ]
        return observation
Esempio n. 13
0
def create_blobs_line(settings, screen, blobs):
    """Создает ряд капель"""
    blob = Blob(settings, screen)
    blob_width = blob.rect.width
    blob_number_x = get_number_blob_x(settings, blob_width)

    # Создаем ряд капель
    for blob_number in range(blob_number_x):
        # Создание капли и размещение ее в ряду
        blob = Blob(settings, screen)
        blob.x = blob_width + 2 * blob_width * blob_number
        blob.rect.x = blob.x
        blobs.add(blob)
Esempio n. 14
0
 def _init_net_blobs(self):
     """
         Expose the blobs (data/diff) of net to python. No copy.
     """
     net_blobs = list()
     for l in self.net.layers:
         if l.type == "Convolution":
             net_blobs.append([Blob(l.blobs[0])])
         elif l.type == "Scale" or l.type == "InnerProduct":
             net_blobs.append([Blob(l.blobs[0]), Blob(l.blobs[1])])
         else:
             net_blobs.append(None)
     return net_blobs
Esempio n. 15
0
 def test_Reshape(self):
     blob = Blob()
     blob.set_data(numpy.array(range(30), float))
     blob.set_diff(numpy.array(range(30), float))
     blob.Reshape((5, 3))
     blob.Reshape((5, 6))
     blob.Reshape((2, 15))
Esempio n. 16
0
    def test_mnist_mlp_net_solver(self):
        train_net = Net()
        test_net = Net()

        bottom = Blob()
        label  = Blob()
        top    = Blob()
        top1   = Blob()
        top2   = Blob()
        loss   = Blob()
        top4   = Blob()
        top5   = Blob()
        top6   = Blob()
        top7   = Blob()

        batch_size = 100

        test = MNISTTestDataLayer(batch_size)
        train = MNISTTrainDataLayer(batch_size)
        acc   = AccuracyLayer()

        fc1  = InnerProductLayer(784,300)
        relu = ReLULayer()
        drop = DropoutLayer(0.75)
        drop2 = DropoutLayer(1.0)
        fc2  = InnerProductLayer(300,10)
        softmaxloss = SoftmaxLossLayer()

        train_net.AddLayer(train, [], [bottom,label])
        train_net.AddLayer(fc1, [bottom], [top])
        train_net.AddLayer(relu, [top], [top1])
        train_net.AddLayer(drop, [top1], [top4])
        train_net.AddLayer(fc2, [top4], [top2])
        train_net.AddLayer(softmaxloss, [top2,label], [loss,top5])

        test_net.AddLayer(test, [], [bottom,label])
        test_net.AddLayer(fc1, [bottom], [top])
        test_net.AddLayer(relu, [top], [top1])
        test_net.AddLayer(drop2, [top1], [top4])
        test_net.AddLayer(fc2, [top4], [top2])
        test_net.AddLayer(softmaxloss, [top2,label], [loss,top5])
        test_net.AddLayer(acc, [top5,label], [top6,top7])

        test_net.AddOutputBlob(top6)
        test_net.AddOutputBlob(top7)

        solver = AdaDeltaSolver(0.1)
        solver.AddTrainNet(train_net)
        solver.AddTestNet(test_net)
        solver.Solve(3000)
Esempio n. 17
0
def load_save():
    filename = 'save.txt'
    f = open(filename, 'r')
    name = json.loads(f.readline().strip())
    player = Player(name)
    player.inventory = json.loads(f.readline())
    for i in range(json.loads(f.readline())):
        blob = Blob()
        data = json.loads(f.readline())
        blob.name, blob.color, blob.egg_color, blob.num_eyes, blob.size, \
        blob.age, blob.happiness, blob.hunger, blob.health, blob.traits, \
        blob.inventory = data
        player.blobs.append(blob)
    return player
Esempio n. 18
0
class EuclideanLossLayer(LossLayer):
    def __init__(self):
        LossLayer.__init__(self)
        self.diff_ = Blob(numpy.float, [6])

    def Reshape(self, bottom, top):
        LossLayer.Reshape(self, bottom, top)
        self.diff_.ReshapeLike(bottom[0])

    def type(self):
        return 'EuclideanLoss'

    def AllowForceBackward(self, bottom_index):
        return True

    def Forward_cpu(self, bottom, top):
        self.diff_.set_data(bottom[0].data() - bottom[1].data())
        dot = numpy.dot(self.diff_.data(), self.diff_.data())
        loss = dot / bottom[0].shape()[0] / 2
        top[0].set_data(loss)

    def Backward_cpu(self, top, propagate_down, bottom):
        print top[0].diff()
        print bottom[0].shape()[0]
        print self.diff_.data()
        bottom[0].set_diff(top[0].diff() / bottom[0].shape()[0] *
                           self.diff_.data())
Esempio n. 19
0
def publish_stats(feature_key,
                  image,
                  classifier_stats,
                  blob_config=BlobConfig()):
    """Calculate biovolume, carbon, hab, and publish to Kafka"""
    # calculate biovolume
    # - scale biovolume for 3d (from ifcb-analysis)
    blob = Blob(image, blob_config)
    biovolume = calc_biovolume(blob)
    mu = 1 / 3.4
    biovolume = biovolume * mu**3
    carbon = calc_carbon(classifier_stats[0], biovolume)
    hab = classifier_stats[0] in hab_species

    time, ifcb_id, roi = feature_key.split('_')
    roi = int(roi)
    timestamp = int(
        datetime.datetime.strptime(time[1:], '%Y%m%dT%H%M%S').timestamp())
    stats = Stats(timestamp, ifcb_id, roi, classifier_stats[0],
                  classifier_stats[2], classifier_stats[1],
                  classifier_stats[3], biovolume, carbon, hab)

    # send to topic with Avro schema
    producer.poll(0)
    producer.produce(topic=config['stats_topic'],
                     key={
                         'pid': f"{time}_{ifcb_id}",
                         'roi': int(roi)
                     },
                     value=stats._asdict())
    producer.flush()
Esempio n. 20
0
    def __init__(self, picture, tau):
        """
        Constructs a blob finder to find blobs in the picture pic,using of threshold tau
        """
        self.tau = tau
        self.photo = picture
        # create a array with picture size
        check = stdarray.create2D(self.photo.height(), self.photo.width(),
                                  False)

        # list of blobs
        self.blobs = []

        #  Modifies RGB photos
        for i in range(self.photo.height()):
            for j in range(self.photo.width()):
                color = self.photo.get(j, i)
                r = color.getRed()
                g = color.getGreen()
                b = color.getBlue()
                if r >= self.tau and g >= self.tau and b >= self.tau:
                    check[i][j] = True

        for i in range(self.photo.height()):
            for j in range(self.photo.width()):
                if check[i][j]:
                    blob = Blob()
                    self.proccess(blob, check, i, j)
                    self.blobs.append(blob)
Esempio n. 21
0
def new_game():
    name = input('Enter name: ')
    player = Player(name)
    first_blob = Blob()
    player.blobs.append(first_blob)
    print('Hello, ' + player.name + ', you found an egg!')
    return player, first_blob
Esempio n. 22
0
def main():
    #red_blob = Blob(BLUE)
    blue_blobs = dict(
        enumerate(
            [Blob(BLUE, WIDTH, HEIGHT) for _ in range(STARTING_BLUE_BLOBS)]))
    red_blobs = dict(
        enumerate(
            [Blob(RED, WIDTH, HEIGHT) for _ in range(STARTING_RED_BLOBS)]))
    display_screen = True
    while display_screen:

        draw_environment([blue_blobs, red_blobs])
        clock.tick(60)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                display_screen = False
Esempio n. 23
0
def main():
    infected_blobs = set([Blob(True) for i in range(STARTING_INFECTED_BLOBS)])
    normal_blobs = set([
        Blob(False)
        for i in range(STARTING_TOTAL_BLOBS - STARTING_INFECTED_BLOBS)
    ])
    RECOVERED = set()
    DEAD = set()
    insert_data(infected_blobs, normal_blobs, RECOVERED, DEAD)
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
        blue_blobs, red_blobs = draw_environment(
            [infected_blobs, normal_blobs, RECOVERED, DEAD])
        clock.tick(60)
Esempio n. 24
0
    def __init__(self, K, N):
        Layer.__init__(self)

        # input number of neuron
        self.K_ = K

        # output number of neuron
        self.N_ = N

        self.bias_term_ = None
        self.bias_multiplier_ = None
        self.transpose_ = False

        self.W = Blob()
        self.b = Blob()
        self.blobs_.append(self.W)
        self.blobs_.append(self.b)
def main():
    # red_blob = Blob(RED)
    # blue_blobs = [Blob(BLUE) for i in range(STARTING_BLUE_BLOBS)]
    blue_blobs = dict(
        enumerate(
            [Blob(BLUE, WIDTH, HEIGHT) for i in range(STARTING_BLUE_BLOBS)]))
    red_blobs = dict(
        enumerate(
            [Blob(RED, WIDTH, HEIGHT) for i in range(STARTING_RED_BLOBS)]))
    # print(blue_blobs)
    while True:
        # for event in pygame.event.get():
        #     if event.type == pygame.QUIT():
        #         pygame.quit()
        #         quit()
        # draw_environment(red_blob)
        draw_environment([blue_blobs, red_blobs])
        clock.tick(60)
Esempio n. 26
0
    def reset(self, population_count, inherit=True):
        '''
        Reset has two options:
            (1) Reset with inheritance populates the gridworld to time 0 
                with blobs in original positions and updates value grid.
            (2) Reset with inheritance creates a fresh grid from previous grid 
                and population count must be inserted.
        '''

        # If inerhitance is true check if population_count is same as before
        if (inherit == True) and (population_count !=
                                  self.beginning_population_count):
            exception_string = 'ERROR: Original population_count is, ' + \
                                str(self.beginning_population_count) + \
                                ', does not match the input population_count, '\
                                + str(population_count) + '.'
            raise Exception(exception_string)

        # Inherit reset
        self.time = -1
        self.status = 'Not Started'
        self.population_count = population_count
        self.beginning_population_count = population_count
        if inherit == True:
            self.positions = deepcopy(self.beginning_positions)

            # Initialise blobs
            blobs = []
            for index, original_positions in enumerate(self.positions[0]):
                blob_name, coords = original_positions
                blobs.append(
                    Blob(name=blob_name,
                         time=0,
                         max_age=30,
                         coords=coords,
                         value_grid=self.blobs[index].value_grid))

            # Update parameters
            self.blobs = blobs
            self.time = 0
            self.status = 'In progress'
            self.iteration_count = self.iteration_count + 1
            self.iteration_rewards.append(self.total_rewards)
            self.total_rewards = 0
            if self.verbose:
                print('\nGridWorld reset and populated with inheritance.\n')

        # Don't inherit
        else:
            self.total_rewards = 0
            del self.positions
            del self.beginning_positions
            del self.blobs
            self.iteration_count = 0
            self.iteration_rewards = []
            if self.verbose:
                print('\nGridWorld reset.\n')
Esempio n. 27
0
    def __init__(self, picture, tau):

        w = picture.width()
        h = picture.height()
        conditon = [None] * h
        pic = [None] * h
        blobs = []

        # creating two w*h array
        for i in range(h):
            pic[i] = [0] * w
            conditon[i] = [False] * w

        # lumanance is for specifing
        # a pixel as a bead or water
        def lumanance(c):
            r = c.getRed()
            g = c.getGreen()
            b = c.getBlue()
            return 0.299 * r + 0.587 * g + 0.114 * b

        for row in range(h):
            for col in range(w):
                c = picture.get(col, row)
                brightness = int(round(lumanance(c)))
                if brightness >= tau:
                    pic[row][col] = 1

        # blobfinder is recursive function
        # that group the beads near to each
        # other as a "blob"

        def blobfinder(pic, condition, blob, row, col):
            if row < 0 or row >= h or col < 0 or col >= w:
                return
            if pic[row][col] == 0:
                return
            elif condition[row][col]:
                return

            blob.add(col, row)
            condition[row][col] = True

            blobfinder(pic, condition, blob, row - 1, col)
            blobfinder(pic, condition, blob, row + 1, col)
            blobfinder(pic, condition, blob, row, col + 1)
            blobfinder(pic, condition, blob, row, col - 1)

        # finding blobs with searching in all
        # pixels of a picture
        for row in range(h):
            for col in range(w):
                if not conditon[row][col] and pic[row][col] == 1:
                    blob = Blob()
                    blobfinder(pic, conditon, blob, row, col)
                    blobs.append(blob)
        self.blobs = blobs
Esempio n. 28
0
    def generate_random_state():
        new_state = WorldState()

        number_of_blobs = 3

        for _ in range(number_of_blobs):
            new_state.blobs.append(Blob(x=random(), y=random()))

        return new_state
Esempio n. 29
0
 def _init_history(self):
     """
         Init the history (momentum) of blobs
     """
     history = list()
     for l in self.net.layers:
         if l.type == "Convolution":
             #                history.append([np.zeros_like(l.blobs[0].data)])
             history.append([Blob(l.blobs[0], copy=True)])
         elif l.type == "Scale" or l.type == "InnerProduct":
             #                history.append([np.zeros_like(l.blobs[0].data),
             #                                np.zeros_like(l.blobs[1].data)])
             history.append(
                 [Blob(l.blobs[0], copy=True),
                  Blob(l.blobs[1], copy=True)])
         else:
             history.append(None)
     return history
Esempio n. 30
0
    def AddTrainNet(self, net):
        Solver.AddTrainNet(self, net)
        params = self.net_.learnable_params()
        for i in range(len(params)):
            h = Blob()
            u = Blob()
            t = Blob()
            h.ReshapeLike(params[i])
            u.ReshapeLike(params[i])
            t.ReshapeLike(params[i])
            self.history_.append(h)
            self.update_.append(u)
            self.temp_.append(t)

        self.history_.extend(self.history_)
    def __init__(self, pic, tau):
        """
        Constructs a blob finder to find blobs in the picture pic, using
        a luminance threshold tau.
        """

        # set the blobs list
        self._blobs = []

        # Create a 2D array list with booleans
        List = stdarray.create2D(pic.width(), pic.height(), False)

        # Enumerate the pixels of pics
        for i in range(pic.width()):
            for j in range(pic.height()):
                blob = Blob()
                self._findBlob(pic, tau, i, j, List, blob)
                if blob.mass() > 0:
                    self._blobs.append(blob)
Esempio n. 32
0
    def blob(self, id):
        """
        The Blob object for the given id

        ``id``
            is the SHA1 id of the blob

        Returns
            ``git.Blob``
        """
        return Blob(self, id=id)
 def __init__(self, x_boundary, y_boundary):
     Blob.__init__(self, (0, 0, 255), x_boundary, y_boundary)
Esempio n. 34
0
        self.type, = struct.unpack("<l", blob[1].data)
        if self.type == RegistryValue.TYPE_STRING:
            self.data = clean_str(blob[2].data)
        elif self.type == RegistryValue.TYPE_DWORD:
            self.data = struct.unpack("<L", blob[2].data[:4])
        else: # elif self.type == RegistryValue.TYPE_BINARY:
            if blob[2].child is not None:
                self.data = blob[2].child
            else:
                self.data = blob[2].data

    def __str__(self):
        return self.__repr__()
    def __repr__(self):
        if self.type == RegistryValue.TYPE_STRING:
            return str("'" + self.data + "'")
        elif self.type == RegistryValue.TYPE_DWORD:
            return str(self.data)
        elif self.type == RegistryValue.TYPE_BINARY:
            if type(self.data) is str:
                return str("".join(["\\x%s" % hex(ord(c))[2:] for c in self.data])) + "'"
            else:
                return repr(self.data)

if __name__ == "__main__":
    handle = open("ClientRegistry.blob", "rb")
    blob = Blob()
    blob.read(handle)
    registry = Registry()
    registry.read(blob)