def display(tfrecord_dir, bfm_path, param_mean_std_path, image_size, num_images=5, n_tex_para=40): print('Loading sdataset %s' % tfrecord_dir) batch_size = 4 dset = dataset.TFRecordDatasetSupervised(tfrecord_dir=tfrecord_dir, batch_size=batch_size, repeat=False, shuffle_mb=0) print('Loading BFM model') bfm = TfMorphableModel(model_path=bfm_path, n_tex_para=n_tex_para) idx = 0 filename = '/opt/project/output/verify_dataset/supervised/20200525/image_batch_{0}_indx_{1}.jpg' unnormalize_labels = fn_unnormalize_300W_LP_labels( param_mean_std_path=param_mean_std_path, image_size=image_size) while idx < num_images: try: image_tensor, labels_tensor = dset.get_minibatch_tf() except tf.errors.OutOfRangeError: break # render images using labels roi, landmarks, pose_para, shape_para, exp_para, color_para, illum_para, tex_para = split_300W_LP_labels( labels_tensor) roi, landmarks, pose_para, shape_para, exp_para, color_para, illum_para, tex_para = unnormalize_labels( batch_size, roi, landmarks, pose_para, shape_para, exp_para, color_para, illum_para, tex_para) image_rendered = render_batch(pose_param=pose_para, shape_param=shape_para, exp_param=exp_para, tex_param=tex_para, color_param=color_para, illum_param=illum_para, frame_height=image_size, frame_width=image_size, tf_bfm=bfm, batch_size=batch_size).numpy().astype( np.uint8) for i in range(batch_size): images = np.concatenate( (image_tensor[i].numpy().astype(np.uint8), image_rendered[i]), axis=0) # images = image_rendered[i] imageio.imsave(filename.format(idx, i), images) idx += 1 print('\nDisplayed %d images' % idx)
def example_render_batch3(pic_names: list, tf_bfm: TfMorphableModel, n_tex_para: int, save_to_folder: str, resolution: int): batch_size = len(pic_names) images_orignal = load_images(pic_names, '/opt/project/examples/Data/80k/') shape_param_batch, exp_param_batch, pose_param_batch = load_params_80k( pic_names=pic_names) shape_param = tf.squeeze(shape_param_batch) exp_param = tf.squeeze(exp_param_batch) pose_param = tf.squeeze(pose_param_batch) pose_param = tf.concat([ pose_param[:, :-1], tf.constant(0.0, shape=(batch_size, 1), dtype=tf.float32), pose_param[:, -1:] ], axis=1) lm = tf_bfm.get_landmarks(shape_param, exp_param, pose_param, batch_size, 450, is_2d=True, is_plot=True) images_rendered = render_batch( pose_param=pose_param, shape_param=shape_param, exp_param=exp_param, tex_param=tf.constant(0.0, shape=(len(pic_names), n_tex_para), dtype=tf.float32), color_param=None, illum_param=None, frame_height=450, frame_width=450, tf_bfm=tf_bfm, batch_size=batch_size).numpy().astype(np.uint8) for i, pic_name in enumerate(pic_names): fig = plt.figure() ax = fig.add_subplot(1, 2, 1) plot_image_w_lm(ax, resolution, images_orignal[i], lm[i]) ax = fig.add_subplot(1, 2, 2) plot_image_w_lm(ax, resolution, images_rendered[i], lm[i]) plt.savefig(os.path.join(save_to_folder, pic_name))
def inverse_rendering(bfm_path, exp_path, pd_model_path, param_mean_std_path, save_to, image_folder, image_names, batch_size, resolution, n_tex_para, n_shape_para): print('Loading BFM model') bfm = TfMorphableModel( model_path=bfm_path, exp_path=exp_path, n_shape_para=n_shape_para, n_tex_para=n_tex_para ) model = load_model(pd_model_path=pd_model_path) rendered_filename_tmp = os.path.join(save_to, '{0}_rendered.jpg') unnormalize_labels = fn_unnormalize_ffhq_labels(param_mean_std_path=param_mean_std_path, image_size=image_size) images = [] images_names = [] plt.gca().set_axis_off() plt.subplots_adjust(top=1, bottom=0, right=1, left=0, hspace=0, wspace=0) plt.margins(0, 0) plt.gca().xaxis.set_major_locator(plt.NullLocator()) plt.gca().yaxis.set_major_locator(plt.NullLocator()) for img_name, img in load_images(image_folder=image_folder, image_names=image_names, resolution=resolution): images.append(img) images_names.append(img_name) if len(images) == batch_size: inference_and_render_images( images=images, images_names=images_names, model=model, bfm=bfm, unnormalize_labels=unnormalize_labels, rendered_filename_tmp=rendered_filename_tmp) images = [] images_names = [] if images: inference_and_render_images( images=images, images_names=images_names, model=model, bfm=bfm, unnormalize_labels=unnormalize_labels, rendered_filename_tmp=rendered_filename_tmp)
def example_render_batch2(pic_names: list, tf_bfm: TfMorphableModel, save_to_folder: str, n_tex_para:int): batch_size = len(pic_names) images_orignal = load_images(pic_names, '/opt/project/examples/Data/300W_LP/') shape_param_batch, exp_param_batch, tex_param_batch, color_param_batch, illum_param_batch, pose_param_batch, lm_batch = \ load_params(pic_names=pic_names, n_tex_para=n_tex_para) # pose_param: [batch, n_pose_param] # shape_param: [batch, n_shape_para] # exp_param: [batch, n_exp_para] # tex_param: [batch, n_tex_para] # color_param: [batch, n_color_para] # illum_param: [batch, n_illum_para] shape_param_batch = tf.squeeze(shape_param_batch) exp_param_batch = tf.squeeze(exp_param_batch) tex_param_batch = tf.squeeze(tex_param_batch) color_param_batch = tf.squeeze(color_param_batch) illum_param_batch = tf.squeeze(illum_param_batch) pose_param_batch = tf.squeeze(pose_param_batch) lm_rended = tf_bfm.get_landmarks(shape_param_batch, exp_param_batch, pose_param_batch, batch_size, 450, is_2d=True, is_plot=True) images_rendered = render_batch( pose_param=pose_param_batch, shape_param=shape_param_batch, exp_param=exp_param_batch, tex_param=tex_param_batch, color_param=color_param_batch, illum_param=illum_param_batch, frame_height=450, frame_width=450, tf_bfm=tf_bfm, batch_size=batch_size ).numpy().astype(np.uint8) for i, pic_name in enumerate(pic_names): fig = plt.figure() ax = fig.add_subplot(1, 2, 1) plot_image_w_lm(ax, 450, images_orignal[i], lm_batch[i]) ax = fig.add_subplot(1, 2, 2) plot_image_w_lm(ax, 450, images_rendered[i], lm_rended[i]) plt.savefig(os.path.join(save_to_folder, pic_name))
is_plot=True) images_rendered = render_batch( pose_param=pose_param_batch, shape_param=shape_param_batch, exp_param=exp_param_batch, tex_param=tex_param_batch, color_param=color_param_batch, illum_param=illum_param_batch, frame_height=450, frame_width=450, tf_bfm=tf_bfm, batch_size=batch_size ).numpy().astype(np.uint8) for i, pic_name in enumerate(pic_names): fig = plt.figure() ax = fig.add_subplot(1, 2, 1) plot_image_w_lm(ax, 450, images_orignal[i], lm_batch[i]) ax = fig.add_subplot(1, 2, 2) plot_image_w_lm(ax, 450, images_rendered[i], lm_rended[i]) plt.savefig(os.path.join(save_to_folder, pic_name)) if __name__ == '__main__': n_tex_para = 40 tf_bfm = TfMorphableModel(model_path='./examples/Data/BFM/Out/BFM.mat', n_tex_para=n_tex_para) save_rendered_to = './output/render_batch/' pic_names = ['IBUG_image_014_01_2', 'AFW_134212_1_0', 'IBUG_image_008_1_0'] example_render_batch(pic_names=pic_names, tf_bfm=tf_bfm, save_to_folder=save_rendered_to, n_tex_para=n_tex_para)
ax.plot(lm[0, 31:36], lm[1, 31:36], marker='o', markersize=2, linestyle='-', color='w', lw=2) ax.plot(lm[0, 36:42], lm[1, 36:42], marker='o', markersize=2, linestyle='-', color='w', lw=2) ax.plot(lm[0, 42:48], lm[1, 42:48], marker='o', markersize=2, linestyle='-', color='w', lw=2) ax.plot(lm[0, 48:60], lm[1, 48:60], marker='o', markersize=2, linestyle='-', color='w', lw=2) ax.plot(lm[0, 60:68], lm[1, 60:68], marker='o', markersize=2, linestyle='-', color='w', lw=2) plt.savefig(filename) if __name__ == '__main__': n_tex_para = 40 bfm = TfMorphableModel(model_path='/opt/project/examples/Data/BFM/Out/BFM.mat', n_tex_para=n_tex_para) output_folder = '/opt/project/output/landmarks/landmark2' pic_names = ['image00002', 'IBUG_image_014_01_2', 'AFW_134212_1_0', 'IBUG_image_008_1_0'] # pic_names = ['IBUG_image_008_1_0'] batch_size = len(pic_names) images = load_images(pic_names, '/opt/project/examples/Data') resolution = 450 shape_param, exp_param, _, _, _, pose_param = load_params(pic_names=pic_names, n_tex_para=n_tex_para, data_folder='/opt/project/examples/Data/') landmarks = compute_landmarks( bfm=bfm, shape_param=shape_param, exp_param=exp_param, pose_param=pose_param, batch_size=batch_size, resolution=resolution) save_landmarks(images=images, landmarks=landmarks, output_folder=output_folder, resolution=resolution)
batch_size=batch_size).numpy().astype(np.uint8) for i, pic_name in enumerate(pic_names): fig = plt.figure() ax = fig.add_subplot(1, 2, 1) plot_image_w_lm(ax, resolution, images_orignal[i], lm[i]) ax = fig.add_subplot(1, 2, 2) plot_image_w_lm(ax, resolution, images_rendered[i], lm[i]) plt.savefig(os.path.join(save_to_folder, pic_name)) if __name__ == '__main__': n_shape_para = 100 n_tex_para = 40 resolution = 450 tf_bfm = TfMorphableModel( model_path='/opt/project/examples/Data/BFM/Out/BFM.mat', exp_path='/opt/project/examples/Data/BFM/Out/exp_80k.npz', n_shape_para=n_shape_para, n_tex_para=n_tex_para) save_rendered_to = './out' pic_names = [ 'afw_225191079_1_aug_29', 'helen_train_164866565_1_aug_28', 'ibug_image_007_aug_18', 'lfpw_train_image_0465_aug_11' ] example_render_batch3(pic_names=pic_names, tf_bfm=tf_bfm, n_tex_para=n_tex_para, save_to_folder=save_rendered_to, resolution=resolution)
from example_utils import load_images, load_params from sandbox.landmarks.check_get_landmarks2 import save_landmarks from tf_3dmm.morphable_model.morphable_model import TfMorphableModel if __name__ == '__main__': n_tex_para = 40 bfm = TfMorphableModel(model_path='/opt/project/examples/Data/BFM/Out/BFM.mat', n_tex_para=n_tex_para) output_folder = '/opt/project/output/landmarks/landmark3' pic_names = ['image00002', 'IBUG_image_014_01_2', 'AFW_134212_1_0', 'IBUG_image_008_1_0'] # pic_names = ['IBUG_image_008_1_0'] batch_size = len(pic_names) images = load_images(pic_names, '/opt/project/examples/Data') resolution = 450 shape_param, exp_param, _, _, _, pose_param = load_params(pic_names=pic_names, n_tex_para=n_tex_para, data_folder='/opt/project/examples/Data/') landmarks = bfm.get_landmarks( shape_param=shape_param, exp_param=exp_param, pose_param=pose_param, batch_size=batch_size, resolution=resolution, is_2d=True, is_plot=True ) save_landmarks(images=images, landmarks=landmarks, output_folder=output_folder, resolution=resolution)
def render_batch(pose_param, shape_param, exp_param, tex_param, color_param, illum_param, frame_width: int, frame_height: int, tf_bfm: TfMorphableModel, batch_size: int): """ render faces in batch :param: pose_param: [batch, n_pose_para] or (batch, 1, n_pose_param) :param: shape_param: [batch, n_shape_para, 1] or [batch, n_shape_para] :param: exp_param: [batch, n_exp_para, 1] or [batch, n_exp_para] :param: tex_param: [batch, n_tex_para, 1] or [batch, n_tex_para] :param: color_param: [batch, 1, n_color_para] or [batch, n_color_para] :param: illum_param: [batch, 1, n_illum_para] or [batch, n_illum_para] :param: frame_width: rendered image width :param: frame_height: rendered image height :param: tf_bfm: basel face model :param: batch_size: batch size :return: images, [batch, frame_width, frame_height, 3] """ assert is_tf_expression(pose_param) pose_shape = tf.shape(pose_param) if pose_shape.shape[0] == 2: tf.debugging.assert_shapes( [(pose_param, (batch_size, tf_bfm.get_num_pose_param()))], message='pose_param shape wrong, dim != ({batch}, {dim})'.format( batch=batch_size, dim=tf_bfm.get_num_pose_param())) pose_param = tf.expand_dims(pose_param, 1) elif pose_shape.shape[0] == 3: tf.debugging.assert_shapes( [(pose_param, (batch_size, 1, tf_bfm.get_num_pose_param()))], message='pose_param shape wrong, dim != ({batch}, 1, {dim})'. format(batch=batch_size, dim=tf_bfm.get_num_pose_param())) else: raise ValueError( 'pose_param shape wrong, dim != ({batch}, 1, {dim}) or ({batch}, {dim})' .format(batch=batch_size, dim=tf_bfm.get_num_pose_param())) vertices = tf_bfm.get_vertices(shape_param=shape_param, exp_param=exp_param, batch_size=batch_size) # vertex_norm = lighting.vertex_normals(vertices, tf_bfm.triangles) vertex_norm = tfg.geometry.representation.mesh.normals.vertex_normals( vertices=vertices, indices=tf.repeat(tf.expand_dims(tf_bfm.triangles, 0), batch_size, axis=0), clockwise=True) colors = tf_bfm.get_vertex_colors(tex_param=tex_param, color_param=color_param, illum_param=illum_param, vertex_norm=-vertex_norm, batch_size=batch_size) colors = tf.clip_by_value(colors / 255., 0., 1.) transformed_vertices = affine_transform(vertices=vertices, scaling=pose_param[:, 0, 6:], angles_rad=pose_param[:, 0, 0:3], t3d=pose_param[:, 0:, 3:6]) transformed_vertices_x = transformed_vertices[:, :, 0] * 2 / frame_width - 1 transformed_vertices_y = transformed_vertices[:, :, 1] * 2 / frame_height - 1 transformed_vertices_z = -transformed_vertices[:, :, 2] / tf.reduce_max( tf.abs(transformed_vertices[:, :, 2])) # Convert vertices to homogeneous coordinates transformed_vertices = tf.concat([ tf.expand_dims(transformed_vertices_x, axis=2), tf.expand_dims(transformed_vertices_y, axis=2), tf.expand_dims(transformed_vertices_z, axis=2), tf.ones_like(transformed_vertices[:, :, -1:]) ], axis=2) # Render the G-buffer image = dirt.rasterise_batch( vertices=transformed_vertices, faces=tf.tile(tf.expand_dims(tf_bfm.triangles, axis=0), (batch_size, 1, 1)), # faces=tf.expand_dims(tf_bfm.triangles, axis=0), vertex_colors=colors, background=tf.zeros([batch_size, frame_height, frame_width, 3]), width=frame_width, height=frame_height, channels=3) return image * 255
def load_3dmm(pic_name): mat_filename = '../../examples/Data/{0}.mat'.format(pic_name) mat_data = sio.loadmat(mat_filename) sp = tf.constant(mat_data['Shape_Para'], dtype=tf.float32) ep = tf.constant(mat_data['Exp_Para'], dtype=tf.float32) tp = tf.constant(mat_data['Tex_Para'], dtype=tf.float32) cp = tf.constant(mat_data['Color_Para'], dtype=tf.float32) ip = tf.constant(mat_data['Illum_Para'], dtype=tf.float32) pp = tf.constant(mat_data['Pose_Para'], dtype=tf.float32) return sp, ep, tp, cp, ip, pp tf_bfm = TfMorphableModel('../../examples/Data/BFM/Out/BFM.mat') image_size = 450 # --load mesh data pic_name1 = 'IBUG_image_014_01_2' pic_name2 = 'AFW_134212_1_0' sp1, ep1, tp1, cp1, ip1, pp1 = load_3dmm(pic_name1) sp2, ep2, tp2, cp2, ip2, pp2 = load_3dmm(pic_name2) mix_ratio = 0.2 spm = sp1 * mix_ratio + sp2 * (1 - mix_ratio) epm = ep1 * mix_ratio + ep2 * (1 - mix_ratio) image1 = render(pose_param=pp1, shape_param=sp1,
n_tex_para = 40 mat_data = sio.loadmat(mat_filename) shape_param = tf.constant(mat_data['Shape_Para'], dtype=tf.float32) shape_param = tf.expand_dims(shape_param, 0) exp_param = tf.constant(mat_data['Exp_Para'], dtype=tf.float32) exp_param = tf.expand_dims(exp_param, 0) tex_param = tf.constant(mat_data['Tex_Para'][:n_tex_para, :], dtype=tf.float32) tex_param = tf.expand_dims(tex_param, 0) color_param = tf.constant(mat_data['Color_Para'], dtype=tf.float32) color_param = tf.expand_dims(color_param, 0) illum_param = tf.constant(mat_data['Illum_Para'], dtype=tf.float32) illum_param = tf.expand_dims(illum_param, 0) pose_param = tf.constant(mat_data['Pose_Para'], dtype=tf.float32) pose_param = tf.expand_dims(pose_param, 0) tf_bfm = TfMorphableModel(model_path='../../examples/Data/BFM/Out/BFM.mat', n_tex_para=n_tex_para) vertices = tf_bfm.get_vertices(shape_param=shape_param, exp_param=exp_param, batch_size=1) vertex_norm = lighting.vertex_normals(vertices, tf_bfm.triangles) bvertex_colors = tf_bfm.get_vertex_colors(tex_param, color_param, illum_param, -vertex_norm, 1) vertex_colors = tf_bfm.get_vertex_colors(tex_param[0], color_param[0], illum_param[0], -vertex_norm[0]) print('done')
from tf_3dmm.mesh.transform import affine_transform from tf_3dmm.morphable_model.morphable_model import TfMorphableModel from scipy import io as sio import tensorflow as tf tf_bfm = TfMorphableModel('../../examples/Data/BFM/Out/BFM.mat') # --load mesh data pic_name = 'IBUG_image_008_1_0' # pic_name = 'IBUG_image_014_01_2' mat_filename = '../../examples/Data/{0}.mat'.format(pic_name) mat_data = sio.loadmat(mat_filename) sp = tf.constant(mat_data['Shape_Para'], dtype=tf.float32) ep = tf.constant(mat_data['Exp_Para'], dtype=tf.float32) vertices = tf_bfm.get_vertices(sp, ep) triangles = tf_bfm.triangles pp = tf.constant(mat_data['Pose_Para'], dtype=tf.float32) s = pp[0, 6] angles = pp[0, 0:3] t = pp[0, 3:6] transformed_vertices = affine_transform(vertices, s, angles, t) landmarks_raw = tf.gather_nd( transformed_vertices, tf.reshape(tf_bfm.get_landmark_indices(), (-1, 1))) landmarks = tf.concat([ tf.reshape(landmarks_raw[:, 0], (-1, 1)), 450 - tf.reshape(landmarks_raw[:, 1], (-1, 1)) - 1 ], axis=1)
def display(tfrecord_dir, bfm_path, exp_path, param_mean_std_path, image_size, num_images=5, n_tex_para=40, n_shape_para=100): print('Loading sdataset %s' % tfrecord_dir) batch_size = 4 dset = dataset.TFRecordDatasetSupervised(tfrecord_dir=tfrecord_dir, batch_size=batch_size, repeat=False, shuffle_mb=0) print('Loading BFM model') bfm = TfMorphableModel(model_path=bfm_path, exp_path=exp_path, n_shape_para=n_shape_para, n_tex_para=n_tex_para) idx = 0 filename = '/opt/project/output/verify_dataset/supervised-80k/20200717/image_batch_{0}_indx_{1}.jpg' unnormalize_labels = fn_unnormalize_80k_labels( param_mean_std_path=param_mean_std_path, image_size=image_size) while idx < num_images: try: image_tensor, labels_tensor = dset.get_minibatch_tf() except tf.errors.OutOfRangeError: break # render images using labels pose_para, shape_para, exp_para, _, _, _ = split_80k_labels( labels_tensor) pose_para, shape_para, exp_para, _, _, _ = unnormalize_labels( batch_size, pose_para, shape_para, exp_para, None, None, None) # add 0 to t3d z axis # 80k dataset only have x, y translation pose_para = tf.concat([ pose_para[:, :-1], tf.constant(0.0, shape=(batch_size, 1), dtype=tf.float32), pose_para[:, -1:] ], axis=1) landmarks = bfm.get_landmarks(shape_para, exp_para, pose_para, batch_size, image_size, is_2d=True, is_plot=True) image_rendered = render_batch( pose_param=pose_para, shape_param=shape_para, exp_param=exp_para, tex_param=tf.constant(0.0, shape=(batch_size, n_tex_para), dtype=tf.float32), color_param=None, illum_param=None, frame_height=image_size, frame_width=image_size, tf_bfm=bfm, batch_size=batch_size).numpy().astype(np.uint8) for i in range(batch_size): fig = plt.figure() # input image ax = fig.add_subplot(1, 2, 1) ax.imshow(image_tensor[i].numpy().astype(np.uint8)) ax.plot(landmarks[i, 0, 0:17], landmarks[i, 1, 0:17], marker='o', markersize=2, linestyle='-', color='w', lw=2) ax.plot(landmarks[i, 0, 17:22], landmarks[i, 1, 17:22], marker='o', markersize=2, linestyle='-', color='w', lw=2) ax.plot(landmarks[i, 0, 22:27], landmarks[i, 1, 22:27], marker='o', markersize=2, linestyle='-', color='w', lw=2) ax.plot(landmarks[i, 0, 27:31], landmarks[i, 1, 27:31], marker='o', markersize=2, linestyle='-', color='w', lw=2) ax.plot(landmarks[i, 0, 31:36], landmarks[i, 1, 31:36], marker='o', markersize=2, linestyle='-', color='w', lw=2) ax.plot(landmarks[i, 0, 36:42], landmarks[i, 1, 36:42], marker='o', markersize=2, linestyle='-', color='w', lw=2) ax.plot(landmarks[i, 0, 42:48], landmarks[i, 1, 42:48], marker='o', markersize=2, linestyle='-', color='w', lw=2) ax.plot(landmarks[i, 0, 48:60], landmarks[i, 1, 48:60], marker='o', markersize=2, linestyle='-', color='w', lw=2) ax.plot(landmarks[i, 0, 60:68], landmarks[i, 1, 60:68], marker='o', markersize=2, linestyle='-', color='w', lw=2) ax2 = fig.add_subplot(1, 2, 2) ax2.imshow(image_rendered[i]) ax2.plot(landmarks[i, 0, 0:17], landmarks[i, 1, 0:17], marker='o', markersize=2, linestyle='-', color='w', lw=2) ax2.plot(landmarks[i, 0, 17:22], landmarks[i, 1, 17:22], marker='o', markersize=2, linestyle='-', color='w', lw=2) ax2.plot(landmarks[i, 0, 22:27], landmarks[i, 1, 22:27], marker='o', markersize=2, linestyle='-', color='w', lw=2) ax2.plot(landmarks[i, 0, 27:31], landmarks[i, 1, 27:31], marker='o', markersize=2, linestyle='-', color='w', lw=2) ax2.plot(landmarks[i, 0, 31:36], landmarks[i, 1, 31:36], marker='o', markersize=2, linestyle='-', color='w', lw=2) ax2.plot(landmarks[i, 0, 36:42], landmarks[i, 1, 36:42], marker='o', markersize=2, linestyle='-', color='w', lw=2) ax2.plot(landmarks[i, 0, 42:48], landmarks[i, 1, 42:48], marker='o', markersize=2, linestyle='-', color='w', lw=2) ax2.plot(landmarks[i, 0, 48:60], landmarks[i, 1, 48:60], marker='o', markersize=2, linestyle='-', color='w', lw=2) ax2.plot(landmarks[i, 0, 60:68], landmarks[i, 1, 60:68], marker='o', markersize=2, linestyle='-', color='w', lw=2) plt.savefig(filename.format(idx, i)) idx += 1
import time import scipy.io as sio import tensorflow as tf import tensorflow_graphics as tfg from dirt import lighting from tf_3dmm.morphable_model.morphable_model import TfMorphableModel tf.debugging.set_log_device_placement(True) n_tex_para = 40 tf_bfm = TfMorphableModel( model_path='/opt/project/examples/Data/BFM/Out/BFM.mat', n_tex_para=n_tex_para) pic_names = [ 'image00002', 'IBUG_image_014_01_2', 'AFW_134212_1_0', 'IBUG_image_008_1_0' ] * 8 batch_size = len(pic_names) def my_load_params(pic_names, n_tex_para): # --load mesh data shape_param_batch = [] exp_param_batch = [] tex_param_batch = [] color_param_batch = [] illum_param_batch = [] pose_param_batch = []
def display(tfrecord_dir, bfm_path, param_mean_std_path, image_size, num_images=5, n_tex_para=40): print('Loading sdataset %s' % tfrecord_dir) batch_size = 4 dset = dataset.TFRecordDatasetSupervised(tfrecord_dir=tfrecord_dir, batch_size=batch_size, repeat=False, shuffle_mb=0) print('Loading BFM model') bfm = TfMorphableModel(model_path=bfm_path, n_tex_para=n_tex_para) idx = 0 filename = '/opt/project/output/verify_dataset/supervised/20200525/image_batch_{0}_indx_{1}.jpg' unnormalize_labels = fn_unnormalize_300W_LP_labels( param_mean_std_path=param_mean_std_path, image_size=image_size) while idx < num_images: try: image_tensor, labels_tensor = dset.get_minibatch_tf() except tf.errors.OutOfRangeError: break # render images using labels roi, landmarks, pose_para, shape_para, exp_para, color_para, illum_para, tex_para = split_300W_LP_labels( labels_tensor) roi, landmarks, pose_para, shape_para, exp_para, color_para, illum_para, tex_para = unnormalize_labels( batch_size, roi, landmarks, pose_para, shape_para, exp_para, color_para, illum_para, tex_para) image_rendered = render_batch(pose_param=pose_para, shape_param=shape_para, exp_param=exp_para, tex_param=tex_para, color_param=color_para, illum_param=illum_para, frame_height=image_size, frame_width=image_size, tf_bfm=bfm, batch_size=batch_size).numpy().astype( np.uint8) for i in range(batch_size): fig = plt.figure() # input image ax = fig.add_subplot(1, 2, 1) ax.imshow(image_tensor[i].numpy().astype(np.uint8)) ax.plot(landmarks[i, 0, 0:17], landmarks[i, 1, 0:17], marker='o', markersize=2, linestyle='-', color='w', lw=2) ax.plot(landmarks[i, 0, 17:22], landmarks[i, 1, 17:22], marker='o', markersize=2, linestyle='-', color='w', lw=2) ax.plot(landmarks[i, 0, 22:27], landmarks[i, 1, 22:27], marker='o', markersize=2, linestyle='-', color='w', lw=2) ax.plot(landmarks[i, 0, 27:31], landmarks[i, 1, 27:31], marker='o', markersize=2, linestyle='-', color='w', lw=2) ax.plot(landmarks[i, 0, 31:36], landmarks[i, 1, 31:36], marker='o', markersize=2, linestyle='-', color='w', lw=2) ax.plot(landmarks[i, 0, 36:42], landmarks[i, 1, 36:42], marker='o', markersize=2, linestyle='-', color='w', lw=2) ax.plot(landmarks[i, 0, 42:48], landmarks[i, 1, 42:48], marker='o', markersize=2, linestyle='-', color='w', lw=2) ax.plot(landmarks[i, 0, 48:60], landmarks[i, 1, 48:60], marker='o', markersize=2, linestyle='-', color='w', lw=2) ax.plot(landmarks[i, 0, 60:68], landmarks[i, 1, 60:68], marker='o', markersize=2, linestyle='-', color='w', lw=2) ax2 = fig.add_subplot(1, 2, 2) ax2.imshow(image_rendered[i]) ax2.plot(landmarks[i, 0, 0:17], landmarks[i, 1, 0:17], marker='o', markersize=2, linestyle='-', color='w', lw=2) ax2.plot(landmarks[i, 0, 17:22], landmarks[i, 1, 17:22], marker='o', markersize=2, linestyle='-', color='w', lw=2) ax2.plot(landmarks[i, 0, 22:27], landmarks[i, 1, 22:27], marker='o', markersize=2, linestyle='-', color='w', lw=2) ax2.plot(landmarks[i, 0, 27:31], landmarks[i, 1, 27:31], marker='o', markersize=2, linestyle='-', color='w', lw=2) ax2.plot(landmarks[i, 0, 31:36], landmarks[i, 1, 31:36], marker='o', markersize=2, linestyle='-', color='w', lw=2) ax2.plot(landmarks[i, 0, 36:42], landmarks[i, 1, 36:42], marker='o', markersize=2, linestyle='-', color='w', lw=2) ax2.plot(landmarks[i, 0, 42:48], landmarks[i, 1, 42:48], marker='o', markersize=2, linestyle='-', color='w', lw=2) ax2.plot(landmarks[i, 0, 48:60], landmarks[i, 1, 48:60], marker='o', markersize=2, linestyle='-', color='w', lw=2) ax2.plot(landmarks[i, 0, 60:68], landmarks[i, 1, 60:68], marker='o', markersize=2, linestyle='-', color='w', lw=2) plt.savefig(filename.format(idx, i)) idx += 1 print('\nDisplayed %d images' % idx)
def init_bfm(self): bfm_path = os.path.join(self.bfm_dir, 'BFM.mat') self.bfm = TfMorphableModel(model_path=bfm_path, exp_path=self.exp_path, n_tex_para=self.n_tex_para, n_shape_para=self.n_shape_para)