def _train(self, images, increment=False, group=None, bounding_box_group_glob=None, verbose=False, batch_size=None): r""" """ # If batch_size is not None, then we may have a generator, else we # assume we have a list. # If batch_size is not None, then we may have a generator, else we # assume we have a list. if batch_size is not None: # Create a generator of fixed sized batches. Will still work even # on an infinite list. image_batches = batch(images, batch_size) else: image_batches = [list(images)] for k, image_batch in enumerate(image_batches): if k == 0: if self.reference_shape is None: # If no reference shape was given, use the mean of the first # batch if batch_size is not None: warnings.warn( 'No reference shape was provided. The ' 'mean of the first batch will be the ' 'reference shape. If the batch mean is ' 'not representative of the true mean, ' 'this may cause issues.', MenpoFitBuilderWarning) self.reference_shape = compute_reference_shape( [i.landmarks[group].lms for i in image_batch], self.diagonal, verbose=verbose) # We set landmarks on the images to archive the perturbations, so # when the default 'None' is used, we need to grab the actual # label to sort out the ambiguity if group is None: group = image_batch[0].landmarks.group_labels[0] # After the first batch, we are incrementing the model if k > 0: increment = True if verbose: print('Computing batch {}'.format(k)) # Train each batch self._train_batch(image_batch, increment=increment, group=group, bounding_box_group_glob=bounding_box_group_glob, verbose=verbose)
def _train( self, template, shapes, increment=False, group=None, shape_forgetting_factor=1.0, verbose=False, batch_size=None ): r""" """ # If batch_size is not None, then we may have a generator, else we # assume we have a list. # If batch_size is not None, then we may have a generator, else we # assume we have a list. if batch_size is not None: # Create a generator of fixed sized batches. Will still work even # on an infinite list. shape_batches = batch(shapes, batch_size) else: shape_batches = [list(shapes)] for k, shape_batch in enumerate(shape_batches): if k == 0: # Rescale the template the reference shape if self.reference_shape is None: # If no reference shape was given, use the mean of the first # batch if batch_size is not None: warnings.warn( "No reference shape was provided. The " "mean of the first batch will be the " "reference shape. If the batch mean is " "not representative of the true mean, " "this may cause issues.", MenpoFitBuilderWarning, ) checks.check_trilist(shape_batch[0], self.transform) self.reference_shape = compute_reference_shape(shape_batch, self.diagonal, verbose=verbose) # Rescale the template the reference shape template = template.rescale_to_pointcloud(self.reference_shape, group=group) # After the first batch, we are incrementing the model if k > 0: increment = True if verbose: print("Computing batch {}".format(k)) # Train each batch self._train_batch( template, shape_batch, increment=increment, group=group, shape_forgetting_factor=shape_forgetting_factor, verbose=verbose, )
def _train(self, images, increment=False, group=None, bounding_box_group_glob=None, verbose=False, batch_size=None): r""" """ # If batch_size is not None, then we may have a generator, else we # assume we have a list. # If batch_size is not None, then we may have a generator, else we # assume we have a list. if batch_size is not None: # Create a generator of fixed sized batches. Will still work even # on an infinite list. image_batches = batch(images, batch_size) else: image_batches = [list(images)] for k, image_batch in enumerate(image_batches): if k == 0: if self.reference_shape is None: # If no reference shape was given, use the mean of the first # batch if batch_size is not None: warnings.warn( "No reference shape was provided. The " "mean of the first batch will be the " "reference shape. If the batch mean is " "not representative of the true mean, " "this may cause issues.", MenpoFitBuilderWarning, ) self.reference_shape = compute_reference_shape( [i.landmarks[group].lms for i in image_batch], self.diagonal, verbose=verbose ) # We set landmarks on the images to archive the perturbations, so # when the default 'None' is used, we need to grab the actual # label to sort out the ambiguity if group is None: group = image_batch[0].landmarks.group_labels[0] # After the first batch, we are incrementing the model if k > 0: increment = True if verbose: print("Computing batch {}".format(k)) # Train each batch self._train_batch( image_batch, increment=increment, group=group, bounding_box_group_glob=bounding_box_group_glob, verbose=verbose, )
def _train(self, images, increment=False, group=None, verbose=False, shape_forgetting_factor=1.0, batch_size=None): r""" """ # If batch_size is not None, then we may have a generator, else we # assume we have a list. # If batch_size is not None, then we may have a generator, else we # assume we have a list. if batch_size is not None: # Create a generator of fixed sized batches. Will still work even # on an infinite list. image_batches = batch(images, batch_size) else: image_batches = [list(images)] for k, image_batch in enumerate(image_batches): if k == 0: if self.reference_shape is None: # If no reference shape was given, use the mean of the first # batch if batch_size is not None: warnings.warn( 'No reference shape was provided. The ' 'mean of the first batch will be the ' 'reference shape. If the batch mean is ' 'not representative of the true mean, ' 'this may cause issues.', MenpoFitBuilderWarning) self.reference_shape = compute_reference_shape( [i.landmarks[group].lms for i in image_batch], self.diagonal, verbose=verbose) # After the first batch, we are incrementing the model if k > 0: increment = True if verbose: print('Computing batch {}'.format(k)) # Train each batch self._train_batch(image_batch, increment=increment, group=group, shape_forgetting_factor=shape_forgetting_factor, verbose=verbose)
def _train(self, images, increment=False, group=None, shape_forgetting_factor=1.0, appearance_forgetting_factor=1.0, verbose=False, batch_size=None): r""" """ # If batch_size is not None, then we may have a generator, else we # assume we have a list. # If batch_size is not None, then we may have a generator, else we # assume we have a list. if batch_size is not None: # Create a generator of fixed sized batches. Will still work even # on an infinite list. image_batches = batch(images, batch_size) else: image_batches = [list(images)] for k, image_batch in enumerate(image_batches): if k == 0: if self.reference_shape is None: # If no reference shape was given, use the mean of the first # batch if batch_size is not None: warnings.warn('No reference shape was provided. The ' 'mean of the first batch will be the ' 'reference shape. If the batch mean is ' 'not representative of the true mean, ' 'this may cause issues.', MenpoFitBuilderWarning) checks.check_landmark_trilist(image_batch[0], self.transform, group=group) self.reference_shape = compute_reference_shape( [i.landmarks[group].lms for i in image_batch], self.diagonal, verbose=verbose) # After the first batch, we are incrementing the model if k > 0: increment = True if verbose: print('Computing batch {}'.format(k)) # Train each batch self._train_batch( image_batch, increment=increment, group=group, shape_forgetting_factor=shape_forgetting_factor, appearance_forgetting_factor=appearance_forgetting_factor, verbose=verbose)
def _train(self, template, shapes, increment=False, group=None, shape_forgetting_factor=1.0, verbose=False, batch_size=None): # If batch_size is not None, then we may have a generator, else we # assume we have a list. if batch_size is not None: # Create a generator of fixed sized batches. Will still work even # on an infinite list. shape_batches = batch(shapes, batch_size) else: shape_batches = [list(shapes)] for k, shape_batch in enumerate(shape_batches): if k == 0: # Rescale the template the reference shape if self.reference_shape is None: # If no reference shape was given, use the mean of the first # batch if batch_size is not None: warnings.warn('No reference shape was provided. The ' 'mean of the first batch will be the ' 'reference shape. If the batch mean is ' 'not representative of the true mean, ' 'this may cause issues.', MenpoFitBuilderWarning) checks.check_trilist(shape_batch[0], self.transform) self.reference_shape = compute_reference_shape( shape_batch, self.diagonal, verbose=verbose) # Rescale the template the reference shape template = template.rescale_to_pointcloud( self.reference_shape, group=group) # After the first batch, we are incrementing the model if k > 0: increment = True if verbose: print('Computing batch {}'.format(k)) # Train each batch self._train_batch(template, shape_batch, increment=increment, group=group, shape_forgetting_factor=shape_forgetting_factor, verbose=verbose)