Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Floating point events + image reconstruction additional arg #8

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions python/conversion/h5writer.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,36 @@
from pathlib import Path
import weakref
from typing import Union

import h5py
import numpy as np

from data.format import Events
from data.format import Events, EventsFloat


class H5Writer:
def __init__(self, outfile: Path):
def __init__(self, outfile: Path, floating_point_coords=False):
assert not outfile.exists(), str(outfile)
self.h5f = h5py.File(str(outfile), 'w')
self._finalizer = weakref.finalize(self, self.close_callback, self.h5f)

coord_dtype = "u2" if not floating_point_coords else "f4"

# create hdf5 datasets
shape = (2**16,)
maxshape = (None,)
compression = 'lzf'
self.h5f.create_dataset('x', shape=shape, dtype='u2', chunks=shape, maxshape=maxshape, compression=compression)
self.h5f.create_dataset('y', shape=shape, dtype='u2', chunks=shape, maxshape=maxshape, compression=compression)
self.h5f.create_dataset('x', shape=shape, dtype=coord_dtype, chunks=shape, maxshape=maxshape, compression=compression)
self.h5f.create_dataset('y', shape=shape, dtype=coord_dtype, chunks=shape, maxshape=maxshape, compression=compression)
self.h5f.create_dataset('p', shape=shape, dtype='u1', chunks=shape, maxshape=maxshape, compression=compression)
self.h5f.create_dataset('t', shape=shape, dtype='i8', chunks=shape, maxshape=maxshape, compression=compression)
self.h5f.create_dataset('t', shape=shape, dtype='u4', chunks=shape, maxshape=maxshape, compression=compression)
self.row_idx = 0

@staticmethod
def close_callback(h5f: h5py.File):
h5f.close()

def add_data(self, events: Events):
def add_data(self, events: Union[Events, EventsFloat]):
current_size = events.size
new_size = self.row_idx + current_size
self.h5f['x'].resize(new_size, axis=0)
Expand Down
29 changes: 27 additions & 2 deletions python/data/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,32 @@ def __post_init__(self):
assert self.x.dtype == np.uint16
assert self.y.dtype == np.uint16
assert self.p.dtype == np.uint8
assert self.t.dtype == np.int64
assert self.t.dtype == np.uint32
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is dangerous since we don't check that an overflow is not happening.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for other loc that are affected by this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I am conviced, I have changed it back to int64. The memory footprint then grows from 12MB -> 13MB, which should be fine.


assert self.x.shape == self.y.shape == self.p.shape == self.t.shape
assert self.x.ndim == 1

# Without the frozen option, we could just do: self.size = self.x.size
super().__setattr__('size', self.x.size)

if self.size > 0:
assert np.max(self.p) <= 1


@dataclass(frozen=True)
class EventsFloat:
x: np.ndarray
y: np.ndarray
p: np.ndarray
t: np.ndarray

size: int = field(init=False)

def __post_init__(self):
assert self.x.dtype == np.float32
assert self.y.dtype == np.float32
assert self.p.dtype == np.uint8
assert self.t.dtype == np.uint32

assert self.x.shape == self.y.shape == self.p.shape == self.t.shape
assert self.x.ndim == 1
Expand Down Expand Up @@ -54,7 +79,7 @@ def __post_init__(self):
x=np.array([0, 1], dtype='uint16'),
y=np.array([1, 2], dtype='uint16'),
p=np.array([0, 0], dtype='uint8'),
t=np.array([0, 5], dtype='int64')),
t=np.array([0, 5], dtype='uint32')),
width=2,
height=3,
t_reconstruction=6)
Expand Down
2 changes: 1 addition & 1 deletion python/data/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def get_events_until(self, time: int) -> Optional[Events]:
np.array([], dtype=np.uint16),
np.array([], dtype=np.uint16),
np.array([], dtype=np.uint8),
np.array([], dtype=np.int64))
np.array([], dtype=np.uint32))
else:
retrieved_events = Events(
self.local_buffer.x[indices],
Expand Down
6 changes: 4 additions & 2 deletions python/offline_reconstruction.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def download_checkpoint(path_to_model):
parser.add_argument('--timestamps_file', '-tsf', help='Path to txt file containing image reconstruction timestamps')
parser.add_argument('--upsample_rate', '-u', type=int, default=1, help='Multiplies the number of reconstructions, which effectively lowers the time window of events for E2VID. These intermediate reconstructions will not be saved to disk.')
parser.add_argument('--verbose', '-v', action='store_true', default=False, help='Verbose output')
parser.add_argument('--index_by_order', '-i', action='store_true', default=False, help='Index reconstrutions with 0,1,2,3...')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

default is False anyway for store_true

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed this with the latest commit


set_inference_options(parser)

Expand Down Expand Up @@ -79,14 +80,15 @@ def download_checkpoint(path_to_model):
print('Will write images to: {}'.format(os.path.join(args.output_folder, args.dataset_name)))
grid = VoxelGrid(model.num_bins, args.width, args.height, upsample_rate=args.upsample_rate)
pbar = tqdm.tqdm(total=len(data_provider))
for events in data_provider:
for j, events in enumerate(data_provider):
if events.events.size > 0:
sliced_events = grid.event_slicer(events.events, events.t_reconstruction)
for i in range(len(sliced_events)):
event_grid, _ = grid.events_to_voxel_grid(sliced_events[i])
event_tensor = torch.from_numpy(event_grid)
if i== len(sliced_events) - 1:
image_reconstructor.update_reconstruction(event_tensor, int(events.t_reconstruction)*1000, save=True)
index = j if args.index_by_order else int(events.t_reconstruction)*1000
image_reconstructor.update_reconstruction(event_tensor, index, save=True)
pbar.update(1)
else:
image_reconstructor.update_reconstruction(event_tensor)