Ejemplo n.º 1
0
 def load_dataset(self):
     if self.dataset["type"] == "image":
         path = os.path.join("Data", "images", self.dataset["file"])
         img = Image.open(path)
         parameters = Parameters({
             "pictures_dim":
             [self.dataset["width"], self.dataset["height"]]
         })
         self.data = MosaicImage(img, parameters)
     elif self.dataset["type"] == "random_image":
         path = os.path.join("Data", "images", self.dataset["file"])
         img = Image.open(path)
         parameters = Parameters({
             "pictures_dim":
             [self.dataset["width"], self.dataset["height"]]
         })
         self.data = MosaicImage(img, parameters)
         self.training_data = RandomImage(img, parameters)
     elif self.dataset["type"] == "tracking":
         path = os.path.join("Data", "tracking", "dataset",
                             self.dataset["file"], "back.jpg")
         #if self.metadata["seed"] % 2 == 1:
         #    path = os.path.join("Data", "tracking", "dataset", self.dataset["file"], "input", "bkg.jpg")
         #else:
         #    path = os.path.join("Data", "tracking", "dataset", self.dataset["file"], "input", "bkg2.jpg")
         img = Image.open(path)
         parameters = Parameters({
             "pictures_dim":
             [self.dataset["width"], self.dataset["height"]]
         })
         self.data = MosaicImage(img, parameters)
     else:
         print("Error : No dataset type specified !")
Ejemplo n.º 2
0
    def learning(self, bkg_image):
        # PARAMETERS
        self.image_parameters = Parameters({"pictures_dim": self.pictures_dim})
        data = MosaicImage(bkg_image, self.image_parameters)
        inputs_SOM = Parameters({"alpha": Variable(start=self.alpha_start, end=self.alpha_end, nb_steps=self.nb_epochs),
                                 "sigma": Variable(start=self.sigma_start, end=self.sigma_end, nb_steps=self.nb_epochs),
                                 "data": data.get_data(),
                                 "neurons_nbr": self.neurons_nb,
                                 "epochs_nbr": self.nb_epochs})
        self.som = SOM(inputs_SOM)

        # RUN
        for i in range(self.nb_epochs):
            # print('Epoch', i)
            self.som.run_epoch()
        self.initial_map = self.som.get_all_winners()
Ejemplo n.º 3
0
    def __init__(self,
                 input_path,
                 output_path,
                 supplements_path,
                 parameters=None):
        self.input_path = input_path
        self.output_path = output_path
        self.supplements_path = supplements_path

        if parameters is None:
            parameters = Parameters()

        self.tau_dt = parameters["tau_dt"] if parameters[
            "tau_dt"] is not None else 0.8
        self.h = parameters["h"] if parameters["h"] is not None else -0.2
        self.gi = parameters["gi"] if parameters["gi"] is not None else 1

        self.excitation_amplitude = parameters[
            "excitation_amplitude"] if parameters[
                "excitation_amplitude"] is not None else 1
        self.excitation_sigma = parameters["excitation_sigma"] if parameters[
            "excitation_sigma"] is not None else 0.05
        self.inhibition_amplitude = parameters[
            "inhibition_amplitude"] if parameters[
                "inhibition_amplitude"] is not None else 0
        self.inhibition_sigma = parameters["inhibition_sigma"] if parameters[
            "inhibition_sigma"] is not None else 0.5

        self.threshold = parameters["threshold"] if parameters[
            "threshold"] is not None else 10
        self.step = parameters["step"] if parameters["step"] is not None else 1
Ejemplo n.º 4
0
def regenerate_database():
    global data_types
    data_types = {
        "spokenDigits":
        SpokenDigitsDataset(
            "/users/yabernar/workspace/watSOM/Data/FSDD/recordings",
            1000).get_data(),
        "images":
        MosaicImage(
            Image.open(
                "/users/yabernar/workspace/watSOM/Code/fast_som/Elijah.png"),
            Parameters({"pictures_dim": [10, 10]})).get_data(),
        "uniform3D":
        uniform(1000, 3),
        "pixel_colors":
        PixelsFromImage(
            Image.open(
                "/users/yabernar/workspace/watSOM/Code/fast_som/Elijah.png"),
            1000).get_data(),
        "catShape":
        GenerateFromShape(
            Image.open(
                "/users/yabernar/workspace/watSOM/Code/fast_som/cat-silhouette.png"
            ), 1000).get_data(),
        "uniform2D":
        uniform(1000, 2)
    }
Ejemplo n.º 5
0
def evaluate_learning(SOM, data_type, neurons_nbr):
    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": data_type,
                         "neurons_nbr": (neurons_nbr, neurons_nbr),
                         "epochs_nbr": nb_epochs})
    som = SOM(inputs)
    som.run()
    nb_comp = np.mean(som.nbr_quad_dist)
    return {"nbr_comparisons": nb_comp, "percentage_calculated": nb_comp/(neurons_nbr**2)*100}
Ejemplo n.º 6
0
def evaluate_reconstruction(SOM, data_type):
    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": data_type,
                         "neurons_nbr": n_nbr,
                         "epochs_nbr": nb_epochs})
    st_som = StandardSOM(inputs)
    st_som.run()

    som = SOM(inputs)
    som.neurons = st_som.neurons
    return {"MSQE_R": som.mean_square_quantization_error()}
Ejemplo n.º 7
0
    def runSOM(self):
        if self.data is None:
            self.load_dataset()
        nb_epochs = self.model["nb_epochs"]
        if "alpha_start" not in self.model: self.model["alpha_start"] = 0.2
        if "alpha_end" not in self.model: self.model["alpha_end"] = 0.05
        if "sigma_start" not in self.model: self.model["sigma_start"] = 0.7
        if "sigma_end" not in self.model: self.model["sigma_end"] = 0.015
        if "nb_images_evals" not in self.dataset:
            self.dataset["nb_images_evals"] = 75
        parameters = Parameters({
            "alpha":
            Variable(start=self.model["alpha_start"],
                     end=self.model["alpha_end"],
                     nb_steps=nb_epochs),
            "sigma":
            Variable(start=self.model["sigma_start"],
                     end=self.model["sigma_end"],
                     nb_steps=nb_epochs),
            "data":
            self.data.get_data(),
            "neurons_nbr": (self.model["width"], self.model["height"]),
            "epochs_nbr":
            nb_epochs
        })
        if self.model["model"] == "standard":
            self.map = SOM(parameters)
        elif self.model["model"] == "fast":
            self.map = FastSOM(parameters)
        elif self.model["model"] == "recursive":
            self.map = RecursiveSOM(parameters)
        else:
            print("Error : Unknown model !")

        # if "initialisation" not in self.codebooks:
        #     self.codebooks["initialisation"] = self.som.neurons.tolist()
        if "final" in self.codebooks:
            self.map.neurons = np.asarray(self.codebooks["final"])
        else:
            self.map.run()
            #for i in range(nb_epochs):
            #    self.map.run_epoch()
            #self.codebooks["final"] = copy.deepcopy(self.map.neurons.tolist())

        # for i in range(nb_epochs):
        #     print("Epoch "+str(i+1))
        #     if "Epoch "+str(i + 1) not in self.codebooks:
        #         if self.training_data is not None:
        #             self.som.data = self.training_data.get_data(self.som.data.shape[0])
        #         self.som.run_epoch()
        #         # self.codebooks["Epoch " + str(i + 1)] = copy.deepcopy(self.som.neurons.tolist())
        #     self.som.run_epoch()
        self.map.data = self.data.get_data()
Ejemplo n.º 8
0
    def __init__(self,
                 input_path,
                 output_path,
                 supplements_path,
                 temporal_ROI,
                 mask_ROI,
                 parameters=None):
        self.mask = mask_ROI
        self.input_path = input_path
        self.output_path = output_path
        self.supplements_path = supplements_path
        self.temporal_ROI = temporal_ROI

        if parameters is None:
            parameters = Parameters()
        self.pictures_dim = parameters["pictures_dim"] if parameters[
            "pictures_dim"] is not None else [10, 10]
        self.threshold = parameters["threshold"] if parameters[
            "threshold"] is not None else 10
        self.step = parameters["step"] if parameters[
            "step"] is not None else 1  # IF CHANGED, NEED TO CHANGE COMPARATOR TOO
        self.cut = parameters["cut"] if parameters["cut"] is not None else 0

        self.image_parameters = Parameters({"pictures_dim": self.pictures_dim})
Ejemplo n.º 9
0
def evaluate(args):
    # pic_dim, neuron_nbr, epochs = args
    # params = Parameters({"pictures_dim": [int(pic_dim), int(pic_dim)], "neurons_nbr": (int(neuron_nbr), int(neuron_nbr)), "epochs_nbr": int(epochs)})
    alpha_start, alpha_end, sigma_start, sigma_end = args
    params = Parameters({
        "alpha_start": alpha_start,
        "alpha_end": alpha_end,
        "sigma_start": sigma_start,
        "sigma_end": sigma_end
    })
    print("Tested params : {}".format(params.data))

    all_videos = []
    categories_list = [1, 3]
    categories = sorted([
        d for d in os.listdir(cdnet_path)
        if os.path.isdir(os.path.join(cdnet_path, d))
    ],
                        key=str.lower)
    for cat in list(categories[i] for i in categories_list):
        elements = sorted([
            d for d in os.listdir(os.path.join(cdnet_path, cat))
            if os.path.isdir(os.path.join(cdnet_path, cat, d))
        ],
                          key=str.lower)
        for elem in elements:
            all_videos.append(os.path.join(cat, elem))

    # pool = mp.Pool(8)
    # pool.starmap(process_video, zip(all_videos, itertools.repeat(params)))
    # pool.close()
    # pool.join()
    #
    # cmp = Comparator()
    # fitness = cmp.evaluate_all(cdnet_path, output_path, categories_list)

    pool = mp.Pool(8)
    results = pool.starmap(process_bkg,
                           zip(all_videos, itertools.repeat(params)))
    print(results)
    pool.close()
    pool.join()

    fitness = np.mean(np.asarray(results))

    print("Measured fitness : {}\n".format(fitness))
    return fitness
Ejemplo n.º 10
0
    def compute_metrics(self):
        self.metrics["Square_error"] = self.map.square_error()
        if self.model["model"] == "gng":
            self.metrics["Neurons"] = len(self.map.network.nodes())
            self.metrics["Connections"] = len(self.map.network.edges())
        if self.dataset["type"] == "tracking":
            current_path = os.path.join("Data", "tracking", "dataset",
                                        self.dataset["file"])
            input_path = os.path.join(current_path, "input")
            roi_file = open(os.path.join(current_path, "temporalROI.txt"),
                            "r").readline().split()
            temporal_roi = (int(roi_file[0]), int(roi_file[1]))
            mask_roi = Image.open(os.path.join(current_path, "ROI.png"))

            nb_img_gen = self.dataset["nb_images_evals"]
            step = 1
            if nb_img_gen > 0:
                step = (temporal_roi[1] + 1 - temporal_roi[0]) // nb_img_gen

            base = os.path.join("Results", "GNGopti", self.metadata["name"],
                                "results")
            output_path = os.path.join(base, self.dataset["file"])
            supplements_path = os.path.join(base, "supplements")

            parameters = Parameters({
                "pictures_dim":
                [self.dataset["width"], self.dataset["height"]],
                "step":
                step
            })

            trackingMetric = TrackingMetrics(input_path,
                                             output_path,
                                             supplements_path,
                                             temporal_roi,
                                             mask_roi,
                                             parameters=parameters)
            trackingMetric.compute(self.map)
            cmp = Comparator()
            fmeasure, precision, recall = cmp.evaluate__folder_c(
                current_path, output_path, step)
            # print(fitness)
            self.metrics["fmeasure"] = fmeasure
            self.metrics["precision"] = precision
            self.metrics["recall"] = recall
Ejemplo n.º 11
0
    def compute_steps_metrics(self):
        # self.metrics["Square_error"] = self.som.square_error()
        # self.metrics["Neurons"] = len(self.som.network.nodes())
        # self.metrics["Connections"] = len(self.som.network.edges())
        if self.dataset["type"] == "tracking":
            current_path = os.path.join("Data", "tracking", "dataset",
                                        self.dataset["file"])
            input_path = os.path.join(current_path, "input")
            roi_file = open(os.path.join(current_path, "temporalROI.txt"),
                            "r").readline().split()
            temporal_roi = (int(roi_file[0]), int(roi_file[1]))
            mask_roi = Image.open(os.path.join(current_path, "ROI.png"))

            base = os.path.join("Results", "GNGoptimisation",
                                self.metadata["name"], "results")
            output_path = os.path.join(base, self.dataset["file"])
            supplements_path = os.path.join(base, "supplements")

            parameters = Parameters({
                "pictures_dim":
                [self.dataset["width"], self.dataset["height"]]
            })

            # ranges = list(range(1, 5)) + list(range(5, 101, 5))
            #res = []
            #ranges = range(1,201)
            #for i in ranges:
            #    cmp = Comparator()
            #    fitness = cmp.evaluate__folder_c(current_path, output_path, i)
            #    res.append(fitness)
            #self.metrics["fmeasure-steps"] = res

            res = []
            nb_image_ranges = range(5, 201)
            for i in nb_image_ranges:
                step = (temporal_roi[1] + 1 - temporal_roi[0]) // i
                cmp = Comparator()
                fitness = cmp.evaluate__folder_c(current_path, output_path,
                                                 step)
                res.append(fitness)
            self.metrics["fmeasure-nbimgs"] = res
Ejemplo n.º 12
0
    def __init__(self, input_path, output_path, supplements_path, temporal_ROI, mask_ROI, parameters=None):
        self.bkg = Image.open(os.path.join(input_path, "bkg.jpg"))
        self.mask = mask_ROI
        self.input_path = input_path
        self.output_path = output_path
        self.supplements_path = supplements_path
        self.temporal_ROI = temporal_ROI

        if parameters is None:
            parameters = Parameters()
        self.neurons_nb = parameters["neurons_nbr"] if parameters["neurons_nbr"] is not None else (10, 10)
        self.pictures_dim = parameters["pictures_dim"] if parameters["pictures_dim"] is not None else [10, 10]
        self.nb_epochs = parameters["nb_epochs"] if parameters["nb_epochs"] is not None else 50

        self.alpha_start = parameters["alpha_start"] if parameters["alpha_start"] is not None else 0.09
        self.alpha_end = parameters["alpha_end"] if parameters["alpha_end"] is not None else 0.22
        self.sigma_start = parameters["sigma_start"] if parameters["sigma_start"] is not None else 0.94
        self.sigma_end = parameters["sigma_end"] if parameters["sigma_end"] is not None else 0.56

        self.threshold = parameters["threshold"] if parameters["threshold"] is not None else 10
        self.step = parameters["step"] if parameters["step"] is not None else 30
Ejemplo n.º 13
0
 def runGNG(self):
     if self.data is None:
         self.load_dataset()
     inputs = Parameters({
         "epsilon_winner":
         self.model["epsilon_winner"],  # 0.1,
         "epsilon_neighbour":
         self.model["epsilon_neighbour"],  # 0.006,
         "maximum_age":
         self.model["maximum_age"],  # 10,
         "error_decrease_new_unit":
         self.model["error_decrease_new_unit"],  # 0.5,
         "error_decrease_global":
         self.model["error_decrease_global"],  # 0.995,
         "data":
         self.data.get_data(),
         "neurons_nbr":
         self.model["nb_neurons"],
         "epochs_nbr":
         self.model["nb_epochs"]
     })
     self.map = GrowingNeuralGas(inputs)
     self.map.run()
Ejemplo n.º 14
0
def evaluate_learning(SOM, data_type):
    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":
        data_type,
        "neurons_nbr":
        n_nbr,
        "epochs_nbr":
        nb_epochs
    })
    som = SOM(inputs)
    som.run()
    msqe_r, nb_errors = som.mean_square_quantization_error_secondary()
    return {
        "MSQE_L": som.mean_square_quantization_error(),
        "MSDtN": som.mean_square_distance_to_neighbour(),
        "MSQE_R": msqe_r,
        "Error Nbr": nb_errors,
        "Error %:": nb_errors / data_type.shape[0] * 100
    }
Ejemplo n.º 15
0
def evaluate(args):
    tau_dt, h, gi, Ap, Sp = args
    params = Parameters({
        "tau_dt": tau_dt,
        "h": h,
        "gi": gi,
        "excitation_amplitude": Ap,
        "excitation_sigma": Sp
    })
    print("Tested params : {}".format(params.data))

    all_videos = []
    categories_list = [1]
    categories = sorted([
        d for d in os.listdir(input_path)
        if os.path.isdir(os.path.join(input_path, d))
    ],
                        key=str.lower)
    for cat in list(categories[i] for i in categories_list):
        elements = sorted([
            d for d in os.listdir(os.path.join(input_path, cat))
            if os.path.isdir(os.path.join(input_path, cat, d))
        ],
                          key=str.lower)
        for elem in elements:
            all_videos.append(os.path.join(cat, elem))

    pool = mp.Pool(8)
    pool.starmap(process_video, zip(all_videos, itertools.repeat(params)))
    pool.close()
    pool.join()

    cmp = Comparator()
    fitness = 1 - cmp.evaluate_all(cdnet_path, output_path, categories_list)

    print("Measured fitness : {}\n".format(fitness))
    return fitness
Ejemplo n.º 16
0
def evaluate(args):
    global run_nb
    if run_nb > 0:
        save_optimisation()
    # pic_dim, neuron_nbr, epochs = args
    # params = Parameters({"pictures_dim": [int(pic_dim), int(pic_dim)], "neurons_nbr": (int(neuron_nbr), int(neuron_nbr)), "epochs_nbr": int(epochs)})
    #alpha_start, alpha_end = args
    #sigma_start, sigma_end = (0.7, 0.015)
    #params = Parameters({"alpha_start": alpha_start, "alpha_end": alpha_end, "sigma_start": sigma_start, "sigma_end": sigma_end})

    epsilon_winner, epsilon_neighbour, maximum_age, error_decrease_new_unit, error_decrease_global, epochs_nbr = args
    params = Parameters({
        "epsilon_winner": epsilon_winner,
        "epsilon_neighbour": epsilon_neighbour,
        "maximum_age": maximum_age,
        "error_decrease_new_unit": error_decrease_new_unit,
        "error_decrease_global": error_decrease_global,
        "epochs_nbr": epochs_nbr
    })

    print("Tested params : {}".format(params.data))

    sr = SimulationRun()
    sr.create(args)
    sr.compute(8)
    sr.all_runs = []
    sr.open_folder(sr.current_path)

    res = []
    for i in sr.all_runs:
        res.append(i.metrics["fmeasure"])
    fitness = 1 - np.mean(np.asarray(res))

    run_nb += 1

    print("Measured fitness : {}".format([fitness, 1 - fitness]))
    return fitness
Ejemplo n.º 17
0
        for i in np.ndindex(winners.shape):
            error[i] = np.mean((self.working_data[i] - self.network.nodes[winners[i]]['vector'])**2)
        return np.mean(error)

    def display(self):
        reconstructed = self.img.reconstruct(self.get_reconstructed_data())
        # som_image = mosaic.reconstruct(gng.get_neural_list(), size=som.neurons_nbr)
        print("Error : ", self.square_error())
        print("Neurons : ", len(self.network.nodes()))
        print("Connections : ", len(self.network.edges()))
        plt.imshow(reconstructed)
        plt.show()


if __name__ == '__main__':
    img = MosaicImage(Image.open(os.path.join("Data", "Tracking", "Dataset", "baseline", "highway", "bkg.jpg")), Parameters({"pictures_dim": [16, 16]}))
    data = img.get_data()
    gng = GrowingNeuralGas(data)
    gng.img = img
    gng.fit_network(e_b=0.1, e_n=0.006, a_max=10, l=200, a=0.5, d=0.995, passes=100, plot_evolution=False)
    reconstructed = img.reconstruct(gng.get_reconstructed_data())
    # som_image = mosaic.reconstruct(gng.get_neural_list(), size=som.neurons_nbr)
    # print(gng.get_all_winners())
    plt.imshow(reconstructed)
    plt.show()
    print(len(gng.network.nodes))

    img2 = MosaicImage(Image.open(os.path.join("Data", "Tracking", "Dataset", "baseline", "highway", "input", "in001010.jpg")), Parameters({"pictures_dim": [16, 16]}))
    data2 = img2.get_data()
    old_winners = gng.get_all_winners()
    gng.data = data2
Ejemplo n.º 18
0
from Code.fast_som.SwarmSOM import SwarmSOM
from Code.fast_som.GreedySOM import GreedySOM
from Code.fast_som.StandardSOM import StandardSOM
from Data.Generated_Data import sierpinski_carpet, uniform
from Data.Generated_from_shape import GenerateFromShape
from Data.Mosaic_Image import MosaicImage

# PARAMETERS

# som_algs = {"standard": StandardSOM, "greedy": GreedySOM, "swarm": SwarmSOM, "fourCorners": FourCornersSOM, "greedyToroidal": GreedyToroidalSOM}
from Data.Pixels_from_Image import PixelsFromImage
from Data.Spoken_digits_dataset import SpokenDigitsDataset

som_algs = {"Fast-SOM": FourCornersSOM, "Fast-Hex": FourCornersSOM_Hex}
data_types = {"spokenDigits": SpokenDigitsDataset("/users/yabernar/workspace/watSOM/Data/FSDD/recordings", 1000).get_data(),
              "images": MosaicImage(Image.open("/users/yabernar/workspace/watSOM/Code/fast_som/Elijah.png"), Parameters({"pictures_dim": [10, 10]})).get_data(),
              "uniform3D": uniform(1000, 3),
              "pixel_colors": PixelsFromImage(Image.open("/users/yabernar/workspace/watSOM/Code/fast_som/Elijah.png"), 1000).get_data(),
              "catShape": GenerateFromShape(Image.open("/users/yabernar/workspace/watSOM/Code/fast_som/cat-silhouette.png"), 1000).get_data(),
              "uniform2D": uniform(1000, 2)}
neuron_numbers = {"12": (12, 12), "medium": (32, 32), "rectangular": (48, 24)}
n_nbr = (12, 12)
nb_epochs = 10


def regenerate_database():
    global data_types
    data_types = {"spokenDigits": SpokenDigitsDataset("/users/yabernar/workspace/watSOM/Data/FSDD/recordings", 1000).get_data(),
                  "images": MosaicImage(Image.open("/users/yabernar/workspace/watSOM/Code/fast_som/Elijah.png"),
                                        Parameters({"pictures_dim": [10, 10]})).get_data(),
                  "uniform3D": uniform(1000, 3),
Ejemplo n.º 19
0
            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)


if __name__ == '__main__':
    start = time.time()
    img = MosaicImage(Image.open("Data/images/Elijah.png"),
                      Parameters({"pictures_dim": [10, 10]}))

    nb_epochs = 100
    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": (8, 8),
        "epochs_nbr":
        nb_epochs
    })
    som = RecursiveSOM(inputs)
    som.run()
Ejemplo n.º 20
0
                    error[i] += np.mean((self.neurons[i] - self.neurons[j])**2)
                    nb_neighbours += 1
            error[i] /= nb_neighbours
        return np.mean(error)


if __name__ == '__main__':
    start = time.time()

    nb_epochs = 50
    inputs = Parameters({
        "alpha":
        Variable(start=0.6, end=0.05, nb_steps=nb_epochs),
        "sigma":
        Variable(start=0.5, end=0.2, nb_steps=nb_epochs),
        "data":
        uniform(200, 3),
        "neurons_nbr": (10, 10),
        "epochs_nbr":
        nb_epochs
    })
    som = GreedySOM(inputs)
    som.run()

    end = time.time()
    print("Executed in " + str(end - start) + " seconds.")
    print(som.mean_square_quantization_error())
    print(som.mean_square_distance_to_neighbour())

# 2  0.000128  0.000151  0.000141    greedy  uniform2D
# 3  0.000735  0.019616  0.032234    greedy  uniform3D
Ejemplo n.º 21
0
                  key=str.lower)
print(categories)
print(elements)

chosen_path = path + "/dataset/" + categories[11] + "/" + elements[0]
temporal_ROI = (1, 200)
plot = None
bkg = Image.open(chosen_path + "/input/" + 'bkg.jpg')
# bkg = Image.open(path2 + video + '{0:05d}.png'.format(1))
# bkg = Image.open(path3 + "example_base.png")

############
# LEARNING #
############
pictures_dim = [10, 10]
parameters = Parameters({"pictures_dim": pictures_dim})
data = MosaicImage(bkg, parameters)
nb_epochs = 50
inputs_SOM = Parameters({
    "alpha":
    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):
Ejemplo n.º 22
0
som_algs = {
    "standard": StandardSOM,
    "fourCorners": FourCornersSOM,
    "hexFourCorners": FourCornersSOM_Hex,
    "hexSOM": HexSOM
}
data_types = {
    "spokenDigits":
    SpokenDigitsDataset(
        "/users/yabernar/workspace/watSOM/Data/FSDD/recordings",
        1000).get_data(),
    "images":
    MosaicImage(
        Image.open(
            "/users/yabernar/workspace/watSOM/Code/fast_som/Elijah.png"),
        Parameters({"pictures_dim": [10, 10]})).get_data(),
    "uniform3D":
    uniform(1000, 3),
    "pixel_colors":
    PixelsFromImage(
        Image.open(
            "/users/yabernar/workspace/watSOM/Code/fast_som/Elijah.png"),
        1000).get_data(),
    "catShape":
    GenerateFromShape(
        Image.open(
            "/users/yabernar/workspace/watSOM/Code/fast_som/cat-silhouette.png"
        ), 1000).get_data(),
    "uniform2D":
    uniform(1000, 2)
}
Ejemplo n.º 23
0
        count = 0
        for i in range(0, size[0] - dim[0], 5):
            for j in range(0, size[1] - dim[1], 5):
                # print(i,j)
                current = data[count]
                count += 1
                # print(current)
                current = current.reshape(dim)
                # current = np.rot90(current, 2, (0, 1))
                # current = np.flip(current, (0,1))
                # print(pixels.shape)
                # print(current.shape)
                # print(self.pictures_dim)
                # print(self.size)
                pixels[i:i + dim[1], j:j + dim[1], :] += current
        pixels *= (255 / 4)
        pixels = np.array(pixels, 'uint8')
        return pixels

    def get_data(self):
        return self.data

    def display(self):
        return plt.imshow(self.image)


if __name__ == '__main__':
    bkg = Image.open("Data/color_test.png")
    pictures_dim = [10, 10]
    parameters = Parameters({"pictures_dim": pictures_dim})
    data = SlidingWindow(bkg, parameters)
Ejemplo n.º 24
0
            error[i] /= nb_neighbours
        return np.mean(error)


if __name__ == '__main__':
    for n in range(10, 61, 2):
        start = time.time()

        nb_epochs = 10
        inputs = Parameters({
            "alpha":
            Variable(start=0.6, end=0.05, nb_steps=nb_epochs),
            "sigma":
            Variable(start=0.5, end=0.2, nb_steps=nb_epochs),
            "data":
            MosaicImage(
                Image.open(
                    "/users/yabernar/workspace/watSOM/Code/fast_som/Elijah.png"
                ), Parameters({"pictures_dim": [10, 10]})).get_data(),
            "neurons_nbr": (n, n),
            "epochs_nbr":
            nb_epochs
        })

        som = FastSOM(inputs)
        som.run()

        end = time.time()
        print("Executed in " + str(end - start) + " seconds.", n, "neurons")
        print("MSQE :", som.mean_square_quantization_error(), "MSDtN :",
              som.mean_square_distance_to_neighbour())
Ejemplo n.º 25
0
            error[i] = np.mean(np.abs(self.data[i] - self.neurons[winners[i]]))
        return np.mean(error)

    def square_error(self, winners=None):
        if winners is None:
            winners = self.get_all_winners()
        error = np.zeros(winners.shape)
        for i in np.ndindex(winners.shape):
            error[i] = np.mean((self.data[i] - self.neurons[winners[i]])**2)
        return np.mean(error)


if __name__ == '__main__':
    start = time.time()

    nb_epochs = 50
    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": sierpinski_carpet(200),
                         "neurons_nbr": (10, 10),
                         "epochs_nbr": nb_epochs})
    som = SOM(inputs)
    som.run()

    end = time.time()
    print("Executed in " + str(end - start) + " seconds.")
    print(som.mean_error(som.get_all_winners()))
    print(som.square_error(som.get_all_winners()))


Ejemplo n.º 26
0
import os
import numpy as np
from PIL import Image, ImageOps, ImageChops
from Code.Parameters import Parameters, Variable
from Code.SOM import SOM
from Data.Mosaic_Image import MosaicImage

pictures_dim = [10, 10]
name = "office"
path = os.path.join("Data", "spikes", "office", "src")
bkg = Image.open(os.path.join(path, name + "0.png"))
img_parameters = Parameters({"pictures_dim": pictures_dim})
data = MosaicImage(bkg, img_parameters)
nb_epochs = 100
parameters = 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":
    data.get_data(),
    "neurons_nbr": (10, 10),
    "epochs_nbr":
    nb_epochs
})
map = SOM(parameters)
for i in range(nb_epochs):
    print("Epoch " + str(i + 1))
    map.run_epoch()

map.data = data.get_data()
Ejemplo n.º 27
0
    def square_error(self, winners=None):
        if winners is None:
            winners = self.get_all_winners()
        error = np.zeros(winners.shape)
        for i in np.ndindex(winners.shape):
            error[i] = np.mean(
                (self.data[i] - self.network.nodes[winners[i]]['vector'])**2)
        return np.mean(error)


if __name__ == '__main__':
    bkg = MosaicImage(
        Image.open(
            os.path.join("Data", "tracking", "dataset", "baseline", "office",
                         "bkg.jpg")), Parameters({"pictures_dim": [16, 16]}))
    image = MosaicImage(
        Image.open(
            os.path.join("Data", "tracking", "dataset", "baseline", "office",
                         "input", "in001010.jpg")),
        Parameters({"pictures_dim": [16, 16]}))

    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,
Ejemplo n.º 28
0
    def get_neural_distances(self, old_winners):
        if self.winners is None:
            self.get_all_winners()
        distances = np.zeros(self.winners.shape)
        for n in self.winners:
            distances = nx.shortest_path_length(self.network, self.network.nodes[n], self.network.nodes[old_winners[n]])
        print(distances)

    def square_error(self):
        if self.winners is None:
            self.get_all_winners()
        error = np.zeros(self.winners.shape)
        for i in np.ndindex(self.winners.shape):
            error[i] = np.mean((self.data[i] - self.network.nodes[self.winners[i]]['vector'])**2)
        return np.mean(error)


if __name__ == '__main__':
    img = MosaicImage(Image.open(os.path.join("Data", "Images", "Lenna.png")), Parameters({"pictures_dim": [10, 10]}))
    data = img.get_data()
    gng = NeuralTopology(data)
    gng.img = img
    gng.fit_network(e_b=0.1, e_n=0.006, a_max=10, l=200, a=0.5, d=0.995, passes=500, plot_evolution=False)
    reconstructed = img.reconstruct(gng.get_reconstructed_data())
    # som_image = mosaic.reconstruct(gng.get_neural_list(), size=som.neurons_nbr)
    print(gng.get_all_winners())
    plt.imshow(reconstructed)
    plt.show()
    # plt.imshow(som_image, cmap='gray')