cache in cache dir, no longer edit json file

This commit is contained in:
AdamRashid96 committed 2023-06-14 05:41:23 +00:00
1 parent 5df7c95542
commit 9c3dcbbf5a
3 files changed
+24 -13

No files matched your search

+24 -13
View File
@@ -16,7 +16,6 @@
Depth dataset. Depth dataset.
""" """
import os
from typing import Dict from typing import Dict
import numpy as np import numpy as np
@@ -48,14 +47,24 @@ class DepthDataset(InputDataset):
"depth_filenames" not in dataparser_outputs.metadata.keys() "depth_filenames" not in dataparser_outputs.metadata.keys()
or dataparser_outputs.metadata["depth_filenames"] is None or dataparser_outputs.metadata["depth_filenames"] is None
): ):
depth_paths = [] cache = dataparser_outputs.image_filenames[0].parent / "depths.npy"
if cache.exists():
# load all the depths
self.depths = np.load(cache)
self.depths = torch.from_numpy(self.depths).to(device)
else:
depth_tensors = []
tranforms = self._find_transform(dataparser_outputs.image_filenames[0]) tranforms = self._find_transform(dataparser_outputs.image_filenames[0])
assert tranforms is not None, "Could not find transforms.json"
data = dataparser_outputs.image_filenames[0].parent data = dataparser_outputs.image_filenames[0].parent
if tranforms is not None:
meta = json.load(open(tranforms, "r")) meta = json.load(open(tranforms, "r"))
frames = meta["frames"] frames = meta["frames"]
filenames = [data / frames[j]["file_path"].split("/")[-1] for j in range(len(frames))] filenames = [data / frames[j]["file_path"].split("/")[-1] for j in range(len(frames))]
os.makedirs(dataparser_outputs.image_filenames[0].parent / "depth", exist_ok=True) else:
meta = None
frames = None
filenames = dataparser_outputs.image_filenames
repo = "isl-org/ZoeDepth" repo = "isl-org/ZoeDepth"
self.zoe = torch.compile(torch.hub.load(repo, "ZoeD_NK", pretrained=True).to(device)) self.zoe = torch.compile(torch.hub.load(repo, "ZoeD_NK", pretrained=True).to(device))
@@ -69,25 +78,27 @@ class DepthDataset(InputDataset):
with torch.no_grad(): with torch.no_grad():
image = torch.permute(image, (2, 0, 1)).unsqueeze(0).to(device) image = torch.permute(image, (2, 0, 1)).unsqueeze(0).to(device)
depth_numpy = self.zoe.infer(image).squeeze().unsqueeze(-1).cpu().numpy() if image.shape[1] == 4:
image = image[:, :3, :, :]
depth_tensor = self.zoe.infer(image).squeeze().unsqueeze(-1)
depth_paths.append(image_filename.parent / "depth" / f"depth{i}.npy") depth_tensors.append(depth_tensor)
meta["frames"][i]["depth_file_path"] = str(
Path(meta["frames"][i]["file_path"]).parent / "depth" / f"depth{i}.npy"
)
np.save(depth_paths[-1], depth_numpy)
json.dump(meta, open(tranforms, "w")) self.depths = torch.stack(depth_tensors)
np.save(cache, self.depths.cpu().numpy())
dataparser_outputs.metadata["depth_filenames"] = depth_paths dataparser_outputs.metadata["depth_filenames"] = None
self.metadata["depth_filenames"] = depth_paths
dataparser_outputs.metadata["depth_unit_scale_factor"] = 1.0 dataparser_outputs.metadata["depth_unit_scale_factor"] = 1.0
self.metadata["depth_filenames"] = None
self.metadata["depth_unit_scale_factor"] = 1.0 self.metadata["depth_unit_scale_factor"] = 1.0
self.depth_filenames = self.metadata["depth_filenames"] self.depth_filenames = self.metadata["depth_filenames"]
self.depth_unit_scale_factor = self.metadata["depth_unit_scale_factor"] self.depth_unit_scale_factor = self.metadata["depth_unit_scale_factor"]
def get_metadata(self, data: Dict) -> Dict: def get_metadata(self, data: Dict) -> Dict:
if self.depth_filenames is None:
return {"depth_image": self.depths[data["image_idx"]]}
filepath = self.depth_filenames[data["image_idx"]] filepath = self.depth_filenames[data["image_idx"]]
height = int(self._dataparser_outputs.cameras.height[data["image_idx"]]) height = int(self._dataparser_outputs.cameras.height[data["image_idx"]])
width = int(self._dataparser_outputs.cameras.width[data["image_idx"]]) width = int(self._dataparser_outputs.cameras.width[data["image_idx"]])
Binary file not shown.
Binary file not shown.