示例#1
0
class RecursiveSOM:
    def __init__(self, parameters):
        self.data = parameters["data"]
        self.level_one = SOM(parameters)
        self.level_two = SOM(parameters)

    def set_data(self, input_data):
        self.data = input_data
        self.level_one.set_data(self.data)
        diff = self.level_one.get_difference()
        diff = (np.asarray(diff) + 1) / 2
        self.level_two.set_data(diff)

    def run(self):
        self.level_one.run()
        diff = self.level_one.get_difference()
        diff = (np.asarray(diff) + 1) / 2
        self.level_two.set_data(diff)
        self.level_two.run()

    def get_reconstructed_data(self, winners=None):
        rec_one = np.asarray(self.level_one.get_reconstructed_data(winners))
        rec_two = np.asarray(self.level_two.get_reconstructed_data())
        rec = rec_one + (rec_two * 2) - 1
        for i in rec:
            for j in np.ndindex(i.shape):
                if i[j] < 0: i[j] = 0
                elif i[j] > 1: i[j] = 1
        return rec

    def get_all_winners(self):
        return self.level_one.get_all_winners()

    def get_neural_distances(self, old_winners, new_winners):
        return self.level_one.get_neural_distances(old_winners, new_winners)

    def mean_error(self):
        rec = self.get_reconstructed_data()
        diff = rec - self.data
        error = []
        for i in diff:
            error.append(np.mean(np.abs(i)))
        return np.mean(error)

    def square_error(self):
        rec = self.get_reconstructed_data()
        diff = rec - self.data
        error = []
        for i in diff:
            error.append(np.mean(i**2))
        return np.mean(error)
示例#2
0
map.data = data.get_data()

output_path = os.path.join("Data", "spikes", "office", "out")
os.makedirs(os.path.join(output_path, "difference"), exist_ok=True)
os.makedirs(os.path.join(output_path, "diff_winners"), exist_ok=True)
os.makedirs(os.path.join(output_path, "saliency"), exist_ok=True)
os.makedirs(os.path.join(output_path, "thresholded"), exist_ok=True)

initial_map = map.get_all_winners()
for i in range(828):
    print("Extracting ", i)
    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)