(ckpt_path, model_name, N=512, model_type='bacon', hidden_layers=8,
hidden_size=256, output_layers=[1, 2, 4, 8], w0=30, pe=8,
filter_mesh=False, scaling=None, return_sdf=False)
| 24 | |
| 25 | |
| 26 | def export_model(ckpt_path, model_name, N=512, model_type='bacon', hidden_layers=8, |
| 27 | hidden_size=256, output_layers=[1, 2, 4, 8], w0=30, pe=8, |
| 28 | filter_mesh=False, scaling=None, return_sdf=False): |
| 29 | |
| 30 | with HiddenPrints(): |
| 31 | # the network has 4 output levels of detail |
| 32 | num_outputs = len(output_layers) |
| 33 | max_frequency = 3*(32,) |
| 34 | |
| 35 | # load model |
| 36 | if len(output_layers) > 1: |
| 37 | model = modules.MultiscaleBACON(3, hidden_size, 1, |
| 38 | hidden_layers=hidden_layers, |
| 39 | bias=True, |
| 40 | frequency=max_frequency, |
| 41 | quantization_interval=np.pi, |
| 42 | is_sdf=True, |
| 43 | output_layers=output_layers, |
| 44 | reuse_filters=True) |
| 45 | |
| 46 | print(model) |
| 47 | ckpt = torch.load(ckpt_path, map_location=device) |
| 48 | model.load_state_dict(ckpt) |
| 49 | model = model.to(device) |
| 50 | |
| 51 | # write output |
| 52 | x = torch.linspace(-0.5, 0.5, N) |
| 53 | if return_sdf: |
| 54 | x = torch.arange(-N//2, N//2) / N |
| 55 | x = x.float() |
| 56 | x, y, z = torch.meshgrid(x, x, x) |
| 57 | render_coords = torch.stack((x.flatten(), y.flatten(), z.flatten()), dim=-1).to(device) |
| 58 | sdf_values = [np.zeros((N**3, 1)) for i in range(num_outputs)] |
| 59 | |
| 60 | # render in a batched fashion to save memory |
| 61 | bsize = int(128**2) |
| 62 | for i in tqdm(range(int(N**3 / bsize))): |
| 63 | coords = render_coords[i*bsize:(i+1)*bsize, :] |
| 64 | out = model({'coords': coords})['model_out'] |
| 65 | |
| 66 | if not isinstance(out, list): |
| 67 | out = [out,] |
| 68 | |
| 69 | for idx, sdf in enumerate(out): |
| 70 | sdf_values[idx][i*bsize:(i+1)*bsize] = sdf.detach().cpu().numpy() |
| 71 | |
| 72 | return [sdf.reshape(N, N, N) for sdf in sdf_values] |
| 73 | |
| 74 | |
| 75 | def normalize(coords, scaling=0.9): |
no test coverage detected