def test_ReLULayer(self): # top0.data = ReLU(bot0.data) # bot0.diff = ReLUGrad(top0.diff) bot0 = Blob() bot0.Reshape([2,6]) bot0.set_data([-1.0,2.0,-3.0,4.0,-5.0,6.0,-1.0,2.0,-3.0,4.0,-5.0,6.0]) bot0.Reshape([2,6]) top0 = Blob() top0.Reshape([2,6]) top0.set_diff([1.0,-2.0,3.0,-4.0,5.0,-6.0,1.0,-2.0,3.0,-4.0,5.0,-6.0]) top0.Reshape([2,6]) expect_bot0 = Blob() expect_bot0.Reshape([2,6]) expect_bot0.set_diff([0.0,-2.0,0.0,-4.0,0.0,-6.0,0.0,-2.0,0.0,-4.0,0.0,-6.0]) expect_bot0.Reshape([2,6]) expect_top0 = Blob() expect_top0.Reshape([2,6]) expect_top0.set_data([0.0,2.0,0.0,4.0,0.0,6.0,0.0,2.0,0.0,4.0,0.0,6.0]) expect_top0.Reshape([2,6]) layer = ReLULayer() layer.Setup([bot0], [top0]) layer.Forward([bot0], [top0]) np.testing.assert_array_equal( expect_top0.data(), top0.data() ) layer.Backward([top0], [], [bot0]) np.testing.assert_array_equal( expect_bot0.diff(), bot0.diff() )
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()
def test_SoftMaxLayer(self): bot0 = Blob() bot0.Reshape((3,)) bot0.set_data([1,2,3]) bot0.Reshape((3,)) top0 = Blob() top0.Reshape((3,)) expect_top0 = Blob() expect_top0.Reshape((3,)) expect_top0.set_data([0.09003057, 0.24472847, 0.66524096]) expect_top0.Reshape((3,)) expect_bot0 = Blob() expect_bot0.Reshape((3,)) expect_bot0.set_diff([0.0, 0.0, 0.0]) expect_bot0.Reshape((3,)) layer = SoftMaxLayer() layer.Setup([bot0], [top0]) layer.Forward([bot0], [top0]) np.testing.assert_array_almost_equal( top0.data(), expect_top0.data() ) top0.set_diff(np.ones_like(top0.data())) layer.Backward([top0], [], [bot0]) np.testing.assert_array_almost_equal( bot0.diff(), expect_bot0.diff() )
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")
def test_MaxPoolingLayer(self): top0 = Blob() bot0 = Blob() bot0.Reshape((1,1,4,4)) bot0.set_data([5,3,1,2,1,2,3,2,4,2,2,5,3,6,1,1]) bot0.Reshape((1,1,4,4)) expect_top0 = Blob() expect_top0.Reshape((1,1,2,2)) expect_top0.set_data([5,3,6,5]) expect_top0.Reshape((1,1,2,2)) expect_bot0 = Blob() expect_bot0.Reshape((1,1,4,4)) expect_bot0.set_diff([1,0,0,0,0,0,0.8,0,0,0,0,0.6,0,0.4,0,0]) expect_bot0.Reshape((1,1,4,4)) layer = MaxPoolingLayer(2,2,2) layer.Setup([bot0], [top0]) layer.Forward([bot0], [top0]) np.testing.assert_array_almost_equal( top0.data(), expect_top0.data() ) top0.set_diff([1.0,0.8,0.4,0.6]) top0.Reshape((1,1,2,2)) layer.Backward([top0], [], [bot0]) np.testing.assert_array_almost_equal( bot0.diff(), expect_bot0.diff() )
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
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)
def AddTrainNet(self, net): Solver.AddTrainNet(self, net) params = self.net_.learnable_params() for i in range(len(params)): s = Blob() r = Blob() s.ReshapeLike(params[i]) r.ReshapeLike(params[i]) self.s_.append(s) self.r_.append(r)
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)
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 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)
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
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)
def play(): blob = Blob(20, windowHeight - 110) ball = Ball(20, windowHeight // 2) wall = Wall(windowWidth // 2 - 20, windowHeight - 250) enemyBlob = Blob(windowWidth - 60, windowHeight - 110) run = True window = pygame.display.set_mode((windowWidth, windowHeight)) ballOn = False while run: pygame.time.delay(50) for event in pygame.event.get(): if (event.type == pygame.QUIT or pygame.key.get_pressed()[pygame.K_ESCAPE]): run = False pygame.quit() return keys = pygame.key.get_pressed() enemyBlob.follow(ball, windowWidth // 2, True) #blob.follow(ball,windowWidth//2,False) if (keys[pygame.K_LEFT] and blob.x > blob.movement): blob.move(0, 0, windowWidth // 2) elif (keys[pygame.K_RIGHT] and blob.x < windowWidth // 2 - blob.movement - 50): blob.move(1, 0, windowWidth // 2) else: blob.resetVelocity() if (keys[pygame.K_UP] and not blob.isJumping): blob.jump() if (keys[pygame.K_TAB]): ballOn = False ball = Ball(50, windowHeight // 2) if (keys[pygame.K_SPACE]): ballOn = True blob.gravity(windowHeight) if (ballOn): ball.move() ball.gravity(windowHeight) ball.collide(blob) ball.collide(enemyBlob) wall.collide(blob) wall.collide(enemyBlob) wall.collide(wall) drawWindow(window, [blob, enemyBlob], [ball], wall)
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 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()
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))
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)
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
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)
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)
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 __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)
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)
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
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
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
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
def coco_collate(data, num_gpus=3, is_train=False): blob = Blob(mode='det', is_train=is_train, num_gpus=num_gpus, batch_size_per_gpu=len(data) // num_gpus) for d in data: blob.append(d) blob.reduce() return blob
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')