Exemplo n.º 1
0
 def generate_image(self, label: np.ndarray, seed: int, size: int) -> str:
     latent = utils.get_random_latent(seed)
     output = self.session.run(self._output_image,
                               feed_dict={
                                   self.latent_placeholder: latent,
                                   self.label_placeholder: label
                               })
     img = utils.process_generator_output(output)[0]
     img = img.resize((size, size), resample=Image.NEAREST)
     img = utils.convert_to_base64_str(img)
     return img
Exemplo n.º 2
0
def generate_pca_image(
        generator: Generate,
        components_multiplier: List,
        seed: int
) -> str:
    components_multiplier = np.array(components_multiplier)
    zero_array = np.zeros(shape=[100])
    components_multiplier = np.concatenate([components_multiplier, zero_array[components_multiplier.shape[0]:]],
                                           axis=-1)
    latent = utils.get_random_latent(seed)
    dlatent = generator.mapping_without_labels(latent)
    with open('../../1_control_without_labels/02_pca/w_pca_100_components_1000000_iterations.pkl', 'rb') as f:
        components, mean = pickle.load(f)
        dlatent += np.sum(components * np.expand_dims(components_multiplier, axis=-1), axis=0)

    output = generator.generate_images_from_dlatents(dlatent)[0]
    return utils.convert_to_base64_str(output)
Exemplo n.º 3
0
def interpolations_cubic(generator: Generate,
                         seed: int,
                         label_0: np.ndarray,
                         label_1: np.ndarray,
                         label_2: np.ndarray,
                         label_3: np.ndarray,
                         interpolation_space: str,
                         size: int = 512,
                         num_steps: int = 25) -> Tuple[List, int]:
    interpolation_dlatents = []
    latent = utils.get_random_latent(seed)
    if interpolation_space == 'w':
        dlatent_0 = generator.map_latent_and_label(latent, label_0)
        dlatent_1 = generator.map_latent_and_label(latent, label_1)
        dlatent_2 = generator.map_latent_and_label(latent, label_2)
        dlatent_3 = generator.map_latent_and_label(latent, label_3)
    for i in range(num_steps):
        magnitude = i / (num_steps - 1)
        if interpolation_space == 'w':
            dlatent_interpolate = CubicInterpolate(dlatent_0, dlatent_1,
                                                   dlatent_2, dlatent_3,
                                                   magnitude)
        if interpolation_space == 'z':
            label_interpolate = CubicInterpolate(label_0, label_1, label_2,
                                                 label_3, magnitude)
            dlatent_interpolate = generator.map_latent_and_label(
                latent, label_interpolate)
        interpolation_dlatents.append(dlatent_interpolate)
    interpolation_dlatents = np.concatenate(interpolation_dlatents, axis=0)
    interpolation_cache = generator.generate_images_from_dlatents(
        interpolation_dlatents, cache_size=num_steps)
    json_list = []
    for i in range(num_steps):
        current_image = interpolation_cache[i].resize((size, size),
                                                      resample=Image.NEAREST)
        json_list.append(utils.convert_to_base64_str(current_image))
    return json_list, num_steps
Exemplo n.º 4
0
def interpolations(generator: Generate,
                   label_left: np.ndarray,
                   seed_left: int,
                   label_right: np.ndarray,
                   seed_right: int,
                   interpolation_space: str,
                   latent_interpolation_space: str = 'z',
                   label_interpolation_space: str = 'z',
                   size: int = 512,
                   cache_size: int = 25,
                   sequence_steps: int = 10) -> Tuple[List, int, str]:
    interpolation_dlatents = []
    json_list = []
    sequence_image = None
    canvas = Image.new('RGB',
                       (int(generator.image_resolution) * sequence_steps,
                        int(generator.image_resolution)), 'white')
    latent_left = utils.get_random_latent(seed_left)
    latent_right = utils.get_random_latent(seed_right)

    if interpolation_space == 'w':
        if generator.separate_mapping:
            if label_interpolation_space == 'w':
                dlabel_left = generator.separate_label_mapping(label_left)
                dlabel_right = generator.separate_label_mapping(label_right)
            if latent_interpolation_space == 'w':
                dlatent_left = generator.separate_latent_mapping(latent_left)
                dlatent_right = generator.separate_latent_mapping(latent_right)
        else:
            dlatent_left = generator.map_latent_and_label(
                latent_left, label_left)
            dlatent_right = generator.map_latent_and_label(
                latent_right, label_right)

    if cache_size > 1:
        for i in range(cache_size):
            magnitude = i / (cache_size - 1)
            if generator.separate_mapping:
                if label_interpolation_space == 'z':
                    label_interpolate = label_left * (
                        1 - magnitude) + label_right * magnitude
                    dlabel_interpolate = generator.separate_label_mapping(
                        label_interpolate)
                elif label_interpolation_space == 'w':
                    dlabel_interpolate = dlabel_left * (
                        1 - magnitude) + dlabel_right * magnitude

                if latent_interpolation_space == 'z':
                    latent_interpolate = latent_left * (
                        1 - magnitude) + latent_right * magnitude
                    dlatent_interpolate = generator.separate_latent_mapping(
                        latent_interpolate)

                elif latent_interpolation_space == 'w':
                    dlatent_interpolate = dlatent_left * (
                        1 - magnitude) + dlatent_right * magnitude
                interpolation_dlatents.append(
                    generator.combine_dlatent_and_dlabel(
                        dlatent_interpolate, dlabel_interpolate))
            else:
                if interpolation_space == 'z':
                    latent_interpolate = latent_left * (
                        1 - magnitude) + latent_right * magnitude
                    label_interpolate = label_left * (
                        1 - magnitude) + label_right * magnitude
                    dlatent_interpolate = generator.map_latent_and_label(
                        latent_interpolate, label_interpolate)
                    interpolation_dlatents.append(dlatent_interpolate)
                elif interpolation_space == 'w':
                    interpolation_dlatents.append(dlatent_left *
                                                  (1 - magnitude) +
                                                  dlatent_right * magnitude)

        interpolation_dlatents = np.concatenate(interpolation_dlatents, axis=0)
        interpolation_cache = generator.generate_images_from_dlatents(
            interpolation_dlatents, cache_size=cache_size)

        for i in range(cache_size):
            current_image = interpolation_cache[i].resize(
                (size, size), resample=Image.NEAREST)
            json_list.append(utils.convert_to_base64_str(current_image))

        if sequence_steps > 1:
            for i in range(sequence_steps):
                index = int(
                    np.round((i / (sequence_steps - 1)) * (cache_size - 1)))
                canvas.paste(interpolation_cache[index],
                             (i * int(generator.image_resolution), 0))
        sequence_image = utils.convert_to_base64_str(canvas)
    return json_list, cache_size, sequence_image
Exemplo n.º 5
0
def interpolation_graph(generator: Generate, label_left: np.ndarray,
                        label_right: np.ndarray, seed_left: int,
                        seed_right: int, num_steps: int,
                        delta_magnitude: float, distance_measure: str,
                        magnitude_sampling: str,
                        normalize: bool) -> Tuple[str, float]:
    assert magnitude_sampling in ['linear', 'normal']
    assert distance_measure in ['gradient', 'vgg16']
    plt.figure()
    latent_left = utils.get_random_latent(seed_left)
    latent_right = utils.get_random_latent(seed_right)
    dlatent_left = generator.map_latent_and_label(latent_left, label_left)
    dlatent_right = generator.map_latent_and_label(latent_right, label_right)
    x_axis = []
    y_axis = []

    if magnitude_sampling == 'linear':
        magnitudes = np.linspace(0, 1, num_steps)
    if magnitude_sampling == 'normal':
        magnitudes = np.sort(
            np.clip(np.random.normal(0.5, 0.2, size=[num_steps]), 0, 1))
        magnitudes[0] = 0
        magnitudes[-1] = 1
    for j in range(num_steps):
        current_magnitude = magnitudes[j]
        if distance_measure == 'gradient':
            grads = generator.gradient_delta_interpolate(
                dlatent_left=dlatent_left,
                dlatent_right=dlatent_right,
                magnitude=current_magnitude,
                delta_magnitude=delta_magnitude)
            sum_square_grads = np.sum(np.square(grads), axis=2)
            distance = np.sqrt(np.mean(sum_square_grads, axis=1))[0]
        if distance_measure == 'vgg16':
            distance = generator.vgg16_distance(
                dlatent_left=dlatent_left,
                dlatent_right=dlatent_right,
                magnitude=current_magnitude,
                delta_magnitude=delta_magnitude)
        x_axis.append(current_magnitude)
        y_axis.append(distance)

    y_axis = np.array(y_axis)
    linear_diff = []
    linear_func = []
    if normalize:
        y_axis = (y_axis - min([y_axis[0], y_axis[-1]])) / np.abs(y_axis[0] -
                                                                  y_axis[-1])
    distance_start_end = np.abs(y_axis[0] - y_axis[-1])
    for j in range(num_steps):
        x = magnitudes[j]
        f = y_axis[0] + x * (y_axis[-1] - y_axis[0]) / x_axis[-1]
        linear_func.append(f)
        linear_diff.append(np.square((y_axis[j] - f) * distance_start_end))

    linear_diff = np.mean(linear_diff)

    plt.plot(x_axis, y_axis)
    plt.plot(x_axis, linear_func)
    plt.xlabel('interpolation magnitude')
    if distance_measure == 'gradient':
        plt.ylabel('gradient magnitude in W')
    if distance_measure == 'vgg16':
        plt.ylabel('perceptual distance')
    plt.xticks(np.arange(0.0, 1.1, 0.1))
    buf = io.BytesIO()
    plt.savefig(buf, format='png')
    buf.seek(0)
    image_buf = Image.open(buf)
    graph_image = utils.convert_to_base64_str(image_buf)
    buf.close()
    plt.clf()
    return graph_image, float(linear_diff)