예제 #1
0
def generate_patch(height, width, color_dict, direction):

    t = r.choice([0, 1], p=[0.9, 0.1])
    # t = r.choice([0,1],p=[1,0])

    if t == 0:
        ### markov stuff
        pattern = m.RMM([0, 1, 2, 3, 4],
                        self_length=100,
                        sinks=0,
                        reduce_sinks=5)
        pattern = m.SProg(values=pattern)
        if direction == 1:
            sample = m.sample_markov_hierarchy(pattern, width)
            sample = np.repeat(sample,
                               repeats=r.choice([1, 2, 3, 4], size=width))
            sample = sample[:width]
            patch = np.tile(sample, reps=height)
        elif direction == -1:
            sample = m.sample_markov_hierarchy(pattern, height)
            sample = np.repeat(sample,
                               repeats=r.choice([1, 2, 3, 4, 5], size=height))
            sample = sample[:height]
            patch = np.repeat(sample, repeats=width)
        patch = patch[:width * height]
        patch = patch.reshape(height, width)

    elif t == 1:
        if direction == 1:
            patch = r.choice([0, 1, 2], size=width * height)
            patch = np.repeat(patch,
                              repeats=r.choice([20, 30, 40, 50],
                                               size=width * height))
            patch = patch[:height * width]
            patch = patch.reshape(height, width)
            patch = np.repeat(patch,
                              repeats=r.choice([2, 3, 10], size=height),
                              axis=0)
            patch = patch[:height, :width]
        elif direction == -1:
            patch = r.choice([0, 1, 2], size=width * height)
            patch = patch.reshape(height, width)
            patch = np.repeat(patch,
                              repeats=r.choice([2, 3, 4], size=height),
                              axis=0)
            patch = patch[:height, :width]
            patch = np.repeat(patch,
                              repeats=r.choice([20, 30, 40, 50], size=width),
                              axis=1)
            patch = patch[:height, :width]

    patch = color.replace_indices_with_colors(patch, color_dict)

    patch[patch < 0] = 0
    patch[patch > 255] = 255
    return patch
                values=vls,
                lenghts=lngts,
                self_length=NUM_CELLS,
                start_probs=[0,1,2,3]),
            num_tiles=num_tiles),self_length=[5,10,15,20,25])
        pattern_models += [model]

    def update_fun(preference_matrix,start_probs):
        return preference_matrix,np.roll(start_probs,shift=1)
    parent = m.RandomMarkovModel(
        values=pattern_models,
        start_probs=0,
        update_fun=update_fun,update_step=1)

    img = gen_portion(
        parent,
        height=HEIGHT,width=WIDTH,
        tile_height=None,tile_width=None)

    final_img_prototype = data.upscale_nearest(img,UPSCALE_FACTOR)
    final_img = color.replace_indices_with_colors(final_img_prototype,COLOR_DICT).astype('uint8')
    if N==1:
        viz.start_color_editing_tool(
            blueprint=final_img_prototype,
            color_dict=COLOR_DICT,
            downsample=2)
    else:
        file.export_image(
            '%d_%d' % (current_iteration,int(round(time.time() * 1000))),
            final_img,format='png')
def generate_patch(height, width, color_dict_1, color_dict_2):

    patch = np.zeros((height, width, 3), dtype='float64')

    color_start_lengths = np.array(
        [int(l) for _, (_, l) in color_dict_1.items()])

    num_color_samples = width // np.min(color_start_lengths) + 20

    pattern = m.FuzzyProgression(values=np.arange(len(color_dict_1)),
                                 positive_shifts=3,
                                 negative_shifts=3,
                                 self_length=num_color_samples)

    raw_sample = m.sample_markov_hierarchy(pattern, num_color_samples)
    sample_start = color.replace_indices_with_colors(raw_sample, color_dict_1)
    sample_end = color.replace_indices_with_colors(raw_sample, color_dict_2)

    switch = np.array([
        r.choice([0, 1], replace=False, size=(2, ))
        for i in range(sample_start.shape[0])
    ])

    sample_start_t = np.where(switch[:, 0][:, None], sample_start, sample_end)
    sample_end_t = np.where(switch[:, 1][:, None], sample_start, sample_end)

    sample_start = sample_start_t
    sample_end = sample_end_t

    start_lengths = color_start_lengths[raw_sample.astype('int32')]
    start_lengths = np.cumsum(start_lengths)

    num_vertical_reps = 2
    num_vertical_samples = height // num_vertical_reps + 3
    model = m.RMM(values=np.arange(0, 41, 5) - 20,
                  self_length=num_vertical_samples)
    offsets = np.stack([
        m.sample_markov_hierarchy(model, num_vertical_samples)
        for _ in range(num_color_samples)
    ],
                       axis=1)

    offsets = np.repeat(offsets,
                        repeats=r.choice(
                            [num_vertical_reps + i for i in range(1)],
                            size=(num_vertical_samples, )),
                        axis=0)

    offsets = np.cumsum(offsets, axis=0)
    offsets += start_lengths
    offsets = np.hstack([np.zeros((offsets.shape[0], 1)), offsets])

    i = 0
    offset_index = 0
    while i < height:

        current_lengths = offsets[offset_index]
        acum_max = np.maximum.accumulate(current_lengths)
        mask = acum_max == current_lengths

        diff = np.diff(current_lengths[mask])

        samples_start_masked = sample_start[mask[1:]]
        samples_end_masked = sample_end[mask[1:]]

        p_switch = 0.75

        switch = r.choice([0, 1],
                          size=samples_start_masked.shape[0],
                          p=[p_switch, 1 - p_switch])
        switch = np.stack((switch, 1 - switch), axis=1)

        sample_start_switched = np.where(switch[:, 0][:, None],
                                         samples_start_masked,
                                         samples_end_masked)
        sample_end_switched = np.where(switch[:, 1][:, None],
                                       samples_start_masked,
                                       samples_end_masked)

        multiples = r.choice([20, 25, 35, 50, 60, 70])

        gradient = generate_gradient(sample_start_switched,
                                     sample_end_switched, diff)[:width]
        patch[i:i + multiples] = gradient[None, :]
        i += multiples
        offset_index += 1

    patch[patch < 0] = 0
    patch[patch > 255] = 255
    return patch
예제 #4
0
        values=m.MarkovModel(values=[0, 1],
                             preference_matrix=[[0, 1], [1, 25]]),
        child_lengths=[WIDTH * i for i in range(4, 6)])

    white_blocks = m.RandomMarkovModel(values=[whites, row_patterns_white],
                                       child_lengths=[5, 6, 7])

    blacks = m.SimpleProgression(
        values=m.MarkovModel(values=[0, 1],
                             preference_matrix=[[25, 1], [1, 0]]),
        child_lengths=[WIDTH * i for i in range(4, 6)])

    black_blocks = m.RandomMarkovModel(values=[blacks, row_patterns_black],
                                       child_lengths=[5, 6, 7])

    parent = m.RandomMarkovModel(values=[white_blocks, black_blocks],
                                 child_lengths=1)

    s = current_iteration * 10
    img = gen_channel(parent, s + 1)[:, :, 0]
    print(img.shape)
    colored_image = color.replace_indices_with_colors(img, COLOR_DICT)
    print(colored_image.shape)

    if N == 1:
        viz.show_image(colored_image)
    else:
        export_image('markov_h_%d' % (current_iteration),
                     colored_image.astype('uint8'),
                     format='png')
def generate_patch(height, width, color_dict, rkey):

    patch = np.zeros((height, width, 3), dtype='float64')

    color_start_lengths = np.array(
        [int(i) for i in color.get_meta_from_palette(color_dict)])

    num_color_samples = width // np.min(color_start_lengths) + 20

    color_codes = color.get_keys_from_palette(color_dict)
    pattern = m.FuzzyProgression(values=color_codes,
                                 positive_shifts=3,
                                 negative_shifts=3,
                                 self_length=num_color_samples,
                                 parent_rkey=r.bind_generator_from(rkey))

    sample_raw_start = m.sample_markov_hierarchy(
        pattern, num_color_samples).astype('int32')
    sample_raw_down_start = m.sample_markov_hierarchy(
        pattern, num_color_samples).astype('int32')
    # print(sample_raw_start)
    sample_raw_end = m.sample_markov_hierarchy(
        pattern, num_color_samples).astype('int32')
    sample_raw_down_end = m.sample_markov_hierarchy(
        pattern, num_color_samples).astype('int32')
    sample_raw_backup = m.sample_markov_hierarchy(
        pattern, num_color_samples).astype('int32')
    # making the probability of same color used smaller
    replace_mask = sample_raw_start == sample_raw_end
    sample_raw_end[replace_mask] = sample_raw_backup[replace_mask]

    sample_start = color.replace_indices_with_colors(sample_raw_start,
                                                     color_dict)
    sample_end = color.replace_indices_with_colors(sample_raw_end, color_dict)

    sample_down_start = color.replace_indices_with_colors(
        sample_raw_down_start, color_dict)
    sample_down_end = color.replace_indices_with_colors(
        sample_raw_down_end, color_dict)

    switch_key = r.bind_generator_from(rkey)
    switch = np.array([
        r.choice_from(switch_key, [0, 1], replace=False, size=(2, ))
        for i in range(sample_start.shape[0])
    ])

    sample_start_t = np.where(switch[:, 0][:, None], sample_start, sample_end)
    sample_end_t = np.where(switch[:, 1][:, None], sample_start, sample_end)

    sample_start = sample_start_t
    sample_end = sample_end_t

    start_lengths = color.get_meta_for_each_sample(sample_raw_start,
                                                   color_dict)

    start_lengths = np.array([int(i) for i in start_lengths])
    start_lengths = np.cumsum(start_lengths)

    num_vertical_reps = 2
    num_vertical_samples = height // num_vertical_reps + 3
    model = m.MarkovModel(
        values=np.arange(0, 41, 10) - 20,
        preference_matrix=data.str2mat(
            '0 1 5 1 0, 1 2 5 1 0, 0 1 10 1 0, 0 1 5 2 1, 0 1 5 1 0'),
        self_length=num_vertical_samples,
        parent_rkey=r.bind_generator_from(rkey))

    offsets = np.stack([
        m.sample_markov_hierarchy(model, num_vertical_samples)
        for _ in range(num_color_samples)
    ],
                       axis=1)

    offsets = np.repeat(offsets,
                        repeats=r.choice_from(
                            rkey, [num_vertical_reps + i for i in range(1)],
                            size=(num_vertical_samples, )),
                        axis=0)

    offsets = np.cumsum(offsets, axis=0)
    offsets += start_lengths
    offsets = np.hstack([np.zeros((offsets.shape[0], 1)), offsets])

    i = 0
    offset_index = 0

    transition = np.linspace(0, 1, num_vertical_samples)
    sample_start_gradient = sample_start[:, :, None] * (
        1 - transition) + sample_down_start[:, :, None] * transition
    sample_end_gradient = sample_end[:, :, None] * (
        1 - transition) + sample_down_end[:, :, None] * transition

    multiples_choices = r.choice_from(rkey,
                                      config.get('multiples-choices',
                                                 [20, 30, 40, 50]),
                                      size=(6, ))
    # print('multiples-choices',multiples_choices)

    while i < height:
        loop_key = r.bind_generator_from(rkey)
        current_lengths = offsets[offset_index]
        acum_max = np.maximum.accumulate(current_lengths)
        mask = acum_max == current_lengths

        diff = np.diff(current_lengths[mask])

        samples_start_masked = sample_start[mask[1:]]
        samples_end_masked = sample_end[mask[1:]]
        #
        # samples_start_masked = sample_start_gradient[:,:,i//num_vertical_reps][mask[1:]]
        # samples_end_masked = sample_end_gradient[:,:,i//num_vertical_reps][mask[1:]]

        p_switch = config.get('gradient-switch-p', 0.5)

        switch = r.choice_from(loop_key, [0, 1],
                               size=samples_start_masked.shape[0],
                               p=[p_switch, 1 - p_switch])
        switch = np.stack((switch, 1 - switch), axis=1)

        sample_start_switched = np.where(switch[:, 0][:, None],
                                         samples_start_masked,
                                         samples_end_masked)
        sample_end_switched = np.where(switch[:, 1][:, None],
                                       samples_start_masked,
                                       samples_end_masked)

        multiples = r.choice_from(loop_key, multiples_choices)

        gradient = generate_gradient(sample_start_switched,
                                     sample_end_switched, diff)[:width]
        patch[i:i + multiples] = gradient[None, :]
        i += multiples
        offset_index += 1

    patch[patch < 0] = 0
    patch[patch > 255] = 255
    return patch