Пример #1
0
    def learn_dict(self, layer):

        n_imgs = len(self.imgs[layer])
        is_grayscale = len(self.imgs[layer][0].shape) == 2
        n_patches = self.n_training_patches[layer]
        patch_size = self.filter_sizes[layer]
        print "---------------------------------------"
        print "learning the dictionary for layer", layer
        print "number of patches:", n_patches
        print "number of atoms:", self.n_atoms[layer]

        X = extract_patches(self.imgs[layer], n_patches=n_patches, patch_size=patch_size,
                            mmap=False, mem="low", n_jobs=self.n_jobs, verbose=True, scale=False)

        if layer == 0:
            # scale to [0,1]
            X /= 255.
            if self.pre_procs is not None:
                for p in range(len(self.pre_procs)):
                    X = self.pre_procs[p](X)
        elif layer > 0:
            # do contrast normalization
            pass

        self.dict_learners[layer].sparse_coder = self.sparse_coders[layer]
        self.dict_learners[layer].n_atoms = self.n_atoms[layer]
        self.dict_learners[layer].fit(X)
        D = self.dict_learners[layer].D
        self.workspace.save(os.path.join("layer" + str(layer), "dict.npy"), D)
        return D
Пример #2
0
    def learn_dict(self, layer):

        n_imgs = len(self.imgs[layer])
        is_grayscale = len(self.imgs[layer][0].shape) == 2
        n_patches = self.n_training_patches[layer]
        patch_size = self.filter_sizes[layer]
        print "---------------------------------------"
        print "learning the dictionary for layer", layer
        print "number of patches:", n_patches
        print "number of atoms:", self.n_atoms[layer]

        X = extract_patches(self.imgs[layer], n_patches=n_patches, patch_size=patch_size,
                            mmap=False, mem="low", n_jobs=self.n_jobs, verbose=True, scale=False)

        if layer == 0:
            # scale to [0,1]
            X /= 255.
            if self.pre_procs is not None:
                for p in range(len(self.pre_procs)):
                    X = self.pre_procs[p](X)
        elif layer > 0:
            # do contrast normalization
            pass

        self.dict_learners[layer].sparse_coder = self.sparse_coders[layer]
        self.dict_learners[layer].n_atoms = self.n_atoms[layer]
        self.dict_learners[layer].fit(X)
        D = self.dict_learners[layer].D
        self.workspace.save(os.path.join("layer" + str(layer), "dict.npy"), D)
        return D
Пример #3
0
def dictionary_learn_ex():

    patch_shape = (18, 18)
    n_atoms = 225
    n_plot_atoms = 225
    n_nonzero_coefs = 2
    n_jobs = 8
    lfw_people = fetch_lfw_people(min_faces_per_person=70, resize=0.4,color=False)
    n_imgs, h, w = lfw_people.images.shape

    imgs = []
    for i in range(n_imgs):
        img = lfw_people.images[i, :, :].reshape((h, w))
        img /= 255.
        imgs.append(img)

    print 'Extracting reference patches...'
    X = extract_patches(imgs, patch_size=patch_shape[0],scale=False,n_patches=int(1e5),verbose=True,n_jobs=n_jobs)
    print "number of patches:", X.shape[1]

    se = sparse_encoder(algorithm='bomp',params={'n_nonzero_coefs': n_nonzero_coefs}, n_jobs=n_jobs)

    odc = online_dictionary_coder(n_atoms=n_atoms, sparse_coder=se, n_epochs=2,
                                  batch_size=1000, non_neg=False, verbose=True, n_jobs=n_jobs)
    odc.fit(X)
    D = odc.D
    plt.figure(figsize=(4.2, 4))
    for i in range(n_plot_atoms):
        plt.subplot(15, 15, i + 1)
        plt.imshow(D[:, i].reshape(patch_shape), cmap=plt.cm.gray)
        plt.subplots_adjust(left=0.0, bottom=0.0, right=1.0, top=1.0, wspace=0.0, hspace=0.0)
        plt.xticks(())
        plt.yticks(())
    plt.show()
Пример #4
0
def whitened_rgb_atoms():

    #a small dataset of images
    imgs = get_images(colored=True)

    #alternatively we could use the lfw dataset
    """
    lfw_people = fetch_lfw_people(min_faces_per_person=70, resize=0.4,color=True)
    faces = lfw_people.data
    n_imgs,h,w,n_channels = lfw_people.images.shape
    imgs = []
    for i in range(n_imgs):
        img = lfw_people.images[i,:,:,:].reshape((h,w,n_channels))
        imgs.append(img)
    """

    patch_shape = (8, 8)
    n_atoms = 100
    n_plot_atoms = 100
    n_nonzero_coefs = 1

    print 'Extracting reference patches...'
    X = extract_patches(imgs,
                        patch_size=patch_shape[0],
                        scale=False,
                        n_patches=int(5e5),
                        mem="low")
    print "number of patches:", X.shape[1]

    wn = preproc("whitening")
    from lyssa.feature_extract.preproc import local_contrast_normalization
    #apply lcn and then whiten the patches
    X = wn(local_contrast_normalization(X))
    #learn the dictionary using Batch Orthognal Matching Pursuit and KSVD
    se = sparse_encoder(algorithm='bomp',
                        params={'n_nonzero_coefs': n_nonzero_coefs},
                        n_jobs=8)
    kc = ksvd_coder(n_atoms=n_atoms,
                    sparse_coder=se,
                    init_dict="data",
                    max_iter=10,
                    verbose=True,
                    approx=False,
                    n_jobs=8)

    kc.fit(X)
    D = kc.D
    for i in range(n_atoms):
        D[:, i] = (D[:, i] - D[:, i].min()) / float(
            (D[:, i].max() - D[:, i].min()))
    #plot the learned dictionary
    plt.figure(figsize=(4.2, 4))
    for i in range(n_plot_atoms):
        plt.subplot(10, 10, i + 1)
        plt.imshow(D[:, i].reshape((patch_shape[0], patch_shape[1], 3)))
        plt.xticks(())
        plt.yticks(())
    plt.show()
Пример #5
0
def dictionary_learn_ex():

    patch_shape = (18, 18)
    n_atoms = 225
    n_plot_atoms = 225
    n_nonzero_coefs = 2
    n_jobs = 8
    lfw_people = fetch_lfw_people(min_faces_per_person=70,
                                  resize=0.4,
                                  color=False)
    n_imgs, h, w = lfw_people.images.shape

    imgs = []
    for i in range(n_imgs):
        img = lfw_people.images[i, :, :].reshape((h, w))
        img /= 255.
        imgs.append(img)

    print 'Extracting reference patches...'
    X = extract_patches(imgs,
                        patch_size=patch_shape[0],
                        scale=False,
                        n_patches=int(1e5),
                        verbose=True,
                        n_jobs=n_jobs)
    print "number of patches:", X.shape[1]

    se = sparse_encoder(algorithm='bomp',
                        params={'n_nonzero_coefs': n_nonzero_coefs},
                        n_jobs=n_jobs)

    odc = online_dictionary_coder(n_atoms=n_atoms,
                                  sparse_coder=se,
                                  n_epochs=2,
                                  batch_size=1000,
                                  non_neg=False,
                                  verbose=True,
                                  n_jobs=n_jobs)
    odc.fit(X)
    D = odc.D
    plt.figure(figsize=(4.2, 4))
    for i in range(n_plot_atoms):
        plt.subplot(15, 15, i + 1)
        plt.imshow(D[:, i].reshape(patch_shape), cmap=plt.cm.gray)
        plt.subplots_adjust(left=0.0,
                            bottom=0.0,
                            right=1.0,
                            top=1.0,
                            wspace=0.0,
                            hspace=0.0)
        plt.xticks(())
        plt.yticks(())
    plt.show()
Пример #6
0
def whitened_rgb_atoms():

    # a small dataset of images
    imgs = get_images(colored=True)

    # alternatively we could use the lfw dataset
    """
    lfw_people = fetch_lfw_people(min_faces_per_person=70, resize=0.4,color=True)
    faces = lfw_people.data
    n_imgs,h,w,n_channels = lfw_people.images.shape
    imgs = []
    for i in range(n_imgs):
        img = lfw_people.images[i,:,:,:].reshape((h,w,n_channels))
        imgs.append(img)
    """

    patch_shape = (8, 8)
    n_atoms = 100
    n_plot_atoms = 100
    n_nonzero_coefs = 1

    print 'Extracting reference patches...'
    X = extract_patches(imgs, patch_size=patch_shape[0], scale=False, n_patches=int(5e5), mem="low")
    print "number of patches:", X.shape[1]

    wn = preproc("whitening")
    from lyssa.feature_extract.preproc import local_contrast_normalization
    # apply lcn and then whiten the patches
    X = wn(local_contrast_normalization(X))
    # learn the dictionary using Batch Orthognal Matching Pursuit and KSVD
    se = sparse_encoder(algorithm='bomp', params={'n_nonzero_coefs': n_nonzero_coefs}, n_jobs=8)
    kc = ksvd_coder(n_atoms=n_atoms, sparse_coder=se, init_dict="data",
                    max_iter=3, verbose=True, approx=False, n_jobs=8)

    kc.fit(X)
    D = kc.D
    for i in range(n_atoms):
        D[:, i] = (D[:, i] - D[:, i].min()) / float((D[:, i].max() - D[:, i].min()))
    # plot the learned dictionary
    plt.figure(figsize=(4.2, 4))
    for i in range(n_plot_atoms):
        plt.subplot(10, 10, i + 1)
        plt.imshow(D[:, i].reshape((patch_shape[0], patch_shape[1], 3)))
        plt.subplots_adjust(left=0.0, bottom=0.0, right=1.0, top=1.0, wspace=0.0, hspace=0.0)
        plt.xticks(())
        plt.yticks(())
    plt.show()
Пример #7
0
    def build_feature_maps(self, layer):

        if (not self.workspace.contains(os.path.join("layer" + str(layer), "dict.npy"))) and self.pretrained_dicts[
            layer] is None:
            D = self.learn_dict(layer)
        elif self.pretrained_dicts[layer] is not None:
            D = self.pretrained_dicts[layer]
            self.workspace.save(os.path.join("layer" + str(layer), "dict.npy"), D)
        else:
            D = self.workspace.load(os.path.join("layer" + str(layer), "dict.npy"))

        if layer > 0:
            input_img_path = "layer" + str(layer) + "/imgs"
            self.imgs[layer] = online_reader(path=os.path.join(self.workspace.base_path, input_img_path), sparse="3D",
                                             prefix="img", suffix="npz")
        feature_maps_path = "layer" + str(layer) + "/feature_maps"
        os.path.join("layer" + str(layer), "feature_map_dims.npy")

        if not os.path.exists(
                os.path.join(self.workspace.base_path, os.path.join("layer" + str(layer), "feature_map_dims.npy"))):
            if not os.path.exists(os.path.join(self.workspace.base_path, feature_maps_path)):
                os.makedirs(os.path.join(self.workspace.base_path, feature_maps_path))
            n_imgs = len(self.imgs[layer])
            is_grayscale = len(self.imgs[layer][0].shape) == 2
            patch_size = self.filter_sizes[layer]
            idxs = [(len(str(n_imgs)) - len(str(i))) * '0' + str(i) for i in range(n_imgs)]
            if layer == 0:
                batch_size = 100
            if layer > 0:
                batch_size = 20
            from lyssa.utils import gen_batches
            batch_idxs = gen_batches(n_imgs, batch_size=batch_size)
            n_batches = len(batch_idxs)
            feature_map_dims = np.zeros((n_imgs, 2))
            for i in range(n_batches):
                sys.stdout.write("\r" + "building feature maps" + ":%3.2f%%" % ((i / float(n_batches)) * 100))
                sys.stdout.flush()
                X, patch_sizes = extract_patches(self.imgs[layer][batch_idxs[i]],
                                                 step_size=self.step_sizes[layer], patch_size=self.filter_sizes[layer],
                                                 mmap=self.mmap, n_jobs=self.n_jobs, scale=False, verbose=False,
                                                 mem="high")

                if layer == 0:
                    # scale to [0,1]
                    X /= 255.
                elif layer > 0:
                    # do contrast normalization
                    pass

                Z = self.feature_encoders[layer].encode(X, D)
                start = 0
                for l, j in enumerate(batch_idxs[i]):
                    img = self.imgs[layer][j]

                    h, w = img.shape[:2]
                    ph, pw = compute_n_patches(h, w, self.filter_sizes[layer], self.step_sizes[layer], padding=False)
                    end = start + patch_sizes[l]
                    fmap_path = os.path.join(self.workspace.base_path, os.path.join(feature_maps_path, "img" + idxs[j]))
                    multiprocessing.Process(target=save_feature_map_proc, args=(fmap_path, Z[:, start:end])).start()
                    start += patch_sizes[l]
                    feature_map_dims[j, 0] = ph
                    feature_map_dims[j, 1] = pw

        self.workspace.save(os.path.join("layer" + str(layer), "feature_map_dims.npy"), feature_map_dims.astype(int))
        self.feature_maps[layer] = self.workspace.load(feature_maps_path, sparse="2D", online=True)
Пример #8
0
    def build_feature_maps(self, layer):

        if (not self.workspace.contains(os.path.join("layer" + str(layer), "dict.npy"))) and \
                        self.pretrained_dicts[layer] is None:
            D = self.learn_dict(layer)
        elif self.pretrained_dicts[layer] is not None:
            D = self.pretrained_dicts[layer]
            self.workspace.save(os.path.join("layer" + str(layer), "dict.npy"), D)
        else:
            D = self.workspace.load(os.path.join("layer" + str(layer), "dict.npy"))

        if layer > 0:
            input_img_path = "layer" + str(layer) + "/imgs"
            self.imgs[layer] = online_reader(path=os.path.join(self.workspace.base_path, input_img_path), sparse="3D",
                                             prefix="img", suffix="npz")
        feature_maps_path = "layer" + str(layer) + "/feature_maps"
        os.path.join("layer" + str(layer), "feature_map_dims.npy")

        if not os.path.exists(
                os.path.join(self.workspace.base_path, os.path.join("layer" + str(layer), "feature_map_dims.npy"))):
            if not os.path.exists(os.path.join(self.workspace.base_path, feature_maps_path)):
                os.makedirs(os.path.join(self.workspace.base_path, feature_maps_path))
            n_imgs = len(self.imgs[layer])
            idxs = [(len(str(n_imgs)) - len(str(i))) * '0' + str(i) for i in range(n_imgs)]
            if layer == 0:
                batch_size = 100
            if layer > 0:
                batch_size = 20
            from lyssa.utils import gen_batches
            batch_idxs = gen_batches(n_imgs, batch_size=batch_size)
            n_batches = len(batch_idxs)
            feature_map_dims = np.zeros((n_imgs, 2))
            for i in range(n_batches):
                sys.stdout.write("\r" + "building feature maps" + ":%3.2f%%" % ((i / float(n_batches)) * 100))
                sys.stdout.flush()
                X, patch_sizes = extract_patches(self.imgs[layer][batch_idxs[i]],
                                                 step_size=self.step_sizes[layer], patch_size=self.filter_sizes[layer],
                                                 mmap=self.mmap, n_jobs=self.n_jobs, scale=False, verbose=False,
                                                 mem="high")

                if layer == 0:
                    # scale to [0,1]
                    X /= 255.
                elif layer > 0:
                    # do contrast normalization
                    pass

                Z = self.feature_encoders[layer].encode(X, D)
                start = 0
                for l, j in enumerate(batch_idxs[i]):
                    img = self.imgs[layer][j]

                    h, w = img.shape[:2]
                    ph, pw = compute_n_patches(h, w, self.filter_sizes[layer], self.step_sizes[layer], padding=False)
                    end = start + patch_sizes[l]
                    fmap_path = os.path.join(self.workspace.base_path, os.path.join(feature_maps_path, "img" + idxs[j]))
                    multiprocessing.Process(target=save_feature_map_proc, args=(fmap_path, Z[:, start:end])).start()
                    start += patch_sizes[l]
                    feature_map_dims[j, 0] = ph
                    feature_map_dims[j, 1] = pw

        self.workspace.save(os.path.join("layer" + str(layer), "feature_map_dims.npy"), feature_map_dims.astype(int))
        self.feature_maps[layer] = self.workspace.load(feature_maps_path, sparse="2D", online=True)