MCPcopy Create free account
hub / github.com/drinkingcoder/NeuralMarker / synthesis_data

Function synthesis_data

synthesis_datasets.py:15–67  ·  view source on GitHub ↗
(args)

Source from the content-addressed store, hash-verified

13
14
15def synthesis_data(args):
16 data = pd.read_csv(args.csv)
17 augmentor = Augmentor(args)
18 forward_warp = ForwardWarp()
19
20 H, W = args.image_size
21 coords1 = coords_grid(1, H, W, 'cuda:0').contiguous().cuda()
22
23 if not os.path.exists(args.save_dir):
24 os.mkdir(args.save_dir)
25 os.mkdir(os.path.join(args.save_dir, 'images'))
26 os.mkdir(os.path.join(args.save_dir, 'flows'))
27 os.mkdir(os.path.join(args.save_dir, 'masks'))
28
29 for idx in tqdm(range(len(data))):
30 item = data.iloc[idx]
31
32 im = cv2.imread(os.path.join(args.root, item['fg_path']))
33 bg = cv2.imread(os.path.join(args.root, item['bg_path']))
34 tnf_type = item['tnf_type']
35 theta = None
36 if tnf_type == 'affine':
37 theta = item[3:9].tolist()
38 elif tnf_type == 'hom':
39 theta = item[3:11].tolist()
40 elif tnf_type == 'tps':
41 theta = item[3:].tolist()
42
43 fg = torch.from_numpy(im).permute([2, 0, 1])
44 theta = torch.Tensor(theta)
45
46 im_warp, grid_map, _, _ = augmentor(fg, tnf_type, theta)
47
48 grid = coords_grid(1, H, W, grid_map.device)[0].permute(1,2,0)
49 flow_gt = grid_map - grid
50
51 out = forward_warp(coords1.cuda(), flow_gt[None].permute([0,3,1,2]).cuda())
52 coords2 = out[0][0] / out[1][0]
53 intensity = torch.norm(out[0][0], dim=0, p=0).cuda()
54 mask = intensity != 0
55 forward_flow = (coords2 - coords1) * mask
56
57 warp_mask = (grid_map[:,:,0] >= 0) & (grid_map[:,:,0] < W) & \
58 (grid_map[:,:,1] >= 0) & (grid_map[:,:,1] < H)
59
60 mask_for_blend = np.expand_dims(warp_mask, 2).repeat(3, 2)
61 im_warp = im_warp.permute([1,2,0]) * mask_for_blend + bg * ~mask_for_blend
62 im_warp = torch.clip(im_warp, 0, 255).numpy().astype(np.uint8)
63
64 cv2.imwrite(os.path.join(args.save_dir, 'images/image_{:06d}_0.png'.format(idx)), im)
65 cv2.imwrite(os.path.join(args.save_dir, 'images/image_{:06d}_1.png'.format(idx)), im_warp)
66 cv2.imwrite(os.path.join(args.save_dir, 'masks/mask_{:06d}.png'.format(idx)), mask.cpu().numpy().astype(np.uint8))
67 writeFlow(os.path.join(args.save_dir, 'flows/flow_{:06d}.flo'.format(idx)), forward_flow[0].permute([1,2,0]).cpu())
68
69
70if __name__ == '__main__':

Callers 1

Calls 4

AugmentorClass · 0.90
ForwardWarpClass · 0.90
coords_gridFunction · 0.90
writeFlowFunction · 0.90

Tested by

no test coverage detected