Exemple #1
0
def img_pre(path_to_images,
            save_dir=None,
            propotion=0.2,
            scale=400.0,
            greyscale=False):
    import menpo.io as mio
    from menpo.visualize import print_progress

    if save_dir is not None:
        mk_dir(save_dir, 0)

    for image_path in path_to_images:
        for img in print_progress(mio.import_images(image_path, verbose=True)):
            if greyscale:
                # convert to greyscale
                if img.n_channels == 3:
                    img = img.as_greyscale()
            # crop to landmarks bounding box with an extra 20% padding
            re_img = img.crop_to_landmarks_proportion(propotion)

            # # rescale image if its diagonal is bigger than 400 pixels
            # d = img.diagonal()
            # if d > scale:
            #     img = img.rescale(scale / d)

            # save enhanced image with lable
            img_suffix = img.path.suffix
            lb_suffix = '.pts'
            new_image_name = '%s' % img.path.name.split('.')[0]
            img_path = os.path.join(save_dir, new_image_name + img_suffix)
            lb_path = os.path.join(save_dir, new_image_name + lb_suffix)
            mio.export_image(re_img, img_path, overwrite=True)
            mio.export_landmark_file(re_img.landmarks['PTS'],
                                     lb_path,
                                     overwrite=True)
Exemple #2
0
def process_video(file, dest):
    if is_video(file):
        create_dir(os.path.dirname(dest))
        frames = mio.import_video(file, normalise=False)
        print('{} contains {} frames'.format(file, len(frames)))
        print('writing landmarks to {}...'.format(dest))
        frames = frames.map(fit_image)
        with open(dest, 'w') as outputfile:
            outwriter = csv.writer(outputfile)
            try:
                for i, frame in enumerate(print_progress(frames)):
                    if 'final_shape' not in frame.landmarks:
                        warnings.warn('no faces detected in the frame {}, '
                                      'initializing landmarks to -1s...'.format(i))
                        # dlib does not fitting from previous initial shape so
                        # leave entire row as -1s
                        # initial_shape = frames[i - 1].landmarks['final_shape'].lms
                        # fitting_result = fit_image.fitter.fit_from_shape(frame, initial_shape)
                        # frame.landmarks['final_shape'] = fitting_result.final_shape
                        landmarks = [-1] * 136
                    else:
                        lmg = frame.landmarks['final_shape']
                        landmarks = lmg['all'].points.reshape((136,)).tolist()  # reshape to 136 points
                    fill_row(outwriter, i, landmarks)
            except Exception as e:
                warnings.warn('Runtime Error at frame {}'.format(i))
                print('initializing landmarks to -1s...')
                fill_row(outwriter, i, [-1] * 136)
Exemple #3
0
def process_video(file, dest):
    if is_video(file):
        create_dir(os.path.dirname(dest))
        frames = mio.import_video(file, normalise=False)
        print('{} contains {} frames'.format(file, len(frames)))
        print('writing landmarks to {}...'.format(dest))
        frames = frames.map(fit_image)
        with open(dest, 'w') as outputfile:
            outwriter = csv.writer(outputfile)
            try:
                for i, frame in enumerate(print_progress(frames)):
                    if 'final_shape' not in frame.landmarks:
                        warnings.warn(
                            'no faces detected in the frame {}, '
                            'initializing landmarks to -1s...'.format(i))
                        # dlib does not fitting from previous initial shape so
                        # leave entire row as -1s
                        # initial_shape = frames[i - 1].landmarks['final_shape'].lms
                        # fitting_result = fit_image.fitter.fit_from_shape(frame, initial_shape)
                        # frame.landmarks['final_shape'] = fitting_result.final_shape
                        landmarks = [-1] * 136
                    else:
                        lmg = frame.landmarks['final_shape']
                        landmarks = lmg['all'].points.reshape(
                            (136, )).tolist()  # reshape to 136 points
                    fill_row(outwriter, i, landmarks)
            except Exception as e:
                warnings.warn('Runtime Error at frame {}'.format(i))
                print('initializing landmarks to -1s...')
                fill_row(outwriter, i, [-1] * 136)
Exemple #4
0
def _import_glob_lazy_list(pattern, extension_map, max_assets=None,
                           landmark_resolver=same_name, shuffle=False,
                           as_generator=False, landmark_ext_map=None,
                           landmark_attach_func=None, importer_kwargs=None,
                           verbose=False):
    filepaths = list(glob_with_suffix(pattern, extension_map,
                                      sort=(not shuffle)))
    if shuffle:
        random.shuffle(filepaths)
    if max_assets:
        filepaths = filepaths[:max_assets]
    n_files = len(filepaths)
    if n_files == 0:
        raise ValueError('The glob {} yields no assets'.format(pattern))

    lazy_list = LazyList([partial(_import, f, extension_map,
                                  landmark_resolver=landmark_resolver,
                                  landmark_ext_map=landmark_ext_map,
                                  landmark_attach_func=landmark_attach_func,
                                  importer_kwargs=importer_kwargs)
                          for f in filepaths])

    if verbose and as_generator:
        # wrap the generator with the progress reporter
        lazy_list = print_progress(lazy_list, prefix='Importing assets',
                                   n_items=n_files)
    elif verbose:
        print('Found {} assets, index the returned LazyList to import.'.format(
            n_files))

    if as_generator:
        return (a for a in lazy_list)
    else:
        return lazy_list
Exemple #5
0
def _import_glob_generator(pattern,
                           extension_map,
                           max_assets=None,
                           landmark_resolver=same_name,
                           shuffle=False,
                           landmark_ext_map=None,
                           importer_kwargs=None,
                           verbose=False):
    filepaths = list(
        glob_with_suffix(pattern, extension_map, sort=(not shuffle)))
    if shuffle:
        random.shuffle(filepaths)
    if max_assets:
        filepaths = filepaths[:max_assets]
    n_files = len(filepaths)
    if n_files == 0:
        raise ValueError('The glob {} yields no assets'.format(pattern))

    generator = _multi_import_generator(filepaths,
                                        extension_map,
                                        landmark_resolver=landmark_resolver,
                                        landmark_ext_map=landmark_ext_map,
                                        importer_kwargs=importer_kwargs)

    if verbose:
        # wrap the generator with the progress reporter
        generator = print_progress(generator,
                                   prefix='Importing assets',
                                   n_items=n_files)

    return generator
Exemple #6
0
def generate_texture_model_from_image_3d_fits(images_and_fits,
                                              lambda_=0.01,
                                              n_components=0.99):
    """Build an ITW texture model from a list of images with associated dense
    3D fits (one per image). Note that the input images should already have an
    image feature taken on them, and have all been resized to a consistent
    scale."""
    f = open("/content/fk.txt", "w")
    f.write("shit")
    f.close()
    feat_img, fit_2d = images_and_fits[0]
    n_channels = feat_img.n_channels
    n_features = n_channels * fit_2d.n_points
    n_samples = len(images_and_fits)
    X = np.empty((n_samples, n_features), dtype=feat_img.pixels.dtype)
    M = np.empty_like(X, dtype=np.bool)
    proportion_masked = []
    for i, (img, fit_2d) in enumerate(
            print_progress(images_and_fits,
                           prefix='Extracting masks & features')):
        features, mask = extract_per_vertex_colour_with_occlusion(fit_2d, img)
        mask_repeated = np.repeat(mask.ravel(), n_channels)
        X[i] = features.ravel()
        M[i] = mask_repeated.ravel()
        proportion_masked.append(mask.sum() / mask.shape[0])
    print('Performing R-PCA to complete missing textures')
    A, E = rpca_missing(X, M, verbose=True, lambda_=lambda_)
    print('R-PCA completed. Building PCA model of features on completed '
          'samples.')
    model = PCAVectorModel(A, inplace=True)
    print('Trimming the components to retain only what was required.')
    model.trim_components(n_components=n_components)

    return model, X, M
Exemple #7
0
def generate_texture_model_from_itwmm(images, mm, id_ind, exp_ind,
                                      template_camera, p, qs, cs,
                                      lambda_=0.01,
                                      n_components=0.99):
    r"""Build a new texture model from an existing model and fitting
    information to a collection of images."""
    n_channels = images[0].n_channels
    n_features = n_channels * mm.n_vertices
    n_samples = len(images)
    X = np.empty((n_samples, n_features), dtype=mm.texture_model.mean().dtype)
    M = np.empty_like(X, dtype=np.bool)
    proportion_masked = []
    for i, (img, q, c) in enumerate(zip(print_progress(images, 'Extracting '
                                                               'masks and '
                                                               'features'), qs,
                                        cs)):
        i_in_img = instance_for_params(mm, id_ind, exp_ind,
                                       template_camera,
                                       p, q, c)['instance_in_img']
        features, mask = extract_per_vertex_colour_with_occlusion(i_in_img, img)
        mask_repeated = np.repeat(mask.ravel(), n_channels)
        X[i] = features.ravel()
        M[i] = mask_repeated.ravel()
        proportion_masked.append(mask.sum() / mask.shape[0])
    print('Extraction concluded. Self-occlusions on average masked {:.0%} of '
          'vertices.'.format(np.array(proportion_masked).mean()))
    print('Performing R-PCA to complete missing textures')
    A, E = rpca_missing(X, M, verbose=True, lambda_=lambda_)
    print('R-PCA completed. Building PCA model of features on completed '
          'samples.')
    model = PCAVectorModel(A, inplace=True)
    print('Trimming the components to retain only what was required.')
    model.trim_components(n_components=n_components)
    return model, X, M
def crop_face():
	filePathSimple = NAME OF THE DIRECTOERY

	files = os.listdir(filePathSimple)

	filePath = THE PATH OF VIDEOs


	vids = import_videos(filePathSimple)

	detector = load_ffld2_frontal_face_detector()


	for utterance in print_progress(vids): # vids is a list with all videos
	  fileName = files.pop(0)

	  whole_path_of_video = filePath + "/" + fileName
	  path_where_you_want_the_uncropped_images_to_be_stored = fileName + "_" + "uncropped"
	  same_as_before_with_cropped_images = fileName + "_" + "cropped"
	#  cropped_image_location = fileName + "_" + "cropped2"
	  cropped_image_location = same_as_before_with_cropped_images

	  print(fileName + " Cropping")

	  fps_check = subprocess.check_output('ffprobe '+whole_path_of_video+' 2>&1 | grep fps',shell=True)

	  fps_check = str(fps_check, 'utf-8')
	  # fps = float(fps_check.split(' fps')[0].split(',')[-1][1:])  ## in our case we know fps=30 so you can just put this

	  fps = 30

	  if not os.path.exists(path_where_you_want_the_uncropped_images_to_be_stored):
	    os.makedirs(path_where_you_want_the_uncropped_images_to_be_stored)

	  if not os.path.exists(same_as_before_with_cropped_images):
	    os.makedirs(same_as_before_with_cropped_images)

	  subprocess.call('ffmpeg -loglevel panic -i '+whole_path_of_video+' -vf fps='+ str(fps)+' '+path_where_you_want_the_uncropped_images_to_be_stored+'/%05d.jpg',shell=True)
	  vv = mio.import_images(path_where_you_want_the_uncropped_images_to_be_stored+'/*.jpg')

	  for cnt, im in enumerate(vv):
	    name = '{0:05d}'.format(cnt+1) # i select to start the images names from 1 and not 0

	    lns = detector(im)
	    if im.landmarks.n_groups == 0:
	        # there are no detections
	        continue
	    if im.landmarks.n_groups == 1:
	      im.constrain_landmarks_to_bounds()
	      mio.export_image(im.crop_to_landmarks(), cropped_image_location+'/'+name+'.jpg', extension=None, overwrite=True)
	    elif  im.landmarks.n_groups > 1:
	      for i in range(im.landmarks.n_groups):
	        im.constrain_landmarks_to_bounds()
	        mio.export_image(im.crop_to_landmarks(group='ffld2_'+str(i)), cropped_image_location+'/'+name+'_'+str(i)+'.jpg', extension=None, overwrite=True)

	  subprocess.call("mv " + filePathSimple + "/" + fileName + " " + filePathSimple + "/Finished", shell=True)
	  print(fileName + " Finished")

	print("All Done")
Exemple #9
0
def densereg_face_iterator(istraining, db='helen'):

    database_path = Path(
        '/vol/atlas/homes/yz4009/databases/DenseReg/FaceRegDataset') / db
    landmarks_path = Path('/vol/atlas/databases') / db

    image_folder = 'Images_Resized'
    uv_folder = 'Labels_Resized'
    z_folder = 'Labels_Resized'

    if istraining == 1:
        database_path = database_path / 'trainset'
        landmarks_path = landmarks_path / 'trainset'
    elif istraining == 0:
        database_path = database_path / 'testset'
        landmarks_path = landmarks_path / 'testset'

    for pimg in print_progress(
            mio.import_images(database_path / image_folder / image_folder)):

        image_name = pimg.path.stem

        # load iuv data
        labels = sio.loadmat(
            str(database_path / uv_folder / uv_folder /
                ('%s.mat' % image_name)))
        iuv = Image(
            np.stack([(labels['LabelsH_resized'] >= 0).astype(np.float32),
                      labels['LabelsH_resized'],
                      labels['LabelsV_resized']]).clip(0, 1))

        # load lms data
        try:
            orig_image = mio.import_image(landmarks_path /
                                          ('%s.jpg' % image_name))
        except:
            orig_image = mio.import_image(landmarks_path /
                                          ('%s.png' % image_name))

        orig_image = orig_image.resize(pimg.shape)

        pimg.landmarks['JOINT'] = orig_image.landmarks['PTS']

        pimg_data = utils.crop_image_bounding_box(
            pimg, pimg.landmarks['JOINT'].bounding_box(), [384, 384], base=256)

        pimg = pimg_data[0]

        iuv_data = utils.crop_image_bounding_box(
            iuv, pimg.landmarks['JOINT'].bounding_box(), [384, 384], base=256)

        iuv = iuv_data[0]

        yield {
            'image': pimg,
            'iuv': iuv,
            'visible_pts': np.array(list(range(68))),
            'marked_index': np.array(list(range(68)))
        }
Exemple #10
0
def save_images_to_dir(images, out_path, output_ext='.jpg'):
    from menpo.visualize import print_progress
    import menpo.io as mio
    if not out_path.exists():
        out_path.mkdir()
    for k, im in enumerate(
            print_progress(images, prefix='Saving images to disk')):
        mio.export_image(im, out_path / '{}{}'.format(k, output_ext))
def PDMModel(path, max_components=None):
    training_shapes = []
    for lg in print_progress(
            mio.import_landmark_files(path / '*.pts', verbose=True)):
        training_shapes.append(lg['all'])
    # train source PDM model
    shape_model = OrthoPDM(training_shapes, max_n_components=max_components)
    return shape_model, training_shapes
def main(directory):
    for portion in portion_to_id.keys():
        print(portion)

        for subj_id in print_progress(portion_to_id[portion]):
            writer = tf.python_io.TFRecordWriter(
                (directory / 'tf_records' / portion /
                 '{}.tfrecords'.format(subj_id)).as_posix())
            serialize_sample(writer, subj_id)
Exemple #13
0
def save_landmarks_to_dir(images, label, out_path, output_ext='.pts'):
    from menpo.visualize import print_progress
    import menpo.io as mio
    if not out_path.exists():
        out_path.mkdir()
    for k, im in enumerate(
            print_progress(images, prefix='Saving landmarks to disk')):
        mio.export_landmark_file(im.landmarks[label],
                                 out_path / '{}{}'.format(k, output_ext))
Exemple #14
0
def _create_dense_diagonal_precision(
    X,
    graph,
    n_features,
    n_features_per_vertex,
    dtype=np.float32,
    n_components=None,
    bias=0,
    return_covariances=False,
    verbose=False,
):
    # Initialize precision
    precision = np.zeros((n_features, n_features), dtype=dtype)
    if return_covariances:
        all_covariances = np.zeros(
            (graph.n_vertices, n_features_per_vertex, n_features_per_vertex),
            dtype=dtype,
        )
    if verbose:
        print_dynamic("Allocated precision matrix of size {}".format(
            bytes_str(precision.nbytes)))

    # Print information if asked
    if verbose:
        vertices = print_progress(
            range(graph.n_vertices),
            n_items=graph.n_vertices,
            prefix="Precision per vertex",
            end_with_newline=False,
        )
    else:
        vertices = range(graph.n_vertices)

    # Compute covariance matrix for each patch
    for v in vertices:
        # find indices in target precision matrix
        i_from = v * n_features_per_vertex
        i_to = (v + 1) * n_features_per_vertex

        # compute covariance
        covmat = np.cov(X[:, i_from:i_to], rowvar=0, bias=bias)
        if return_covariances:
            all_covariances[v] = covmat

        # invert it
        covmat = _covariance_matrix_inverse(covmat, n_components)

        # insert to precision matrix
        precision[i_from:i_to, i_from:i_to] = covmat

    # return covariances
    if return_covariances:
        return precision, all_covariances
    else:
        return precision
Exemple #15
0
def _import_glob_lazy_list(
    pattern,
    extension_map,
    max_assets=None,
    landmark_resolver=same_name,
    shuffle=False,
    as_generator=False,
    landmark_ext_map=None,
    landmark_attach_func=None,
    importer_kwargs=None,
    verbose=False,
):
    filepaths = list(glob_with_suffix(pattern, extension_map, sort=(not shuffle)))
    if shuffle:
        random.shuffle(filepaths)
    if (max_assets is not None) and max_assets <= 0:
        raise ValueError(
            "Max elements should be positive" " ({} provided)".format(max_assets)
        )
    elif max_assets:
        filepaths = filepaths[:max_assets]

    n_files = len(filepaths)
    if n_files == 0:
        raise ValueError("The glob {} yields no assets".format(pattern))

    lazy_list = LazyList(
        [
            partial(
                _import,
                f,
                extension_map,
                landmark_resolver=landmark_resolver,
                landmark_ext_map=landmark_ext_map,
                landmark_attach_func=landmark_attach_func,
                importer_kwargs=importer_kwargs,
            )
            for f in filepaths
        ]
    )

    if verbose and as_generator:
        # wrap the generator with the progress reporter
        lazy_list = print_progress(
            lazy_list, prefix="Importing assets", n_items=n_files
        )
    elif verbose:
        print("Found {} assets, index the returned LazyList to import.".format(n_files))

    if as_generator:
        return (a for a in lazy_list)
    else:
        return lazy_list
Exemple #16
0
def get_images_from_image_folder(top_dir,
                                 ext='.jpg',
                                 restrict_to_landmarked=False):
    images = []
    folders = glob.glob(os.path.join(top_dir, '*'))
    for label, folder in enumerate(tqdm(folders)):
        for img in print_progress(
                mio.import_images(os.path.join(folder, '*' + ext),
                                  verbose=False)):
            images.append(img)
    if restrict_to_landmarked:
        images = [image for image in images if 'PTS' in image.landmarks.keys()]
    return images
Exemple #17
0
def PointDistributionModel(imgFolder):
    '''LOAD IMAGES'''
    path_to_lfpw = Path(imgFolder)
    training_shapes = []
    for lg in print_progress(
            mio.import_landmark_files(path_to_lfpw / '*.pts', verbose=True)):
        training_shapes.append(lg['all'])
    '''TRAIN PDM MODEL'''
    shape_model = OrthoPDM(training_shapes, max_n_components=None)
    '''MODIFY PARAMETERS'''
    shape_model.n_active_components = 20
    shape_model.n_active_components = 0.95
    return shape_model
def pca_and_weights(meshes, retain_eig_cum_val=0.997, verbose=False):
    model = PCAModel(meshes, verbose=verbose)
    n_comps_retained = (model.eigenvalues_cumulative_ratio() <
                        retain_eig_cum_val).sum()
    if verbose:
        print('\nRetaining {:.2%} of eigenvalues keeps {} components'.format(
            retain_eig_cum_val, n_comps_retained))
    model.trim_components(retain_eig_cum_val)
    if verbose:
        meshes = print_progress(meshes, prefix='Calculating weights')
    weights = (np.vstack([model.project(m)
                          for m in meshes]) / np.sqrt(model.eigenvalues))
    return model, weights
def AAMModel(path, max_shape=None, max_appearance=None):
    training_images = []
    for img in print_progress(mio.import_images(path, verbose=True)):
        labeller(img, 'PTS', face_ibug_68_to_face_ibug_68_trimesh)
        training_images.append(img)
    aam_model = HolisticAAM(training_images,
                            group='face_ibug_68_trimesh',
                            scales=(0.5, 1.0),
                            holistic_features=fast_dsift,
                            verbose=True,
                            max_shape_components=max_shape,
                            max_appearance_components=max_appearance)
    return aam_model, training_images
Exemple #20
0
def pca(path_to_images, max_n_components=None):
    path_to_lfpw = Path(path_to_images)

    training_shapes = []
    for lg in print_progress(
            mio.import_landmark_files(path_to_lfpw / '*.pts', verbose=True)):
        training_shapes.append(lg)  # lg['all']

    shape_model = OrthoPDM(training_shapes, max_n_components=max_n_components)
    print(shape_model)
    # visualize_pointclouds(training_shapes)
    # instance = shape_model.similarity_model.instance([100., -300., 0., 0.])
    # instance.view(render_axes=False)
    return shape_model
Exemple #21
0
def _increment_dense_diagonal_precision(
    X,
    mean_vector,
    covariances,
    n,
    graph,
    n_features,
    n_features_per_vertex,
    dtype=np.float32,
    n_components=None,
    bias=0,
    verbose=False,
):
    # Initialize precision
    precision = np.zeros((n_features, n_features), dtype=dtype)

    # Print information if asked
    if verbose:
        print_dynamic("Allocated precision matrix of size {}".format(
            bytes_str(precision.nbytes)))
        vertices = print_progress(
            range(graph.n_vertices),
            n_items=graph.n_vertices,
            prefix="Precision per vertex",
            end_with_newline=False,
        )
    else:
        vertices = range(graph.n_vertices)

    # Compute covariance matrix for each patch
    for v in vertices:
        # find indices in target precision matrix
        i_from = v * n_features_per_vertex
        i_to = (v + 1) * n_features_per_vertex

        # get data
        edge_data = X[:, i_from:i_to]
        m = mean_vector[i_from:i_to]

        # increment
        _, covariances[v] = _increment_multivariate_gaussian_cov(
            edge_data, m, covariances[v], n, bias=bias)

        # invert it
        precision[i_from:i_to, i_from:i_to] = _covariance_matrix_inverse(
            covariances[v], n_components)

    # return covariances
    return precision, covariances
Exemple #22
0
def get_shapes_from_image_folder(top_dir):
    """
    simple helper function to extract saved .pts shapes from a torchvision-like image folder
    """
    shapes = []
    folders = glob.glob(os.path.join(top_dir, '*'))
    for label, folder in enumerate(tqdm(folders)):
        for lg in print_progress(
                mio.import_landmark_files(os.path.join(folder, '*.pts'),
                                          verbose=False)):
            try:
                shapes.append(lg['all'])
            except:
                shapes.append(lg['PTS'])

    return shapes
Exemple #23
0
def transform_pts_temp_v2(image_dir_path, db_name, type_name='trainval'):
    import menpo.io as mio
    from menpo.visualize import print_progress
    img_dir, lb_dir = mk_img_lb_dir(db_name, type_name)
    impaths = filter(lambda x: 'pts' not in x, os.listdir(image_dir_path))
    for imgpath in print_progress(impaths):
        imgpath = os.path.join(image_dir_path, imgpath)
        newpath = transform_impath(imgpath)
        img = mio.import_image(newpath)

        img_suffix = img.path.suffix
        lb_suffix = '.txt'
        image_name = img.path.name.split('.')[0]
        image_type = img.path._str.split('/')[11]
        image_name = '%s_%s' % (image_type, image_name)

        dataType = filter(lambda x: x == image_type, support_types)[0]
        if dataType == 'afw':
            image_name_list = image_name.split('_')
            image_name_list.pop(2)  # remove index
            image_name = '_'.join(image_name_list)
        img_path = os.path.join(img_dir, image_name + img_suffix)
        lb_path = os.path.join(lb_dir, image_name + lb_suffix)

        # save image
        mio.export_image(img, img_path, overwrite=True)
        # save label
        lb = ['None'] * 166
        kps = [str(kp) for kp in (img.landmarks['PTS'].as_vector() + 1)]
        xy = img.landmarks['PTS'].bounds()[0][::-1]
        zk = img.landmarks['PTS'].bounds()[1][::-1]
        x = str(xy[0])
        y = str(xy[1])
        w = str(zk[0] - xy[0])
        h = str(zk[1] - xy[1])
        box_hxyw = [h] + [x] + [y] + [w]
        kps_t = np.array(kps[0::2])
        kps[0::2] = kps[1::2]
        kps[1::2] = kps_t
        lb[30:] = kps
        lb[25:29] = box_hxyw
        with open(lb_path, 'a') as f:
            f.write(' '.join(lb) + '\n')

        fi = FaceImage(img_path)
        # fi.vis()
    pass
 def loadImages(self, path_to_training_images):
     training_images = []
     for img in print_progress(mio.import_images(path_to_training_images, verbose=True)):
         # convert to greyscale
         if img.n_channels == 3:
             img = img.as_greyscale()
         # crop to landmarks bounding box with an extra 20% padding
         img = img.crop_to_landmarks_proportion(0.2)
         # rescale image if its diagonal is bigger than 400 pixels
         d = img.diagonal()
         if d > 400:
             img = img.rescale(400.0 / d)
         # define a TriMesh which will be useful for Piecewise Affine Warp of HolisticAAM
         labeller(img, 'PTS', face_ibug_68_to_face_ibug_68_trimesh)
         # append to list
         training_images.append(img)
     return training_images
def get_image_feature(img_path, img_list_path, model_path, epoch_num, gpu_id):
    img_list = open(img_list_path)
    embedding = Embedding(model_path, epoch_num, gpu_id)
    files = img_list.readlines()
    img_feats = []
    faceness_scores = []
    for img_index, each_line in enumerate(print_progress(files)):
        name_lmk_score = each_line.strip().split(' ')
        img_name = os.path.join(img_path, name_lmk_score[0])
        img = cv2.imread(img_name)
        lmk = np.array([float(x) for x in name_lmk_score[1:-1]], dtype=np.float32)
        lmk = lmk.reshape( (5,2) )
        img_feats.append(embedding.get(img,lmk))
        faceness_scores.append(name_lmk_score[-1])
    img_feats = np.array(img_feats).astype(np.float32)
    faceness_scores = np.array(faceness_scores).astype(np.float32)
    return img_feats, faceness_scores
Exemple #26
0
def _create_dense_diagonal_precision(X, graph, n_features,
                                     n_features_per_vertex,
                                     dtype=np.float32, n_components=None,
                                     bias=0, return_covariances=False,
                                     verbose=False):
    # Initialize precision
    precision = np.zeros((n_features, n_features), dtype=dtype)
    if return_covariances:
        all_covariances = np.zeros(
            (graph.n_vertices, n_features_per_vertex, n_features_per_vertex),
            dtype=dtype)
    if verbose:
        print_dynamic('Allocated precision matrix of size {}'.format(
            bytes_str(precision.nbytes)))

    # Print information if asked
    if verbose:
        vertices = print_progress(
            range(graph.n_vertices), n_items=graph.n_vertices,
            prefix='Precision per vertex', end_with_newline=False)
    else:
        vertices = range(graph.n_vertices)

    # Compute covariance matrix for each patch
    for v in vertices:
        # find indices in target precision matrix
        i_from = v * n_features_per_vertex
        i_to = (v + 1) * n_features_per_vertex

        # compute covariance
        covmat = np.cov(X[:, i_from:i_to], rowvar=0, bias=bias)
        if return_covariances:
            all_covariances[v] = covmat

        # invert it
        covmat = _covariance_matrix_inverse(covmat, n_components)

        # insert to precision matrix
        precision[i_from:i_to, i_from:i_to] = covmat

    # return covariances
    if return_covariances:
        return precision, all_covariances
    else:
        return precision
Exemple #27
0
def _compute_minimum_spanning_tree(shapes,
                                   root_vertex=0,
                                   prefix='',
                                   verbose=False):
    # initialize weights matrix
    n_vertices = shapes[0].n_points
    weights = np.zeros((n_vertices, n_vertices))

    # print progress if requested
    range1 = range(n_vertices - 1)
    if verbose:
        range1 = print_progress(
            range1,
            end_with_newline=False,
            prefix='{}Deformation graph - Computing complete graph`s '
            'weights'.format(prefix))

    # compute weights
    for i in range1:
        for j in range(i + 1, n_vertices, 1):
            # create data matrix of edge
            diffs_x = [s.points[i, 0] - s.points[j, 0] for s in shapes]
            diffs_y = [s.points[i, 1] - s.points[j, 1] for s in shapes]
            coords = np.array([diffs_x, diffs_y])

            # compute mean and covariance
            m = np.mean(coords, axis=1)
            c = np.cov(coords)

            # get weight
            for im in range(len(shapes)):
                weights[i, j] += -np.log(
                    multivariate_normal.pdf(coords[:, im], mean=m, cov=c))
            weights[j, i] = weights[i, j]

    # create undirected graph
    complete_graph = UndirectedGraph(weights)

    if verbose:
        print_dynamic('{}Deformation graph - Minimum spanning graph '
                      'computed.\n'.format(prefix))

    # compute minimum spanning graph
    return complete_graph.minimum_spanning_tree(root_vertex)
Exemple #28
0
def transform_pts(image_dir_path, db_name, type_name='trainval', box_c=0):
    import menpo.io as mio
    from menpo.visualize import print_progress
    img_dir, lb_dir = mk_img_lb_dir(db_name, type_name)
    for img in print_progress(mio.import_images(image_dir_path, verbose=True)):
        img_suffix = img.path.suffix
        lb_suffix = '.txt'
        image_name = img.path.name.split('.')[0]
        img_path = os.path.join(img_dir, image_name + img_suffix)
        lb_path = os.path.join(lb_dir, image_name + lb_suffix)
        # save image
        mio.export_image(img, img_path, overwrite=True)
        # save label
        lb = ['None'] * (30 + img.landmarks['PTS'].n_points * 2)
        kps = [str(kp) for kp in img.landmarks['PTS'].as_vector() + 1]
        if box_c:
            pickle_path = img.path._str.split('.')[0] + '.pkl'
            with open(pickle_path, 'rb') as f:
                box = pickle.load(f)['box']
                x = str(box[0])
                y = str(box[1])
                w = str(box[2] - box[0])
                h = str(box[3] - box[1])
                box_hxyw = [h] + [x] + [y] + [w]
        else:
            xy = img.landmarks['PTS'].bounds()[0][::-1] + 1
            zk = img.landmarks['PTS'].bounds()[1][::-1] + 1
            x = str(xy[0])
            y = str(xy[1])
            w = str(zk[0] - xy[0])
            h = str(zk[1] - xy[1])
            box_hxyw = [h] + [x] + [y] + [w]
        kps_t = np.array(kps[0::2])
        kps[0::2] = kps[1::2]
        kps[1::2] = kps_t
        lb[30:] = kps
        lb[25:29] = box_hxyw
        with open(lb_path, 'w') as f:
            f.write(' '.join(lb))

        fi = FaceImage(img_path)
        # fi.vis()
    pass
Exemple #29
0
def densereg_pose_iterator():
    database_path = Path(
        '/vol/atlas/homes/yz4009/databases/DenseReg/IBUG_UP_DATA/')
    image_path = database_path / 'ProcessImagesStatic'
    iuv_path = database_path / 'Processed_correspondence_Static'

    iuv_map = sio.loadmat(str(database_path / 'GMDS.mat'))
    index_i, index_u, index_v = iuv_map['Index'].squeeze(
    ), iuv_map['Final_U'].squeeze(), iuv_map['Final_V'].squeeze()
    index_i = np.concatenate([[0], index_i])
    index_u = np.concatenate([[0], index_u])
    index_v = np.concatenate([[0], index_v])

    for pimg in print_progress(mio.import_images(image_path)[1:]):

        image_name = pimg.path.stem
        bbox = PointCloud(pimg.bounds()).bounding_box()
        # load iuv data
        iuv_mat = sio.loadmat(str(iuv_path /
                                  ('%s.mat' % image_name)))['I_correspondence']
        iuv = Image(
            np.stack([
                index_i[iuv_mat], index_u[iuv_mat] * 255,
                index_v[iuv_mat] * 255
            ]).astype(np.uint8))

        # load lms data

        pimg_data = utils.crop_image_bounding_box(pimg,
                                                  bbox, [384, 384],
                                                  base=256)

        pimg = pimg_data[0]

        iuv_data = utils.crop_image_bounding_box(iuv,
                                                 bbox, [384, 384],
                                                 base=256,
                                                 order=0)

        iuv = iuv_data[0]

        yield {'image': pimg, 'iuv': iuv}
Exemple #30
0
def _compute_minimum_spanning_tree(shapes, root_vertex=0, prefix='',
                                   verbose=False):
    # initialize weights matrix
    n_vertices = shapes[0].n_points
    weights = np.zeros((n_vertices, n_vertices))

    # print progress if requested
    range1 = range(n_vertices-1)
    if verbose:
        range1 = print_progress(
            range1, end_with_newline=False,
            prefix='{}Deformation graph - Computing complete graph`s '
                   'weights'.format(prefix))

    # compute weights
    for i in range1:
        for j in range(i+1, n_vertices, 1):
            # create data matrix of edge
            diffs_x = [s.points[i, 0] - s.points[j, 0] for s in shapes]
            diffs_y = [s.points[i, 1] - s.points[j, 1] for s in shapes]
            coords = np.array([diffs_x, diffs_y])

            # compute mean and covariance
            m = np.mean(coords, axis=1)
            c = np.cov(coords)

            # get weight
            for im in range(len(shapes)):
                weights[i, j] += -np.log(multivariate_normal.pdf(coords[:, im],
                                                                 mean=m, cov=c))
            weights[j, i] = weights[i, j]

    # create undirected graph
    complete_graph = UndirectedGraph(weights)

    if verbose:
        print_dynamic('{}Deformation graph - Minimum spanning graph '
                      'computed.\n'.format(prefix))

    # compute minimum spanning graph
    return complete_graph.minimum_spanning_tree(root_vertex)
Exemple #31
0
def _import_glob_generator(pattern, extension_map, max_assets=None,
                           landmark_resolver=same_name,
                           landmark_ext_map=None, importer_kwargs=None,
                           verbose=False):
    filepaths = list(glob_with_suffix(pattern, extension_map))
    if max_assets:
        filepaths = filepaths[:max_assets]
    n_files = len(filepaths)
    if n_files == 0:
        raise ValueError('The glob {} yields no assets'.format(pattern))

    generator = _multi_import_generator(
        filepaths, extension_map,  landmark_resolver=landmark_resolver,
        landmark_ext_map=landmark_ext_map, importer_kwargs=importer_kwargs)

    if verbose:
        # wrap the generator with the progress reporter
        generator = print_progress(generator, prefix='Importing assets',
                                   n_items=n_files)

    return generator
Exemple #32
0
def process_video(file, dest):
    if is_video(file):
        try:
            frames = mio.import_video(file, normalise=False)
        except IOError:
            warnings.warn('IO error reading video file {}, '.format(file) +
                          'the file may be corrupted or the video format is unsupported, skipping...')
        except ValueError as e:
            warnings.warn('Value Error reading video file {}, '.format(file) +
                          e.message)
            return
        # check if directory is non empty
        if os.path.dirname(dest):
            create_dir(os.path.dirname(dest))
        print('{} contains {} frames'.format(file, len(frames)))
        print('writing landmarks to {}...'.format(dest))
        frames = frames.map(fit_image)
        with open(dest, 'w') as outputfile:
            outwriter = csv.writer(outputfile)
            try:
                for i, frame in enumerate(print_progress(frames)):
                    if 'final_shape' not in frame.landmarks:
                        warnings.warn('no faces detected in the frame {}, '
                                      'initializing landmarks to -1s...'.format(i))
                        # dlib does not fitting from previous initial shape so
                        # leave entire row as -1s
                        # initial_shape = frames[i - 1].landmarks['final_shape'].lms
                        # fitting_result = fit_image.fitter.fit_from_shape(frame, initial_shape)
                        # frame.landmarks['final_shape'] = fitting_result.final_shape
                        landmarks = [-1] * NO_LANDMARKS*2
                    else:
                        lmg = frame.landmarks['final_shape']
                        landmarks = lmg['all'].points.reshape((NO_LANDMARKS*2,)).tolist()  # reshape to 136 points
                    fill_row(outwriter, i, landmarks)
            except Exception as e:
                warnings.warn('Runtime Error at frame {}'.format(i))
                print('initializing landmarks to -1s...')
                fill_row(outwriter, i, [-1] * NO_LANDMARKS*2)
Exemple #33
0
def _increment_dense_diagonal_precision(X, mean_vector, covariances, n, graph,
                                        n_features, n_features_per_vertex,
                                        dtype=np.float32, n_components=None,
                                        bias=0, verbose=False):
    # Initialize precision
    precision = np.zeros((n_features, n_features), dtype=dtype)

    # Print information if asked
    if verbose:
        print_dynamic('Allocated precision matrix of size {}'.format(
            bytes_str(precision.nbytes)))
        vertices = print_progress(
            range(graph.n_vertices), n_items=graph.n_vertices,
            prefix='Precision per vertex', end_with_newline=False)
    else:
        vertices = range(graph.n_vertices)

    # Compute covariance matrix for each patch
    for v in vertices:
        # find indices in target precision matrix
        i_from = v * n_features_per_vertex
        i_to = (v + 1) * n_features_per_vertex

        # get data
        edge_data = X[:, i_from:i_to]
        m = mean_vector[i_from:i_to]

        # increment
        _, covariances[v] = _increment_multivariate_gaussian_cov(
            edge_data, m, covariances[v], n, bias=bias)

        # invert it
        precision[i_from:i_to, i_from:i_to] = _covariance_matrix_inverse(
            covariances[v], n_components)

    # return covariances
    return precision, covariances
Exemple #34
0
def fit_pre(image_paths):
    import menpo.io as mio
    from menpowidgets import visualize_images
    from menpo.visualize import print_progress

    training_images = []
    for image_path in image_paths:
        for img in print_progress(mio.import_images(image_path, verbose=True)):
            # convert to greyscale
            if img.n_channels == 3:
                img = img.as_greyscale()
            # crop to landmarks bounding box with an extra 20% padding
            img = img.crop_to_landmarks_proportion(0.2)
            # rescale image if its diagonal is bigger than 400 pixels
            d = img.diagonal()
            if d > 400:
                img = img.rescale(400.0 / d)


#             %matplotlib inline
#             visualize_images(img)
# append to list
            training_images.append(img)
    return training_images
Exemple #35
0
def ffmpeg_video_exporter(images, out_path, fps=30, codec='libx264',
                          preset='medium', bitrate=None, verbose=False,
                          **kwargs):
    r"""
    Uses subprocess PIPE to export the images using FFMPEG.

    There are is one important environment variable that can be set to alter
    the behaviour of this function:

        ================== ======================================
        ENV Variable       Definition
        ================== ======================================
        MENPO_FFMPEG_CMD   The path to the 'ffmpeg' executable.
        ================== ======================================

    Parameters
    ----------
    images : `list` of :map:`Image`
        List of Menpo images to export as a video.
    out_path : `Path`
        Path to save the video to.
    fps : `int`, optional
        The number of frames per second.
    codec : `str`, optional
        The video codec to use. Default 'libx264', which represents the
        widely available mpeg4. Except when saving .wmv files, then the
        defaults is 'msmpeg4' which is more commonly supported for windows.
    preset : `str`, optional
        The preset FFMPEG compression level.
        Please check out the documentation for more information:
        https://trac.ffmpeg.org/wiki/Encode/H.264#a2.Chooseapreset
    bitrate: `str`, optional
        The output video bitrate.
    verbose : `bool`, optional
        If ``True``, print a progress bar.
    **kwargs : `dict`, optional
        Extra parameters for advanced video exporting options.
        They are passed through directly to FFMPEG and they should.
        be organised in pairs of option/value in a dictionary.
        For instance: ``{'crf' : '0'} # equivalent to -crf 0 in ffmpeg.``
        You can find further details in the documentation:
        https://ffmpeg.org/ffmpeg.html#Options
    """
    # Some of the below was inspired by moviepy:
    #   https://github.com/Zulko/moviepy/blob/master/moviepy/video/io/ffmpeg_writer.py
    # and is used under the terms of the MIT license which can be found at
    #   https://github.com/Zulko/moviepy/blob/master/LICENCE.txt
    first_image = images[0]
    frame_shape = first_image.shape
    # If the first image is gray then all the images will be assumed to be
    # gray
    colour = 'rgb24' if images[0].n_channels == 3 else 'gray8'
    cmd = [_FFMPEG_CMD(), '-y',
           '-s', '{}x{}'.format(frame_shape[1], frame_shape[0]),
           '-r', str(fps),
           '-an',
           '-pix_fmt', colour,
           '-c:v', 'rawvideo', '-f', 'rawvideo',
           '-i', '-']
    if codec:
        cmd.extend(['-vcodec', codec])
    if preset:
        cmd.extend(['-preset', preset])
    if bitrate:
        cmd.extend(['-b', str(bitrate)])
    # add the optional kwargs for FFMPEG options.
    for key, value in kwargs.items():
        cmd.extend(['-{}'.format(key), value])
    cmd.append(str(out_path))

    images = (print_progress(images, prefix='Exporting frames') if verbose
              else images)

    # Pipe stdout to DEVNULL to ignore it
    with _call_subprocess(sp.Popen(cmd, stdin=sp.PIPE, stderr=sp.PIPE,
                                   stdout=DEVNULL)) as pipe:
        for k, image in enumerate(images):
            try:
                if image.n_channels != 1 and colour == 'gray8':
                    warnings.warn('Frame {} is non-greyscale and the initial '
                                  'frame was greyscale. This frame will be '
                                  'corrupted.'.format(k))
                if image.shape != frame_shape:  # Valid due to tuple/int
                    warnings.warn('Frame {} is not the same shape as the '
                                  'initial frame and therefore the output '
                                  'may be corrupted.'.format(k))

                i = image.pixels_with_channels_at_back(out_dtype=np.uint8)
                # Handle the case of a greyscale image amidst colour images
                if image.n_channels == 1 and colour == 'rgb24':
                    # Repeat the channels axis 3 times
                    i = i.reshape(i.shape + (1,)).repeat(3, axis=2)
                pipe.stdin.write(i.tostring())
            except IOError:
                error = ('FFMPEG encountered the following error while '
                         'writing the video:\n\n{}'.format(
                    pipe.stderr.read().decode()))
                # Re-raise the error for a useful error message
                raise IOError(error)
Exemple #36
0
import menpo
import menpo.io as mio
from menpodetect.ffld2 import load_ffld2_frontal_face_detector
from os.path import join
import subprocess

detector = load_ffld2_frontal_face_detector()

pb = ('/vol/bitbucket/ml9915/annotateVideos2/')
pcrop = ('/vol/bitbucket/ml9915/ffcropped2/')
puncrop = ('/vol/bitbucket/ml9915/ffuncropped2/')

files = os.listdir(pb)
files = filter(lambda x: not x.startswith("._"), files)

for filename in print_progress(files):
    if (filename == '.DS_Store'):
        continue
    if not os.path.exists(pcrop + filename.split('.')[0] + '/'):
        os.makedirs(pcrop + filename.split('.')[0] + '/')
    if not os.path.exists(puncrop + filename.split('.')[0] + '/'):
        os.makedirs(puncrop + filename.split('.')[0] + '/')

    fps = 30
    subprocess.call('ffmpeg -loglevel panic -i ' + pb + filename +
                    ' -vf fps=' + str(fps) + ' ' + puncrop +
                    filename.split('.')[0] + '/%05d.jpg',
                    shell=True)
    vv = mio.import_images(puncrop + filename.split('.')[0] + '/*.jpg')

    # import the video
Exemple #37
0
def as_matrix(vectorizables, length=None, return_template=False, verbose=False):
    r"""
    Create a matrix from a list/generator of :map:`Vectorizable` objects.
    All the objects in the list **must** be the same size when vectorized.

    Consider using a generator if the matrix you are creating is large and
    passing the length of the generator explicitly.

    Parameters
    ----------
    vectorizables : `list` or generator if :map:`Vectorizable` objects
        A list or generator of objects that supports the vectorizable interface
    length : `int`, optional
        Length of the vectorizable list. Useful if you are passing a generator
        with a known length.
    verbose : `bool`, optional
        If ``True``, will print the progress of building the matrix.
    return_template : `bool`, optional
        If ``True``, will return the first element of the list/generator, which
        was used as the template. Useful if you need to map back from the
        matrix to a list of vectorizable objects.

    Returns
    -------
    M : (length, n_features) `ndarray`
        Every row is an element of the list.
    template : :map:`Vectorizable`, optional
        If ``return_template == True``, will return the template used to
        build the matrix `M`.
    """
    # get the first element as the template and use it to configure the
    # data matrix
    if length is None:
        # samples is a list
        length = len(vectorizables)
        template = vectorizables[0]
        vectorizables = vectorizables[1:]
    else:
        # samples is an iterator
        template = next(vectorizables)
    n_features = template.n_parameters
    template_vector = template.as_vector()

    data = np.zeros((length, n_features), dtype=template_vector.dtype)
    if verbose:
        print('Allocated data matrix of size {} '
              '({} samples)'.format(bytes_str(data.nbytes), length))

    # now we can fill in the first element from the template
    data[0] = template_vector
    del template_vector

    if verbose:
        vectorizables = print_progress(vectorizables, n_items=length, offset=1,
                                       prefix='Building data matrix')

    # 1-based as we have the template vector set already
    for i, sample in enumerate(vectorizables, 1):
        if i >= length:
            break
        data[i] = sample.as_vector()

    if return_template:
        return data, template
    else:
        return data
Exemple #38
0
def as_matrix(vectorizables, length=None, return_template=False, verbose=False):
    r"""
    Create a matrix from a list/generator of :map:`Vectorizable` objects.
    All the objects in the list **must** be the same size when vectorized.

    Consider using a generator if the matrix you are creating is large and
    passing the length of the generator explicitly.

    Parameters
    ----------
    vectorizables : `list` or generator if :map:`Vectorizable` objects
        A list or generator of objects that supports the vectorizable interface
    length : `int`, optional
        Length of the vectorizable list. Useful if you are passing a generator
        with a known length.
    verbose : `bool`, optional
        If ``True``, will print the progress of building the matrix.
    return_template : `bool`, optional
        If ``True``, will return the first element of the list/generator, which
        was used as the template. Useful if you need to map back from the
        matrix to a list of vectorizable objects.

    Returns
    -------
    M : (length, n_features) `ndarray`
        Every row is an element of the list.
    template : :map:`Vectorizable`, optional
        If ``return_template == True``, will return the template used to
        build the matrix `M`.

    Raises
    ------
    ValueError
        ``vectorizables`` terminates in fewer than ``length`` iterations
    """
    # get the first element as the template and use it to configure the
    # data matrix
    if length is None:
        # samples is a list
        length = len(vectorizables)
        template = vectorizables[0]
        vectorizables = vectorizables[1:]
    else:
        # samples is an iterator
        template = next(vectorizables)
    n_features = template.n_parameters
    template_vector = template.as_vector()

    data = np.zeros((length, n_features), dtype=template_vector.dtype)
    if verbose:
        print(
            "Allocated data matrix of size {} "
            "({} samples)".format(bytes_str(data.nbytes), length)
        )

    # now we can fill in the first element from the template
    data[0] = template_vector
    del template_vector

    # ensure we take at most the remaining length - 1 elements
    vectorizables = islice(vectorizables, length - 1)

    if verbose:
        vectorizables = print_progress(
            vectorizables,
            n_items=length,
            offset=1,
            prefix="Building data matrix",
            end_with_newline=False,
        )

    # 1-based as we have the template vector set already
    i = 0
    for i, sample in enumerate(vectorizables, 1):
        data[i] = sample.as_vector()

    # we have exhausted the iterable, but did we get enough items?
    if i != length - 1:  # -1
        raise ValueError(
            "Incomplete data matrix due to early iterator "
            "termination (expected {} items, got {})".format(length, i + 1)
        )

    if return_template:
        return data, template
    else:
        return data
Exemple #39
0
def as_matrix(vectorizables, length=None, return_template=False, verbose=False):
    r"""
    Create a matrix from a list/generator of :map:`Vectorizable` objects.
    All the objects in the list **must** be the same size when vectorized.

    Consider using a generator if the matrix you are creating is large and
    passing the length of the generator explicitly.

    Parameters
    ----------
    vectorizables : `list` or generator if :map:`Vectorizable` objects
        A list or generator of objects that supports the vectorizable interface
    length : `int`, optional
        Length of the vectorizable list. Useful if you are passing a generator
        with a known length.
    verbose : `bool`, optional
        If ``True``, will print the progress of building the matrix.
    return_template : `bool`, optional
        If ``True``, will return the first element of the list/generator, which
        was used as the template. Useful if you need to map back from the
        matrix to a list of vectorizable objects.

    Returns
    -------
    M : (length, n_features) `ndarray`
        Every row is an element of the list.
    template : :map:`Vectorizable`, optional
        If ``return_template == True``, will return the template used to
        build the matrix `M`.

    Raises
    ------
    ValueError
        ``vectorizables`` terminates in fewer than ``length`` iterations
    """
    # get the first element as the template and use it to configure the
    # data matrix
    if length is None:
        # samples is a list
        length = len(vectorizables)
        template = vectorizables[0]
        vectorizables = vectorizables[1:]
    else:
        # samples is an iterator
        template = next(vectorizables)
    n_features = template.n_parameters
    template_vector = template.as_vector()

    data = np.zeros((length, n_features), dtype=template_vector.dtype)
    if verbose:
        print('Allocated data matrix of size {} '
              '({} samples)'.format(bytes_str(data.nbytes), length))

    # now we can fill in the first element from the template
    data[0] = template_vector
    del template_vector

    # ensure we take at most the remaining length - 1 elements
    vectorizables = islice(vectorizables, length - 1)

    if verbose:
        vectorizables = print_progress(vectorizables, n_items=length, offset=1,
                                       prefix='Building data matrix')

    # 1-based as we have the template vector set already
    i = 0
    for i, sample in enumerate(vectorizables, 1):
        data[i] = sample.as_vector()

    # we have exhausted the iterable, but did we get enough items?
    if i != length - 1:  # -1
        raise ValueError('Incomplete data matrix due to early iterator '
                         'termination (expected {} items, got {})'.format(
            length, i + 1))

    if return_template:
        return data, template
    else:
        return data
Exemple #40
0
def _increment_sparse_precision(X, mean_vector, covariances, n, graph,
                                n_features, n_features_per_vertex,
                                mode='concatenation', dtype=np.float32,
                                n_components=None, bias=0, verbose=False):
    # check mode argument
    if mode not in ['concatenation', 'subtraction']:
        raise ValueError("mode must be either ''concatenation'' "
                         "or ''subtraction''; {} is given.".format(mode))

    # Initialize arrays
    all_blocks = np.zeros((graph.n_edges * 4,
                           n_features_per_vertex, n_features_per_vertex),
                          dtype=dtype)
    columns = np.zeros(graph.n_edges * 4)
    rows = np.zeros(graph.n_edges * 4)

    # Print information if asked
    if verbose:
        edges = print_progress(range(graph.n_edges), n_items=graph.n_edges,
                               prefix='Precision per edge',
                               end_with_newline=False)
    else:
        edges = range(graph.n_edges)

    # Compute covariance matrix for each edge, invert it and store it
    count = -1
    for e in edges:
        # edge vertices
        v1 = graph.edges[e, 0]
        v2 = graph.edges[e, 1]

        # find indices in data matrix
        v1_from = v1 * n_features_per_vertex
        v1_to = (v1 + 1) * n_features_per_vertex
        v2_from = v2 * n_features_per_vertex
        v2_to = (v2 + 1) * n_features_per_vertex

        # data concatenation
        if mode == 'concatenation':
            edge_data = X[:, list(range(v1_from, v1_to)) +
                             list(range(v2_from, v2_to))]
            m = mean_vector[list(range(v1_from, v1_to)) +
                            list(range(v2_from, v2_to))]
        else:
            edge_data = X[:, v1_from:v1_to] - X[:, v2_from:v2_to]
            m = mean_vector[v1_from:v1_to] - mean_vector[v2_from:v2_to]

        # increment
        _, covariances[e] = _increment_multivariate_gaussian_cov(
            edge_data, m, covariances[e], n, bias=bias)

        # invert it
        covmat = _covariance_matrix_inverse(covariances[e], n_components)

        # store it
        if mode == 'concatenation':
            # v1, v1
            count += 1
            all_blocks[count] = \
                covmat[:n_features_per_vertex, :n_features_per_vertex]
            rows[count] = v1
            columns[count] = v1
            # v2, v2
            count += 1
            all_blocks[count] = \
                covmat[n_features_per_vertex::, n_features_per_vertex::]
            rows[count] = v2
            columns[count] = v2
            # v1, v2
            count += 1
            all_blocks[count] = \
                covmat[:n_features_per_vertex, n_features_per_vertex::]
            rows[count] = v1
            columns[count] = v2
            # v2, v1
            count += 1
            all_blocks[count] = \
                covmat[n_features_per_vertex::, :n_features_per_vertex]
            rows[count] = v2
            columns[count] = v1
        else:
            # v1, v1
            count += 1
            all_blocks[count] = covmat
            rows[count] = v1
            columns[count] = v1
            # v2, v2
            count += 1
            all_blocks[count] = covmat
            rows[count] = v2
            columns[count] = v2
            # v1, v2
            count += 1
            all_blocks[count] = -covmat
            rows[count] = v1
            columns[count] = v2
            # v2, v1
            count += 1
            all_blocks[count] = -covmat
            rows[count] = v2
            columns[count] = v1

    # sort rows, columns and all_blocks
    rows_arg_sort = rows.argsort()
    columns = columns[rows_arg_sort]
    all_blocks = all_blocks[rows_arg_sort]
    rows = rows[rows_arg_sort]

    # create indptr
    n_rows = graph.n_vertices
    indptr = np.zeros(n_rows + 1)
    for i in range(n_rows):
        inds, = np.where(rows == i)
        if inds.size == 0:
            indptr[i + 1] = indptr[i]
        else:
            indptr[i] = inds[0]
            indptr[i + 1] = inds[-1] + 1

    # create block sparse matrix
    return (bsr_matrix((all_blocks, columns, indptr),
                       shape=(n_features, n_features), dtype=dtype),
            covariances)
Exemple #41
0
def _increment_dense_precision(X, mean_vector, covariances, n, graph,
                               n_features, n_features_per_vertex,
                               mode='concatenation', dtype=np.float32,
                               n_components=None, bias=0, verbose=False):
    # check mode argument
    if mode not in ['concatenation', 'subtraction']:
        raise ValueError("mode must be either ''concatenation'' "
                         "or ''subtraction''; {} is given.".format(mode))

    # Initialize precision
    precision = np.zeros((n_features, n_features), dtype=dtype)

    # Print information if asked
    if verbose:
        print_dynamic('Allocated precision matrix of size {}'.format(
            bytes_str(precision.nbytes)))
        edges = print_progress(range(graph.n_edges), n_items=graph.n_edges,
                               prefix='Precision per edge',
                               end_with_newline=False)
    else:
        edges = range(graph.n_edges)

    # Compute covariance matrix for each edge, invert it and store it
    for e in edges:
        # edge vertices
        v1 = graph.edges[e, 0]
        v2 = graph.edges[e, 1]

        # find indices in data matrix
        v1_from = v1 * n_features_per_vertex
        v1_to = (v1 + 1) * n_features_per_vertex
        v2_from = v2 * n_features_per_vertex
        v2_to = (v2 + 1) * n_features_per_vertex

        # data concatenation
        if mode == 'concatenation':
            edge_data = X[:, list(range(v1_from, v1_to)) +
                             list(range(v2_from, v2_to))]
            m = mean_vector[list(range(v1_from, v1_to)) +
                            list(range(v2_from, v2_to))]
        else:
            edge_data = X[:, v1_from:v1_to] - X[:, v2_from:v2_to]
            m = mean_vector[v1_from:v1_to] - mean_vector[v2_from:v2_to]

        # increment
        _, covariances[e] = _increment_multivariate_gaussian_cov(
            edge_data, m, covariances[e], n, bias=bias)

        # invert it
        covmat = _covariance_matrix_inverse(covariances[e], n_components)

        # store it
        if mode == 'concatenation':
            # v1, v1
            precision[v1_from:v1_to, v1_from:v1_to] += \
                covmat[:n_features_per_vertex, :n_features_per_vertex]
            # v2, v2
            precision[v2_from:v2_to, v2_from:v2_to] += \
                covmat[n_features_per_vertex::, n_features_per_vertex::]
            # v1, v2
            precision[v1_from:v1_to, v2_from:v2_to] = \
                covmat[:n_features_per_vertex, n_features_per_vertex::]
            # v2, v1
            precision[v2_from:v2_to, v1_from:v1_to] = \
                covmat[n_features_per_vertex::, :n_features_per_vertex]
        elif mode == 'subtraction':
            # v1, v2
            precision[v1_from:v1_to, v2_from:v2_to] = -covmat
            # v2, v1
            precision[v2_from:v2_to, v1_from:v1_to] = -covmat
            # v1, v1
            precision[v1_from:v1_to, v1_from:v1_to] += covmat
            # v2, v2
            precision[v2_from:v2_to, v2_from:v2_to] += covmat

    # return covariances
    return precision, covariances
Exemple #42
0
def _increment_sparse_diagonal_precision(X, mean_vector, covariances, n, graph,
                                         n_features, n_features_per_vertex,
                                         dtype=np.float32, n_components=None,
                                         bias=0, verbose=False):
    # initialize covariances matrix
    all_blocks = np.zeros((graph.n_vertices,
                           n_features_per_vertex, n_features_per_vertex),
                          dtype=dtype)
    columns = np.zeros(graph.n_vertices)
    rows = np.zeros(graph.n_vertices)

    # Print information if asked
    if verbose:
        vertices = print_progress(
            range(graph.n_vertices), n_items=graph.n_vertices,
            prefix='Precision per vertex', end_with_newline=False)
    else:
        vertices = range(graph.n_vertices)

    # Compute covariance matrix for each patch
    for v in vertices:
        # find indices in target precision matrix
        i_from = v * n_features_per_vertex
        i_to = (v + 1) * n_features_per_vertex

        # get data
        edge_data = X[:, i_from:i_to]
        m = mean_vector[i_from:i_to]

        # increment
        _, covariances[v] = _increment_multivariate_gaussian_cov(
            edge_data, m, covariances[v], n, bias=bias)

        # invert it
        all_blocks[v] = _covariance_matrix_inverse(covariances[v], n_components)

        # store the inverse covariance and its locations
        rows[v] = v
        columns[v] = v

    # sort rows, columns and all_blocks
    rows_arg_sort = rows.argsort()
    columns = columns[rows_arg_sort]
    all_blocks = all_blocks[rows_arg_sort]
    rows = rows[rows_arg_sort]

    # create indptr
    n_rows = graph.n_vertices
    indptr = np.zeros(n_rows + 1)
    for i in range(n_rows):
        inds, = np.where(rows == i)
        if inds.size == 0:
            indptr[i + 1] = indptr[i]
        else:
            indptr[i] = inds[0]
            indptr[i + 1] = inds[-1] + 1

    # create block sparse matrix
    return (bsr_matrix((all_blocks, columns, indptr),
                       shape=(n_features, n_features), dtype=dtype),
            covariances)