def evaluate_model( model, num_views, path, device, args, normalise_scale=1, ): import pickle was_training = model.training model.eval() with torch.no_grad(): m = pickle.load( open('models/basicModel_%s_lbs_10_207_0_v1.0.0.pkl' % args.gender[0])) csv_name = os.path.join(path, 'real_male.csv') real_height = [1.760837, 1.696549, 1.779879, 1.766942, 1.780513] # Check real human with open(csv_name, mode='w') as csv_file: for i in range(0, args.dataset_size): inputs = torch.FloatTensor([]) for k in range(0, num_views): # go through the views img_name = os.path.join(path, '%d_%d.png' % (i, k)) # image img = Image.open(img_name) img = ToTensor()(img)[0] ''' for x in range(img.shape[0]): for y in range(img.shape[1]): if img[x][y] > 0.02: img[x][y] = 1 ''' img.unsqueeze_(0) img = torch.cat((img, img, img), 0) img.unsqueeze_(0) inputs = torch.cat((inputs, img), 0) inputs = inputs.to(device) par_prd = model(inputs) batch = par_prd.shape[0] rots, poses, betas = decompose_par(par_prd) mesh_prd = par_to_mesh(args.gender, rots, poses, betas) X, Y, Z = [ mesh_prd[:, :, 0], mesh_prd[:, :, 1], mesh_prd[:, :, 2] ] h_prd, w_prd, c_prd, n_prd, a_prd = vertex2measurements( X, Y, Z) vertices = torch.reshape(mesh_prd, (batch, -1)) ratio = real_height[i] / h_prd csv_writer = csv.writer(csv_file, delimiter=',', lineterminator='\n') csv_writer.writerow([i, 'Height', float(real_height[i])]) csv_writer.writerow([ i, 'Waist', float(w_prd * ratio), ]) csv_writer.writerow([ i, 'Chest', float(c_prd * ratio), ]) csv_writer.writerow([i, 'Neck', float(n_prd * ratio)]) csv_writer.writerow([ i, 'Arm', float(a_prd * ratio), ]) csv_writer.writerow(['\n']) # rendering vertices_num = 6890 batch = 1 img_height = 264 img_width = 192 # frontal view vertices = mesh_prd name = '%d_0_prd' % i v = vertices.squeeze().to("cpu").numpy() angle = np.pi axis = np.array([0, 0, 1]) v = transpose_mesh(v, angle, axis) angle = np.pi axis = np.array([0, 1, 0]) v = transpose_mesh(v, angle, axis) v_0 = scale_mesh(v, 400, 400, vertices_num=vertices_num) mesh2Image(v_0, m['f'], batch, path, name, 400, 400, vertices_num=vertices_num) image = Image.open(join(path, name + '.png')) output = scale_fixed_height(image, img_height, img_width) output_name = join(path, name + '.png') output.save(output_name) # side view name = '%d_1_prd' % i angle = np.pi / 2 axis = np.array([0, -1, 0]) v = transpose_mesh(v, angle, axis) v_1 = scale_mesh(v, 400, 400, vertices_num=vertices_num) mesh2Image(v_1, m['f'], batch, path, name, 400, 400, vertices_num=vertices_num) image = Image.open(join(path, name + '.png')) output = scale_fixed_height(image, img_height, img_width) output_name = join(path, name + '.png') output.save(output_name) # mesh generator outmesh_path = join(path, '%d.obj' % i) with open(outmesh_path, 'w') as fp: for v in mesh_prd[0]: #fp.write( 'v %f %f %f\n' % ( float(v[0]),float(v[1]), float(v[2])) ) fp.write('v %f %f %f\n' % (v[0], v[1], v[2])) for f in m[ 'f'] + 1: # Faces are 1-based, not 0-based in obj files fp.write('f %d %d %d\n' % (f[0], f[1], f[2])) csv_file.close() model.train(mode=was_training)
def main(): args = parse_args() #args.dataset_size = 10 gender = 'male' print('-----------------------------------------------------------') print 'Gender: ', gender m = pickle.load( open('models/basicModel_%s_lbs_10_207_0_v1.0.0.pkl' % gender[0])) dataset_size = args.dataset_size print 'Dataset range:', args.start_num, ' - ', dataset_size + args.start_num - 1 parent_dic = '/home/yifu/workspace/data/synthetic/noisy/val' print 'Data Path: ', parent_dic device = torch.device("cuda:%d" % args.gpu if torch.cuda.is_available() else "cpu") torch.cuda.set_device(device) print('-----------------------------------------------------------') if raw_input('Confirm the above setting? (yes/no): ') != 'yes': print('Terminated') exit() print 'Data generation starts' print('------------------------') for i in range(0, dataset_size): i = i + args.start_num rots = (torch.zeros(1, 3)).cuda() # global rotation poses = (torch.zeros(1, 23, 3)).cuda() # joints variation = 3 betas = (torch.rand(1, 10) * variation * 2 - variation).cuda() # shapes poses[0][17 - 1][2] = np.pi / 4 # right shoulder joint poses[0][16 - 1][2] = -np.pi / 4 # left shoulder joint variation = 0.3 poses[0][17 - 1][2] += (torch.rand(1)[0] * variation * 2 - variation).cuda() poses[0][16 - 1][2] = -poses[0][17 - 1][2] variation = 0.15 poses[0][16 - 1][2] += (torch.rand(1)[0] * variation * 2 - variation).cuda() poses[0][1 - 1][2] = np.pi / 40 variation = np.pi / 40 poses[0][1 - 1][2] += (torch.rand(1)[0] * variation * 2 - variation).cuda() poses[0][2 - 1][2] = -np.pi / 40 variation = np.pi / 40 poses[0][2 - 1][2] += (torch.rand(1)[0] * variation * 2 - variation).cuda() variation = 0.03 for j in range(0, 23): for k in range(0, 3): poses[0][j][k] += torch.rand(1)[0] * variation * 2 - variation vertices = par_to_mesh(gender, rots, poses, betas) # rendering vertices_num = 6890 path = parent_dic batch = 1 img_height = 264 img_width = 192 # frontal view name = '%d_0' % i v = vertices.squeeze().to("cpu").numpy() angle = np.pi axis = np.array([0, 0, 1]) v = transpose_mesh(v, angle, axis) angle = np.pi axis = np.array([0, 1, 0]) v = transpose_mesh(v, angle, axis) v_0 = scale_mesh(v, 400, 400, vertices_num=vertices_num) mesh2Image(v_0, m['f'], batch, path, name, 400, 400, vertices_num=vertices_num) image = Image.open(join(path, name + '.png')) output = scale_fixed_height(image, img_height, img_width) output_name = join(path, name + '.png') output.save(output_name) # side view name = '%d_1' % i angle = np.pi / 2 axis = np.array([0, -1, 0]) v = transpose_mesh(v, angle, axis) v_1 = scale_mesh(v, 400, 400, vertices_num=vertices_num) mesh2Image(v_1, m['f'], batch, path, name, 400, 400, vertices_num=vertices_num) image = Image.open(join(path, name + '.png')) output = scale_fixed_height(image, img_height, img_width) output_name = join(path, name + '.png') output.save(output_name) # pickle poses = torch.reshape(poses, (1, -1)) parameters = torch.cat((rots[0], poses[0], betas[0]), 0).to("cpu") pickle.dump(parameters, open('%s/%d' % (path, i), 'wb')) '''
csv_writer.writerow([i, 'Arm', arm]) csv_writer.writerow(['\n']) batch = 1 height = 400 width = 400 mesh = vertices name = name[:-4] #face = m.f faces = faces.astype(np.uint32) name = name + '_0' angle = np.pi axis = np.array([0, 0, 1]) mesh = transpose_mesh(mesh, angle, axis) angle = np.pi axis = np.array([0, 1, 0]) mesh = transpose_mesh(mesh, angle, axis) mesh_0 = scale_mesh(mesh, height, width, vertices_num = vertices_n) mesh2Image(mesh_0, faces, batch, path, name, height, width, vertices_num = vertices_n) name = name[:-2] + '_1' angle = -np.pi/2 axis = np.array([0, 1, 0]) mesh = transpose_mesh(mesh, angle, axis) mesh_1 = scale_mesh(mesh, height, width,vertices_num = vertices_n) mesh2Image(mesh_1, faces, batch, path, name, height, width,vertices_num = vertices_n)
h, w, c, n, a = vertex2measurements(X, Y, Z) #vertices = par_to_mesh(gender, rots, poses, betas) # rendering parent_dic = '/home/yifu/workspace/data_smpl/test' vertices_num = 6890 path = parent_dic batch = 1 # frontal view name = 'test_0' v = vertices.squeeze().to("cpu").numpy() angle = np.pi axis = np.array([0, 0, 1]) v = transpose_mesh(v, angle, axis) angle = np.pi axis = np.array([0, 1, 0]) v = transpose_mesh(v, angle, axis) v_0 = scale_mesh(v, 400, 400, vertices_num=vertices_num) mesh2Image(v_0, m['f'], batch, path, name, 400, 400, vertices_num=vertices_num) # side view name = 'test_1' angle = np.pi / 2 axis = np.array([0, -1, 0]) v = transpose_mesh(v, angle, axis) v_1 = scale_mesh(v, 400, 400, vertices_num=vertices_num) mesh2Image(v_1, m['f'], batch, path, name, 400, 400, vertices_num=vertices_num) # mesh generator outmesh_path = join(path, '%d.obj' % i)