Пример #1
0
def create_from_pkl_img(tfrecord_dir, image_dir, pickle_dir, shuffle):
    print('Loading images from "%s"' % image_dir)

    image_filenames = sorted(glob.glob(os.path.join(image_dir, '*')))
    pickle_filenames = sorted(glob.glob(os.path.join(pickle_dir, '*')))
    if len(image_filenames) == 0:
        error('No input images found')

    # good_ids =  mio.import_pickle('/vol/construct3dmm/visualizations/nicp/mein3d/good_ids.pkl')

    img = mio.import_pickle(pickle_filenames[0])
    resolution = img.shape[2]
    channels = img.shape[0] if img.ndim == 3 else 1
    if img.shape[1] != resolution:
        error('Input images must have the same width and height')
    if resolution != 2**int(np.floor(np.log2(resolution))):
        error('Input image resolution must be a power-of-two')
    if channels not in [1, 3]:
        error('Input images must be stored as RGB or grayscale')

    with TFRecordExporter(tfrecord_dir, len(image_filenames)) as tfr:
        order = tfr.choose_shuffled_order() if shuffle else np.arange(
            len(image_filenames))
        for idx in range(order.size):
            img = mio.import_image(image_filenames[order[idx]]).pixels.astype(
                np.float32) * 2 - 1
            pkl = mio.import_pickle(pickle_filenames[order[idx]]).astype(
                np.float32)
            # pkl[0, :, :] = scipy.ndimage.gaussian_filter(pkl[0, :, :], 2)
            # pkl[1, :, :] = scipy.ndimage.gaussian_filter(pkl[1, :, :], 2)
            # pkl[2, :, :] = scipy.ndimage.gaussian_filter(pkl[2, :, :], 2)
            # img_resized = np.stack((cv2.resize(img[0],dsize=(256,256)),cv2.resize(img[1],dsize=(256,256)),cv2.resize(img[2],dsize=(256,256))))
            tfr.add_both(np.concatenate([img, pkl]))
Пример #2
0
def test_importing_pickle_encoding_ignored_py2(version_info, is_file,
                                               mock_open, mock_pickle):
    version_info.major = 2
    mock_pickle.return_value = {'test': 1}
    is_file.return_value = True

    mio.import_pickle('mocked.pkl', encoding='latin1')
    assert 'encoding' not in mock_pickle.call_args[1]
Пример #3
0
def test_importing_pickle_encoding_py3(version_info, is_file, mock_open,
                                       mock_pickle):
    version_info.major = 3
    mock_pickle.return_value = {"test": 1}
    is_file.return_value = True

    mio.import_pickle("mocked.pkl", encoding="latin1")
    assert mock_pickle.call_args[1].get("encoding") == "latin1"
Пример #4
0
def test_importing_pickle_encoding_py3(version_info, is_file, mock_open,
                                       mock_pickle):
    version_info.major = 3
    mock_pickle.return_value = {'test': 1}
    is_file.return_value = True

    mio.import_pickle('mocked.pkl', encoding='latin1')
    assert mock_pickle.call_args[1].get('encoding') == 'latin1'
Пример #5
0
def test_importing_pickle_encoding_ignored_py2(version_info, is_file,
                                               mock_open, mock_pickle):
    version_info.major = 2
    mock_pickle.return_value = {"test": 1}
    is_file.return_value = True

    mio.import_pickle("mocked.pkl", encoding="latin1")
    assert "encoding" not in mock_pickle.call_args[1]
Пример #6
0
def test_importing_pickle_encoding_ignored_py2(version_info, is_file, mock_open,
                                               mock_pickle):
    version_info.major = 2
    mock_pickle.return_value = {'test': 1}
    is_file.return_value = True

    mio.import_pickle('mocked.pkl', encoding='latin1')
    assert 'encoding' not in mock_pickle.call_args[1]
Пример #7
0
def test_importing_pickle_encoding_py3(version_info, is_file, mock_open,
                                       mock_pickle):
    version_info.major = 3
    mock_pickle.return_value = {'test': 1}
    is_file.return_value = True

    mio.import_pickle('mocked.pkl', encoding='latin1')
    assert mock_pickle.call_args[1].get('encoding') == 'latin1'
Пример #8
0
def load_fw_expression_fwc():
    expression_model = mio.import_pickle('./identity_texture_emotion.pkl')['expression']
    expression_model._components /= 100000
    expression_model._mean /= 100000
    tr = mio.import_pickle('/vol/atlas/databases/lsfm/corrective_translation.pkl')
    expression_model._components = tr.apply(expression_model._components.reshape(29, -1, 3)).reshape(29, -1)
    expression_model._mean = tr.apply(expression_model._mean.reshape(-1, 3)).reshape(-1)
    expression_model.n_active_components = 5
    return expression_model
Пример #9
0
 def __init__(self, batch_size=32):
     self.name = 'JamesRenders'
     self.batch_size = batch_size
     self.root = Path('/data/datasets/renders/v1')
     self.tfrecord_names = ['train_v2.tfrecords']
     self.model = mio.import_pickle(
         '/vol/construct3dmm/experiments/models/nicp/mein3d/full_unmasked_good_200.pkl')[
             'model']
     self.settings = mio.import_pickle(
         '/vol/construct3dmm/experiments/nicptexture/settings.pkl',
         encoding='latin1')
Пример #10
0
def load_fitter(name):
    r"""
    Load a fitter with identifier ``name``, pulling it from a remote URL if
    needed.

    Parameters
    ----------
    name : `str`
        The identifier of the fitter, e.g. `balanced_frontal_face_aam`

    Returns
    -------
    fitter : `Fitter`
        A pre-trained menpofit `Fitter` that is ready to use.
    """
    path = path_of_fitter(name)
    if not path.exists():
        print('Downloading {} fitter'.format(name))
        url = url_of_fitter(name)
        print(url, path)
        download_file(url, path, verbose=True)
    # Load the pickle and immediately invoke it.
    try:
        return import_pickle(path)()
    except Exception as e:
        # Hmm something went wrong, and we couldn't load this fitter.
        # Purge it so next time we will redownload.
        print('Error loading fitter - purging damaged file: {}'.format(path))
        print('Please try running again')
        path.unlink()
        raise e
Пример #11
0
def load_fitter(name):
    r"""
    Load a fitter with identifier ``name``, pulling it from a remote URL if
    needed.

    Parameters
    ----------
    name : `str`
        The identifier of the fitter, e.g. `balanced_frontal_face_aam`

    Returns
    -------
    fitter : `Fitter`
        A pre-trained menpofit `Fitter` that is ready to use.
    """
    path = path_of_fitter(name)
    if not path.exists():
        print('Downloading {} fitter'.format(name))
        url = url_of_fitter(name)
        print(url, path)
        download_file(url, path, verbose=True)
    # Load the pickle and immediately invoke it.
    try:
        return import_pickle(path)()
    except Exception as e:
        # Hmm something went wrong, and we couldn't load this fitter.
        # Purge it so next time we will redownload.
        print('Error loading fitter - purging damaged file: {}'.format(path))
        print('Please try running again')
        path.unlink()
        raise e
Пример #12
0
def create_from_images(tfrecord_dir, image_dir, shuffle):
    print('Loading images from "%s"' % image_dir)
    image_filenames = sorted(glob.glob(os.path.join(image_dir, '*')))
    if len(image_filenames) == 0:
        error('No input images found')

    good_ids = mio.import_pickle(
        '/vol/construct3dmm/visualizations/nicp/mein3d/good_ids.pkl')

    img = np.asarray(PIL.Image.open(image_filenames[0]))
    resolution = img.shape[0]
    channels = img.shape[2] if img.ndim == 3 else 1
    # if img.shape[1] != resolution:
    #     error('Input images must have the same width and height')
    # if resolution != 2 ** int(np.floor(np.log2(resolution))):
    #     error('Input image resolution must be a power-of-two')
    if channels not in [1, 3]:
        error('Input images must be stored as RGB or grayscale')

    with TFRecordExporter(tfrecord_dir, len(image_filenames)) as tfr:
        order = tfr.choose_shuffled_order() if shuffle else np.arange(
            len(image_filenames))
        for idx in range(order.size):
            img = PIL.Image.open(image_filenames[order[idx]])
            # img.crop((41, 0, img.size[0]-42, resolution))
            # new_im = PIL.Image.new("RGB", (512, 512),(0, 0, 0))
            # new_im.paste(img, ((512 - img.size[0]) // 2,(512 - img.size[1]) // 2))
            img = np.asarray(img)
            if channels == 1:
                img = img[np.newaxis, :, :]  # HW => CHW
            else:
                img = img.transpose(2, 0, 1)  # HWC => CHW
            tfr.add_image(img)
    def data_fn_h5py():

        with h5py.File(dataset_path) as df:
            if is_training:
                all_cmesh = df['colour_mesh'][:8000]
            else:
                all_cmesh = df['colour_mesh'][8000:]

            L, D, U, A = mio.import_pickle(
                '/vol/phoebe/yz4009/databases/mesh/meta/mein3dcrop_LDUA.pkl',
                encoding='latin1')
            all_cmesh = D[0].dot(
                all_cmesh.transpose([1, 0, 2]).reshape([
                    D[0].shape[1], -1
                ])).reshape([D[0].shape[0], -1,
                             config.inputs_channels]).transpose([1, 0, 2])
            all_cmesh = all_cmesh.astype(np.float32)

        input_fn = tf.estimator.inputs.numpy_input_fn(
            x={
                "cmesh": all_cmesh,
            },
            y={"mask": np.ones_like(all_cmesh)[..., :1]},
            shuffle=True,
            batch_size=BATCH_SIZE,
            num_epochs=num_epochs,
            num_threads=config.no_thread)

        return input_fn
Пример #14
0
        def wrapper(index):
            path = self.root / (index + '.pkl')

            p = mio.import_pickle(path)

            px = p['img'].resize(shape).pixels_with_channels_at_back()
            return px.astype(np.float32), p['weights'].astype(np.float32)
Пример #15
0
def evaluate(dataset_path):
  """Evaluate model on Dataset for a number of steps."""
  with tf.Graph().as_default(), tf.device('/cpu:0'):
    train_dir = Path(FLAGS.checkpoint_dir)
    reference_shape = mio.import_pickle(train_dir / 'reference_shape.pkl')
    
    images, gt_truth, inits, _ = data_provider.batch_inputs(
            [dataset_path], reference_shape,
            batch_size=FLAGS.batch_size, is_training=False)

    mirrored_images, _, mirrored_inits, shapes = data_provider.batch_inputs(
        [dataset_path], reference_shape,
        batch_size=FLAGS.batch_size, is_training=False, mirror_image=True)

    print('Loading model...')
    # Build a Graph that computes the logits predictions from the
    # inference model.
    with tf.device(FLAGS.device):
        patch_shape = (FLAGS.patch_size, FLAGS.patch_size)
        pred, _, _ = mdm_model.model(images, inits, patch_shape=patch_shape)

        tf.get_variable_scope().reuse_variables()

        pred_mirrored, _, _ = mdm_model.model(
            mirrored_images, mirrored_inits, patch_shape=patch_shape)

    pred_images, = tf.py_func(utils.batch_draw_landmarks,
            [images, pred], [tf.float32])
    gt_images, = tf.py_func(utils.batch_draw_landmarks,
            [images, gt_truth], [tf.float32])

    summaries = []
    summaries.append(tf.image_summary('images',
        tf.concat(2, [gt_images, pred_images]), max_images=5))
    
    avg_pred = pred + tf.py_func(flip_predictions, (pred_mirrored, shapes), (tf.float32, ))[0]
    avg_pred /= 2.

    # Calculate predictions.
    norm_error = mdm_model.normalized_rmse(avg_pred, gt_truth)

    # Restore the moving average version of the learned variables for eval.
    variable_averages = tf.train.ExponentialMovingAverage(
        mdm_train.MOVING_AVERAGE_DECAY)
    variables_to_restore = variable_averages.variables_to_restore()
    saver = tf.train.Saver(variables_to_restore)

    # Build the summary operation based on the TF collection of Summaries.
    summary_op = tf.merge_summary(summaries)

    graph_def = tf.get_default_graph().as_graph_def()
    summary_writer = tf.train.SummaryWriter(FLAGS.eval_dir,
                                            graph_def=graph_def)

    while True:
      _eval_once(saver, summary_writer, norm_error, summary_op)
      if FLAGS.run_once:
        break
      time.sleep(FLAGS.eval_interval_secs)
Пример #16
0
        def wrapper(index):
            index = index.decode("utf-8")

            heatmap = mio.import_pickle(
                self.root / '{}+hm.pkl'.format(index))

            heatmap = heatmap.pixels_with_channels_at_back()
            return heatmap.astype(np.float32)
Пример #17
0
def test_importing_pickle(is_file, mock_open, mock_pickle):
    mock_pickle.return_value = {'test': 1}
    is_file.return_value = True

    objs = mio.import_pickle('mocked.pkl')
    assert isinstance(objs, dict)
    assert 'test' in objs
    assert objs['test'] == 1
Пример #18
0
def test_importing_pickle(is_file, mock_open, mock_pickle):
    mock_pickle.return_value = {'test': 1}
    is_file.return_value = True

    objs = mio.import_pickle('mocked.pkl')
    assert isinstance(objs, dict)
    assert 'test' in objs
    assert objs['test'] == 1
Пример #19
0
def test_importing_pickle(is_file, mock_open, mock_pickle):
    mock_pickle.return_value = {"test": 1}
    is_file.return_value = True

    objs = mio.import_pickle("mocked.pkl")
    assert isinstance(objs, dict)
    assert "test" in objs
    assert objs["test"] == 1
Пример #20
0
        def wrapper(index):
            index = index.decode("utf-8")
            result = []

            for i in [0, 1, 2, 4]:
                svs = mio.import_pickle(
                    self.root / '{}+svs_dark+{:02d}.pkl'.format(index, i))
                svs = svs.pixels_with_channels_at_back()
                result.append(svs)
            return np.array(result).astype(np.float32)
Пример #21
0
 def validate(self, valid_img_path):
     assert (os.path.exists(
         self.template_filename)), 'template.pts not exists!'
     valid_imgs = self.load_database(valid_img_path)
     valid_imgs = self.equalize_hist(valid_imgs)
     if self.fitter is None and not os.path.exists(self.model_filename):
         ValueError(
             'neither model file nor fitter exists, please train first')
     if self.fitter is None:
         self.fitter = mio.import_pickle(self.model_filename)
     self._validate_without_gt(valid_imgs, self.fitter)
Пример #22
0
def test_register_pickle_importer(is_file):
    obj = object()

    def foo_importer(filepath, **kwargs):
        return obj

    is_file.return_value = True

    with patch.dict(mio.input.extensions.pickle_types, {}, clear=True):
        mio.register_pickle_importer('.foo', foo_importer)
        new_obj = mio.import_pickle('fake.foo')
    assert new_obj is obj
Пример #23
0
    def _load_landmark_fitter(self):
        if self._fitter_type == 'aam':
            self._aam_fitter = mio.import_pickle(self._aam_fitter_file)
            fitter = LucasKanadeAAMFitter(self._aam_fitter, lk_algorithm_cls=WibergInverseCompositional,
                                          n_shape=self._shape_components, n_appearance=self._appearance_components)
        elif self._fitter_type == 'ert':
            fitter = DlibWrapper(
                path.join(current_path, '../pretrained/shape_predictor_68_face_landmarks.dat'))
        else:
            raise Exception('unknown fitter, did you mean aam/ert?')

        self._landmark_fitter = fitter
Пример #24
0
def test_register_pickle_importer(is_file):
    obj = object()

    def foo_importer(filepath, **kwargs):
        return obj

    is_file.return_value = True

    with patch.dict(mio.input.extensions.pickle_types, {}, clear=True):
        mio.register_pickle_importer('.foo', foo_importer)
        new_obj = mio.import_pickle('fake.foo')
    assert new_obj is obj
Пример #25
0
def evaluate(dataset_path):
    """Evaluate model on Dataset for a number of steps."""
    with tf.Graph().as_default(), tf.device('/cpu:0'):
        # Get images and labels from the dataset.
        #reference_shape = mio.import_pickle(
        #        FLAGS.checkpoint_dir + '/reference_shape.pkl')
        reference_shape = mio.import_pickle('reference_shape.pkl')
        print(reference_shape.shape)
        images, gt_truth, inits = data_provider.batch_inputs(
            [dataset_path],
            reference_shape,
            batch_size=FLAGS.batch_size,
            is_training=False)
        print('Loading model...')
        # Build a Graph that computes the logits predictions from the
        # inference model.
        with tf.device(FLAGS.device):
            pred, _, _ = mdm_model.model(images, inits)

        pred_images, = tf.py_func(utils.batch_draw_landmarks, [images, pred],
                                  [tf.float32])
        gt_images, = tf.py_func(utils.batch_draw_landmarks, [images, gt_truth],
                                [tf.float32])

        summaries = []
        summaries.append(
            tf.image_summary('images',
                             tf.concat(2, [gt_images, pred_images]),
                             max_images=5))
        # Calculate predictions.
        norm_error = mdm_model.normalized_rmse(pred, gt_truth)

        # Restore the moving average version of the learned variables for eval.
        variable_averages = tf.train.ExponentialMovingAverage(
            mdm_train.MOVING_AVERAGE_DECAY)
        variables_to_restore = variable_averages.variables_to_restore()
        saver = tf.train.Saver(variables_to_restore)

        # Build the summary operation based on the TF collection of Summaries.
        summary_op = tf.merge_summary(summaries)

        graph_def = tf.get_default_graph().as_graph_def()
        summary_writer = tf.train.SummaryWriter(FLAGS.eval_dir,
                                                graph_def=graph_def)

        while True:
            _eval_once(saver, summary_writer, norm_error, summary_op)
            if FLAGS.run_once:
                break
            time.sleep(FLAGS.eval_interval_secs)
Пример #26
0
    def _load_landmark_fitter(self):
        if self._fitter_type == 'aam':
            self._aam_fitter = mio.import_pickle(self._aam_fitter_file)
            fitter = LucasKanadeAAMFitter(
                self._aam_fitter,
                lk_algorithm_cls=WibergInverseCompositional,
                n_shape=self._shape_components,
                n_appearance=self._appearance_components)
        elif self._fitter_type == 'ert':
            _, _, fitter68 = maybe_download_models()
            fitter = DlibWrapper(fitter68)
        else:
            raise Exception('unknown fitter, did you mean aam/ert?')

        self._landmark_fitter = fitter
Пример #27
0
def ckpt_pb(pb_path):
    tf.reset_default_graph()
    config = tf.ConfigProto(allow_soft_placement=True)
    config.gpu_options.allow_growth = True
    with tf.Session() as sess:
        path_base = Path(g_config['eval_dataset']).parent.parent
        _mean_shape = mio.import_pickle(path_base / 'reference_shape.pkl')
        _mean_shape = data_provider.align_reference_shape_to_112(_mean_shape)
        assert isinstance(_mean_shape, np.ndarray)
        print(_mean_shape.shape)

        tf_img = tf.placeholder(dtype=tf.float32,
                                shape=(1, 112, 112, 3),
                                name='inputs/input_img')
        tf_dummy = tf.placeholder(dtype=tf.float32,
                                  shape=(1, 73, 2),
                                  name='inputs/input_shape')
        tf_shape = tf.constant(_mean_shape,
                               dtype=tf.float32,
                               shape=(73, 2),
                               name='MeanShape')

        model = mdm_model.MDMModel(tf_img,
                                   tf_dummy,
                                   tf_shape,
                                   batch_size=1,
                                   num_patches=g_config['num_patches'],
                                   num_channels=3,
                                   is_training=False)

        saver = tf.train.Saver()
        ckpt = tf.train.get_checkpoint_state(g_config['train_dir'])
        if ckpt and ckpt.model_checkpoint_path:
            saver.restore(sess, ckpt.model_checkpoint_path)
            global_step = ckpt.model_checkpoint_path.split('/')[-1].split(
                '-')[-1]
            print('Successfully loaded model from {} at step={}.'.format(
                ckpt.model_checkpoint_path, global_step))
        else:
            print('No checkpoint file found')
            return

        output_graph_def = tf.graph_util.convert_variables_to_constants(
            sess,
            tf.get_default_graph().as_graph_def(), ['Network/Predict/add'])

        with tf.gfile.FastGFile(pb_path, mode='wb') as f:
            f.write(output_graph_def.SerializeToString())
Пример #28
0
 def __init__(self,
              batch_size=64,
              db_name='WPUTEDB-train',
              shape=(250, 190),
              num_classes=500,
              root='/homes/yz4009/wd/PickleModel/EarRecognition/',
              is_training=True):
     self.name = db_name
     self.batch_size = batch_size
     self.root = Path(root)
     self.dataset = mio.import_pickle(str(self.root /
                                          '{}.pkl'.format(db_name)),
                                      encoding='latin1')
     self.num_classes = num_classes
     self.shape = shape
     self.is_training = is_training
Пример #29
0
    def __init__(self, batch_size=1):
        super().__init__(batch_size)
        from menpo.transform import Translation, scale_about_centre
        import menpo3d.io as m3dio

        self.name = 'FDDB'
        self.root = Path('/vol/atlas/databases/fddb_ibug')
        template = m3dio.import_mesh(
            '/vol/construct3dmm/regression/src/template.obj')
        template = Translation(-template.centre()).apply(template)
        self.template = scale_about_centre(template, 1. /
                                           1000.).apply(template)
        pca_path = '/homes/gt108/Projects/ibugface/pose_settings/pca_params.pkl'
        self.eigenvectors, self.eigenvalues, self.h_mean, self.h_max = mio.import_pickle(
            pca_path)
        self.image_extension = '.jpg'
        self.lms_extension = '.ljson'
        def __init__(self, *args, **kwargs):
            # hyperparameters
            self.BATCH_SIZE = FLAGS.batch_size
            self.LR = FLAGS.lr
            self.opt = FLAGS.opt
            self.NK = FLAGS.k_poly
            self.use_colour = FLAGS.colour
            self.inputs_channels = 6 if FLAGS.colour else 3
            self.lr_decay = FLAGS.lr_decay
            self.warm_start_from = FLAGS.warm_start_from
            self.EMBEDING = FLAGS.embedding
            self.LOGDIR = self.format_folder(FLAGS)
            self.NUM_GPUS = len(FLAGS.gpu.split(','))
            self.TOTAL_EPOCH = FLAGS.n_epoch
            self.no_thread = FLAGS.no_thread
            self.dataset_path = FLAGS.dataset_path
            self.test_path = FLAGS.test_path
            self.train_format = 'tf' if FLAGS.dataset_path.endswith(
                '.tfrecord') else 'h5py'
            self.test_format = 'tf' if FLAGS.test_path.endswith(
                '.tfrecord') else 'h5py'
            # constant
            self.INPUT_SHAPE = 112
            self.FILTERS = [16, 16, 16, 32]
            self.DB_SIZE = 8000
            self.EPOCH_STEPS = self.DB_SIZE // (self.BATCH_SIZE *
                                                self.NUM_GPUS)

            # graph constant
            self.luda_file = 'mein3dcrop_7k_LDUA.pkl'
            # self.luda_file = 'lsfm_LDUA.pkl'
            self.symetric_index = np.load(FLAGS.meta_path +
                                          '/symetric_index_full.npy').astype(
                                              np.int32)
            self.shape_model = mio.import_pickle(FLAGS.meta_path +
                                                 '/mein3dcrop_7k_mesh.pkl',
                                                 encoding='latin1')
            self.trilist = self.shape_model.trilist
            self.graph_laplacians, self.downsampling_matrices, self.upsamling_matrices, self.adj_matrices = mio.import_pickle(
                FLAGS.meta_path + '/' + self.luda_file, encoding='latin1')
            self.N_VERTICES = self.graph_laplacians[0].shape[0]
def process_data(dir):
    dirlist = os.listdir(dir)
    dirlist = [d for d in dirlist if not os.path.isfile(os.path.join(dir, d))]
    dirlist = sorted(dirlist)
    # load the aam model
    aam = mio.import_pickle("aam.pkl")
    # create fiiter
    fitter = LucasKanadeAAMFitter(aam,
                                  lk_algorithm_cls=WibergInverseCompositional,
                                  n_shape=16,
                                  n_appearance=104)
    # Load detector
    detector = load_dlib_frontal_face_detector()
    #load the sentences
    for j, subdir in enumerate(dirlist):
        ids = os.listdir(os.path.join(dir, subdir, "video/"))
        ids = [i for i in ids if "head" not in i]
        ids = sorted(ids)
        for k, id in enumerate(ids):
            t = time.time()
            video = os.path.join(dir, subdir, "video", id + '/')
            process_one_sentence(video, fitter, detector)
            print(j, k, time.time() - t)
Пример #32
0
import numpy as np
from pysofia import sofia_ml
import menpo.io as mio

n_samples, n_features = 500, 2000
# X = np.random.randn(n_samples, n_features)
# w = np.random.randn(n_features)
# y = (X.dot(w) + np.random.randn(n_samples)).astype(np.int)
#
# mio.export_pickle([X, y], 'tmp.pkl', overwrite=False)

data = mio.import_pickle('tmp.pkl')
X = data[0]
y = data[1]

coef = sofia_ml.sgd_train(X, y, None, 0.01, n_features=n_features)

coef2 = sofia_ml.sgd_train(X, y, None, 0.01, n_features=n_features)

# prediction = sofia_ml.svm_predict(X, coef, sofia_ml.predict_type.logistic)

print "done"

Пример #33
0
    def build_data():
        # ### load models
        shape_model = mio.import_pickle('/homes/yz4009/wd/notebooks/Projects/MLProjects/models/all_all_all.pkl')
        uv_shape = (INPUT_SHAPE, INPUT_SHAPE)
        template = shape_model.instance([])
        UV_template = optimal_cylindrical_unwrap(template).apply(template)
        UV_template.points = UV_template.points[:, [1, 0]]
        UV_template.points[:, 0] = UV_template.points[:, 0].max() - UV_template.points[:, 0]

        UV_template.points -= UV_template.points.min(axis=0)
        UV_template.points /= UV_template.points.max(axis=0)
        UV_template.points *= np.array([uv_shape])


        # ### Generate IUV

        def rotation_matrix(angle, direction, point=None):
            sina = math.sin(angle)
            cosa = math.cos(angle)
            direction = np.array(direction).astype(np.float64)
            # rotation matrix around unit vector
            R = np.diag([cosa, cosa, cosa])
            R += np.outer(direction, direction) * (1.0 - cosa)
            direction *= sina
            R += np.array([[ 0.0,         -direction[2],  direction[1]],
                            [ direction[2], 0.0,          -direction[0]],
                            [-direction[1], direction[0],  0.0]])
            M = np.identity(4)
            M[:3, :3] = R
            if point is not None:
                # rotation not around origin
                point = np.array(point[:3], dtype=np.float64, copy=False)
                M[:3, 3] = point - np.dot(R, point)
            return M

        def random_sample_UV(n_pcs = 50, random_angle = 45, shape = [INPUT_SHAPE, INPUT_SHAPE]):
            # random mesh
            sample_mesh = shape_model.instance(np.random.sample([n_pcs]) * 3, normalized_weights=True)
            sample_mesh = ColouredTriMesh(sample_mesh.points, trilist=sample_mesh.trilist)
            sample_mesh = lambertian_shading(sample_mesh)
            sample_mesh = Homogeneous(rotation_matrix(np.deg2rad(90), [0,0,1])).apply(sample_mesh)
            sample_mesh = Homogeneous(rotation_matrix(np.deg2rad(180), [1,0,0])).apply(sample_mesh)
            sample_mesh = Scale([100,100,100]).apply(sample_mesh)

            # random rotation and scale and translation
            sample_mesh = Homogeneous(rotation_matrix(np.deg2rad(np.random.rand() * random_angle - random_angle / 2), [0,0,1])).apply(sample_mesh)
            sample_mesh = Homogeneous(rotation_matrix(np.deg2rad(np.random.rand() * random_angle - random_angle / 2), [0,1,0])).apply(sample_mesh)
            sample_mesh = Homogeneous(rotation_matrix(np.deg2rad(np.random.rand() * random_angle - random_angle / 2), [1,0,0])).apply(sample_mesh)

            sample_mesh = Scale(np.tile(np.random.sample([1]), [3]) * 1. + 0.5).apply(sample_mesh)

            # translate to image centre
            sample_mesh = Translation([shape[0] / 2,shape[1] / 2,0]).apply(sample_mesh)
            
            # random perturbation
            sample_mesh = Translation(np.random.sample([3]) * 128 - 64).apply(sample_mesh)

            u_mesh = ColouredTriMesh(
                sample_mesh.points, 
                trilist=sample_mesh.trilist, 
                colours=UV_template.points[:,0,None])
            u_image = rasterize_mesh(u_mesh, shape)
            v_mesh = ColouredTriMesh(
                sample_mesh.points, 
                trilist=sample_mesh.trilist, 
                colours=UV_template.points[:,1,None])
            v_image = rasterize_mesh(v_mesh, shape)

            IUV_image = Image(np.concatenate([u_image.mask.pixels,u_image.pixels / 255., v_image.pixels / 255.]).clip(0,1))
            return IUV_image

        def one_hot(a):
            a = a.astype(np.int32)
            n_parts = np.max(a) + 1
            b = np.zeros((len(a), n_parts))
            b[np.arange(len(a)), a] = 1
            return b

        def rgb_iuv(rgb):
            # formation
            iuv_mask = rgb[..., 0]
            n_parts = int(np.max(iuv_mask) + 1)
            iuv_one_hot = one_hot(iuv_mask.flatten()).reshape(iuv_mask.shape + (n_parts,))

            # normalised uv
            uv = rgb[..., 1:] / 255. if np.max(rgb[..., 1:]) > 1 else rgb[..., 1:]
            u = iuv_one_hot * uv[..., 0][..., None]
            v = iuv_one_hot * uv[..., 1][..., None]

            iuv = np.concatenate([iuv_one_hot, u, v], 2)

            return iuv

        class FaceIUVSequence(dm.utils.Sequence):

            def __init__(self, batch_size=16, is_validation=False, validation_size=BATCH_SIZE):
                self.batch_size = validation_size if is_validation else batch_size
                self.size = validation_size if is_validation else 10000 // BATCH_SIZE
                super().__init__()

            def __len__(self):
                return self.size

            def __getitem__(self, idx):
                batch_img = [
                    rgb_iuv(random_sample_UV(shape = [256, 256]).pixels_with_channels_at_back()) for _ in range(self.batch_size)
                ]
                
                batch_x = batch_y = batch_img = np.array(batch_img)
                return [batch_x], [batch_y]

        return FaceIUVSequence(batch_size=BATCH_SIZE)
Пример #34
0
def template_fw():
    return mio.import_pickle('/vol/atlas/databases/itwmm/mein3d_fw_correspond_mean.pkl.gz')
Пример #35
0
def load_basel_shape():
    shape_model = mio.import_pickle('/vol/atlas/databases/lsfm/shape_PCAModel.pkl', encoding='latin1')
    landmarks = m3io.import_landmark_file(TEMPLATE_LMS_PATH)
    return shape_model, landmarks
Пример #36
0
def train(scope=''):
    """Train on dataset for a number of steps."""
    with tf.Graph().as_default() as graph, tf.device('/gpu:0'):
        # Global steps
        tf_global_step = tf.get_variable(
            'GlobalStep', [],
            initializer=tf.constant_initializer(0),
            trainable=False)

        # Learning rate
        tf_lr = tf.train.exponential_decay(g_config['learning_rate'],
                                           tf_global_step,
                                           g_config['learning_rate_step'],
                                           g_config['learning_rate_decay'],
                                           staircase=True,
                                           name='LearningRate')
        tf.summary.scalar('learning_rate', tf_lr, collections=['train'])

        # Create an optimizer that performs gradient descent.
        opt = tf.train.AdamOptimizer(tf_lr)

        data_provider.prepare_images(g_config['train_dataset'].split(':'),
                                     num_patches=g_config['num_patches'],
                                     verbose=True)
        path_base = Path(g_config['train_dataset'].split(':')[0]).parent.parent
        _mean_shape = mio.import_pickle(path_base / 'reference_shape.pkl')
        _mean_shape = data_provider.align_reference_shape_to_112(_mean_shape)
        assert (isinstance(_mean_shape, np.ndarray))
        assert (_mean_shape.shape[0] == g_config['num_patches'])

        tf_mean_shape = tf.constant(_mean_shape,
                                    dtype=tf.float32,
                                    name='MeanShape')

        def get_random_sample(image, shape, rotation_stddev=10):
            # Read a random image with landmarks and bb
            image = menpo.image.Image(image.transpose((2, 0, 1)), copy=False)
            image.landmarks['PTS'] = PointCloud(shape)

            if np.random.rand() < .5:
                image = utils.mirror_image(image)
            if np.random.rand() < .5:
                theta = np.random.normal(scale=rotation_stddev)
                rot = menpo.transform.rotate_ccw_about_centre(
                    image.landmarks['PTS'], theta)
                image = image.warp_to_shape(image.shape, rot)
            bb = image.landmarks['PTS'].bounding_box().points
            miny, minx = np.min(bb, 0)
            maxy, maxx = np.max(bb, 0)
            bbsize = max(maxx - minx, maxy - miny)
            center = [(miny + maxy) / 2., (minx + maxx) / 2.]
            shift = (np.random.rand(2) - 0.5) * 0.6 * bbsize
            image.landmarks['bb'] = PointCloud([
                [
                    center[0] - bbsize * 0.5 + shift[0],
                    center[1] - bbsize * 0.5 + shift[1]
                ],
                [
                    center[0] + bbsize * 0.5 + shift[0],
                    center[1] + bbsize * 0.5 + shift[1]
                ],
            ]).bounding_box()
            proportion = 1.0 / 6.0 + float(np.random.rand() - 0.5) / 6.
            image = image.crop_to_landmarks_proportion(proportion, group='bb')
            image = image.resize((112, 112))

            random_image = image.pixels.transpose(1, 2, 0).astype('float32')
            random_shape = image.landmarks['PTS'].points.astype('float32')
            return random_image, random_shape

        def decode_feature_and_augment(serialized):
            feature = {
                'train/image': tf.FixedLenFeature([], tf.string),
                'train/shape': tf.VarLenFeature(tf.float32),
            }
            features = tf.parse_single_example(serialized, features=feature)
            decoded_image = tf.decode_raw(features['train/image'], tf.float32)
            decoded_image = tf.reshape(decoded_image, (448, 448, 3))
            decoded_shape = tf.sparse.to_dense(features['train/shape'])
            decoded_shape = tf.reshape(decoded_shape,
                                       (g_config['num_patches'], 2))

            random_image, random_shape = tf.py_func(
                get_random_sample, [decoded_image, decoded_shape],
                [tf.float32, tf.float32],
                stateful=True,
                name='RandomSample')
            return data_provider.distort_color(random_image), random_shape

        def decode_feature(serialized):
            feature = {
                'validate/image': tf.FixedLenFeature([], tf.string),
                'validate/shape': tf.VarLenFeature(tf.float32),
            }
            features = tf.parse_single_example(serialized, features=feature)
            decoded_image = tf.decode_raw(features['validate/image'],
                                          tf.float32)
            decoded_image = tf.reshape(decoded_image, (112, 112, 3))
            decoded_shape = tf.sparse.to_dense(features['validate/shape'])
            decoded_shape = tf.reshape(decoded_shape,
                                       (g_config['num_patches'], 2))
            return decoded_image, decoded_shape

        with tf.name_scope('DataProvider'):
            tf_dataset = tf.data.TFRecordDataset(
                [str(path_base / 'train.bin')])
            tf_dataset = tf_dataset.repeat()
            tf_dataset = tf_dataset.map(decode_feature_and_augment,
                                        num_parallel_calls=5)
            tf_dataset = tf_dataset.batch(g_config['batch_size'], True)
            tf_dataset = tf_dataset.prefetch(1)
            tf_iterator = tf_dataset.make_one_shot_iterator()
            tf_images, tf_shapes = tf_iterator.get_next(name='Batch')
            tf_images.set_shape([g_config['batch_size'], 112, 112, 3])
            tf_shapes.set_shape([g_config['batch_size'], 73, 2])

            tf_dataset_v = tf.data.TFRecordDataset(
                [str(path_base / 'validate.bin')])
            tf_dataset_v = tf_dataset_v.repeat()
            tf_dataset_v = tf_dataset_v.map(decode_feature,
                                            num_parallel_calls=5)
            tf_dataset_v = tf_dataset_v.batch(50, True)
            tf_dataset_v = tf_dataset_v.prefetch(1)
            tf_iterator_v = tf_dataset_v.make_one_shot_iterator()
            tf_images_v, tf_shapes_v = tf_iterator_v.get_next(
                name='ValidateBatch')
            tf_images_v.set_shape([50, 112, 112, 3])
            tf_shapes_v.set_shape([50, 73, 2])

        print('Defining model...')
        with tf.device(g_config['train_device']):
            tf_model = mdm_model.MDMModel(tf_images,
                                          tf_shapes,
                                          tf_mean_shape,
                                          batch_size=g_config['batch_size'],
                                          num_patches=g_config['num_patches'],
                                          num_channels=3)
            tf_grads = opt.compute_gradients(tf_model.nme)
            with tf.name_scope('Validate'):
                tf_model_v = mdm_model.MDMModel(
                    tf_images_v,
                    tf_shapes_v,
                    tf_mean_shape,
                    batch_size=50,
                    num_patches=g_config['num_patches'],
                    num_channels=3,
                    is_training=False)
        tf.summary.histogram('dx',
                             tf_model.prediction - tf_shapes,
                             collections=['train'])

        bn_updates = tf.get_collection(tf.GraphKeys.UPDATE_OPS, scope)

        # Add histograms for gradients.
        for grad, var in tf_grads:
            if grad is not None:
                tf.summary.histogram(var.op.name + '/gradients',
                                     grad,
                                     collections=['train'])

        # Apply the gradients to adjust the shared variables.
        with tf.name_scope('Optimizer', values=[tf_grads, tf_global_step]):
            apply_gradient_op = opt.apply_gradients(tf_grads,
                                                    global_step=tf_global_step)

        # Add histograms for trainable variables.
        for var in tf.trainable_variables():
            tf.summary.histogram(var.op.name, var, collections=['train'])

        with tf.name_scope('MovingAverage', values=[tf_global_step]):
            variable_averages = tf.train.ExponentialMovingAverage(
                g_config['MOVING_AVERAGE_DECAY'], tf_global_step)
            variables_to_average = (tf.trainable_variables() +
                                    tf.moving_average_variables())
            variables_averages_op = variable_averages.apply(
                variables_to_average)

        # Group all updates to into a single train op.
        bn_updates_op = tf.group(*bn_updates, name='BNGroup')
        train_op = tf.group(apply_gradient_op,
                            variables_averages_op,
                            bn_updates_op,
                            name='TrainGroup')

        # Create a saver.
        saver = tf.train.Saver()

        train_summary_op = tf.summary.merge_all('train')
        validate_summary_op = tf.summary.merge_all('validate')

        config = tf.ConfigProto(allow_soft_placement=True)
        config.gpu_options.allow_growth = True
        sess = tf.Session(graph=graph, config=config)
        init = tf.global_variables_initializer()
        print('Initializing variables...')
        sess.run(init)
        print('Initialized variables.')

        # Assuming model_checkpoint_path looks something like:
        #   /ckpt/train/model.ckpt-0,
        # extract global_step from it.
        start_step = 0
        ckpt = tf.train.get_checkpoint_state(g_config['train_dir'])
        if ckpt and ckpt.model_checkpoint_path:
            saver.restore(sess, ckpt.model_checkpoint_path)
            start_step = int(
                ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1]) + 1
            print('%s: Restart from %s' %
                  (datetime.now(), g_config['train_dir']))
        else:
            ckpt = tf.train.get_checkpoint_state(g_config['ckpt_dir'])
            if ckpt and ckpt.model_checkpoint_path:
                saver.restore(sess, ckpt.model_checkpoint_path)
                tf_global_step_op = tf_global_step.assign(0)
                sess.run(tf_global_step_op)
                print('%s: Pre-trained model restored from %s' %
                      (datetime.now(), g_config['ckpt_dir']))

        train_writer = tf.summary.FileWriter(g_config['train_dir'] + '/train',
                                             sess.graph)
        validate_writer = tf.summary.FileWriter(
            g_config['train_dir'] + '/validate', sess.graph)

        print('Starting training...')
        steps_per_epoch = 15000 / g_config['batch_size']
        for step in range(start_step, g_config['max_steps']):
            if step % steps_per_epoch == 0:
                start_time = time.time()
                _, train_loss, train_summary = sess.run(
                    [train_op, tf_model.nme, train_summary_op])
                duration = time.time() - start_time
                validate_loss, validate_summary = sess.run(
                    [tf_model_v.nme, validate_summary_op])
                train_writer.add_summary(train_summary, step)
                validate_writer.add_summary(validate_summary, step)

                print('%s: step %d, loss = %.4f (%.3f sec/batch)' %
                      (datetime.now(), step, train_loss, duration))
                print('%s: step %d, validate loss = %.4f' %
                      (datetime.now(), step, validate_loss))
            else:
                start_time = time.time()
                _, train_loss = sess.run([train_op, tf_model.nme])
                duration = time.time() - start_time
                if step % 100 == 0:
                    print('%s: step %d, loss = %.4f (%.3f sec/batch)' %
                          (datetime.now(), step, train_loss, duration))

            assert not np.isnan(train_loss), 'Model diverged with loss = NaN'

            if step % steps_per_epoch == 0 or (step +
                                               1) == g_config['max_steps']:
                checkpoint_path = os.path.join(g_config['train_dir'],
                                               'model.ckpt')
                saver.save(sess, checkpoint_path, global_step=step)
Пример #37
0
from menpo.shape import PointDirectedGraph


#预测
def file_name_except_format(file_dir):
    L = []
    for root, dirs, files in os.walk(file_dir):
        for file in files:
            if os.path.splitext(file)[1] == '.png':
                L.append(os.path.splitext(file)[0])
    return L


image_path_pred = "D:/电信研究院/人脸矫正/labelme-master/examples/transfer/pred"
#加载保存的模型
fitter = mio.import_pickle('pretrained12131_aam.pkl')()
pred_images = []
# load landmarked images
for i in mio.import_images(image_path_pred, max_images=None, verbose=True):
    # convert it to grayscale if needed
    if i.n_channels == 3:
        i = i.as_greyscale(mode='luminosity')

    # append it to the list
    pred_images.append(i)
png_list = file_name_except_format(image_path_pred)
cnt = 0
for i in pred_images:
    # Load detector
    detect = load_dlib_frontal_face_detector()
    # Detect