def extract_image(self, input_path, output_path, supplements_path, image_nb, save=True):
        current = Image.open(os.path.join(input_path, "in{0:06d}.jpg".format(image_nb)))
        new_data = MosaicImage(current, self.image_parameters)
        self.som.set_data(new_data.get_data())
        winners = self.som.get_all_winners()
        diff_winners = np.zeros(winners.shape)
        for j in range(len(winners)):
            diff_winners[j] = manhattan_distance(np.asarray(winners[j]), np.asarray(self.initial_map[j]))
        diff_winners = diff_winners.reshape(new_data.nb_pictures)
        diff_winners = np.kron(diff_winners, np.ones((self.pictures_dim[0], self.pictures_dim[1])))
        #             diff_winners *= 30  # Use this parameter ?
        diff_winners = ImageOps.autocontrast(Image.fromarray(diff_winners).convert('L'))

        reconstructed = Image.fromarray(new_data.reconstruct(self.som.get_reconstructed_data(winners)))
        som_difference = ImageOps.autocontrast(ImageChops.difference(reconstructed, current).convert('L'))
        som_difference_modulated = ImageChops.multiply(som_difference, diff_winners)

        # Binarizing
        fn = lambda x: 255 if x > self.threshold else 0
        thresholded = som_difference_modulated.convert('L').point(fn, mode='1')

        result = ImageChops.multiply(thresholded, self.mask)

        # Saving
        if save:
            som_difference.save(os.path.join(supplements_path, "difference", "dif{0:06d}.png".format(image_nb)))
            diff_winners.save(os.path.join(supplements_path, "diff_winners", "win{0:06d}.png".format(image_nb)))
            som_difference_modulated.save(os.path.join(supplements_path, "saliency", "sal{0:06d}.png".format(image_nb)))
            thresholded.save(os.path.join(supplements_path, "thresholded", "thr{0:06d}.png".format(image_nb)))

            result.save(os.path.join(output_path, "bin{0:06d}.png".format(image_nb)))
Exemple #2
0
    nb_epochs = 100
    inputs = Parameters({
        "epsilon_winner": 0.1,
        "epsilon_neighbour": 0.006,
        "maximum_age": 10,
        "error_decrease_new_unit": 0.5,
        "error_decrease_global": 0.995,
        "data": bkg.get_data(),
        "neurons_nbr": 200,
        "epochs_nbr": nb_epochs
    })
    gng = GrowingNeuralGas(inputs)

    start = time.time()
    gng.run()
    end = time.time()

    print("Executed in " + str(end - start) + " seconds.")
    print(gng.square_error(gng.get_all_winners()))
    reconstructed = bkg.reconstruct(gng.get_reconstructed_data())
    # plt.imshow(reconstructed)
    print(len(gng.network.nodes()))

    old_winners = gng.get_all_winners()
    gng.set_data(image.get_data())
    dists = gng.get_neural_distances(old_winners)
    with np.printoptions(threshold=np.inf, suppress=True, linewidth=720):
        print(dists)
    # plt.imshow(som_image, cmap='gray')
Exemple #3
0
        img.get_data(),
        "neurons_nbr": (8, 8),
        "epochs_nbr":
        nb_epochs
    })
    som = RecursiveSOM(inputs)
    som.run()

    end = time.time()
    print("Executed in " + str(end - start) + " seconds.")
    print("Mean :", som.mean_error(), "compared to",
          som.level_one.mean_error())
    print("Square : ", som.square_error(), "compared to",
          som.level_one.square_error())

    two = img.reconstruct(som.get_reconstructed_data())
    one = img.reconstruct(som.level_one.get_reconstructed_data())

    ah = MosaicImage(Image.open("Data/images/ah.png"),
                     Parameters({"pictures_dim": [10, 10]}))
    som.set_data(ah.get_data())

    ah_two = img.reconstruct(som.get_reconstructed_data())
    ah_one = img.reconstruct(som.level_one.get_reconstructed_data())

    first_stage = img.reconstruct(som.level_one.get_neural_list(),
                                  size=som.level_one.neurons_nbr)
    second_stage = img.reconstruct(som.level_two.get_neural_list(),
                                   size=som.level_two.neurons_nbr)

    Image.fromarray(one).save("Results/Recursive/one.png")
Exemple #4
0
    current = Image.open(os.path.join(path, name + str(i) + ".png"))
    new_data = MosaicImage(current, img_parameters)
    map.set_data(new_data.get_data())
    winners = map.get_all_winners()
    diff_winners = map.get_neural_distances(initial_map, winners)
    diff_winners -= 0
    diff_winners[diff_winners < 0] = 0
    diff_winners = diff_winners.reshape(new_data.nb_pictures)
    diff_winners = np.kron(diff_winners,
                           np.ones((pictures_dim[0], pictures_dim[1])))
    # diff_winners *= 30  # Use this parameter ?
    diff_winners = ImageOps.autocontrast(
        Image.fromarray(diff_winners).convert('L'))

    reconstructed = Image.fromarray(
        new_data.reconstruct(map.get_reconstructed_data(winners)))
    som_difference = ImageOps.autocontrast(
        ImageChops.difference(reconstructed, current).convert('L'))
    som_difference_modulated = ImageChops.multiply(som_difference,
                                                   diff_winners)

    # Binarizing
    fn = lambda x: 255 if x > 10 else 0
    thresholded = som_difference_modulated.convert('L').point(fn, mode='1')

    result = Image.new("L", current.size)
    result.paste(thresholded, (0, 0))

    som_difference_modulated = ImageOps.autocontrast(som_difference_modulated)
    som_difference.save(
        os.path.join(output_path, "difference", "dif" + str(i) + ".png"))
Exemple #5
0
    Variable(start=0.5, end=0.25, nb_steps=nb_epochs),
    "sigma":
    Variable(start=0.1, end=0.03, nb_steps=nb_epochs),
    "data":
    data.get_data(),
    "neurons_nbr": (10, 10),
    "epochs_nbr":
    nb_epochs
})
som = SOM(inputs_SOM)
for i in range(nb_epochs):
    print('Epoch ', i)
    som.run_epoch()
    original = data.image
    reconstructed = Image.fromarray(
        data.reconstruct(som.get_reconstructed_data()))
    som_image = data.reconstruct(som.get_neural_list(), size=som.neurons_nbr)
    difference_image = ImageChops.difference(original,
                                             reconstructed).convert('L')
    difference = np.asarray(difference_image)
    # difference = np.sum(difference, axis=2)
    # difference = np.divide(difference, 255*3)
    difference = np.divide(difference, 255)
    print(np.mean(np.square(difference)))
    som_image = Image.fromarray(som_image)

    # reconstructed.save("/users/yabernar/Documents/Presentation resources/Examples/last_run/training/reconstructed"+"{0:02d}.png".format(i))
    # som_image.save("/users/yabernar/Documents/Presentation resources/Examples/last_run/training/som_image{0:02d}.png".format(i))
    # difference_image.save("/users/yabernar/Documents/Presentation resources/Examples/last_run/training/difference{0:02d}.png".format(i))

    if plot is None:
Exemple #6
0

if __name__ == '__main__':
    img = MosaicImage(Image.open(os.path.join("Data", "Images", "Lenna.png")),
                      Parameters({"pictures_dim": [20, 20]}))
    data = img.get_data()

    start = time.time()

    nb_epochs = 20
    inputs = Parameters({
        "alpha":
        Variable(start=0.6, end=0.05, nb_steps=nb_epochs),
        "sigma":
        Variable(start=0.5, end=0.001, nb_steps=nb_epochs),
        "data":
        img.get_data(),
        "neurons_nbr": (5, 5),
        "epochs_nbr":
        nb_epochs
    })
    som = SelfOrganizingTopology(inputs)
    som.run()

    end = time.time()
    print("Executed in " + str(end - start) + " seconds.")
    print(som.square_error())
    reconstructed = img.reconstruct(som.get_reconstructed_data())
    plt.imshow(reconstructed)
    plt.show()
Exemple #7
0
    Variable(start=0.5, end=0.25, nb_steps=nb_epochs),
    "sigma":
    Variable(start=0.1, end=0.03, nb_steps=nb_epochs),
    "data":
    data.get_data(),
    "neurons_nbr": (10, 10),
    "epochs_nbr":
    nb_epochs
})
som = SOM(inputs_SOM)
for i in range(nb_epochs):
    print('Epoch ', i)
    som.run_epoch()
    original = data.image
    reconstructed = data.reconstruct(som.get_reconstructed_data())
    som_image = mosaic.reconstruct(som.get_neural_list(), size=som.neurons_nbr)
    difference = ImageChops.difference(
        original, Image.fromarray(reconstructed)).convert('L')
    difference = np.asarray(difference)
    # difference = np.sum(difference, axis=2)
    # difference = np.divide(difference, 255*3)
    difference = np.divide(difference, 255)
    print(np.mean(np.square(difference)))
    if plot is None:
        plot = []
        plt.subplot(2, 2, 1)
        plot.append(plt.imshow(original))
        plt.subplot(2, 2, 2)
        plot.append(plt.imshow(reconstructed, cmap='gray'))
        plt.subplot(2, 2, 3)
        plot.append(plt.imshow(som_image, cmap='gray'))