예제 #1
0
    def forward(self, batch_size):
        self.hidden = torch.zeros(self.n_lstm_layers, batch_size, self.hidden_dim).to(device)
        self.cell_state = torch.zeros(self.n_lstm_layers, batch_size, self.hidden_dim).to(device)

        y_list = []
        start_elements = self.vocab.get_random_elements(batch_size)
        start_elements_tensors_list = []
        for _j, element in enumerate(start_elements):
            start_elements_tensors_list.append(self.vocab.get_embedding(element))

        x = torch.stack(start_elements_tensors_list, dim=0).to(device)
        y_list.append(x)
        length_data = torch.LongTensor([1] * batch_size)
        sequence_filled = torch.BoolTensor([False] * batch_size)

        noise_singal = noise(batch_size).to(device)
        x = torch.cat((x, noise_singal), dim=1).reshape(batch_size, 1, -1)
        length_sequence_processed = 1

        while length_sequence_processed < max_sequence_length:
            lstm_out, (self.hidden, self.cell_state) = self.lstm(x, (self.hidden, self.cell_state))
            lstm_out = lstm_out.contiguous().view(-1, self.hidden_dim)
            fc_out = self.fc(lstm_out)
            a1 = torch.pow(fc_out, 2)
            a2 = torch.sum(a1, dim=1)
            a3 = torch.sqrt(a2).unsqueeze(-1)
            y = fc_out / a3
            y_list.append(y)
            length_sequence_processed += 1

            is_end_of_seq = self.vocab.is_end_of_sequence(y)
            sequence_filled = sequence_filled + is_end_of_seq
            length_increment = (sequence_filled == False).type(torch.long)
            length_data = length_data + length_increment

            noise_singal = noise(batch_size).to(device)
            x = torch.cat((y, noise_singal), dim=1).reshape(batch_size, 1, -1)

        y_final = torch.stack(y_list, dim=1).to(device)
        l_final = length_data

        return y_final, l_final
예제 #2
0
    def setup(self):
        """
        Load images.
        """
        ## Generate images
        self.PARAMETERS = {}

        self.PARAMETERS.update(common.blank_dimensions())

        base_color, base_depth = common.blank_dimensions(self.DEFAULT_DIMS)

        # Size of rectangle and radius of circle
        for size in [20, 40, 80, 100, 120]:
            color_image, depth_image = np.copy(base_color), np.copy(base_depth)

            cv2.rectangle(color_image, (213, 240), (426+size, 480+size), (255, 255, 255), thickness=4)
            cv2.rectangle(depth_image, (213, 240), (426+size, 480+size), (255), thickness=4)

            for location in [(640+213, 240), (640+213, 480), (640+426, 240), (640+426, 480)]:
                cv2.circle(color_image, location, size, (255, 255, 255), thickness=4)
                cv2.circle(depth_image, location, size, (255), thickness=4)

            self.PARAMETERS.update({f'size={size}': (color_image, depth_image)})

        # Adding more circles
        for n_obj in range(4, 9):
            color_image, depth_image = np.copy(base_color), np.copy(base_depth)

            cv2.rectangle(color_image, (213, 240), (426, 480), (255, 255, 255), thickness=4)
            cv2.rectangle(depth_image, (213, 240), (426, 480), (255), thickness=4)

            for location in [
                    (800, 180), (800, 360), (800, 540), (960, 180), (960, 360),
                    (960, 540), (1120, 180), (1120, 360), (1120, 540)][:n_obj]:
                cv2.circle(color_image, location, self.DEFAULT_RADIUS, (255, 255, 255), thickness=4)
                cv2.circle(depth_image, location, self.DEFAULT_RADIUS, (255), thickness=4)

            self.PARAMETERS.update({f'n_circle={n_obj}': (color_image, depth_image)})

        # On default noise specturm
        for title, (color_image, depth_image) in common.noise().items():
            cv2.rectangle(color_image, (213, 240), (426, 480), (255, 255, 255), thickness=4)
            cv2.rectangle(depth_image, (213, 240), (426, 480), (255), thickness=4)

            for location in [(640+213, 240), (640+213, 480), (640+426, 240), (640+426, 480)]:
                cv2.circle(color_image, location, self.DEFAULT_RADIUS, (255, 255, 255), thickness=4)
                cv2.circle(depth_image, location, self.DEFAULT_RADIUS, (255), thickness=4)

            self.PARAMETERS.update({f'{title} single': (color_image, depth_image)})
예제 #3
0
    def setup(self):
        """
        Load images.
        """
        ## Generate images
        self.PARAMETERS = {}

        self.PARAMETERS.update(common.blank_dimensions())

        base_color, base_depth = common.blank_dimensions(self.DEFAULT_DIMS)

        # Small text to 1/4 image
        for size in [1, 3, 6, 10]:
            color_image, depth_image = np.copy(base_color), np.copy(base_depth)

            cv2.putText(color_image, self.DEFAULT_WORD, (640, 360),
                        cv2.FONT_HERSHEY_SIMPLEX, size, (255, 255, 255), 3)
            cv2.putText(depth_image, self.DEFAULT_WORD, (640, 360),
                        cv2.FONT_HERSHEY_SIMPLEX, size, (255), 3)

            self.PARAMETERS.update(
                {f'size={size}': (color_image, depth_image)})

        # One to each corner
        for n_text in range(4):
            color_image, depth_image = np.copy(base_color), np.copy(base_depth)

            for location in [(320, 180), (320, 540), (960, 180),
                             (960, 540)][:n_text]:
                cv2.putText(color_image, self.DEFAULT_WORD, location,
                            cv2.FONT_HERSHEY_SIMPLEX, self.DEFAULT_SIZE,
                            (255, 255, 255), 3)
                cv2.putText(depth_image, self.DEFAULT_WORD, location,
                            cv2.FONT_HERSHEY_SIMPLEX, self.DEFAULT_SIZE, (255),
                            3)

            self.PARAMETERS.update(
                {f'n_text={n_text}': (color_image, depth_image)})

        # On default noise specturm
        for title, (color_image, depth_image) in common.noise().items():
            cv2.putText(color_image, self.DEFAULT_WORD, (640, 360),
                        cv2.FONT_HERSHEY_SIMPLEX, self.DEFAULT_SIZE,
                        (255, 255, 255), 3)
            cv2.putText(depth_image, self.DEFAULT_WORD, (640, 360),
                        cv2.FONT_HERSHEY_SIMPLEX, self.DEFAULT_SIZE, (255), 3)

            self.PARAMETERS.update(
                {f'{title} single': (color_image, depth_image)})
예제 #4
0
    def setup(self):
        """
        Configure blob detector and initialize images.
        """
        ## Generate images
        self.PARAMETERS = {}

        self.PARAMETERS.update(common.blank_dimensions())

        base_color, base_depth = common.blank_dimensions(self.DEFAULT_DIMS)

        #
        for radius in [25, 50, 100, 250]:
            color_image, depth_image = np.copy(base_color), np.copy(base_depth)

            cv2.circle(color_image, (640, 360), radius, (255, 255, 255), thickness=-1)
            cv2.circle(depth_image, (640, 360), radius, (255), thickness=-1)

            self.PARAMETERS.update({f'radius={radius}': (color_image, depth_image)})

        # One to each corner
        for n_obj in range(4):
            color_image, depth_image = np.copy(base_color), np.copy(base_depth)

            for location in [(320, 180), (320, 540), (960, 180), (960, 540)][:n_obj]:
                cv2.circle(color_image, location, self.DEFAULT_RADIUS, (255, 255, 255), thickness=-1)
                cv2.circle(depth_image, location, self.DEFAULT_RADIUS, (255), thickness=-1)

            self.PARAMETERS.update({f'n_obj={n_obj}': (color_image, depth_image)})

        # On default noise specturm
        for title, (color_image, depth_image) in common.noise().items():
            cv2.circle(color_image, (640, 360), self.DEFAULT_RADIUS, (255, 255, 255), thickness=-1)
            cv2.circle(depth_image, (640, 360), self.DEFAULT_RADIUS, (255), thickness=-1)

            self.PARAMETERS.update({f'{title} single': (color_image, depth_image)})

        ## Read current params & setup obstacle detector
        prefix = '' if os.path.isdir("times") else '..'

        config_filename = os.path.join(prefix, '..', 'obstacle', 'config.json')

        with open(config_filename, 'r') as config_file:
            config = json.load(config_file)

        self.blob_finder = ObstacleFinder(params=import_params(config))
예제 #5
0
    def setup(self):
        """
        Load images.
        """
        ## Generate images
        self.PARAMETERS = {}

        self.PARAMETERS.update(common.blank_dimensions())

        base_color, base_depth = common.blank_dimensions(self.DEFAULT_DIMS)

        #
        for thickness in [3, 5, 10, 20]:
            color_image, depth_image = np.copy(base_color), np.copy(base_depth)

            cv2.line(color_image, (360, 0), (360, 1280), (255, 0, 0),
                     thickness)
            cv2.line(depth_image, (360, 0), (360, 1280), (255), thickness)

            self.PARAMETERS.update(
                {f'thickness={thickness}': (color_image, depth_image)})

        # One to each corner
        for n_obj in range(4):
            color_image, depth_image = np.copy(base_color), np.copy(base_depth)

            for location in [144, 288, 432, 576][:n_obj]:
                cv2.line(color_image, (location, 0), (location, 1280),
                         (255, 0, 0), self.DEFAULT_THICKNESS)
                cv2.line(depth_image, (location, 0), (location, 1280), (255),
                         self.DEFAULT_THICKNESS)

            self.PARAMETERS.update(
                {f'n_obj={n_obj}': (color_image, depth_image)})

        # On default noise specturm
        for title, (color_image, depth_image) in common.noise().items():
            cv2.line(color_image, (360, 0), (360, 1280), (255, 0, 0),
                     self.DEFAULT_THICKNESS)
            cv2.line(depth_image, (360, 0), (360, 1280), (255),
                     self.DEFAULT_THICKNESS)

            self.PARAMETERS.update(
                {f'{title} single': (color_image, depth_image)})
예제 #6
0
def sky(b: bool):
    if not b:
        return screen.fill((0, 0, 0))
    global clouds
    global darkness
    day_length_in_minutes = 24
    time_rate = size[1] / (day_length_in_minutes * 30 * fps)
    adjusted_tick = tick * time_rate
    sun_x = size[0] - adjusted_tick % size[0] * 2
    moon_x = (9 / 8 * -adjusted_tick) % size[0] * 2
    solar_eclipse = abs(sun_x - moon_x) < block_size
    is_day = -block_size < sun_x < size[0]
    is_moon = moon_x < size[0]
    is_dawn = size[0] - block_size < sun_x < size[0]
    is_dusk = -block_size < sun_x < 0
    # light level calculation
    background_light = 16
    light_sources = [background_light]
    if solar_eclipse:
        light_sources.append(255 * abs(sun_x - moon_x) / block_size)
    elif is_dawn:
        light_sources.append(255 * (size[0] - sun_x) / block_size)
    elif is_dusk:
        light_sources.append(255 * (block_size + sun_x) / block_size)
    elif is_day:
        light_sources.append(255)
    if is_moon and not solar_eclipse:
        light_sources.append(128)
    # figure out lighting from sources
    if 0 <= player['pos'][1]:
        torchlight = 255 * is_lit(player['pos'], world) / torch_range
    else:
        torchlight = 255
    light_sources.append(torchlight)
    if torchlight and is_exposed_to_sun(player['pos'], world):
        light_level = int(max(light_sources))
    elif torchlight:
        light_level = int(torchlight)
    else:
        light_level = background_light
    # main
    m = {
        True: (96, 192, 224) if is_day else (0, 0, 0),
        False: (0, 0, 0),
    }
    screen.fill(m[b])
    # sun/moon
    sun_coords = sun_x, size[1] // 4, block_size, block_size
    moon_coords = moon_x, size[1] // 4, block_size, block_size
    pygame.draw.rect(screen, (255, 255, 0), sun_coords)
    pygame.draw.rect(screen, (192, 192, 192),
                     moon_coords)  # todo vary brightness via sin with sun
    # clouds
    if not clouds:
        cloud_scale = 8
        cloud_size = size[0] // cloud_scale * 2, size[1] // cloud_scale * 2
        cloud_map = {
            True: (255, 255, 255, 192),
            False: (0, 0, 0, 0),
        }
        clouds = pygame.Surface((size[0] * 2, size[1] * 2), pygame.SRCALPHA)
        noisemap = noise(cloud_size)
        for x in range(cloud_size[0]):
            for y in range(cloud_size[1]):
                color = cloud_map[.6 < noisemap[y][x]]
                if cloud_scale == 1:
                    clouds.set_at((x, y), color)
                else:
                    cloud_rect = x * cloud_scale, y * cloud_scale, cloud_scale, cloud_scale
                    pygame.draw.rect(clouds, color, cloud_rect)
    screen.blit(clouds, (0 - player['pos'][0], 0 - player['pos'][1]))
    # darkness
    darkness = pygame.Surface(size, pygame.SRCALPHA)
    darkness.fill((0, 0, 0, 255 - light_level))
예제 #7
0
def test_robust_sampling(conf, training_result):
    """
    Sampling rate vs Reconstruction Quality at fixed noise std.

    Parameters
    ----------
    conf : conf_loader.Conf
        Experiment parameters
    training_result : touple
        Retrun values of function `training`

    Returns
    -------
    srange : np.array
        sampling rate range used
    sampling_rate : float
        sampling rate used
    (bk_ser, fa_ser, rc_ser) : (np.array, np.array, np.array)
        SER for `k-best`, `f_avg` and `LSC` methods
    (bk_mse, fa_mse, rc_mse) : (np.array, np.array, np.array)
        mse for `k-best`, `f_avg` and `LSC` methods
    """
    (sort, tros), (energy, comulated, bands), codebooks = training_result
    n_bands = conf.nbands
    # matrxi to vector (m2v) and vector to matrix (v2m) functions
    m2v, v2m = conf.vect_functions()
    subcfg = conf.testing['robust_sampling']

    # Load testing set
    testing, Testing = common.load_dataset(conf.testingset_path(),
                                           conf.fformat, conf.size())
    FlatTst = m2v(Testing)[:, sort]

    # f_avg sampling pattern
    Omega = energy.argsort()[::-1]

    # lsc sampling pattern
    Omegas = [c.sampling_pattern() for c in codebooks]

    shape = testing[0].shape
    n = np.prod(shape)
    N = len(testing)

    srange = np.logspace(*subcfg['sampling_range'])
    sigma = subcfg['noise_rate']

    bk_ser = np.zeros(len(srange))
    fa_ser = np.zeros(len(srange))
    rc_ser = np.zeros(len(srange))

    bk_mse = np.zeros(len(srange))
    fa_mse = np.zeros(len(srange))
    rc_mse = np.zeros(len(srange))

    print('Sampling rate at fixed noise test:')
    for i, rate in enumerate(srange):
        print(f'\r {i+1:3d}/{len(srange)}', flush=True, end='')
        M = int(round(n * rate))
        m = int(round(M / n_bands))
        ms = lsc.num_samples(bands, m)
        M = np.sum(ms)
        smalls = [omega[:y] for omega, y in zip(Omegas, ms)]

        for idx in range(N):
            reference = common.norm(testing[idx])
            X = FlatTst[idx] + common.noise(sigma, n)

            Xsbs = lsc.split(X, bands)
            Ysbs = lsc.sub_sample(Xsbs, Omegas, m)
            recovered = [
                codebooks[b].reconstruct(Ysbs[b], smalls[b])
                for b in range(len(bands))
            ]
            Y = v2m((lsc.union(recovered))[tros], shape)
            y = common.norm(common.pos(common.ifft2(Y).real))

            BK = X.copy()[tros]
            O = np.abs(BK).argsort()[::-1]
            BK[O[M:]] = 0
            BK = v2m(BK, shape)
            bK = common.norm(common.pos(common.ifft2(BK).real))

            FA = X.copy()[tros]
            FA[Omega[M:]] = 0
            FA = v2m(FA, shape)
            fA = common.norm(common.pos(common.ifft2(FA).real))

            fa_ser[i] += common.SER(reference, fA) / N
            bk_ser[i] += common.SER(reference, bK) / N
            rc_ser[i] += common.SER(reference, y) / N

            fa_mse[i] += mse(reference, fA) / N
            bk_mse[i] += mse(reference, bK) / N
            rc_mse[i] += mse(reference, y) / N
    print(' [done]')
    return srange, sigma, (bk_ser, fa_ser, rc_ser), (bk_mse, fa_mse, rc_mse)
예제 #8
0
def test_robust_visual(conf, training_result, idx):
    """
    Noise std vs Reconstruction Quality at fixed sampling rate visual test.

    Parameters
    ----------
    conf : conf_loader.Conf
        Experiment parameters
    training_result : touple
        Retrun values of function `training`
    idx : int
        Id of image to test

    Returns
    -------
    srange : np.array
        noise std range used
    sampling_rate : float
        sampling rate used
    (bk_ser, fa_ser, rc_ser) : (np.array, np.array, np.array)
        SER for `k-best`, `f_avg` and `LSC` methods at different noise std
    (bk_mse, fa_mse, rc_mse) : (np.array, np.array, np.array)
        mse for `k-best`, `f_avg` and `LSC` methods at different noise std
    """
    (sort, tros), (energy, comulated, bands), codebooks = training_result
    n_bands = conf.nbands
    # matrxi to vector (m2v) and vector to matrix (v2m) functions
    m2v, v2m = conf.vect_functions()
    subcfg = conf.testing['robust_reconstruction_visual']

    testing, Testing = common.load_dataset(conf.testingset_path(),
                                           conf.fformat, conf.size())
    FlatTst = m2v(Testing)[:, sort]

    # f_avg sampling pattern
    Omega = energy.argsort()[::-1]

    # lsc sampling pattern
    Omegas = [c.sampling_pattern() for c in codebooks]

    shape = testing[0].shape
    n = np.prod(shape)

    srange = np.logspace(*subcfg['noise_range'])
    sampling_rate = subcfg['sampling_rate']

    W, H = shape
    Wt = W * 3
    Ht = H * len(srange)

    res = np.zeros((Wt, Ht))

    print(f'Robust Reconstruction Quality Visual Test (img {idx}):')
    for i, sigma in enumerate(srange):
        print(f'\r {i+1:3d}/{len(srange)}', flush=True, end='')
        X = FlatTst[idx] + common.noise(sigma, n)

        M = int(round(sampling_rate * n))
        m = int(round(M / n_bands))
        ms = lsc.num_samples(bands, m)
        M = np.sum(ms)

        smalls = [omega[:y] for omega, y in zip(Omegas, ms)]

        Xsbs = lsc.split(X, bands)
        Ysbs = lsc.sub_sample(Xsbs, Omegas, m)

        recovered = [
            codebooks[b].reconstruct(Ysbs[b], smalls[b])
            for b in range(len(bands))
        ]
        Y = v2m((lsc.union(recovered))[tros], shape)
        y = common.norm(common.pos(common.ifft2(Y).real))

        BK = X.copy()[tros]
        O = np.abs(BK).argsort()[::-1]
        BK[O[M:]] = 0
        BK = v2m(BK, shape)
        bK = common.norm(common.pos(common.ifft2(BK).real))

        FA = X.copy()[tros]
        FA[Omega[M:]] = 0
        FA = v2m(FA, shape)
        fA = common.norm(common.pos(common.ifft2(FA).real))

        res[:W, H * i:H * (i + 1)] = bK
        res[W:2 * W, H * i:H * (i + 1)] = fA
        res[2 * W:3 * W, H * i:H * (i + 1)] = y
    print('\t[done]')
    return srange, res