本文整理汇总了Python中menpo.model.PCAModel.trim_components方法的典型用法代码示例。如果您正苦于以下问题:Python PCAModel.trim_components方法的具体用法?Python PCAModel.trim_components怎么用?Python PCAModel.trim_components使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类menpo.model.PCAModel
的用法示例。
在下文中一共展示了PCAModel.trim_components方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _build_shape_model
# 需要导入模块: from menpo.model import PCAModel [as 别名]
# 或者: from menpo.model.PCAModel import trim_components [as 别名]
def _build_shape_model(shapes, max_components):
r"""
Builds a shape model given a set of shapes.
Parameters
----------
shapes: list of :map:`PointCloud`
The set of shapes from which to build the model.
max_components: None or int or float
Specifies the number of components of the trained shape model.
If int, it specifies the exact number of components to be retained.
If float, it specifies the percentage of variance to be retained.
If None, all the available components are kept (100% of variance).
Returns
-------
shape_model: :class:`menpo.model.pca`
The PCA shape model.
"""
# build shape model
shape_model = PCAModel(shapes)
if max_components is not None:
# trim shape model if required
shape_model.trim_components(max_components)
return shape_model
示例2: test_pca_trim
# 需要导入模块: from menpo.model import PCAModel [as 别名]
# 或者: from menpo.model.PCAModel import trim_components [as 别名]
def test_pca_trim():
samples = [PointCloud(np.random.randn(10)) for _ in range(10)]
model = PCAModel(samples)
# trim components
model.trim_components(5)
# number of active components should be the same as number of components
assert_equal(model.n_active_components, model.n_components)
示例3: _build_shape_model
# 需要导入模块: from menpo.model import PCAModel [as 别名]
# 或者: from menpo.model.PCAModel import trim_components [as 别名]
def _build_shape_model(cls, shapes, max_components):
r"""
Builds a shape model given a set of shapes.
Parameters
----------
shapes: list of :map:`PointCloud`
The set of shapes from which to build the model.
max_components: None or int or float
Specifies the number of components of the trained shape model.
If int, it specifies the exact number of components to be retained.
If float, it specifies the percentage of variance to be retained.
If None, all the available components are kept (100% of variance).
Returns
-------
shape_model: :class:`menpo.model.pca`
The PCA shape model.
"""
# centralize shapes
centered_shapes = [Translation(-s.centre()).apply(s) for s in shapes]
# align centralized shape using Procrustes Analysis
gpa = GeneralizedProcrustesAnalysis(centered_shapes)
aligned_shapes = [s.aligned_source() for s in gpa.transforms]
# build shape model
shape_model = PCAModel(aligned_shapes)
if max_components is not None:
# trim shape model if required
shape_model.trim_components(max_components)
return shape_model
示例4: __init__
# 需要导入模块: from menpo.model import PCAModel [as 别名]
# 或者: from menpo.model.PCAModel import trim_components [as 别名]
def __init__(self, data, max_n_components=None):
if isinstance(data, PCAModel):
shape_model = data
else:
aligned_shapes = align_shapes(data)
shape_model = PCAModel(aligned_shapes)
if max_n_components is not None:
shape_model.trim_components(max_n_components)
super(PDM, self).__init__(shape_model)
# Default target is the mean
self._target = self.model.mean()
示例5: test_pca_variance_after_trim
# 需要导入模块: from menpo.model import PCAModel [as 别名]
# 或者: from menpo.model.PCAModel import trim_components [as 别名]
def test_pca_variance_after_trim():
samples = [PointCloud(np.random.randn(10)) for _ in range(10)]
model = PCAModel(samples)
# set number of active components
model.trim_components(5)
# kept variance must be smaller than total variance
assert(model.variance() < model.original_variance())
# kept variance ratio must be smaller than 1.0
assert(model.variance_ratio() < 1.0)
# noise variance must be bigger than 0.0
assert(model.noise_variance() > 0.0)
# noise variance ratio must also be bigger than 0.0
assert(model.noise_variance_ratio() > 0.0)
# inverse noise variance is computable
assert(model.inverse_noise_variance() == 1 / model.noise_variance())
示例6: _build_appearance_model_full
# 需要导入模块: from menpo.model import PCAModel [as 别名]
# 或者: from menpo.model.PCAModel import trim_components [as 别名]
def _build_appearance_model_full(all_patches, n_appearance_parameters,
level_str, verbose):
# build appearance model
if verbose:
print_dynamic('{}Training appearance distribution'.format(level_str))
# apply pca
appearance_model = PCAModel(all_patches)
# trim components
if n_appearance_parameters is not None:
appearance_model.trim_components(n_appearance_parameters)
# get mean appearance vector
app_mean = appearance_model.mean().as_vector()
# compute covariance matrix
app_cov = appearance_model.components.T.dot(np.diag(1/appearance_model.eigenvalues)).dot(appearance_model.components)
return app_mean, app_cov
示例7: _build_appearance_model_full_yorgos
# 需要导入模块: from menpo.model import PCAModel [as 别名]
# 或者: from menpo.model.PCAModel import trim_components [as 别名]
def _build_appearance_model_full_yorgos(all_patches, n_appearance_parameters,
patches_image_shape, level_str, verbose):
# build appearance model
if verbose:
print_dynamic('{}Training appearance distribution'.format(level_str))
# get mean appearance vector
n_images = len(all_patches)
tmp = np.empty(patches_image_shape + (n_images,))
for c, i in enumerate(all_patches):
tmp[..., c] = i.pixels
app_mean = np.mean(tmp, axis=-1)
# apply pca
appearance_model = PCAModel(all_patches)
# trim components
if n_appearance_parameters is not None:
appearance_model.trim_components(n_appearance_parameters)
# compute covariance matrix
app_cov = np.eye(appearance_model.n_features, appearance_model.n_features) - appearance_model.components.T.dot(appearance_model.components)
return app_mean, app_cov
示例8: _build_appearance_model_full
# 需要导入模块: from menpo.model import PCAModel [as 别名]
# 或者: from menpo.model.PCAModel import trim_components [as 别名]
def _build_appearance_model_full(all_patches, n_appearance_parameters,
patches_len, level_str, verbose):
# build appearance model
if verbose:
print_dynamic('{}Training appearance distribution'.format(level_str))
# get mean appearance vector
n_images = len(all_patches)
tmp = np.empty((patches_len, n_images))
for c, i in enumerate(all_patches):
tmp[..., c] = vectorize_patches_image(i)
app_mean = np.mean(tmp, axis=1)
# apply pca
appearance_model = PCAModel(all_patches)
# trim components
if n_appearance_parameters is not None:
appearance_model.trim_components(n_appearance_parameters)
# compute covariance matrix
app_cov = appearance_model.components.T.dot(np.diag(1/appearance_model.eigenvalues)).dot(appearance_model.components)
return app_mean, app_cov
示例9: test_pca_trim_negative_float
# 需要导入模块: from menpo.model import PCAModel [as 别名]
# 或者: from menpo.model.PCAModel import trim_components [as 别名]
def test_pca_trim_negative_float():
samples = [PointCloud(np.random.randn(10)) for _ in range(10)]
model = PCAModel(samples)
# no negative number of components
model.trim_components(-2)
示例10: build
# 需要导入模块: from menpo.model import PCAModel [as 别名]
# 或者: from menpo.model.PCAModel import trim_components [as 别名]
#.........这里部分代码省略.........
print_dynamic('- Building model\n')
shape_models = []
appearance_models = []
# for each pyramid level (high --> low)
for j in range(self.n_levels):
# since models are built from highest to lowest level, the
# parameters in form of list need to use a reversed index
rj = self.n_levels - j - 1
if verbose:
level_str = ' - '
if self.n_levels > 1:
level_str = ' - Level {}: '.format(j + 1)
# get feature images of current level
feature_images = []
for c, g in enumerate(generators):
if verbose:
print_dynamic(
'{}Computing feature space/rescaling - {}'.format(
level_str,
progress_bar_str((c + 1.) / len(generators),
show_bar=False)))
feature_images.append(next(g))
# extract potentially rescaled shapes
shapes = [i.landmarks[group][label] for i in feature_images]
# define shapes that will be used for training
if j == 0:
original_shapes = shapes
train_shapes = shapes
else:
if self.scaled_shape_models:
train_shapes = shapes
else:
train_shapes = original_shapes
# train shape model and find reference frame
if verbose:
print_dynamic('{}Building shape model'.format(level_str))
shape_model = build_shape_model(
train_shapes, self.max_shape_components[rj])
reference_frame = self._build_reference_frame(shape_model.mean())
# add shape model to the list
shape_models.append(shape_model)
# compute transforms
if verbose:
print_dynamic('{}Computing transforms'.format(level_str))
# Create a dummy initial transform
s_to_t_transform = self.transform(
reference_frame.landmarks['source'].lms,
reference_frame.landmarks['source'].lms)
# warp images to reference frame
warped_images = []
for c, i in enumerate(feature_images):
if verbose:
print_dynamic('{}Warping images - {}'.format(
level_str,
progress_bar_str(float(c + 1) / len(feature_images),
show_bar=False)))
# Setting the target can be significantly faster for transforms
# such as CachedPiecewiseAffine
s_to_t_transform.set_target(i.landmarks[group][label])
warped_images.append(i.warp_to_mask(reference_frame.mask,
s_to_t_transform))
# attach reference_frame to images' source shape
for i in warped_images:
i.landmarks['source'] = reference_frame.landmarks['source']
# build appearance model
if verbose:
print_dynamic('{}Building appearance model'.format(level_str))
appearance_model = PCAModel(warped_images)
# trim appearance model if required
if self.max_appearance_components[rj] is not None:
appearance_model.trim_components(
self.max_appearance_components[rj])
# add appearance model to the list
appearance_models.append(appearance_model)
if verbose:
print_dynamic('{}Done\n'.format(level_str))
# reverse the list of shape and appearance models so that they are
# ordered from lower to higher resolution
shape_models.reverse()
appearance_models.reverse()
n_training_images = len(images)
return self._build_aam(shape_models, appearance_models,
n_training_images)
示例11: build
# 需要导入模块: from menpo.model import PCAModel [as 别名]
# 或者: from menpo.model.PCAModel import trim_components [as 别名]
def build(self, images, group=None, label=None, verbose=False, **kwargs):
# compute reference shape
reference_shape = self._compute_reference_shape(images, group, label,
verbose)
# normalize images
images = self._normalize_images(images, group, label, reference_shape,
verbose)
# build models at each scale
if verbose:
print_dynamic('- Building models\n')
shape_models = []
appearance_models = []
classifiers = []
# for each pyramid level (high --> low)
for j, s in enumerate(self.scales):
if verbose:
if len(self.scales) > 1:
level_str = ' - Level {}: '.format(j)
else:
level_str = ' - '
# obtain image representation
if j == 0:
# compute features at highest level
feature_images = self._compute_features(images, level_str,
verbose)
level_images = feature_images
elif self.scale_features:
# scale features at other levels
level_images = self._scale_images(feature_images, s,
level_str, verbose)
else:
# scale images and compute features at other levels
scaled_images = self._scale_images(images, s, level_str,
verbose)
level_images = self._compute_features(scaled_images,
level_str, verbose)
# extract potentially rescaled shapes ath highest level
level_shapes = [i.landmarks[group][label]
for i in level_images]
# obtain shape representation
if j == 0 or self.scale_shapes:
# obtain shape model
if verbose:
print_dynamic('{}Building shape model'.format(level_str))
shape_model = self._build_shape_model(
level_shapes, self.max_shape_components)
# add shape model to the list
shape_models.append(shape_model)
else:
# copy precious shape model and add it to the list
shape_models.append(deepcopy(shape_model))
# obtain warped images
warped_images = self._warp_images(level_images, level_shapes,
shape_model.mean(), level_str,
verbose)
# obtain appearance model
if verbose:
print_dynamic('{}Building appearance model'.format(level_str))
appearance_model = PCAModel(warped_images)
# trim appearance model if required
if self.max_appearance_components is not None:
appearance_model.trim_components(
self.max_appearance_components)
# add appearance model to the list
appearance_models.append(appearance_model)
if isinstance(self, GlobalUnifiedBuilder):
# obtain parts images
parts_images = self._parts_images(level_images, level_shapes,
level_str, verbose)
else:
# parts images are warped images
parts_images = warped_images
# build desired responses
mvn = multivariate_normal(mean=np.zeros(2), cov=self.covariance)
grid = build_sampling_grid(self.parts_shape)
Y = [mvn.pdf(grid + offset) for offset in self.offsets]
# build classifiers
n_landmarks = level_shapes[0].n_points
level_classifiers = []
for l in range(n_landmarks):
if verbose:
print_dynamic('{}Building classifiers - {}'.format(
level_str,
progress_bar_str((l + 1.) / n_landmarks,
show_bar=False)))
X = [i.pixels[l] for i in parts_images]
clf = self.classifier(X, Y, **kwargs)
level_classifiers.append(clf)
#.........这里部分代码省略.........
示例12: aam_builder
# 需要导入模块: from menpo.model import PCAModel [as 别名]
# 或者: from menpo.model.PCAModel import trim_components [as 别名]
#.........这里部分代码省略.........
shapes = [i.landmarks[group][label].lms for i in images]
reference_shape = mean_pointcloud(shapes)
if diagonal_range:
x, y = reference_shape.range()
scale = diagonal_range / np.sqrt(x**2 + y**2)
Scale(scale, reference_shape.n_dims).apply_inplace(reference_shape)
images = [i.rescale_to_reference_shape(reference_shape, group=group,
label=label,
interpolator=interpolator)
for i in images]
if scaled_reference_frames:
print '- Setting gaussian smoothing generators'
generator = [i.smoothing_pyramid(n_levels=n_levels,
downscale=downscale)
for i in images]
else:
print '- Setting gaussian pyramid generators'
generator = [i.gaussian_pyramid(n_levels=n_levels,
downscale=downscale)
for i in images]
print '- Building model pyramids'
shape_models = []
appearance_models = []
# for each level
for j in np.arange(n_levels):
print ' - Level {}'.format(j)
print ' - Computing feature_type'
images = [compute_features(g.next(), feature_type) for g in generator]
# extract potentially rescaled shapes
shapes = [i.landmarks[group][label].lms for i in images]
if scaled_reference_frames or j == 0:
print ' - Building shape model'
if j != 0:
shapes = [Scale(1/downscale, n_dims=shapes[0].n_dims).apply(s)
for s in shapes]
# centralize shapes
centered_shapes = [Translation(-s.centre).apply(s) for s in shapes]
# align centralized shape using Procrustes Analysis
gpa = GeneralizedProcrustesAnalysis(centered_shapes)
aligned_shapes = [s.aligned_source for s in gpa.transforms]
# build shape model
shape_model = PCAModel(aligned_shapes)
if max_shape_components is not None:
# trim shape model if required
shape_model.trim_components(max_shape_components)
print ' - Building reference frame'
mean_shape = mean_pointcloud(aligned_shapes)
if patch_size is not None:
# build patch based reference frame
reference_frame = build_patch_reference_frame(
mean_shape, boundary=boundary, patch_size=patch_size)
else:
# build reference frame
reference_frame = build_reference_frame(
mean_shape, boundary=boundary, trilist=trilist)
# add shape model to the list
shape_models.append(shape_model)
print ' - Computing transforms'
transforms = [transform_cls(reference_frame.landmarks['source'].lms,
i.landmarks[group][label].lms)
for i in images]
print ' - Warping images'
images = [i.warp_to(reference_frame.mask, t,
interpolator=interpolator)
for i, t in zip(images, transforms)]
for i in images:
i.landmarks['source'] = reference_frame.landmarks['source']
if patch_size:
for i in images:
i.build_mask_around_landmarks(patch_size, group='source')
else:
for i in images:
i.constrain_mask_to_landmarks(group='source', trilist=trilist)
print ' - Building appearance model'
appearance_model = PCAModel(images)
# trim appearance model if required
if max_appearance_components is not None:
appearance_model.trim_components(max_appearance_components)
# add appearance model to the list
appearance_models.append(appearance_model)
# reverse the list of shape and appearance models so that they are
# ordered from lower to higher resolution
shape_models.reverse()
appearance_models.reverse()
return AAM(shape_models, appearance_models, transform_cls, feature_type,
reference_shape, downscale, patch_size, interpolator)
示例13: build
# 需要导入模块: from menpo.model import PCAModel [as 别名]
# 或者: from menpo.model.PCAModel import trim_components [as 别名]
def build(self, images, group=None, label=None, verbose=False):
# compute reference shape
reference_shape = self._compute_reference_shape(images, group, label,
verbose)
# normalize images
images = self._normalize_images(images, group, label, reference_shape,
verbose)
# build models at each scale
if verbose:
print_dynamic('- Building models\n')
shape_models = []
appearance_models = []
# for each pyramid level (high --> low)
for j, s in enumerate(self.scales):
if verbose:
if len(self.scales) > 1:
level_str = ' - Level {}: '.format(j)
else:
level_str = ' - '
# obtain image representation
if j == 0:
# compute features at highest level
feature_images = self._compute_features(images, level_str,
verbose)
level_images = feature_images
elif self.scale_features:
# scale features at other levels
level_images = self._scale_images(feature_images, s,
level_str, verbose)
else:
# scale images and compute features at other levels
scaled_images = self._scale_images(images, s, level_str,
verbose)
level_images = self._compute_features(scaled_images,
level_str, verbose)
# extract potentially rescaled shapes ath highest level
level_shapes = [i.landmarks[group][label]
for i in level_images]
# obtain shape representation
if j == 0 or self.scale_shapes:
# obtain shape model
if verbose:
print_dynamic('{}Building shape model'.format(level_str))
shape_model = self._build_shape_model(
level_shapes, self.max_shape_components)
# add shape model to the list
shape_models.append(shape_model)
else:
# copy precious shape model and add it to the list
shape_models.append(deepcopy(shape_model))
# obtain warped images
warped_images = self._warp_images(level_images, level_shapes,
shape_model.mean, level_str,
verbose)
# obtain appearance model
if verbose:
print_dynamic('{}Building appearance model'.format(level_str))
appearance_model = PCAModel(warped_images)
# trim appearance model if required
if self.max_appearance_components is not None:
appearance_model.trim_components(
self.max_appearance_components)
# add appearance model to the list
appearance_models.append(appearance_model)
if verbose:
print_dynamic('{}Done\n'.format(level_str))
# reverse the list of shape and appearance models so that they are
# ordered from lower to higher resolution
shape_models.reverse()
appearance_models.reverse()
self.scales.reverse()
aam = self._build_aam(shape_models, appearance_models, reference_shape)
return aam
示例14: _train
# 需要导入模块: from menpo.model import PCAModel [as 别名]
# 或者: from menpo.model.PCAModel import trim_components [as 别名]
def _train(self, images, group=None, verbose=False):
checks.check_landmark_trilist(images[0], self.transform, group=group)
self.reference_shape = compute_reference_shape(
[i.landmarks[group] for i in images],
self.diagonal, verbose=verbose)
# normalize images
images = rescale_images_to_reference_shape(
images, group, self.reference_shape, verbose=verbose)
if self.sigma:
images = [fsmooth(i, self.sigma) for i in images]
# Build models at each scale
if verbose:
print_dynamic('- Building models\n')
feature_images = []
# for each scale (low --> high)
for j in range(self.n_scales):
if verbose:
if len(self.scales) > 1:
scale_prefix = ' - Scale {}: '.format(j)
else:
scale_prefix = ' - '
else:
scale_prefix = None
# Handle holistic features
if j == 0 and self.holistic_features[j] == no_op:
# Saves a lot of memory
feature_images = images
elif j == 0 or self.holistic_features[j] is not self.holistic_features[j - 1]:
# Compute features only if this is the first pass through
# the loop or the features at this scale are different from
# the features at the previous scale
feature_images = compute_features(images,
self.holistic_features[j],
prefix=scale_prefix,
verbose=verbose)
# handle scales
if self.scales[j] != 1:
# Scale feature images only if scale is different than 1
scaled_images = scale_images(feature_images, self.scales[j],
prefix=scale_prefix,
verbose=verbose)
else:
scaled_images = feature_images
# Extract potentially rescaled shapes
scale_shapes = [i.landmarks[group] for i in scaled_images]
# Build the shape model
if verbose:
print_dynamic('{}Building shape model'.format(scale_prefix))
shape_model = self._build_shape_model(scale_shapes, j)
self.shape_models.append(shape_model)
# Obtain warped images - we use a scaled version of the
# reference shape, computed here. This is because the mean
# moves when we are incrementing, and we need a consistent
# reference frame.
scaled_reference_shape = Scale(self.scales[j], n_dims=2).apply(
self.reference_shape)
warped_images = self._warp_images(scaled_images, scale_shapes,
scaled_reference_shape,
j, scale_prefix, verbose)
# obtain appearance model
if verbose:
print_dynamic('{}Building appearance model'.format(
scale_prefix))
appearance_model = PCAModel(warped_images)
# trim appearance model if required
if self.max_appearance_components[j] is not None:
appearance_model.trim_components(
self.max_appearance_components[j])
# add appearance model to the list
self.appearance_models.append(appearance_model)
expert_ensemble = self.expert_ensemble_cls[j](
images=scaled_images, shapes=scale_shapes,
patch_shape=self.patch_shape[j],
patch_normalisation=self.patch_normalisation,
cosine_mask=self.cosine_mask,
context_shape=self.context_shape[j],
sample_offsets=self.sample_offsets,
prefix=scale_prefix, verbose=verbose)
self.expert_ensembles.append(expert_ensemble)
if verbose:
print_dynamic('{}Done\n'.format(scale_prefix))
示例15: test_pca_trim_variance_limit
# 需要导入模块: from menpo.model import PCAModel [as 别名]
# 或者: from menpo.model.PCAModel import trim_components [as 别名]
def test_pca_trim_variance_limit():
samples = [PointCloud(np.random.randn(10)) for _ in range(10)]
model = PCAModel(samples)
# impossible to keep more than 1.0 ratio variance
model.trim_components(2.5)