Refactored dataloader

This commit is contained in:
Chung Min Kim committed 2023-04-02 01:49:30 +00:00
1 parent a87b7c3b53
commit dec7d25852
8 files changed
+845 -2

No files matched your search

@@ -0,0 +1,43 @@
import os
import typing
from abc import ABC, ABCMeta, abstractmethod
from pathlib import Path
import numpy as np
import torch
class FeatureDataloader(ABC):
def __init__(
self,
cfg: dict,
device: torch.device,
image_list: torch.Tensor, # (N, 3, H, W)
cache_path: Path,
):
self.cfg = cfg
self.device = device
self.cache_path = cache_path
self.data = None # only expect data to be cached, nothing else
self.try_load(image_list) # don't save image_list, avoid duplicates
@abstractmethod
def __call__(self, img_points):
# img_points: (B, 3) # (img_ind, x, y)
pass
@abstractmethod
def create(self, image_list: torch.Tensor):
pass
def load(self):
self.data = torch.from_numpy(np.load(self.cache_path))
def save(self):
np.save(self.cache_path, self.data)
def try_load(self, img_list: torch.Tensor):
try:
self.load()
except FileNotFoundError:
self.create(img_list)
self.save()