def test_export_pickle(mock_open, exists, pickle_dump): exists.return_value = False fake_path = '/fake/fake.pkl' with open(fake_path) as f: type(f).name = PropertyMock(return_value=fake_path) mio.export_pickle(test_lg, f) pickle_dump.assert_called_once()
def load_n_create_generator(pattern, detector_name, group=None, overwrite=False): # from menpo.landmark import LandmarkGroup from menpo.model import PCAModel try: cur_detector = _DETECTORS[detector_name]() except KeyError: detector_list = ', '.join(list(_DETECTORS.keys())) raise ValueError('Valid detector types are: {}'.format(detector_list)) print('Running {} detector on {}'.format(detector_name, pattern)) bboxes = [ (img, detect_and_check(img, cur_detector, group=group)) for img in mio.import_images(pattern, normalise=False, verbose=True) ] # find all the detections that did not fail detections = list(filter(lambda x: x[1] is not None, bboxes)) print('Creating a model out of {} detections.'.format(len(detections))) # normalize these to size [1, 1], centred on origin normed_detections = [ normalize(im.landmarks[group].bounding_box()).apply(det) for im, det in detections ] # build a PCA model from good detections pca = PCAModel(normed_detections) mio.export_pickle(pca, '{}_gen.pkl'.format(detector_name), overwrite=overwrite)
def test_export_pickle(mock_open, exists, pickle_dump): exists.return_value = False fake_path = '/fake/fake.pkl' with open(fake_path) as f: type(f).name = PropertyMock(return_value=fake_path) mio.export_pickle(test_lg, f) assert pickle_dump.call_count == 1
def train(self, train_img_path): train_imgs = self.load_database(train_img_path) train_imgs = self.equalize_hist(train_imgs) if self.algo == 'aam': trainer = HolisticAAM(train_imgs, group='PTS', verbose=True, diagonal=120, scales=(0.5, 1.0)) self.fitter = LucasKanadeAAMFitter(trainer, n_shape=[6, 12], n_appearance=0.5) mio.export_pickle(self.fitter, self.model_filename) print('aam model trained and exported!') elif self.algo == 'asm': trainer = CLM(train_imgs, group='PTS', verbose=True, diagonal=120, scales=(0.5, 1.0)) self.fitter = GradientDescentCLMFitter(trainer, n_shape=[6, 12]) mio.export_pickle(self.fitter, self.model_filename) print('asm model trained and exported!') else: ValueError('algorithm must be aam or asm!')
def load_n_create_generator(pattern, detector_name, group=None, overwrite=False): import menpo.io as mio from menpo.landmark import LandmarkGroup from menpo.model import PCAModel try: detector = _DETECTORS[detector_name]() except KeyError: detector_list = ', '.join(list(_DETECTORS.keys())) raise ValueError('Valid detector types are: {}'.format(detector_list)) print('Running {} detector on {}'.format(detector_name, pattern)) bboxes = [(img, detect_and_check(img, detector, group=group)) for img in mio.import_images(pattern, normalise=False, verbose=True)] # find all the detections that did not fail detections = filter(lambda x: x[1] is not None, bboxes) print('Creating a model out of {} detections.'.format(len(detections))) # normalize these to size [1, 1], centred on origin normed_detections = [ normalize(im.landmarks[group].lms.bounding_box()).apply(det) for im, det in detections ] # build a PCA model from good detections pca = PCAModel(normed_detections) mio.export_pickle(pca, '{}_gen.pkl'.format(detector_name), overwrite=overwrite)
def train(): path_to_images = 'lfpw/ceph_trainset/' training_images = [] for img in mio.import_images(path_to_images, verbose=True): # if img.n_channels == 3: # img = img.as_greyscale() img = img.crop_to_landmarks_proportion(0.2) d = img.diagonal() if d > 400: img = img.rescale(400.0 / d) training_images.append(img) # patch_aam = PatchAAM(training_images, group='PTS', patch_shape=[(15, 15), (23, 23)], # diagonal=200, scales=(0.5, 1.0), holistic_features=fast_dsift, # max_shape_components=60, max_appearance_components=200, # verbose=True) patch_aam = PatchAAM(training_images, group='PTS', patch_shape=[(16, 19), (19, 16)], diagonal=200, scales=(0.5, 1), holistic_features=fast_dsift, max_shape_components=74, max_appearance_components=175, verbose=True) fitter = LucasKanadeAAMFitter(patch_aam, lk_algorithm_cls=WibergInverseCompositional, n_shape=[10, 30], n_appearance=[40, 160]) mio.export_pickle(fitter, '26_img_35_pnt.pkl', overwrite=True) return fitter
def test_export_pickle_with_path_uses_open(mock_open, exists, pickle_dump): exists.return_value = False fake_path = _norm_path("fake.pkl.gz") mock_open_enter = MagicMock() # Make sure the name attribute returns the path mock_open_enter.__enter__.return_value.configure_mock(name=fake_path) mock_open.return_value = mock_open_enter mio.export_pickle(test_lg, fake_path) pickle_dump.assert_called_once() mock_open.assert_called_once_with(fake_path, "wb")
def test_export_pickle_with_path_uses_open(mock_open, exists, pickle_dump): exists.return_value = False fake_path = str(_norm_path('fake.pkl.gz')) mock_open_enter = MagicMock() # Make sure the name attribute returns the path mock_open_enter.__enter__.return_value.configure_mock(name=fake_path) mock_open.return_value = mock_open_enter mio.export_pickle(test_lg, fake_path) assert pickle_dump.call_count == 1 mock_open.assert_called_with(fake_path, 'wb')
def test_export_pickle_with_path_expands_vars(mock_open, exists, pickle_dump): exists.return_value = False fake_path = '~/fake/fake.pkl.gz' mock_open_enter = MagicMock() # Make sure the name attribute returns the path mock_open_enter.__enter__.return_value.configure_mock(name=fake_path) mock_open.return_value = mock_open_enter mio.export_pickle(test_lg, fake_path) assert pickle_dump.call_count == 1 expected_path = os.path.join(os.path.expanduser('~'), 'fake', 'fake.pkl.gz') mock_open.assert_called_with(expected_path, 'wb')
def test_export_pickle_with_path_expands_vars(mock_open, exists, pickle_dump): exists.return_value = False fake_path = "~/fake/fake.pkl.gz" mock_open_enter = MagicMock() # Make sure the name attribute returns the path mock_open_enter.__enter__.return_value.configure_mock(name=fake_path) mock_open.return_value = mock_open_enter mio.export_pickle(test_lg, fake_path) pickle_dump.assert_called_once() expected_path = os.path.join(os.path.expanduser("~"), "fake", "fake.pkl.gz") mock_open.assert_called_once_with(expected_path, "wb")
def process_clip(clip_name, paths, training_images, img_type, loop, svm_params, mi=110, d_aam=130, n_s=None, n_a=None): """ Processes a clip. Accepts a clip (along with its params and paths), trains a person-specific part based AAM (pbaam) and then fits it to all the frames. :param clip_name: str: Name of the clip. :param paths: dict: Required paths for training/fitting/exporting data. :param training_images: list: List of menpo images (generic images) appended to the person specific ones. :param img_type: str: Suffix (extension) of the frames, e.g. '.png'. :param loop: bool: Declares whether this is a 2nd fit for AAM (loop). :param svm_params: dict: Required params for SVM classification. If 'apply' is False, the rest are not used. Otherwise, requires reference frame and classifier loaded. :param mi: int: (optional) Max images of the clip loaded for the pbaam. :param d_aam: int: (optional) Diagonal of AAM (param in building it). :param n_s: int/list/None: (optional) Number of shapes for AAM (as expected in menpofit). :param n_a: int/list/None: (optional) Number of appearances for AAM (as expected in menpofit). :return: """ global fitter # paths and list of frames frames_path = paths['clips'] + frames + clip_name + sep if not check_path_and_landmarks(frames_path, clip_name, paths['in_lns'] + clip_name): return False list_frames = sorted(os.listdir(frames_path)) pts_p = mkdir_p(paths['out_lns'] + clip_name + sep) svm_p = mkdir_p(paths['out_svm'] + clip_name + sep) # svm path # loading images from the clip training_detector = load_images(list(list_frames), frames_path, paths['in_lns'], clip_name, training_images=list(training_images), max_images=mi) print('\nBuilding Part based AAM for the clip {}.'.format(clip_name)) aam = PatchAAM(training_detector, verbose=True, holistic_features=features, patch_shape=patch_shape, diagonal=d_aam, scales=(.5, 1)) del training_detector sampling_step = 2 sampling_mask = np.zeros(patch_shape, dtype=np.bool) # create the sampling mask sampling_mask[::sampling_step, ::sampling_step] = True fitter = LucasKanadeAAMFitter(aam, lk_algorithm_cls=fit_alg, n_shape=n_s, n_appearance=n_a, sampling=sampling_mask) # save the AAM model (requires plenty of disk space for each model). aam.features = None export_pickle(aam, paths['out_model'] + clip_name + '.pkl', overwrite=True) aam.features = features del aam clip = Clip(clip_name, paths['clips'], frames, [paths['in_lns'], paths['in_fit_lns']], [pts_p, svm_p]) # [process_frame(frame_name, clip, img_type, svm_params,loop) for frame_name in list_frames]; Parallel(n_jobs=-1, verbose=4)(delayed(process_frame)(frame_name, clip, img_type, svm_params, loop) for frame_name in list_frames) fitter = [] # reset fitter return True
def storeBB(ims, filename): p_out = '/vol/bitbucket/ml9915/landmarksLeft/' bboxes = {} for cnt, im in enumerate(ims): if im.n_channels == 3: im = im.as_greyscale() # ffld2 detector returns bounding_boxes lns = ffld2_detector(im) if im.landmarks.n_groups == 0: # there are no detections print 'error' continue name = '{0:06d}'.format(cnt) bboxes[name] = lns # export the boundingbox mio.export_pickle(bboxes, p_out + filename +'.pkl', overwrite=True)
def write_pkl(file_path, pkl_path, crop_scale=0.1, to_gray_scale=True, resize=False, resize_shape=(128, 128), resize_order=1, to_CNN=False): if os.path.exists(file_path): my_images = [] print('Digging into %s' % file_path) else: raise IOError('File path %s not found. Please have it checked' % file_path) exit() for i, image in enumerate(mio.import_images(file_path, verbose=True)): if image.has_landmarks and 'PTS' in image.landmarks.group_labels: new_my_image = my_image(image, crop_scale=crop_scale, to_gray_scale=to_gray_scale, resize=resize, resize_shape=resize_shape, resize_order=resize_order, to_CNN=to_CNN) my_images.append(new_my_image) # # Show images and landmarks # image_shown = utils.add_landmarks(new_my_image.image, new_my_image.landmarks) # cv2.imshow('image',cv2.cvtColor(image_shown, cv2.COLOR_RGB2BGR)) # cv2.waitKey(0) & 0xFF print('%d / %d in dataset' % (i, len(mio.import_images(file_path)))) else: print("Landmarks not found!!!") print('Finished loading %s' % file_path) print('Writing to %s, this may take quite a long time.' % pkl_path) mio.export_pickle(my_images, pkl_path) print('Finished writing to %s.' % pkl_path)
def train(scope=''): """Train on dataset for a number of steps.""" with tf.Graph().as_default(), tf.device('/cpu:0'): # Create a variable to count the number of train() calls. This equals the # number of batches processed * FLAGS.num_gpus. global_step = tf.get_variable( 'global_step', [], initializer=tf.constant_initializer(0), trainable=False) train_dirs = FLAGS.datasets.split(':') _shape = data_provider.build_reference_shape(train_dirs) mio.export_pickle(_shape, FLAGS.train_dir + '/reference_shape.pkl') reference_shape = tf.constant( _shape, name='reference_shape') # Calculate the learning rate schedule. num_batches_per_epoch = 100 num_epochs_per_decay = 5 decay_steps = int(num_batches_per_epoch * num_epochs_per_decay) # Decay the learning rate exponentially based on the number of steps. lr = tf.train.exponential_decay(FLAGS.initial_learning_rate, global_step, decay_steps, FLAGS.learning_rate_decay_factor, staircase=True) # Create an optimizer that performs gradient descent. opt = tf.train.AdamOptimizer(lr) # Override the number of preprocessing threads to account for the increased # number of GPU towers. num_preprocess_threads = FLAGS.num_preprocess_threads images, lms, inits = data_provider.batch_inputs(train_dirs, reference_shape, FLAGS.batch_size, is_training=True) with tf.device('/gpu:0'): # Retain the summaries from the final tower. summaries = tf.get_collection(tf.GraphKeys.SUMMARIES, scope) predictions, dxs, _ = mdm_model.model(images, inits) total_loss = 0 for i, dx in enumerate(dxs): norm_error = mdm_model.normalized_rmse(dx + inits, lms) tf.histogram_summary('errors', norm_error) loss = tf.reduce_mean(norm_error) total_loss += loss summaries.append(tf.scalar_summary('losses/step_{}'.format(i), loss)) # Calculate the gradients for the batch of data grads = opt.compute_gradients(total_loss) summaries.append(tf.scalar_summary('losses/total', total_loss)) pred_images, = tf.py_func(utils.batch_draw_landmarks, [images, predictions], [tf.float32]) gt_images, = tf.py_func(utils.batch_draw_landmarks, [images, lms], [tf.float32]) summary = tf.image_summary('images', tf.concat(2, [gt_images, pred_images]), max_images=5) summaries.append(tf.histogram_summary('dx', predictions - inits)) summaries.append(summary) batchnorm_updates = tf.get_collection(slim.ops.UPDATE_OPS_COLLECTION, scope) # Add a summary to track the learning rate. summaries.append(tf.scalar_summary('learning_rate', lr)) # Add histograms for gradients. for grad, var in grads: if grad is not None: summaries.append( tf.histogram_summary(var.op.name + '/gradients', grad)) # Apply the gradients to adjust the shared variables. apply_gradient_op = opt.apply_gradients(grads, global_step=global_step) # Add histograms for trainable variables. for var in tf.trainable_variables(): summaries.append(tf.histogram_summary(var.op.name, var)) # Track the moving averages of all trainable variables. # Note that we maintain a "double-average" of the BatchNormalization # global statistics. This is more complicated then need be but we employ # this for backward-compatibility with our previous models. variable_averages = tf.train.ExponentialMovingAverage( MOVING_AVERAGE_DECAY, global_step) # Another possibility is to use tf.slim.get_variables(). 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. # NOTE: Currently we are not using batchnorm in MDM. batchnorm_updates_op = tf.group(*batchnorm_updates) train_op = tf.group(apply_gradient_op, variables_averages_op, batchnorm_updates_op) # Create a saver. saver = tf.train.Saver(tf.all_variables()) # Build the summary operation from the last tower summaries. summary_op = tf.merge_summary(summaries) # Build an initialization operation to run below. init = tf.initialize_all_variables() # Start running operations on the Graph. allow_soft_placement must be set to # True to build towers on GPU, as some of the ops do not have GPU # implementations. sess = tf.Session(config=tf.ConfigProto( allow_soft_placement=True)) sess.run(init) if FLAGS.pretrained_model_checkpoint_path: assert tf.gfile.Exists(FLAGS.pretrained_model_checkpoint_path) variables_to_restore = tf.get_collection( slim.variables.VARIABLES_TO_RESTORE) restorer = tf.train.Saver(variables_to_restore) restorer.restore(sess, FLAGS.pretrained_model_checkpoint_path) print('%s: Pre-trained model restored from %s' % (datetime.now(), FLAGS.pretrained_model_checkpoint_path)) # Start the queue runners. tf.train.start_queue_runners(sess=sess) summary_writer = tf.train.SummaryWriter(FLAGS.train_dir) for step in xrange(FLAGS.max_steps): start_time = time.time() _, loss_value = sess.run([train_op, total_loss]) duration = time.time() - start_time assert not np.isnan(loss_value), 'Model diverged with loss = NaN' if step % 10 == 0: examples_per_sec = FLAGS.batch_size / float(duration) format_str = ('%s: step %d, loss = %.2f (%.1f examples/sec; %.3f ' 'sec/batch)') print(format_str % (datetime.now(), step, loss_value, examples_per_sec, duration)) if step % 10 == 0: summary_str = sess.run(summary_op) summary_writer.add_summary(summary_str, step) # Save the model checkpoint periodically. if step % 50 == 0 or (step + 1) == FLAGS.max_steps: checkpoint_path = os.path.join(FLAGS.train_dir, 'model.ckpt') saver.save(sess, checkpoint_path, global_step=step)
def generate_fake_images(run_id, snapshot=None, grid_size=[1, 1], batch_size=8, num_pngs=1, image_shrink=1, png_prefix=None, random_seed=1000, minibatch_size=8): network_pkl = misc.locate_network_pkl(run_id, snapshot) if png_prefix is None: png_prefix = misc.get_id_string_for_network_pkl(network_pkl) + '-' random_state = np.random.RandomState(random_seed) print('Loading network from "%s"...' % network_pkl) G, D, Gs = misc.load_network_pkl(run_id, snapshot) result_subdir = misc.create_result_subdir(config_test.result_dir, config_test.desc) for png_idx in range(int(num_pngs / batch_size)): start = time.time() print('Generating png %d-%d / %d... in ' % (png_idx * batch_size, (png_idx + 1) * batch_size, num_pngs), end='') latents = misc.random_latents(np.prod(grid_size) * batch_size, Gs, random_state=random_state) labels = np.zeros([latents.shape[0], 7], np.float32) images = Gs.run(latents, labels, minibatch_size=minibatch_size, num_gpus=config_test.num_gpus, out_shrink=image_shrink) for i in range(batch_size): if images.shape[1] == 3: mio.export_pickle( images[i], os.path.join( result_subdir, '%s%06d.pkl' % (png_prefix, png_idx * batch_size + i))) # misc.save_image(images[i], os.path.join(result_subdir, '%s%06d.png' % (png_prefix, png_idx*batch_size+i)), [0,255], grid_size) elif images.shape[1] == 6: mio.export_pickle(images[i][3:6], os.path.join( result_subdir, '%s%06d.pkl' % (png_prefix, png_idx * batch_size + i)), overwrite=True) misc.save_image( images[i][0:3], os.path.join( result_subdir, '%s%06d.png' % (png_prefix, png_idx * batch_size + i)), [-1, 1], grid_size) elif images.shape[1] == 9: mio.export_pickle(images[i][3:6], os.path.join( result_subdir, '%s%06d_shp.pkl' % (png_prefix, png_idx * batch_size + i)), overwrite=True) mio.export_pickle(images[i][6:9], os.path.join( result_subdir, '%s%06d_nor.pkl' % (png_prefix, png_idx * batch_size + i)), overwrite=True) misc.save_image( images[i][0:3], os.path.join( result_subdir, '%s%06d.png' % (png_prefix, png_idx * batch_size + i)), [-1, 1], grid_size) print('%0.2f seconds' % (time.time() - start)) open(os.path.join(result_subdir, '_done.txt'), 'wt').close()
def generate_interpolation_images(run_id, snapshot=None, grid_size=[1, 1], image_shrink=1, image_zoom=1, duration_sec=60.0, smoothing_sec=1.0, mp4=None, mp4_fps=30, mp4_codec='libx265', mp4_bitrate='16M', random_seed=1000, minibatch_size=8): network_pkl = misc.locate_network_pkl(run_id, snapshot) if mp4 is None: mp4 = misc.get_id_string_for_network_pkl(network_pkl) + '-lerp.mp4' num_frames = int(np.rint(duration_sec * mp4_fps)) random_state = np.random.RandomState(random_seed) print('Loading network from "%s"...' % network_pkl) G, D, Gs = misc.load_network_pkl(run_id, snapshot) print('Generating latent vectors...') shape = [num_frames, np.prod(grid_size)] + [ Gs.input_shape[1:][0] + Gs.input_shapes[1][1:][0] ] # [frame, image, channel, component] all_latents = random_state.randn(*shape).astype(np.float32) all_latents = scipy.ndimage.gaussian_filter( all_latents, [smoothing_sec * mp4_fps] + [0] * len(Gs.input_shape), mode='wrap') all_latents /= np.sqrt(np.mean(np.square(all_latents))) #10 10 10 10 5 3 10 # model = mio.import_pickle('../models/lsfm_shape_model_fw.pkl') # facesoft_model = mio.import_pickle('../models/facesoft_id_and_exp_3d_face_model.pkl')['shape_model'] # lsfm_model = m3io.import_lsfm_model('/home/baris/Projects/faceganhd/models/all_all_all.mat') # model_mean = lsfm_model.mean().copy() # mask = mio.import_pickle('../UV_spaces_V2/mask_full_2_crop.pkl') lsfm_tcoords = \ mio.import_pickle('512_UV_dict.pkl')['tcoords'] lsfm_params = [] result_subdir = misc.create_result_subdir(config_test.result_dir, config_test.desc) for png_idx in range(int(num_frames / minibatch_size)): start = time.time() print('Generating png %d-%d / %d... in ' % (png_idx * minibatch_size, (png_idx + 1) * minibatch_size, num_frames), end='') latents = all_latents[png_idx * minibatch_size:(png_idx + 1) * minibatch_size, 0, :Gs.input_shape[1:][0]] labels = all_latents[png_idx * minibatch_size:(png_idx + 1) * minibatch_size, 0, Gs.input_shape[1:][0]:] labels_softmax = softmax(labels) * np.array([10, 10, 10, 10, 5, 3, 10]) images = Gs.run(latents, labels_softmax, minibatch_size=minibatch_size, num_gpus=config_test.num_gpus, out_shrink=image_shrink) for i in range(minibatch_size): texture = Image(np.clip(images[i, 0:3] / 2 + 0.5, 0, 1)) img_shape = ndimage.gaussian_filter(images[i, 3:6], sigma=(0, 3, 3), order=0) mesh_raw = from_UV_2_3D(Image(img_shape), topology='full', uv_layout='oval') # model_mean.points[mask,:] = mesh_raw.points normals = images[i, 6:9] normals_norm = (normals - normals.min()) / (normals.max() - normals.min()) mesh = mesh_raw #facesoft_model.reconstruct(model_mean).from_mask(mask) # lsfm_params.append(lsfm_model.project(mesh_raw)) t_mesh = TexturedTriMesh(mesh.points, lsfm_tcoords.points, texture, mesh.trilist) m3io.export_textured_mesh( t_mesh, os.path.join(result_subdir, '%06d.obj' % (png_idx * minibatch_size + i)), texture_extension='.png') fix_obj( os.path.join(result_subdir, '%06d.obj' % (png_idx * minibatch_size + i))) mio.export_image( Image(normals_norm), os.path.join(result_subdir, '%06d_nor.png' % (png_idx * minibatch_size + i))) print('%0.2f seconds' % (time.time() - start)) mio.export_pickle(lsfm_params, os.path.join(result_subdir, 'lsfm_params.pkl')) open(os.path.join(result_subdir, '_done.txt'), 'wt').close()
def process_clip(clip_name, paths, training_images, img_type, loop, svm_params, mi=110, d_aam=130, n_s=None, n_a=None): """ Processes a clip. Accepts a clip (along with its params and paths), trains a person-specific part based AAM (pbaam) and then fits it to all the frames. :param clip_name: str: Name of the clip. :param paths: dict: Required paths for training/fitting/exporting data. :param training_images: list: List of menpo images (generic images) appended to the person specific ones. :param img_type: str: Suffix (extension) of the frames, e.g. '.png'. :param loop: bool: Declares whether this is a 2nd fit for AAM (loop). :param svm_params: dict: Required params for SVM classification. If 'apply' is False, the rest are not used. Otherwise, requires reference frame and classifier loaded. :param mi: int: (optional) Max images of the clip loaded for the pbaam. :param d_aam: int: (optional) Diagonal of AAM (param in building it). :param n_s: int/list/None: (optional) Number of shapes for AAM (as expected in menpofit). :param n_a: int/list/None: (optional) Number of appearances for AAM (as expected in menpofit). :return: """ global fitter # paths and list of frames frames_path = paths['clips'] + frames + clip_name + sep if not check_path_and_landmarks(frames_path, clip_name, paths['in_lns'] + clip_name): return False list_frames = sorted(os.listdir(frames_path)) pts_p = mkdir_p(paths['out_lns'] + clip_name + sep) svm_p = mkdir_p(paths['out_svm'] + clip_name + sep) # svm path # loading images from the clip training_detector = load_images(list(list_frames), frames_path, paths['in_lns'], clip_name, training_images=list(training_images), max_images=mi) print('\nBuilding Part based AAM for the clip {}.'.format(clip_name)) aam = PatchAAM(training_detector, verbose=True, holistic_features=features, patch_shape=patch_shape, diagonal=d_aam, scales=(.5, 1)) del training_detector fitter = LucasKanadeAAMFitter(aam, n_shape=n_s, n_appearance=n_a) # save the AAM model (requires plenty of disk space for each model). aam.features = None export_pickle(aam, paths['out_model'] + clip_name + '.pkl', overwrite=True) del aam clip = Clip(clip_name, paths['clips'], frames, [paths['in_lns'], paths['in_fit_lns']], [pts_p, svm_p]) # [process_frame(frame_name, clip, img_type, svm_params,loop) for frame_name in list_frames]; Parallel(n_jobs=-1, verbose=4)( delayed(process_frame)(frame_name, clip, img_type, svm_params, loop) for frame_name in list_frames) fitter = [] # reset fitter return True
def prepare_images(paths, num_patches, verbose=True): """Save Train/Test/Validate Images to TFRecord, for ShuffleNet Args: paths: a list of strings containing the data directories. num_patches: number of landmarks verbose: boolean, print debugging info. Returns: None """ if len(paths) == 0: return # .../<Dataset>/Images/*.png -> .../<Dataset> path_base = Path(paths[0]).parent.parent image_paths = [] # First & Second: get all image paths; split to train, test and validate. 7:2:1 if Path(path_base / 'train_img.txt').exists(): with Path(path_base / 'train_img.txt').open('rb') as train_ifs, \ Path(path_base / 'test_img.txt').open('rb') as test_ifs, \ Path(path_base / 'val_img.txt').open('rb') as val_ifs: train_paths = [Path(line[:-1].decode('utf-8')) for line in train_ifs.readlines()] test_paths = [Path(line[:-1].decode('utf-8')) for line in test_ifs.readlines()] val_paths = [Path(line[:-1].decode('utf-8')) for line in val_ifs.readlines()] print('Found Train/Test/Validate {}/{}/{}'.format(len(train_paths), len(test_paths), len(val_paths))) else: for path in paths: for file in Path('.').glob(path): image_paths.append(file) print('Got all image paths...') random.shuffle(image_paths) num_train = int(len(image_paths) * 0.9) num_test = int(len(image_paths) * 0.09) train_paths = sorted(image_paths[:num_train]) test_paths = sorted(image_paths[num_train:num_train+num_test]) val_paths = sorted(image_paths[num_train+num_test:]) with Path(path_base / 'train_img.txt').open('wb') as train_ofs, \ Path(path_base / 'test_img.txt').open('wb') as test_ofs, \ Path(path_base / 'val_img.txt').open('wb') as val_ofs: train_ofs.writelines([str(line).encode('utf-8') + b'\n' for line in train_paths]) test_ofs.writelines([str(line).encode('utf-8') + b'\n' for line in test_paths]) val_ofs.writelines([str(line).encode('utf-8') + b'\n' for line in val_paths]) print('Write Train/Test/Validate {}/{}/{}'.format(len(train_paths), len(test_paths), len(val_paths))) # Third: export reference shape on train if Path(path_base / 'mean_shape.pkl').exists(): mean_shape = mshape.PointCloud(mio.import_pickle(path_base / 'mean_shape.pkl')) print('Imported mean_shape.pkl') else: mean_shape = mshape.PointCloud(build_mean_shape(train_paths, num_patches)) mio.export_pickle(mean_shape.points, path_base / 'mean_shape.pkl', overwrite=True) print('Created mean_shape.pkl') # Fourth: image shape & pca # No need for ShuffleNet # Fifth: train data if Path(path_base / 'train_0.bin').exists(): pass else: print('preparing train data') random.shuffle(train_paths) num_write = 4 num_process = num_write * 2 augment = 20 image_per_calc = int((len(train_paths) + num_process - 1) / num_process) manager = multiprocessing.Manager() message_queue = [manager.Queue(64) for _ in range(num_write)] calc_pool = multiprocessing.Pool(num_process) write_pool = multiprocessing.Pool(num_write) for i in range(num_write): train_paths_1 = train_paths[(i * 2) * image_per_calc: (i * 2 + 1) * image_per_calc] train_paths_2 = train_paths[(i * 2 + 1) * image_per_calc: (i * 2 + 2) * image_per_calc] calc_pool.apply_async(process_images, args=( message_queue[i], i * 2, augment, train_paths_1, )) calc_pool.apply_async(process_images, args=( message_queue[i], i * 2 + 1, augment, train_paths_2, )) write_pool.apply_async(write_images, args=( message_queue[i], i, path_base, (len(train_paths_1) + len(train_paths_2)) * augment, )) calc_pool.close() write_pool.close() calc_pool.join() write_pool.join() print('prepared train data') # Sixth: test data if Path(path_base / 'test.bin').exists(): pass else: with tf.io.TFRecordWriter(str(path_base / 'test.bin')) as ofs: print('Preparing test data...') counter = 0 for path in test_paths: counter += 1 if verbose: status = 10.0 * counter / len(test_paths) status_str = '\rPreparing {:2.2f}%['.format(status * 10) for i in range(int(status)): status_str += '=' for i in range(int(status), 10): status_str += ' ' status_str += '] {} '.format(path) print(status_str, end='') mp_image = load_image(path, 1. / 6., 112) mp_image.landmarks['init'] = mshape.PointCloud( align_reference_shape_to_112(mean_shape.points.astype(np.float32)) ) image = mp_image.pixels.transpose(1, 2, 0).astype(np.float32) shape = mp_image.landmarks['PTS'].points.astype(np.float32) init = mp_image.landmarks['init'].points.astype(np.float32) features = tf.train.Features( feature={ 'test/image': tf.train.Feature( bytes_list=tf.train.BytesList( value=[tf.compat.as_bytes(image.tostring())]) ), 'test/shape': tf.train.Feature( float_list=tf.train.FloatList(value=shape.flatten()) ), 'test/init': tf.train.Feature( float_list=tf.train.FloatList(value=init.flatten()) ) } ) ofs.write(tf.train.Example(features=features).SerializeToString()) if verbose: print('') # Seven: validate data if Path(path_base / 'validate.bin').exists(): pass else: random.shuffle(val_paths) with tf.io.TFRecordWriter(str(path_base / 'validate.bin')) as ofs: print('Preparing validate data...') counter = 0 for path in val_paths: counter += 1 if verbose: status = 10.0 * counter / len(val_paths) status_str = '\rPreparing {:2.2f}%['.format(status * 10) for i in range(int(status)): status_str += '=' for i in range(int(status), 10): status_str += ' ' status_str += '] {} '.format(path) print(status_str, end='') mp_image = load_image(path, 1. / 6., 112) image = mp_image.pixels.transpose(1, 2, 0).astype(np.float32) shape = mp_image.landmarks['PTS'].points.astype(np.float32) features = tf.train.Features( feature={ 'validate/image': tf.train.Feature( bytes_list=tf.train.BytesList( value=[tf.compat.as_bytes(image.tostring())]) ), 'validate/shape': tf.train.Feature( float_list=tf.train.FloatList(value=shape.flatten()) ) } ) ofs.write(tf.train.Example(features=features).SerializeToString()) if verbose: print('')
def prepare_images(paths, num_patches=73, verbose=True): """Save Train Images to TFRecord Args: paths: a list of strings containing the data directories. num_patches: number of landmarks verbose: boolean, print debugging info. Returns: None """ if len(paths) == 0: return # .../<Dataset>/Images/*.png -> .../<Dataset> path_base = Path(paths[0]).parent.parent image_paths = [] # First: get all image paths for path in paths: for file in Path('.').glob(path): try: mio.import_landmark_file( str(Path(file.parent.parent / 'BoundingBoxes' / (file.stem + '.pts'))) ) except ValueError: continue image_paths.append(file) print('Got all image paths...') # Second: split to train, test and validate. 7:2:1 if Path(path_base / 'train_img.txt').exists(): with Path(path_base / 'train_img.txt').open('rb') as train_ifs, \ Path(path_base / 'test_img.txt').open('rb') as test_ifs, \ Path(path_base / 'val_img.txt').open('rb') as val_ifs: train_paths = [Path(line[:-1].decode('utf-8')) for line in train_ifs.readlines()] test_paths = [Path(line[:-1].decode('utf-8')) for line in test_ifs.readlines()] val_paths = [Path(line[:-1].decode('utf-8')) for line in val_ifs.readlines()] else: random.shuffle(image_paths) num_train = int(len(image_paths) * 0.7) num_test = int(len(image_paths) * 0.2) train_paths = sorted(image_paths[:num_train]) test_paths = sorted(image_paths[num_train:num_train+num_test]) val_paths = sorted(image_paths[num_train+num_test:]) with Path(path_base / 'train_img.txt').open('wb') as train_ofs, \ Path(path_base / 'test_img.txt').open('wb') as test_ofs, \ Path(path_base / 'val_img.txt').open('wb') as val_ofs: train_ofs.writelines([str(line).encode('utf-8') + b'\n' for line in train_paths]) test_ofs.writelines([str(line).encode('utf-8') + b'\n' for line in test_paths]) val_ofs.writelines([str(line).encode('utf-8') + b'\n' for line in val_paths]) print('Found Train/Test/Validate {}/{}/{}'.format(len(train_paths), len(test_paths), len(val_paths))) # Third: export reference shape on train if Path(path_base / 'reference_shape.pkl').exists(): reference_shape = PointCloud(mio.import_pickle(path_base / 'reference_shape.pkl')) else: reference_shape = PointCloud(build_reference_shape(train_paths, num_patches)) mio.export_pickle(reference_shape.points, path_base / 'reference_shape.pkl', overwrite=True) print('Created reference_shape.pkl') # Fourth: image shape & pca image_shape = [0, 0, 3] # [H, W, C] if Path(path_base / 'pca.bin').exists() and Path(path_base / 'meta.txt').exists(): with Path(path_base / 'meta.txt').open('r') as ifs: image_shape = [int(x) for x in ifs.read().split(' ')] else: with tf.io.TFRecordWriter(str(path_base / 'pca.bin')) as ofs: counter = 0 for path in train_paths: counter += 1 if verbose: status = 10.0 * counter / len(train_paths) status_str = '\rPreparing {:2.2f}%['.format(status * 10) for i in range(int(status)): status_str += '=' for i in range(int(status), 10): status_str += ' ' status_str += '] {} '.format(path) print(status_str, end='') mp_image = mio.import_image(path) mp_image.landmarks['bb'] = mio.import_landmark_file( str(Path(mp_image.path.parent.parent / 'BoundingBoxes' / (mp_image.path.stem + '.pts'))) ) mp_image = mp_image.crop_to_landmarks_proportion(0.3, group='bb') mp_image = mp_image.rescale_to_pointcloud(reference_shape, group='PTS') mp_image = grey_to_rgb(mp_image) assert(mp_image.pixels.shape[0] == image_shape[2]) image_shape[0] = max(mp_image.pixels.shape[1], image_shape[0]) image_shape[1] = max(mp_image.pixels.shape[2], image_shape[1]) features = tf.train.Features( feature={ 'pca/shape': tf.train.Feature( float_list=tf.train.FloatList(value=mp_image.landmarks['PTS'].points.flatten()) ), 'pca/bb': tf.train.Feature( float_list=tf.train.FloatList(value=mp_image.landmarks['bb'].points.flatten()) ), } ) ofs.write(tf.train.Example(features=features).SerializeToString()) if verbose: print('') with Path(path_base / 'meta.txt').open('w') as ofs: for s in image_shape[:-1]: ofs.write('{} '.format(s)) ofs.write('{}'.format(image_shape[-1])) print('Image shape', image_shape) # Fifth: train data if Path(path_base / 'train.bin').exists(): pass else: random.shuffle(train_paths) with tf.io.TFRecordWriter(str(path_base / 'train.bin')) as ofs: print('Preparing train data...') counter = 0 for path in train_paths: counter += 1 if verbose: status = 10.0 * counter / len(train_paths) status_str = '\rPreparing {:2.2f}%['.format(status * 10) for i in range(int(status)): status_str += '=' for i in range(int(status), 10): status_str += ' ' status_str += '] {} '.format(path) print(status_str, end='') mp_image = mio.import_image(path) mp_image.landmarks['bb'] = mio.import_landmark_file( str(Path(mp_image.path.parent.parent / 'BoundingBoxes' / (mp_image.path.stem + '.pts'))) ) mp_image = mp_image.crop_to_landmarks_proportion(0.3, group='bb') mp_image = mp_image.rescale_to_pointcloud(reference_shape, group='PTS') mp_image = grey_to_rgb(mp_image) # Padding to the same size height, width = mp_image.pixels.shape[1:] # [C, H, W] dy = max(int((image_shape[0] - height - 1) / 2), 0) dx = max(int((image_shape[1] - width - 1) / 2), 0) padded_image = np.random.rand(*image_shape).astype(np.float32) padded_image[dy:(height + dy), dx:(width + dx), :] = mp_image.pixels.transpose(1, 2, 0) padded_landmark = mp_image.landmarks['PTS'].points padded_landmark[:, 0] += dy padded_landmark[:, 1] += dx features = tf.train.Features( feature={ 'train/image': tf.train.Feature( bytes_list=tf.train.BytesList(value=[tf.compat.as_bytes(padded_image.tostring())]) ), 'train/shape': tf.train.Feature( float_list=tf.train.FloatList(value=padded_landmark.flatten()) ) } ) ofs.write(tf.train.Example(features=features).SerializeToString()) if verbose: print('') # Sixth: test data if Path(path_base / 'test.bin').exists(): pass else: with tf.io.TFRecordWriter(str(path_base / 'test.bin')) as ofs: print('Preparing test data...') counter = 0 for path in test_paths: counter += 1 if verbose: status = 10.0 * counter / len(test_paths) status_str = '\rPreparing {:2.2f}%['.format(status * 10) for i in range(int(status)): status_str += '=' for i in range(int(status), 10): status_str += ' ' status_str += '] {} '.format(path) print(status_str, end='') mp_image = mio.import_image(path) mp_image.landmarks['bb'] = mio.import_landmark_file( str(Path(mp_image.path.parent.parent / 'BoundingBoxes' / (mp_image.path.stem + '.pts'))) ) mp_image = mp_image.crop_to_landmarks_proportion(0.3, group='bb') mp_bb = mp_image.landmarks['bb'].bounding_box() mp_image.landmarks['init'] = align_shape_with_bounding_box(reference_shape, mp_bb) mp_image = mp_image.rescale_to_pointcloud(reference_shape, group='init') mp_image = grey_to_rgb(mp_image) # Padding to the same size height, width = mp_image.pixels.shape[1:] # [C, H, W] dy = max(int((256 - height - 1) / 2), 0) # 200*(1+0.3*2)/sqrt(2) == 226.7 dx = max(int((256 - width - 1) / 2), 0) # 200*(1+0.3*2)/sqrt(2) == 226.7 padded_image = np.random.rand(256, 256, 3).astype(np.float32) padded_image[dy:(height + dy), dx:(width + dx), :] = mp_image.pixels.transpose(1, 2, 0) padded_landmark = mp_image.landmarks['PTS'].points padded_landmark[:, 0] += dy padded_landmark[:, 1] += dx padded_init_landmark = mp_image.landmarks['init'].points padded_init_landmark[:, 0] += dy padded_init_landmark[:, 1] += dx features = tf.train.Features( feature={ 'test/image': tf.train.Feature( bytes_list=tf.train.BytesList( value=[tf.compat.as_bytes(padded_image.tostring())]) ), 'test/shape': tf.train.Feature( float_list=tf.train.FloatList(value=padded_landmark.flatten()) ), 'test/init': tf.train.Feature( float_list=tf.train.FloatList(value=padded_init_landmark.flatten()) ) } ) ofs.write(tf.train.Example(features=features).SerializeToString()) if verbose: print('')
def extract_save_features(self, files): r""" Uses the input files as train AAMs and store the resulting pickle on the disk Parameters ---------- files Returns ------- """ # 1. fetch all video frames, attach landmarks frames = mio.import_video(files[0], landmark_resolver=self._myresolver, normalize=True, exact_frame_count=True) # frames = frames.map(AAMFeature._preprocess) idx_above_thresh, idx_lip_opening = landmark_filter( files[0], self._landmarkDir, threshold=self._confidence_thresh, keep=self._kept_frames) frames = frames[idx_above_thresh] frames = frames[idx_lip_opening] frames = frames.map(attach_semantic_landmarks) if self._greyscale is True: frames = frames.map(convert_to_grayscale) # initial AAM training if self._warpType == 'holistic': aam = HolisticAAM(frames, group=self._landmarkGroup, holistic_features=self._features, reference_shape=None, diagonal=self._diagonal, scales=self._scales, max_shape_components=self._max_shape_components, max_appearance_components=self._max_appearance_components, verbose=False) elif self._warpType == 'patch': aam = PatchAAM(frames, group=self._landmarkGroup, holistic_features=self._features, diagonal=self._diagonal, scales=self._scales, max_shape_components=self._max_shape_components, max_appearance_components=self._max_appearance_components, patch_shape=self._extractOpts['patch_shape'], verbose=False) else: raise Exception('Unknown warp type. Did you mean holistic/patch ?') frame_buffer = LazyList.init_from_iterable([]) buffer_len = 256 for idx, file in enumerate(files[1:]): # useful to check progress with open('./run/log_' + self._outModelName + '.txt', 'w') as log: log.write(str(idx) + ' ' + file + '\n') frames = mio.import_video(file, landmark_resolver=self._myresolver, normalize=True, exact_frame_count=True) idx_above_thresh, idx_lip_opening = landmark_filter( file, landmark_dir=self._landmarkDir, threshold=self._confidence_thresh, keep=self._kept_frames) frames = frames[idx_above_thresh] frames = frames[idx_lip_opening] frames = frames.map(attach_semantic_landmarks) if self._greyscale is True: frames = frames.map(convert_to_grayscale) frame_buffer += frames if len(frame_buffer) > buffer_len: # 2. retrain AAM aam.increment(frame_buffer, group=self._landmarkGroup, shape_forgetting_factor=1.0, appearance_forgetting_factor=1.0, verbose=False, batch_size=None) del frame_buffer frame_buffer = LazyList.init_from_iterable([]) else: pass if len(frame_buffer) != 0: # # deplete remaining frames aam.increment(frame_buffer, group=self._landmarkGroup, shape_forgetting_factor=1.0, appearance_forgetting_factor=1.0, verbose=False, batch_size=None) del frame_buffer mio.export_pickle(obj=aam, fp=self._outDir + self._outModelName, overwrite=True, protocol=4)
stock.monthly_change = result['break_out'] stock.save() for result in get_period_analysis(Stock, period=90): stock = result['stock'] stock.seasonally_change = result['break_out'] stock.save() for result in get_period_analysis(Stock, period=180): stock = result['stock'] stock.halfyearly_change = result['break_out'] stock.save() for result in get_period_analysis(Stock, period=365): stock = result['stock'] stock.yearly_change = result['break_out'] stock.save() print 'Periodical Analysis Done' if args.ml_analysis: print 'Machine Learning Analysis Started Data Collection' store_path = os.path.abspath(os.path.join(os.path.dirname(__file__), 'data')) for (pa, pb) in [(30,7),(90,30),(180,30),(180,90),(365,30),(365,90),(365,180)]: print 'Collecting Pattern: {} - {}'.format(pa,pb) slices = data_slicing(Stock, period=pa, forward=pb) mio.export_pickle(slices, '{}/{:03d}_{:03d}_slices.pkl'.format(store_path,pa,pb), overwrite=True) print 'Machine Learning Analysis Done'
def test_export_pickle_with_path_uses_open(mock_open, exists, pickle_dump): exists.return_value = False fake_path = '/fake/fake.pkl.gz' mio.export_pickle(test_lg, fake_path) pickle_dump.assert_called_once() mock_open.assert_called_once_with(fake_path, 'wb')
def generate_fake_images(run_id, snapshot=None, grid_size=[1, 1], batch_size=8, num_pngs=1, image_shrink=1, png_prefix=None, random_seed=1000, minibatch_size=8): network_pkl = misc.locate_network_pkl(run_id, snapshot) if png_prefix is None: png_prefix = misc.get_id_string_for_network_pkl(network_pkl) + '-' random_state = np.random.RandomState(random_seed) print('Loading network from "%s"...' % network_pkl) G, D, Gs = misc.load_network_pkl(run_id, snapshot) lsfm_model = m3io.import_lsfm_model( '/home/baris/Projects/faceganhd/models/all_all_all.mat') lsfm_tcoords = \ mio.import_pickle('/home/baris/Projects/team members/stelios/UV_spaces_V2/UV_dicts/full_face/512_UV_dict.pkl')[ 'tcoords'] lsfm_params = [] result_subdir = misc.create_result_subdir(config.result_dir, config.desc) for png_idx in range(int(num_pngs / batch_size)): start = time.time() print('Generating png %d-%d / %d... in ' % (png_idx * batch_size, (png_idx + 1) * batch_size, num_pngs), end='') latents = misc.random_latents(np.prod(grid_size) * batch_size, Gs, random_state=random_state) labels = np.zeros([latents.shape[0], 0], np.float32) images = Gs.run(latents, labels, minibatch_size=minibatch_size, num_gpus=config.num_gpus, out_shrink=image_shrink) for i in range(batch_size): if images.shape[1] == 3: mio.export_pickle( images[i], os.path.join( result_subdir, '%s%06d.pkl' % (png_prefix, png_idx * batch_size + i))) # misc.save_image(images[i], os.path.join(result_subdir, '%s%06d.png' % (png_prefix, png_idx*batch_size+i)), [0,255], grid_size) elif images.shape[1] == 6: mio.export_pickle(images[i][3:6], os.path.join( result_subdir, '%s%06d.pkl' % (png_prefix, png_idx * batch_size + i)), overwrite=True) misc.save_image( images[i][0:3], os.path.join( result_subdir, '%s%06d.png' % (png_prefix, png_idx * batch_size + i)), [-1, 1], grid_size) elif images.shape[1] == 9: texture = Image(np.clip(images[i, 0:3] / 2 + 0.5, 0, 1)) mesh_raw = from_UV_2_3D(Image(images[i, 3:6])) normals = images[i, 6:9] normals_norm = (normals - normals.min()) / (normals.max() - normals.min()) mesh = lsfm_model.reconstruct(mesh_raw) lsfm_params.append(lsfm_model.project(mesh_raw)) t_mesh = TexturedTriMesh(mesh.points, lsfm_tcoords.points, texture, mesh.trilist) m3io.export_textured_mesh( t_mesh, os.path.join(result_subdir, '%06d.obj' % (png_idx * minibatch_size + i)), texture_extension='.png') mio.export_image( Image(normals_norm), os.path.join( result_subdir, '%06d_nor.png' % (png_idx * minibatch_size + i))) shape = images[i, 3:6] shape_norm = (shape - shape.min()) / (shape.max() - shape.min()) mio.export_image( Image(shape_norm), os.path.join( result_subdir, '%06d_shp.png' % (png_idx * minibatch_size + i))) mio.export_pickle( t_mesh, os.path.join(result_subdir, '%06d.pkl' % (png_idx * minibatch_size + i))) print('%0.2f seconds' % (time.time() - start)) open(os.path.join(result_subdir, '_done.txt'), 'wt').close()
landmarks_dataset[i] = string_array_to_np_array(labels.iloc[i]) return landmarks_dataset # input dataset_csv_path = "/disk1/ofirbartal/Projects/Dataset/GANeratedHands_Release/dataset_csv/test_dataset.csv" landmarks = process_csv_input(dataset_csv_path) start = time.time() # PDM training_shapes = [PointCloud(l) for l in landmarks] shape_model = OrthoPDM(training_shapes, max_n_components=None) shape_model.n_active_components = 0.95 end = time.time() # shape_model = import_pickle(Path('pdm_weights.pkl')) print(shape_model) print("PDM Training Time: {}s".format(end - start)) export_pickle(shape_model, Path('test_pdm_weights.pkl'), overwrite=True) # shape_model.set_target(result_landmarks) # project the target # shape_model.target # the projected target # draw #create canvas on which the triangles will be visualized # canvas = np.full([400,400], 255).astype('uint8') #convert to 3 channel RGB for fun colors! # canvas = cv2.cvtColor(canvas,cv2.COLOR_GRAY2RGB) # draw_shapes(canvas,np.add([mean],100))
def prepare_images(paths, num_patches=73, verbose=True): """Save Train Images to TFRecord, for ShuffleNet Args: paths: a list of strings containing the data directories. num_patches: number of landmarks verbose: boolean, print debugging info. Returns: None """ if len(paths) == 0: return # .../<Dataset>/Images/*.png -> .../<Dataset> path_base = Path(paths[0]).parent.parent image_paths = [] # First & Second: get all image paths; split to train, test and validate. 7:2:1 if Path(path_base / 'train_img.txt').exists(): with Path(path_base / 'train_img.txt').open('rb') as train_ifs, \ Path(path_base / 'test_img.txt').open('rb') as test_ifs, \ Path(path_base / 'val_img.txt').open('rb') as val_ifs: train_paths = [ Path(line[:-1].decode('utf-8')) for line in train_ifs.readlines() ] test_paths = [ Path(line[:-1].decode('utf-8')) for line in test_ifs.readlines() ] val_paths = [ Path(line[:-1].decode('utf-8')) for line in val_ifs.readlines() ] print('Found Train/Test/Validate {}/{}/{}'.format( len(train_paths), len(test_paths), len(val_paths))) else: for path in paths: for file in Path('.').glob(path): try: mio.import_landmark_file( str( Path(file.parent.parent / 'BoundingBoxes' / (file.stem + '.pts')))) except ValueError: continue image_paths.append(file) print('Got all image paths...') random.shuffle(image_paths) num_train = int(len(image_paths) * 0.7) num_test = int(len(image_paths) * 0.2) train_paths = sorted(image_paths[:num_train]) test_paths = sorted(image_paths[num_train:num_train + num_test]) val_paths = sorted(image_paths[num_train + num_test:]) with Path(path_base / 'train_img.txt').open('wb') as train_ofs, \ Path(path_base / 'test_img.txt').open('wb') as test_ofs, \ Path(path_base / 'val_img.txt').open('wb') as val_ofs: train_ofs.writelines( [str(line).encode('utf-8') + b'\n' for line in train_paths]) test_ofs.writelines( [str(line).encode('utf-8') + b'\n' for line in test_paths]) val_ofs.writelines( [str(line).encode('utf-8') + b'\n' for line in val_paths]) print('Write Train/Test/Validate {}/{}/{}'.format( len(train_paths), len(test_paths), len(val_paths))) # Third: export reference shape on train if Path(path_base / 'reference_shape.pkl').exists(): reference_shape = PointCloud( mio.import_pickle(path_base / 'reference_shape.pkl')) else: reference_shape = PointCloud( build_reference_shape(train_paths, num_patches)) mio.export_pickle(reference_shape.points, path_base / 'reference_shape.pkl', overwrite=True) print('Created reference_shape.pkl') # Fourth: image shape & pca # No need for ShuffleNet # Fifth: train data if Path(path_base / 'train.bin').exists(): pass else: random.shuffle(train_paths) with tf.io.TFRecordWriter(str(path_base / 'train.bin')) as ofs: print('Preparing train data...') counter = 0 for path in train_paths: counter += 1 if verbose: status = 10.0 * counter / len(train_paths) status_str = '\rPreparing {:2.2f}%['.format(status * 10) for i in range(int(status)): status_str += '=' for i in range(int(status), 10): status_str += ' ' status_str += '] {} '.format(path) print(status_str, end='') mp_image = load_image(path, 0.7, 336) image = mp_image.pixels.transpose(1, 2, 0).astype(np.float32) shape = mp_image.landmarks['PTS'].points features = tf.train.Features( feature={ 'train/image': tf.train.Feature(bytes_list=tf.train.BytesList( value=[tf.compat.as_bytes(image.tostring())])), 'train/shape': tf.train.Feature(float_list=tf.train.FloatList( value=shape.flatten())) }) ofs.write( tf.train.Example(features=features).SerializeToString()) if verbose: print('') # Sixth: test data if Path(path_base / 'test.bin').exists(): pass else: with tf.io.TFRecordWriter(str(path_base / 'test.bin')) as ofs: print('Preparing test data...') counter = 0 for path in test_paths: counter += 1 if verbose: status = 10.0 * counter / len(test_paths) status_str = '\rPreparing {:2.2f}%['.format(status * 10) for i in range(int(status)): status_str += '=' for i in range(int(status), 10): status_str += ' ' status_str += '] {} '.format(path) print(status_str, end='') mp_image = load_image(path, 1. / 6., 112) mp_image.landmarks['init'] = PointCloud( align_reference_shape_to_112(reference_shape.points)) image = mp_image.pixels.transpose(1, 2, 0).astype(np.float32) shape = mp_image.landmarks['PTS'].points init = mp_image.landmarks['init'].points features = tf.train.Features( feature={ 'test/image': tf.train.Feature(bytes_list=tf.train.BytesList( value=[tf.compat.as_bytes(image.tostring())])), 'test/shape': tf.train.Feature(float_list=tf.train.FloatList( value=shape.flatten())), 'test/init': tf.train.Feature(float_list=tf.train.FloatList( value=init.flatten())) }) ofs.write( tf.train.Example(features=features).SerializeToString()) if verbose: print('')
dist_list = [np.sqrt(i[0] * i[0] + i[1] * i[1]) for i in dist] dist_np = np.array(dist_list) print(dist_np) print(np.average(dist_np)) def chen_comput_relative_error(ground_truth_np, predict_value_np): dist = ground_truth_np - predict_value_np dist_list = [np.sqrt(i[0] * i[0] + i[1] * i[1]) for i in dist] dist_np = np.array(dist_list) print(dist_np) print(np.average(dist_np)) if TRAIN: mio.export_pickle(fitter, '26_img_35_pnt.pkl', overwrite=True) else: fitter = mio.import_pickle('26_img_35_pnt.pkl') tmp = [ 187, 276, 326, 343, 344, 357, 358, 360, 361, 362, 363, 372, 376, 392, 399, 403, 433, 437, 465, 493, 192, 195, 196, 200, 202, 203, 204, 233, 234, 236, 240, 247, 259, 276, 280, 283, 314, 327, 336, 340, 341, 345 ] img_path = R"C:\Users\chen\Desktop\250\output" coordinate_path = R"C:\Users\chen\Desktop\250\coordinate" img_file_list = os.listdir(img_path) for img in img_file_list: # t = input('image number:')
def auto_construct(pdm, images, trilist=None, fit_group='init', train_group='final', models=[], errors=[], costs=[], isplot=False, feature=[igo] * 10, diagonal=200, scales=(0.5, 1.0), n_shape=[2, 4], n_appearance=[20, 30], max_iters=10, generative_iter=30, discriminative_iter = 10, n_processes=24, inc_appearance=0, model_class=HolisticAAM, increament=False, update_shape=False, shape_forgetting_factor=1.0, appearance_forgetting_factor=1.0, export_path=None ): # initialisation DB_size = len(images) / 2 DB1 = images[:DB_size] DB2 = images[DB_size:] init_shape = pdm.shape_models[-1].model.mean() n_iteration = 0 if trilist is None: trilist = TriMesh(init_shape.points).trilist for j in xrange(discriminative_iter): i_appearance = np.array(n_appearance) + np.array(inc_appearance) if (i_appearance > 1).any(): i_appearance = i_appearance.astype(int).tolist() else: i_appearance = i_appearance.tolist() # ------------ generative iterations ------------- for i in xrange(generative_iter): print 'Discriminative Iter: {}, Generative Iter: {}'.format(j, i) aam_fitter = LucasKanadeAAMFitter(pdm, n_shape=n_shape, n_appearance=i_appearance) pdm, error = generative_construct( DB1, aam_fitter, trilist, fit_group=fit_group, train_group=train_group, label='iteration_{:03d}'.format(n_iteration), feature=feature[j], diagonal=diagonal, scales=scales, original_shape_model=None if update_shape else pdm.shape_models, n_processes=n_processes, model_class=model_class, increament_model=pdm if increament else None, shape_forgetting_factor=shape_forgetting_factor, appearance_forgetting_factor=appearance_forgetting_factor, max_iters=max_iters ) n_iteration += 1 models.append(pdm) errors.append(error) if export_path: mio.export_pickle([images, models, errors], export_path, overwrite=True) if isplot: plot(errors) # ----------- discriminative iterations------------ aam_fitter = LucasKanadeAAMFitter(pdm, n_shape=n_shape, n_appearance=i_appearance) frs = mp_fit(DB2, aam_fitter, group=fit_group, n_processes=n_processes, max_iters=max_iters) for img, fr in zip(DB2, frs): img.landmarks[train_group] = fr.final_shape sdm = RegularizedSDM( DB2, diagonal=diagonal, alpha=100, group=train_group, n_iterations=4, scales=(0.5,0.5,1.0,1.0), patch_features=patch_features, n_perturbations=30, patch_shape=[(25, 25), (15, 15), (15,15), (9,9)], verbose=True ) pdm, error = generative_construct( DB1, sdm, trilist, fit_group=fit_group, train_group=train_group, label='discriminative_{:02d}'.format(j), original_shape_model=None if update_shape else pdm.shape_models, feature=feature[j], diagonal=diagonal, scales=scales, n_processes=n_processes, model_class=model_class, increament_model=pdm if increament else None, shape_forgetting_factor=shape_forgetting_factor, appearance_forgetting_factor=appearance_forgetting_factor, max_iters=max_iters ) models.append(pdm) errors.append(error) if export_path: mio.export_pickle([images, models, errors], export_path, overwrite=True) if isplot: plot(errors) return models[-2]
stock.seasonally_change = result['break_out'] stock.save() for result in get_period_analysis(Stock, period=180): stock = result['stock'] stock.halfyearly_change = result['break_out'] stock.save() for result in get_period_analysis(Stock, period=365): stock = result['stock'] stock.yearly_change = result['break_out'] stock.save() print 'Periodical Analysis Done' if args.ml_analysis: print 'Machine Learning Analysis Started Data Collection' store_path = os.path.abspath( os.path.join(os.path.dirname(__file__), 'data')) for (pa, pb) in [(30, 7), (90, 30), (180, 30), (180, 90), (365, 30), (365, 90), (365, 180)]: print 'Collecting Pattern: {} - {}'.format(pa, pb) slices = data_slicing(Stock, period=pa, forward=pb) mio.export_pickle(slices, '{}/{:03d}_{:03d}_slices.pkl'.format( store_path, pa, pb), overwrite=True) print 'Machine Learning Analysis Done'
def generate_interpolation_images(run_id, snapshot=None, grid_size=[1, 1], image_shrink=1, image_zoom=1, duration_sec=60.0, smoothing_sec=1.0, mp4=None, mp4_fps=30, mp4_codec='libx265', mp4_bitrate='16M', random_seed=1000, minibatch_size=8): network_pkl = misc.locate_network_pkl(run_id, snapshot) if mp4 is None: mp4 = misc.get_id_string_for_network_pkl(network_pkl) + '-lerp.mp4' num_frames = int(np.rint(duration_sec * mp4_fps)) random_state = np.random.RandomState(random_seed) print('Loading network from "%s"...' % network_pkl) G, D, Gs = misc.load_network_pkl(run_id, snapshot) print('Generating latent vectors...') shape = [num_frames, np.prod(grid_size) ] + Gs.input_shape[1:] # [frame, image, channel, component] all_latents = random_state.randn(*shape).astype(np.float32) all_latents = scipy.ndimage.gaussian_filter( all_latents, [smoothing_sec * mp4_fps] + [0] * len(Gs.input_shape), mode='wrap') all_latents /= np.sqrt(np.mean(np.square(all_latents))) lsfm_model = m3io.import_lsfm_model( '/home/baris/Projects/faceganhd/models/all_all_all.mat') lsfm_tcoords = \ mio.import_pickle('/home/baris/Projects/team members/stelios/UV_spaces_V2/UV_dicts/full_face/512_UV_dict.pkl')[ 'tcoords'] lsfm_params = [] result_subdir = misc.create_result_subdir(config.result_dir, config.desc) for png_idx in range(int(num_frames / minibatch_size)): start = time.time() print('Generating png %d-%d / %d... in ' % (png_idx * minibatch_size, (png_idx + 1) * minibatch_size, num_frames), end='') latents = all_latents[png_idx * minibatch_size:(png_idx + 1) * minibatch_size, 0] labels = np.zeros([latents.shape[0], 0], np.float32) images = Gs.run(latents, labels, minibatch_size=minibatch_size, num_gpus=config.num_gpus, out_shrink=image_shrink) for i in range(minibatch_size): texture = Image(np.clip(images[i, 0:3] / 2 + 0.5, 0, 1)) mesh_raw = from_UV_2_3D(Image(images[i, 3:6])) normals = images[i, 6:9] normals_norm = (normals - normals.min()) / (normals.max() - normals.min()) mesh = lsfm_model.reconstruct(mesh_raw) lsfm_params.append(lsfm_model.project(mesh_raw)) t_mesh = TexturedTriMesh(mesh.points, lsfm_tcoords.points, texture, mesh.trilist) m3io.export_textured_mesh( t_mesh, os.path.join(result_subdir, '%06d.obj' % (png_idx * minibatch_size + i)), texture_extension='.png') mio.export_image( Image(normals_norm), os.path.join(result_subdir, '%06d_nor.png' % (png_idx * minibatch_size + i))) print('%0.2f seconds' % (time.time() - start)) mio.export_pickle(lsfm_params, os.path.join(result_subdir, 'lsfm_params.pkl')) open(os.path.join(result_subdir, '_done.txt'), 'wt').close()
def load_images(paths, group=None, verbose=True): """Loads and rescales input images to the diagonal of the reference shape. Args: paths: a list of strings containing the data directories. reference_shape: a numpy array [num_landmarks, 2] group: landmark group containing the grounth truth landmarks. verbose: boolean, print debugging info. Returns: images: a list of numpy arrays containing images. shapes: a list of the ground truth landmarks. reference_shape: a numpy array [num_landmarks, 2]. shape_gen: PCAModel, a shape generator. """ images = [] shapes = [] bbs = [] reference_shape = PointCloud(build_reference_shape(paths)) for path in paths: if verbose: print('Importing data from {}'.format(path)) for im in mio.import_images(path, verbose=verbose, as_generator=True): group = group or im.landmarks[group]._group_label bb_root = im.path.parent.relative_to(im.path.parent.parent.parent) if 'set' not in str(bb_root): bb_root = im.path.parent.relative_to(im.path.parent.parent) im.landmarks['bb'] = mio.import_landmark_file(str(Path( 'bbs') / bb_root / (im.path.stem + '.pts'))) im = im.crop_to_landmarks_proportion(0.3, group='bb') im = im.rescale_to_pointcloud(reference_shape, group=group) im = grey_to_rgb(im) images.append(im.pixels.transpose(1, 2, 0)) shapes.append(im.landmarks[group].lms) bbs.append(im.landmarks['bb'].lms) train_dir = Path(FLAGS.train_dir) mio.export_pickle(reference_shape.points, train_dir / 'reference_shape.pkl', overwrite=True) print('created reference_shape.pkl using the {} group'.format(group)) pca_model = detect.create_generator(shapes, bbs) # Pad images to max length max_shape = np.max([im.shape for im in images], axis=0) max_shape = [len(images)] + list(max_shape) padded_images = np.random.rand(*max_shape).astype(np.float32) print(padded_images.shape) for i, im in enumerate(images): height, width = im.shape[:2] dy = max(int((max_shape[1] - height - 1) / 2), 0) dx = max(int((max_shape[2] - width - 1) / 2), 0) lms = shapes[i] pts = lms.points pts[:, 0] += dy pts[:, 1] += dx lms = lms.from_vector(pts) padded_images[i, dy:(height+dy), dx:(width+dx)] = im return padded_images, shapes, reference_shape.points, pca_model
def load_images(paths, group=None, verbose=True): """Loads and rescales input images to the diagonal of the reference shape. Args: paths: a list of strings containing the data directories. reference_shape: a numpy array [num_landmarks, 2] group: landmark group containing the grounth truth landmarks. verbose: boolean, print debugging info. Returns: images: a list of numpy arrays containing images. shapes: a list of the ground truth landmarks. reference_shape: a numpy array [num_landmarks, 2]. shape_gen: PCAModel, a shape generator. """ images = [] shapes = [] bbs = [] reference_shape = PointCloud(build_reference_shape(paths)) for path in paths: if verbose: print('Importing data from {}'.format(path)) for im in mio.import_images(path, verbose=verbose, as_generator=True): group = group or im.landmarks[group]._group_label bb_root = im.path.parent.relative_to(im.path.parent.parent.parent) if 'set' not in str(bb_root): bb_root = im.path.parent.relative_to(im.path.parent.parent) im.landmarks['bb'] = mio.import_landmark_file( str(Path('bbs') / bb_root / (im.path.stem + '.pts'))) im = im.crop_to_landmarks_proportion(0.3, group='bb') im = im.rescale_to_pointcloud(reference_shape, group=group) im = grey_to_rgb(im) images.append(im.pixels.transpose(1, 2, 0)) shapes.append(im.landmarks[group].lms) bbs.append(im.landmarks['bb'].lms) train_dir = Path(FLAGS.train_dir) mio.export_pickle(reference_shape.points, train_dir / 'reference_shape.pkl', overwrite=True) print('created reference_shape.pkl using the {} group'.format(group)) pca_model = detect.create_generator(shapes, bbs) # Pad images to max length max_shape = np.max([im.shape for im in images], axis=0) max_shape = [len(images)] + list(max_shape) padded_images = np.random.rand(*max_shape).astype(np.float32) print(padded_images.shape) for i, im in enumerate(images): height, width = im.shape[:2] dy = max(int((max_shape[1] - height - 1) / 2), 0) dx = max(int((max_shape[2] - width - 1) / 2), 0) lms = shapes[i] pts = lms.points pts[:, 0] += dy pts[:, 1] += dx lms = lms.from_vector(pts) padded_images[i, dy:(height + dy), dx:(width + dx)] = im return padded_images, shapes, reference_shape.points, pca_model
def load_images_aflw(paths, group=None, verbose=True, PLOT=True, AFLW=False, PLOT_shape=False): """Loads and rescales input knn_2D to the diagonal of the reference shape. Args: paths: a list of strings containing the data directories. reference_shape (meanshape): a numpy array [num_landmarks, 2] group: landmark group containing the grounth truth landmarks. verbose: boolean, print debugging info. Returns: knn_2D: a list of numpy arrays containing knn_2D. shapes: a list of the ground truth landmarks. reference_shape (meanshape): a numpy array [num_landmarks, 2]. shape_gen: PCAModel, a shape generator. """ images = [] shapes = [] bbs = [] shape_space = [] plot_shape_x = [] plot_shape_y = [] # compute mean shape if AFLW: # reference_shape = PointCloud(mio.import_pickle(Path('/home/hliu/gmh/RL_FA/mdm_aflw/ckpt/train_aflw') / 'reference_shape.pkl')) reference_shape = mio.import_pickle( Path('/home/hliu/gmh/RL_FA/mdm_aflw/ckpt/train_aflw') / 'reference_shape.pkl') else: reference_shape = PointCloud(build_reference_shape(paths)) for path in paths: if verbose: print('Importing data from {}'.format(path)) for im in mio.import_images(path, verbose=verbose, as_generator=True): # group = group or im.landmarks[group]._group_label group = group or im.landmarks.keys()[0] bb_root = im.path.parent.relative_to(im.path.parent.parent.parent) if 'set' not in str(bb_root): bb_root = im.path.parent.relative_to(im.path.parent.parent) if AFLW: im.landmarks['bb'] = im.landmarks['PTS'].lms.bounding_box() else: im.landmarks['bb'] = mio.import_landmark_file( str(Path('bbs') / bb_root / (im.path.stem + '.pts'))) im = im.crop_to_landmarks_proportion(0.3, group='bb') im = im.rescale_to_pointcloud(reference_shape, group=group) im = grey_to_rgb(im) # knn_2D.append(im.pixels.transpose(1, 2, 0)) shapes.append(im.landmarks[group].lms) shape_space.append(im.landmarks[group].lms.points) bbs.append(im.landmarks['bb'].lms) if PLOT_shape: x_tmp = np.sum((im.landmarks[group].lms.points[:, 0] - reference_shape.points[:, 0])) y_tmp = np.sum((im.landmarks[group].lms.points[:, 1] - reference_shape.points[:, 1])) if x_tmp < 0 and y_tmp < 0: plot_shape_x.append(x_tmp) plot_shape_y.append(y_tmp) shape_space = np.array(shape_space) print('shape_space:', shape_space.shape) train_dir = Path(FLAGS.train_dir) if PLOT_shape: k_nn_plot_x = [] k_nn_plot_y = [] centers = utils.k_means(shape_space, 500, num_patches=19) centers = np.reshape(centers, [-1, 19, 2]) for i in range(centers.shape[0]): x_tmp = np.sum((centers[i, :, 0] - reference_shape.points[:, 0])) y_tmp = np.sum((centers[i, :, 1] - reference_shape.points[:, 1])) if x_tmp < 0 and y_tmp < 0: k_nn_plot_x.append(x_tmp) k_nn_plot_y.append(y_tmp) # plt.scatter(plot_shape_x, plot_shape_y, s=20) # plt.scatter(k_nn_plot_x, k_nn_plot_y, s=40) # plt.xticks(()) # plt.yticks(()) # plt.show() # pdb.set_trace() np.save(train_dir / 'shape_space_all.npy', shape_space) # centers = utils.k_means(shape_space, 100) # centers = np.reshape(centers, [-1, 68, 2]) # np.save(train_dir/'shape_space_origin.npy', centers) # print('created shape_space.npy using the {} group'.format(group)) # exit(0) mio.export_pickle(reference_shape.points, train_dir / 'reference_shape.pkl', overwrite=True) print('created reference_shape.pkl using the {} group'.format(group)) pca_model = detect.create_generator(shapes, bbs) # Pad knn_2D to max length max_shape = [272, 261, 3] padded_images = np.random.rand(*max_shape).astype(np.float32) print(padded_images.shape) if PLOT: # plot without padding centers = utils.k_means(shape_space, 500, num_patches=19) centers = np.reshape(centers, [-1, 19, 2]) plot_img = cv2.imread('a.png').transpose(2, 0, 1) centers_tmp = np.zeros(centers.shape) # menpo_img = mio.import_image('a.png') menpo_img = menpo.image.Image(plot_img) for i in range(centers.shape[0]): menpo_img.view() min_y = np.min(centers[i, :, 0]) min_x = np.min(centers[i, :, 1]) centers_tmp[i, :, 0] = centers[i, :, 0] - min_y + 20 centers_tmp[i, :, 1] = centers[i, :, 1] - min_x + 20 print(centers_tmp[i, :, :]) menpo_img.landmarks['center'] = PointCloud(centers_tmp[i, :, :]) menpo_img.view_landmarks(group='center', marker_face_colour='b', marker_size='16') # menpo_img.landmarks['center'].view(render_legend=True) plt.savefig('plot_shape_space_aflw/' + str(i) + '.png') plt.close() exit(0) # !!!shape_space without delta, which means shape_space has already been padded! # delta = np.zeros(shape_space.shape) for i, im in enumerate(images): height, width = im.shape[:2] dy = max(int((max_shape[0] - height - 1) / 2), 0) dx = max(int((max_shape[1] - width - 1) / 2), 0) lms = shapes[i] pts = lms.points pts[:, 0] += dy pts[:, 1] += dx shape_space[i, :, 0] += dy shape_space[i, :, 1] += dx # delta[i][:, 0] = dy # delta[i][:, 1] = dx lms = lms.from_vector(pts) padded_images[i, dy:(height + dy), dx:(width + dx)] = im # shape_space = np.concatenate((shape_space, delta), 2) centers = utils.k_means(shape_space, 1000, num_patches=19) centers = np.reshape(centers, [-1, 19, 2]) # pdb.set_trace() np.save(train_dir / 'shape_space.npy', centers) print('created shape_space.npy using the {} group'.format(group)) exit(0) return padded_images, shapes, reference_shape.points, pca_model, centers
import menpo.io as mio from menpofit.aam import HolisticAAM from menpo.feature import fast_dsift def process(image, crop_proportion=0.2, max_diagonal=400): if image.n_channels == 3: image = image.as_greyscale() image = image.crop_to_landmarks_proportion(crop_proportion) d = image.diagonal() if d > max_diagonal: image = image.rescale(float(max_diagonal) / d) #labeller(image, 'PTS', face_ibug_68_to_face_ibug_68_trimesh) return image path_to_images = '../../../data/lfpw/trainset_34/' training_images = mio.import_images(path_to_images, verbose=True) training_images = training_images.map(process) aam = HolisticAAM(training_images, holistic_features=fast_dsift, verbose=True, scales=1, max_shape_components=16, max_appearance_components=104) mio.export_pickle(aam, "aam.pkl", overwrite=True, protocol=4)
# list of numpy arrays we have already constructed (one per level) fitter_kwargs = dict(lk_algorithm_cls=WibergInverseCompositional) # kwargs for fitter.fit_from_{bb, shape} # (note here we reuse the same kwargs twice) fit_kwargs = dict(max_iters=[25, 5]) # Partial over the PickleWrappedFitter to prepare an object that can be # invoked at load time fitter_wrapper = partial(PickleWrappedFitter, LucasKanadeAAMFitter, fitter_args, fitter_kwargs, fit_kwargs, fit_kwargs, image_preprocess=image_greyscale_crop_preprocess) # save the pickle down.这里输入模型保存文件名 mio.export_pickle(fitter_wrapper, 'pretrained12131_aam.pkl') # % matplotlib inline # method to load a database def load_database(path_to_images, crop_percentage, max_images=None): images = [] # load landmarked images for i in mio.import_images(path_to_images, max_images=max_images, verbose=True): # crop image i = i.crop_to_landmarks_proportion(crop_percentage) # convert it to grayscale if needed if i.n_channels == 3: i = i.as_greyscale(mode='luminosity')