if __name__ == '__main__': with open(pred_path,'r') as predf, open(target_path, 'r') as tarf: print(pred_path) print(target_path) pred_data = json.load(predf) tar_data = json.load(tarf) if show_ground_truth: draw(frames=np.array(pred_data['skeletons']),video_path=pred_video, tempo_path=tempo_path, target_frames=np.array(tar_data['skeletons'])) else: draw(frames=np.array(pred_data['skeletons']),video_path=pred_video, tempo_path=tempo_path) if to_video: start, end = load_start_end_frame_num(config_path) gr_duration,_ = load_skeleton(target_path) pred_duration,_ = load_skeleton(pred_path) audio = AudioFileClip(audio_path) start = start + gr_duration -pred_duration end = start + pred_duration sub = audio.subclip(start/fps, end/fps) print('Analyzed the audio, found a period of %.02f seconds' % sub.duration) video = VideoFileClip(pred_video, audio=False) video = video.set_audio(sub) video.write_videofile(pred_video2) pass # with open(input_path2,'r') as fin: # data2 = json.load(fin) # # draw(np.array(data2['skeletons']), center=np.array(data2['center']), avi_path=file_path2, tempo_path=tempo_path)
def test(test_sample_dir, acoustic_features_scaler, motion_features_scaler, tempo_scaler=None): print("\n........testing........\n") test_acoustic_features, temporal_indexes, test_motion_features = load_features_from_dir( test_sample_dir) if tempo_scaler is not None: temporal_indexes = tempo_scaler.transform(temporal_indexes) test_acoustic_features = acoustic_features_scaler.transform( test_acoustic_features) test_motion_features = motion_features_scaler.transform( test_motion_features) if with_tempo_features: test_acoustic_features = np.hstack( (test_acoustic_features, temporal_indexes)) # num_test_seq = int(len(test_acoustic_features) / seq_len) # test_acoustic_features = test_acoustic_features[:num_test_seq * seq_len, :].reshape(num_test_seq, seq_len, -1) # test_motion_features = test_motion_features[:num_test_seq * seq_len, :].reshape(num_test_seq, seq_len, -1) print("shape:{0}".format(test_acoustic_features.shape)) # model_path = os.path.join(ck_dir, model_name + "_epoch_%d.pth"%last_epoch) model_path = os.path.join(ck_dir, model_name + "_latest.pth") print("model load from %s" % model_path) checkpoint = torch.load(model_path) model = create_model(model_name) model.load_state_dict(checkpoint['net']) model.eval() # test_dataset = TensorDataset(torch.from_numpy(test_acoustic_features[np.newaxis,:]), torch.from_numpy(test_motion_features[np.newaxis,:])) test_dataset = TensorDataset( torch.from_numpy(test_acoustic_features[np.newaxis, ]), torch.from_numpy(test_motion_features[np.newaxis, ])) test_dataloader = DataLoader(dataset=test_dataset, batch_size=test_batch_size, shuffle=False, drop_last=False, num_workers=num_workers) criterion = nn.MSELoss() predict_motion_features = np.empty([0, motion_size]) with torch.no_grad(): for i, (batch_x, batch_y) in enumerate(test_dataloader): batch_x, batch_y = Variable(batch_x), Variable(batch_y) batch_x, batch_y = batch_x.to(device), batch_y.to(device) output = model(batch_x) if model_name == 'LSTM-AE'or model_name=='LSTM-VAE'\ : loss = criterion(output[1], batch_y) output = np.reshape(output[1].detach().cpu().numpy(), newshape=[-1, motion_size]) batch_y = np.reshape(batch_y.detach().cpu().numpy(), newshape=[-1, motion_size]) else: # loss = criterion(output, batch_y) output = np.reshape(output.detach().cpu().numpy(), newshape=[-1, motion_size]) batch_y = np.reshape(batch_y.detach().cpu().numpy(), newshape=[-1, motion_size]) # output = output.detach().cpu().numpy() # loss_data = loss.detach().cpu().numpy() predict_motion_features = np.append(predict_motion_features, output, axis=0) predict_real_motion_features = motion_features_scaler.inverse_transform( predict_motion_features) predict_real_motion_features = np.reshape( predict_real_motion_features, newshape=[-1, motion_size // 3, 3]) center = load_skeleton( os.path.join(test_sample_dir, 'skeletons.json'))[1][:len(predict_real_motion_features)] if with_centering: for i in range(len(predict_real_motion_features)): for j in range(len(predict_real_motion_features[i])): predict_real_motion_features[i][j] += center[i] data = dict() output_json_fn = os.path.join(test_sample_dir, output_json) data['length'] = len(predict_real_motion_features) data['center'] = center data['skeletons'] = predict_real_motion_features.tolist() with open(output_json_fn, 'w') as f: json.dump(data, f) print("saved as %s" % output_json_fn) pass