def fit(self, X, y=None):
     # Compute the codebook
     self.codebook = Codebook(K=self.K,
                              no_dump=self.no_dump_codebook,
                              force_reload=self.force_reload)
     self.codebook.fit(X)
     return self
Beispiel #2
0
    def __init__(self, shape):
        self.codebooks = []
        self.height = shape[1]
        self.width = shape[0]

        for i in range(self.width):
            for j in range(self.height):
                self.codebooks.append(Codebook())
Beispiel #3
0
    def train(self,
              instances,
              dev_set=None,
              max_epoch=30,
              learning_rate=.5,
              batch_size=30):
        # Construct a statistical model from labeled instances.

        self.codebook = Codebook()
        self.codebook.supervised_populate(instances)

        self.parameters = np.zeros((self.codebook.dimension()))
        self._train_sgd(instances, dev_set, max_epoch, learning_rate,
                        batch_size)
    def __init__(self,
                 data,
                 neighborhood,
                 normalizer=None,
                 mapsize=None,
                 mask=None,
                 mapshape='planar',
                 lattice='rect',
                 initialization='pca',
                 training='batch',
                 name='sompy',
                 component_names=None):
        """
        Self Organizing Map

        :param data: data to be clustered, represented as a matrix of n rows,
            as inputs and m cols as input features
        :param neighborhood: neighborhood object calculator.
        :param normalizer: normalizer object calculator.
        :param mapsize: tuple/list defining the dimensions of the som. If
            single number is provided is considered as the number of nodes.
        :param mask: mask
        :param mapshape: shape of the som.
        :param lattice: type of lattice.
        :param initialization: method to be used for initialization of the som.
        :param name: name used to identify the som
        :param training: Training mode (seq, batch)
        """
        self._data = normalizer.normalize(data) if normalizer else data
        self._normalizer = normalizer
        self._dim = data.shape[1]
        self._dlen = data.shape[0]
        self._dlabel = None
        self._bmu = None

        self.name = name
        self.data_raw = data
        self.neighborhood = neighborhood
        self.mapshape = mapshape
        self.initialization = initialization
        self.mask = mask or np.ones([1, self._dim])
        mapsize = self.calculate_map_size(lattice) if not mapsize else mapsize
        self.codebook = Codebook(mapsize, lattice)
        self.training = training
        self._component_names = self.build_component_names(
        ) if component_names is None else [component_names]
        self._distance_matrix = self.calculate_map_dist()
Beispiel #5
0
    def __init__(self, Y, D, sigma, b=1e-8, scale=1):

        self.input_shape = Y.shape
        self.sigma = sigma
        self.scale = scale

        m, r, c = D.shape
        _, _, self.h, self.w = Y.shape
        self.hx = int(np.ceil(self.h * scale)) if self.h > 1 else self.h
        self.wx = int(np.ceil(self.w * scale)) if self.w > 1 else self.w

        bh = int(2 * np.ceil(3 * sigma[0] * scale) + 1) if self.h > 1 else 1
        bw = int(2 * np.ceil(3 * sigma[1] * scale) + 1) if self.w > 1 else 1
        self.psf_support_scaled = (bh, bw)

        bh = int(2 * np.ceil(3 * sigma[0]) + 1) if self.h > 1 else 1
        bw = int(2 * np.ceil(3 * sigma[1]) + 1) if self.w > 1 else 1
        self.psf_support = (bh, bw)

        # Set up X
        x_shape = (m, self.hx, self.wx)
        self.X = torch.ones(x_shape).float()

        # Set up Y
        self.Y = torch.tensor(Y).float().flatten(start_dim=0, end_dim=1)

        # Set up b
        self.b = torch.tensor(b).float()
        if self.b.ndim == 4:
            self.b = self.b.flatten(start_dim=0, end_dim=1)

        # Set up codebook
        self.codebook = Codebook(D)

        # Set up spatial blurr
        self.psf = PSF((self.h, self.w), sigma, scale)

        # Prepare constant
        ones_channels = torch.ones((r * c, 1, 1))
        ones_space = torch.ones((1, self.h, self.w))
        self.denominator =  self.codebook.matmul_t(ones_channels) * \
                            self.psf.matmul_t(ones_space)

        # Compute Yhat = DXG
        self.Yhat = self.__forward(self.X)
Beispiel #6
0
    def codebook(self):
        """ Export a text file code book of categories and codes.
        """

        Codebook(self.app, self.ui.textEdit)
Beispiel #7
0
def build_codebook(encoder, dataset, args):
    embed_bb = args.getboolean('Embedding', 'EMBED_BB')
    codebook = Codebook(encoder, dataset, embed_bb)
    return codebook
Beispiel #8
0
                 "../images/Crowd_PETS09/S0/Background/View_001/Time_13-06/"))
EPSILON_1 = 1
TRAIN_N_IMG = 10

if __name__ == '__main__':

    img_list = os.listdir(TRAINING_PATH)[:TRAIN_N_IMG]

    for it, file in enumerate(img_list):

        img = cv2.cvtColor(cv2.imread(os.path.join(TRAINING_PATH, file)),
                           cv2.COLOR_BGR2RGB).astype(float)

        if it == 0:
            codebooks = np.array(
                [Codebook() for pixel in range(img.shape[0] * img.shape[1])])

        for px_row in img:
            for px_idx, px in enumerate(px_row):

                R = px[0]
                G = px[1]
                B = px[2]
                X = np.array([R, G, B])
                I = sqrt(R**2 + G**2 + B**2)
                pixel = Pixel(X, I)

                codebook_empty = True
                no_match = True

                for codeword in codebooks[px_idx].codewords:
 def fit(self, X, y=None):
     # Compute the codebook
     self.codebook = Codebook(K=self.K, no_dump=self.no_dump_codebook)
     self.codebook.fit(X['descriptors'])
     return self
Beispiel #10
0
    def __init__(self,
                 data,
                 neighborhood,
                 normalizer=None,
                 mapsize=None,
                 mask=None,
                 mapshape='planar',
                 lattice='rect',
                 initialization='pca',
                 training='batch',
                 radius_train='linear',
                 name='sompy',
                 component_names=None,
                 components_to_plot=None,
                 isNormalized=False):
        """
        Self Organizing Map

        :param data: data to be clustered, represented as a matrix of n rows,
            as inputs and m cols as input features
        :param neighborhood: neighborhood object calculator.
        :param normalizer: normalizer object calculator.
        :param mapsize: tuple/list defining the dimensions of the som. If
            single number is provided is considered as the number of nodes.
        :param mask: mask
        :param mapshape: shape of the som.
        :param lattice: type of lattice.
        :param initialization: method to be used for initialization of the som.
        :param name: name used to identify the som
        :param training: Training mode (seq, batch)
        """

        if data is not None:  # ajout LB
            if normalizer and isNormalized == False:
                for i in range(len(normalizer)):
                    data[:, i] = normalizer[i].normalize(data[:, i])
                self._data = data
            else:
                self._data = data
            self._data = self._data.astype('double')  # ajout LB
            self._dim = data.shape[1]
            self._dlen = data.shape[0]
        else:
            self._data = None
            self._dim = None
            self._dlen = None

        self._normalizer = normalizer
        self._dlabel = None
        self._bmu = None

        self.name = name
        self.data_raw = data
        self.neighborhood = neighborhood
        self.mapshape = mapshape
        self.initialization = initialization
        self.mask = mask
        mapsize = self.calculate_map_size(lattice) if not mapsize else mapsize
        self.mapsize = mapsize
        self.codebook = Codebook(mapsize, lattice)
        self.training = training
        self.radius_train = radius_train
        self._component_names = self.build_component_names(
        ) if component_names is None else [component_names]
        self._distance_matrix = self.calculate_map_dist()
        self.components_to_plot = components_to_plot
Beispiel #11
0
    def __init__(self,
                 data,
                 neighborhood,
                 normalizer=None,
                 mapsize=None,
                 mask=None,
                 mapshape='planar',
                 lattice='rect',
                 initialization='pca',
                 training='batch',
                 name='sompy'):
        """
        Self Organizing Map

        :param data: data to be clustered, represented as a matrix of n rows,
            as inputs and m cols as input features
        :param neighborhood: neighborhood object calculator.
        :param normalizer: normalizer object calculator.
        :param mapsize: tuple/list defining the dimensions of the som. If
            single number is provided is considered as the number of nodes.
        :param mask: mask
        :param mapshape: shape of the som.
        :param lattice: type of lattice.
        :param initialization: method to be used for initialization of the som.
        :param name: name used to identify the som
        :param training: Training mode (seq, batch)
        """
        print "DDDD", data
        self._data = data  # normalizer.normalize(data) if normalizer else data
        print "data  : \n", self._data
        print "map size  : ", mapsize

        self._normalizer = normalizer
        print "_normalizer is a object to class normalizer : ", self._normalizer
        self._dim = data.shape[1]
        print "data shape[1]  :  ", self._dim
        self._dlen = data.shape[0]
        print "data shape[0] : ", data.shape[0]
        self._dlabel = None
        print "data label  : ", self._dlabel
        self._bmu = None
        print "intial bmu  :  ", self._bmu
        self.name = name
        print "name   :  ", self.name
        self.data_raw = data
        print "data_raw  : ", self.data_raw
        #print "data_raw  : ", self.data_raw
        self.neighborhood = neighborhood
        self.mapshape = mapshape
        print "mapshape : ", self.mapshape
        self.initialization = initialization
        print "initilization : ", self.initialization
        self.mask = mask or np.ones([1, self._dim])
        print "mask  : ", self.mask
        self.codebook = Codebook(mapsize, lattice)
        print "--------codebook is a object to class Codebook---------\n", self.codebook
        self.training = training
        print "training mode  : ", self.training

        self._component_names = self.build_component_names()
        print "Initial component name  : ", self._component_names
        self._distance_matrix = self.calculate_map_dist()
        print "initial distance matrix  : ", self._distance_matrix
Beispiel #12
0
    def codebook(self):
        """ Export a code book of categories and codes.
        """

        Codebook(self.settings, self.ui.textEdit)