print('img_np.HAPE', img_np.shape) img = torch.from_numpy(img_np).float() # Get predict heatmap heatmaps_pred_eg = net_hg_torch(img) heatmaps_pred_eg = heatmaps_pred_eg heatmaps_pred_eg = heatmaps_pred_eg[-1].double() #(1,16,64,64) # Reshape pred heatmaps heatmaps_pred_eg_np = heatmaps_pred_eg.detach().numpy() heatmaps_pred_eg_np = np.squeeze(heatmaps_pred_eg_np, axis=0) heatmaps_pred_eg_np = np.swapaxes(np.swapaxes(heatmaps_pred_eg_np, 0, 2), 0, 1) #(64,64,16) # Show heatmaps imgutils.show_heatmaps(img_np_copy, heatmaps_pred_eg_np) # Stack points coord_joints = modelutils.heatmaps_to_coords(heatmaps_pred_eg_np, resolu_out=[256, 256], prob_threshold=0.1) imgutils.show_stack_joints(img_np_copy, coord_joints, draw_lines=True) #========================================================== TEST BY DATASET ========================================================== # num_subset = 1 # 1-22 # num_instance = 277 # 0-999 256 # # name_file_img = 'datasets/np__train_imgs_' + str(num_subset) + '.npy' # name_file_heatmap = 'datasets/np__train_heatmaps_' + str(num_subset) + '.npy' # name_file_pt = 'datasets/np__train_pts_' + str(num_subset) + '.npy' #
loss = criteon(heatmaps_pred, heatmaps, target_weight) print('LOSS:', loss) optimizer.zero_grad() loss.backward() optimizer.step() # Show pred heatmaps heatmaps_pred_np = heatmaps_pred_copy.detach().numpy() # heatmaps_pred_np = np.squeeze(heatmaps_pred_np, axis=0) heatmaps_pred_np = np.swapaxes(np.swapaxes(heatmaps_pred_np, 0, 2), 0, 1) # (64,64,16) img_np = img_copy.detach().numpy() # img_np = np.squeeze(img_np, axis=0) img_np = np.swapaxes(np.swapaxes(img_np, 0, 2), 0, 1) # (64,64,16) imgutils.show_heatmaps(img_np, heatmaps_pred_np) plt.pause(1) plt.ioff() # if step > 5: # break # ================================== Load the model ================================== # net_hg_torch = hg_torch(num_stacks=1, num_blocks=1, num_classes=16) # path_model_torch_load = 'models/modelparams_hg_torch_' + str(num_subset) + '.pkl' # net_hg_torch.load_state_dict(torch.load(path_model_torch_load)) # net_hg_torch.eval() # print('===============MODEL LOADED===============') # ================================== Save the model ================================== path_model_torch = 'models/modelparams_hg_torch_' + str(num_subset) + '.pkl'
# Show last layer output heatmaps_pred_copy = heatmaps_pred_final[1] heatmaps_copy = heatmaps[1] img_copy = img[1] # Show pred heatmaps heatmaps_pred_np = heatmaps_pred_copy.detach().cpu().numpy() heatmaps_pred_np = np.transpose(heatmaps_pred_np, (1, 2, 0)) heatmaps_np = heatmaps_copy.detach().cpu().numpy() heatmaps_np = np.transpose(heatmaps_np, (1, 2, 0)) img_np = img_copy.detach().cpu().numpy() img_np = np.transpose(img_np, (1, 2, 0)) # In case too many figs to print if step % 5 == 0: imgutils.show_heatmaps(img_np, heatmaps_pred_np, num_fig=1) imgutils.show_heatmaps(img_np, heatmaps_np, num_fig=2) plt.pause(0.5) print('===================================================') if (step % 300 == 0) and (step > 0): suffix_epoch = i suffix_step = step path_ckpt_torch = 'models/ckpt_hg_torch_EPOCH' + str( suffix_epoch) + 'STEP' + str(suffix_step) + '.tar' torch.save( { 'epoch': i, 'step': step, 'model_state_dict': net_hg_torch.state_dict(), 'optimizer_state_dict': optimizer.state_dict(),
# # print('img_np', img_np.shape) # # print('heatmaps_np', heatmaps_np.shape) # img_np = np.swapaxes(np.swapaxes(img_np, 0, 2), 0, 1) # heatmaps_np = np.swapaxes(np.swapaxes(heatmaps_np, 0, 2), 0, 1) # print('img_np', img_np.shape) # print('heatmaps_np', heatmaps_np.shape) # # imgutils.show_heatmaps(img_np, heatmaps_np) # joint_pts = modelutils.heatmaps_to_coords(heatmaps_np, resolu_out=[64,64], prob_threshold=0.0) # img_np = skimage.transform.resize(img_np, [64,64]) # imgutils.show_stack_joints(img_np, joint_pts, c=[0, 0]) # pass if __name__ == '__main__': # ========== Test dataset ========== ds_torch = DatasetTorch(use_flip=True, use_rand_color=True) data_loader = datatorch.DataLoader(ds_torch, batch_size=1, shuffle=False) for step, (img, heatmaps, pts) in enumerate(data_loader): print('img', img) # print('heatmaps', heatmaps.shape) # print('pts', pts.shape) img = np.transpose(img.squeeze().detach().numpy(), (1, 2, 0)) # img = np.fliplr(img) heatmaps = np.transpose(heatmaps.squeeze().detach().numpy(), (1, 2, 0)) pts = pts.squeeze().detach().numpy() print('pts', pts) print('===========================================================') imgutils.show_heatmaps(img, heatmaps)
# ================================== Visualize ================================== # Load a single image num_eg = 123 img_eg = train_imgs[num_eg, ...] #(256,256,3) img_eg_np = img_eg.numpy() img_eg = img_eg[np.newaxis, ...] #(1,256,256,3) # Load heatmaps heatmaps_eg = train_heatmaps[num_eg, ...] heatmaps_eg = heatmaps_eg.numpy() heatmaps_eg_pred = heatmap_pred = net_hg(img_eg) heatmaps_eg_pred = heatmaps_eg_pred[-1] heatmaps_eg_pred = tf.squeeze(heatmaps_eg_pred, axis=0) #(64,64,16) heatmaps_eg_pred_np = heatmaps_eg_pred.numpy() coord_joints_eg_pred = modelutils.heatmaps_to_coords(heatmaps_eg_pred, resolu_out=[256, 256]) coord_joints_true = train_pts[num_eg, ...].numpy() * (256 / 64) # Show imgs imgutils.show_stack_joints(img_eg_np, coord_joints_true) imgutils.show_stack_joints(img_eg_np, coord_joints_eg_pred) # Show heatmaps imgutils.show_heatmaps(img_eg_np, heatmaps_eg) imgutils.show_heatmaps(img_eg_np, heatmaps_eg_pred_np) # ================================== Visualize ================================== print('===============================DONE===============================')