hzxie/Pix2Vox

binvox_rw write error

Closed this issue · 25 comments

voxels_flat = np.transpose(dense_voxel_data, (0, 2, 1)).flatten()
Value Error Axes does not match array

hzxie commented

Sorry. This code does not belong to our repository.

Could you give me more information (e.g., Stacktrace)?
What the dimension of dense_voxel_data?

dense_voxel_data = {ndarray} [[[[0 0 0 ... 0 0 0]\n [0 0 0 ... 0 0 0]\n [0 0 0 ... 0 0 0]\n ...\n [0 0 0 ... 0 0 0]\n [0 0 0 ... 0 0 0]\n [0 0 0 ... 0 0 0]]\n\n [[0 0 0 ... 0 0 0]\n [0 0 0 ... 0 0 0]\n [0 0 0 ... 0 0 0]\n ...\n [0 0 0 ... 0 0 0]\n [0 0 0 ... 0 0 0]\n [0 0 0 ... 0 0 0]]\n\n [[0 0 0 ... 0 0 0]\n [0 0 0 ... 0 0 0]\n [0 0 0 ... 0 0 0]\n ...\n [0 0 0 ... 0 0 0]\n [0 0 0 ... 0 0 0]\n [0 0 0 ... 0 0 0]]\n\n ...\n\n [[0 0 0 ... 0 0 0]\n [0 0 0 ... 0 0 0]\n [0 0 0 ... 0 0 0]\n ...\n [0 0 0 ... 0 0 0]\n [0 0 0 ... 0 0 0]\n [0 0 0 ... 0 0 0]]\n\n [[0 0 0 ... 0 0 0]\n [0 0 0 ... 0 0 0]\n [0 0 0 ... 0 0 0]\n ...\n [0 0 0 ... 0 0 0]\n [0 0 0 ... 0 0 0]\n [0 0 0 ... 0 0 0]]\n\n [[0 0 0 ... 0 0 0]\n [0 0 0 ... 0 0 0]\n [0 0 0 ... 0 0 0]\n ...\n [0 0 0 ... 0 0 0]\n [0 0 0 ... 0 0 0]\n [0 0 0 ... 0 0 0]]]]...View as Array
min = {int32} 0
max = {int32} 0
shape = {tuple} <class 'tuple'>: (1, 32, 32, 32)
dtype = {dtype} int32
size = {int} 32768
array = {NdArrayItemsContainer} <pydevd_plugins.extensions.types.pydevd_plugin_numpy_types.NdArrayItemsContainer object at 0x00000214B704B408>
fp = {BufferedWriter} <_io.BufferedWriter name='./Dataset/MyData/img/car/0.binvox'>
voxel_model = {Voxels} <utils.binvox_rw.Voxels object at 0x00000214B717B648>
axis_order = {str} 'xyz'
data = {ndarray} [[[[4.12672758e-03 9.65105253e-04 4.31137247e-04 ... 8.34944192e-04\n 1.15571811e-03 2.34578969e-03]\n [6.66419975e-04 7.60835028e-05 5.23159542e-05 ... 8.94689467e-04\n 1.25940854e-03 1.63068483e-03]\n [4.29904059e-04 1.93328615e-05 6.54814039e-06 ... 1.64839160e-03\n 2.13542068e-03 2.35589431e-03]\n ...\n [6.47075532e-04 1.66824393e-04 8.87120987e-05 ... 3.96082287e-05\n 1.05672225e-04 3.33915959e-04]\n [1.23017374e-03 2.86122464e-04 1.53107394e-04 ... 1.31211258e-04\n 1.91692947e-04 7.43913813e-04]\n [3.76402633e-03 1.12431322e-03 6.25228800e-04 ... 8.41685629e-04\n 1.23549323e-03 3.16208438e-03]]\n\n [[1.24823116e-03 4.83557757e-04 2.33715793e-04 ... 3.39462655e-04\n 4.68665879e-04 1.17384817e-03]\n [2.35141517e-04 7.20135286e-05 3.65141896e-05 ... 2.84382870e-04\n 7.30881118e-04 1.26359216e-03]\n [1.11213492e-04 2.66721654e-05 2.78255429e-05 ... 4.85076307e-04\n 1.56331132e-03 2.75347452e-03]\n ...\n [5.72633173e-04 3.35470395e-04 1.95151515e-04 ... 1.91......View as Array
dims = {tuple} <class 'tuple'>: (32, 32, 32)
scale = {int} 1
translate = {tuple} <class 'tuple'>: (0, 0, 0)

dense_voxel_data = voxel_model.data.astype(int)

making all the array values to be zero

Traceback (most recent call last):
File "C:\Work\Pix2Vox\core\test.py", line 332, in test_3dreconstruct
vox.write(f)
File "C:\Work\Pix2Vox\utils\binvox_rw.py", line 103, in write
write(self, fp)
File "C:\Work\Pix2Vox\utils\binvox_rw.py", line 270, in write
voxels_flat = np.transpose(dense_voxel_data, (0, 2, 1)).flatten()
File "C:\Users\703257542\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\core\fromnumeric.py", line 639, in transpose
return _wrapfunc(a, 'transpose', axes)
File "C:\Users\703257542\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\core\fromnumeric.py", line 56, in _wrapfunc
return getattr(obj, method)(*args, **kwds)
ValueError: axes don't match array
[INFO/MainProcess] process shutting down

Process finished with exit code 1

Sorry. This code does not belong to our repository.

Could you give me more information (e.g., Stacktrace)?
What the dimension of dense_voxel_data?

Traceback (most recent call last):
File "C:\Work\Pix2Vox\core\test.py", line 332, in test_3dreconstruct
vox.write(f)
File "C:\Work\Pix2Vox\utils\binvox_rw.py", line 103, in write
write(self, fp)
File "C:\Work\Pix2Vox\utils\binvox_rw.py", line 270, in write
voxels_flat = np.transpose(dense_voxel_data, (0, 2, 1)).flatten()
File "C:\Users\703257542\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\core\fromnumeric.py", line 639, in transpose
return _wrapfunc(a, 'transpose', axes)
File "C:\Users\703257542\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\core\fromnumeric.py", line 56, in _wrapfunc
return getattr(obj, method)(*args, **kwds)
ValueError: axes don't match array
[INFO/MainProcess] process shutting down

Process finished with exit code 1

hzxie commented

So, what's the shape of dense_voxel_data?
If you cannot answer this question, please paste the whole code.
Because the code DOES NOT belong to the repository.

Shape is {tuple} <class 'tuple'>: (1, 32, 32, 32)

hzxie commented

I think you should change

vox = utils.binvox_rw.Voxels(generated_volume.cpu().numpy(), (32,) * 3, (0,) * 3, 1, 'xyz')

to

vox = utils.binvox_rw.Voxels(generated_volume.cpu().squeeze().numpy(), (32,) * 3, (0,) * 3, 1, 'xyz')

Thanks Haozhe after using the squeeze method the binvox_rw write function works but the function
dense_voxel_data = voxel_model.data.astype(int) makes all the values to zero.

This is the generated volume which i am getting using your code which i am trying to write as a .binvox file :

generated_volume
tensor([[[[4.1267e-03, 9.6511e-04, 4.3114e-04, ..., 8.3494e-04,
1.1557e-03, 2.3458e-03],
[6.6642e-04, 7.6084e-05, 5.2316e-05, ..., 8.9469e-04,
1.2594e-03, 1.6307e-03],
[4.2990e-04, 1.9333e-05, 6.5481e-06, ..., 1.6484e-03,
2.1354e-03, 2.3559e-03],
...,
[6.4708e-04, 1.6682e-04, 8.8712e-05, ..., 3.9608e-05,
1.0567e-04, 3.3392e-04],
[1.2302e-03, 2.8612e-04, 1.5311e-04, ..., 1.3121e-04,
1.9169e-04, 7.4391e-04],
[3.7640e-03, 1.1243e-03, 6.2523e-04, ..., 8.4169e-04,
1.2355e-03, 3.1621e-03]],

     [[1.2482e-03, 4.8356e-04, 2.3372e-04,  ..., 3.3946e-04,
       4.6867e-04, 1.1738e-03],
      [2.3514e-04, 7.2014e-05, 3.6514e-05,  ..., 2.8438e-04,
       7.3088e-04, 1.2636e-03],
      [1.1121e-04, 2.6672e-05, 2.7826e-05,  ..., 4.8508e-04,
       1.5633e-03, 2.7535e-03],
      ...,
      [5.7263e-04, 3.3547e-04, 1.9515e-04,  ..., 1.9168e-04,
       1.8218e-04, 7.9509e-04],
      [6.0061e-04, 5.0089e-04, 2.6987e-04,  ..., 1.8754e-04,
       2.5838e-04, 1.3682e-03],
      [2.7935e-03, 1.0700e-03, 6.9801e-04,  ..., 5.5089e-04,
       1.1646e-03, 2.2660e-03]],

     [[8.3342e-04, 3.7738e-04, 1.8523e-04,  ..., 2.9103e-04,
       4.1959e-04, 1.6692e-03],
      [1.4831e-04, 7.3638e-05, 3.7141e-05,  ..., 2.5092e-04,
       5.0098e-04, 1.8103e-03],
      [1.3346e-04, 3.0158e-05, 7.6942e-05,  ..., 1.8579e-04,
       5.9823e-04, 3.5739e-03],
      ...,
      [7.3049e-04, 3.7956e-04, 1.9406e-04,  ..., 8.2338e-04,
       6.6436e-04, 1.4323e-03],
      [1.0307e-03, 3.4426e-04, 2.3588e-04,  ..., 4.8665e-04,
       9.3873e-04, 2.4509e-03],
      [4.1243e-03, 1.4817e-03, 8.8745e-04,  ..., 3.8709e-04,
       1.1723e-03, 3.3872e-03]],

     ...,

     [[2.4786e-03, 1.4231e-03, 4.4256e-04,  ..., 3.7579e-06,
       1.0051e-05, 2.5806e-04],
      [1.6049e-03, 1.5841e-03, 9.8844e-04,  ..., 4.4138e-06,
       2.1401e-05, 2.8819e-04],
      [2.9380e-03, 3.8464e-03, 3.1284e-03,  ..., 2.2055e-05,
       8.3209e-05, 8.2161e-04],
      ...,
      [1.6210e-03, 1.8717e-03, 1.0397e-03,  ..., 1.2567e-05,
       1.4342e-05, 5.8072e-05],
      [1.9632e-03, 1.0356e-03, 5.5876e-04,  ..., 8.2965e-05,
       7.4242e-05, 8.1218e-05],
      [3.1834e-03, 3.4522e-04, 1.2929e-04,  ..., 1.5880e-04,
       1.4074e-04, 3.0808e-04]],

     [[2.0523e-03, 8.4266e-04, 3.1679e-04,  ..., 1.0897e-05,
       3.9238e-05, 5.3946e-04],
      [1.1115e-03, 8.1393e-04, 2.8463e-04,  ..., 8.8959e-06,
       3.4940e-05, 4.2866e-04],
      [2.0666e-03, 1.0217e-03, 7.7191e-04,  ..., 1.7736e-05,
       8.7457e-05, 1.4318e-03],
      ...,
      [1.5367e-03, 1.2740e-03, 4.9352e-04,  ..., 1.6640e-05,
       2.0056e-05, 7.8161e-05],
      [2.2363e-03, 7.7593e-04, 3.9221e-04,  ..., 5.8742e-05,
       4.2468e-05, 6.9234e-05],
      [3.4652e-03, 6.4622e-04, 2.0349e-04,  ..., 1.2668e-04,
       1.2545e-04, 3.7705e-04]],

     [[2.5710e-03, 3.6081e-04, 2.0609e-04,  ..., 2.5336e-04,
       3.6796e-04, 1.9725e-03],
      [8.5946e-04, 1.9003e-04, 1.0290e-04,  ..., 4.4002e-05,
       1.2250e-04, 1.5619e-03],
      [7.8602e-04, 3.1345e-04, 1.7823e-04,  ..., 4.9947e-05,
       1.5558e-04, 2.5232e-03],
      ...,
      [1.7887e-03, 2.9945e-04, 1.7562e-04,  ..., 1.0260e-04,
       7.1047e-05, 3.2550e-04],
      [2.1848e-03, 5.2423e-04, 3.6802e-04,  ..., 1.3060e-04,
       1.1143e-04, 4.2711e-04],
      [6.4037e-03, 2.3628e-03, 1.9670e-03,  ..., 2.5904e-04,
       3.4433e-04, 1.8915e-03]]]])

After using dense_voxel_data = voxel_model.data.astype(int)

dense_voxel_data = {ndarray} [[[0 0 0 ... 0 0 0]\n [0 0 0 ... 0 0 0]\n [0 0 0 ... 0 0 0]\n ...\n [0 0 0 ... 0 0 0]\n [0 0 0 ... 0 0 0]\n [0 0 0 ... 0 0 0]]\n\n [[0 0 0 ... 0 0 0]\n [0 0 0 ... 0 0 0]\n [0 0 0 ... 0 0 0]\n ...\n [0 0 0 ... 0 0 0]\n [0 0 0 ... 0 0 0]\n [0 0 0 ... 0 0 0]]\n\n [[0 0 0 ... 0 0 0]\n [0 0 0 ... 0 0 0]\n [0 0 0 ... 0 0 0]\n ...\n [0 0 0 ... 0 0 0]\n [0 0 0 ... 0 0 0]\n [0 0 0 ... 0 0 0]]\n\n ...\n\n [[0 0 0 ... 0 0 0]\n [0 0 0 ... 0 0 0]\n [0 0 0 ... 0 0 0]\n ...\n [0 0 0 ... 0 0 0]\n [0 0 0 ... 0 0 0]\n [0 0 0 ... 0 0 0]]\n\n [[0 0 0 ... 0 0 0]\n [0 0 0 ... 0 0 0]\n [0 0 0 ... 0 0 0]\n ...\n [0 0 0 ... 0 0 0]\n [0 0 0 ... 0 0 0]\n [0 0 0 ... 0 0 0]]\n\n [[0 0 0 ... 0 0 0]\n [0 0 0 ... 0 0 0]\n [0 0 0 ... 0 0 0]\n ...\n [0 0 0 ... 0 0 0]\n [0 0 0 ... 0 0 0]\n [0 0 0 ... 0 0 0]]]...

the .binvox files are of only 310bytes. when i try to visualize nothing is there. what am i doing wrong my test_net(test_3dreconstructor) has any mistakes/issues?

hzxie commented

Change

generated_volume.cpu().numpy()

to

th = 0.5
generated_volume.ge(th).cpu().numpy()

Still the same Haozhe. I see the values in generated volume are quite small and in fact they are lesser that 1. Just to add to that i wanted to ask that i am using 4 images and have kept N_VIEWS_RENDERING as 4 but still it is generating 4 .binvox files.

After using threshold the vox values become zeros nd without that they are in the range of 10^-3 (pretty small values say 0.0043, 0.00097 ........)

hzxie commented

Sorry. I cannot understand what you are talking about.
First, I think the solution mentioned above is absolutely correct.
Second, please tell me the shape of generated_volume.

hzxie commented

Of course, the values are all below ones. Because I applied a sigmoid function before output.
THEY ARE PROBABLITIES!!

shape = {Size} torch.Size([1, 32, 32, 32])

hzxie commented

As I mentioned above, please use torch.squeeze().
Please refer to https://pytorch.org/docs/stable/torch.html#torch.squeeze

Yes Haozhe i am using squeeze as you had suggested. Sorry for troubling you so much.
generated_volume is of size (1, 32, 32, 32) but as per your suggestions i am using
vox = utils.binvox_rw.Voxels(generated_volume.cpu(th).squeeze().numpy(), (32,) * 3, (0,) * 3, 1, 'xyz')
which makes vox as (32,32,32)

hzxie commented

What's this?

generated_volume.cpu(th).squeeze().numpy()

I am trying to write the generated volume as per your code in test_net

with torch.no_grad():
# Get data from data loader
rendering_images = utils.network_utils.var_or_cuda(rendering_images)
#ground_truth_volume = utils.network_utils.var_or_cuda(ground_truth_volume)

    # Test the encoder, decoder, refiner and merger
    image_features = encoder(rendering_images)
    raw_features, generated_volume = decoder(image_features)

    if cfg.NETWORK.USE_MERGER and epoch_idx >= cfg.TRAIN.EPOCH_START_USE_MERGER:
        generated_volume = merger(raw_features, generated_volume)
    else:
        generated_volume = torch.mean(generated_volume, dim=1)
    #encoder_loss = bce_loss(generated_volume, ground_truth_volume) * 10

    if cfg.NETWORK.USE_REFINER and epoch_idx >= cfg.TRAIN.EPOCH_START_USE_REFINER:
        generated_volume = refiner(generated_volume)
        #refiner_loss = bce_loss(generated_volume, ground_truth_volume) * 10
    #else:
        #refiner_loss = encoder_loss

    #Write generated volume
    output_file_path = cfg.DATASETS.MYDATA.OUTPUT_PATH % (taxonomy_id, sample_name)
    with open(output_file_path, 'wb') as f:
        th = 15 
        vox = utils.binvox_rw.Voxels(generated_volume.cpu(th).squeeze().numpy(), (32,) * 3, (0,) * 3, 1, 'xyz')
        vox.write(f)
hzxie commented

I never told you to write code like that.
Please look at the following code CAREFULLY.

Change

generated_volume.cpu().numpy()

to

th = 0.5
generated_volume.ge(th).cpu().numpy()

Originally posted by @hzxie in #22 (comment)

If you don't understand torch.ge(), please refer to the PyTorch document.

You are rite Haozhe. While typing above i made a mistake. Actually i am using the below code

        with open(output_file_path, 'wb') as f:
            th = 0.5
            vox = utils.binvox_rw.Voxels(generated_volume.ge(th).cpu().squeeze().numpy(), (32,) * 3, (0,) * 3, 1, 'xyz')
            vox.write(f)

i am using squeeze as per your suggestions to change the generated volume shape from (1, 32, 32, 32) to vox size (32, 32, 32)

The challenge i am facing is the generated_volume values (probablities) are too low in the order of 10^-3.

Haozhe i tried debugging using Shapenet data even and i am getting similar generated_volume values and i feel its rite and there may be an issue in the way i am writing the voxels using binvox_rw

Haozhe i made the changes in your code

Change
generated_volume.cpu().numpy()
to
th = 0.5
generated_volume.ge(th).cpu().numpy()

I was making changes in the write voxel code which i added (which was not helping) but now i made changes in your code:

Append generated volumes to TensorBoard

        if output_dir and sample_idx < 3:
            img_dir = output_dir % 'images'
            # Volume Visualization
            th = 0.2
            gv = generated_volume.ge(th).cpu().numpy()
            rendering_views = utils.binvox_visualization.get_volume_views(gv, os.path.join(img_dir, 'test'),
                                                                          epoch_idx)

Now after changing th = 0.2 i am able to see voxel image in the test folder but not appropriate one. I am trying to debug further