Delete duplicate/old file: \/data_loader.py
Browse files- ///data_loader.py +0 -98
///data_loader.py
DELETED
|
@@ -1,98 +0,0 @@
|
|
| 1 |
-
#!/usr/bin/python
|
| 2 |
-
# A simple data loader that imports the train and test mat files
|
| 3 |
-
# from the `filename` and converts them to torch.tesnors()
|
| 4 |
-
# to be loaded for training and testing DLoc network
|
| 5 |
-
# `features_wo_offset`: targets for the consistency decoder
|
| 6 |
-
# `features_w_offset` : inputs for the network/encoder
|
| 7 |
-
# `labels_gaussian_2d`: targets for the location decoder
|
| 8 |
-
import torch
|
| 9 |
-
import h5py
|
| 10 |
-
import scipy.io
|
| 11 |
-
import numpy as np
|
| 12 |
-
|
| 13 |
-
def load_data(filename):
|
| 14 |
-
print('Loading '+filename)
|
| 15 |
-
arrays = {}
|
| 16 |
-
f = h5py.File(filename,'r')
|
| 17 |
-
features_wo_offset = torch.tensor(np.transpose(np.array(f.get('features_wo_offset'), dtype=np.float32)), dtype=torch.float32)
|
| 18 |
-
features_w_offset = torch.tensor(np.transpose(np.array(f.get('features_w_offset'), dtype=np.float32)), dtype=torch.float32)
|
| 19 |
-
labels_gaussian_2d = torch.tensor(np.transpose(np.array(f.get('labels_gaussian_2d'), dtype=np.float32)), dtype=torch.float32)
|
| 20 |
-
|
| 21 |
-
return features_wo_offset,features_w_offset, labels_gaussian_2d
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
class LazyMultiHDF5Dataset(torch.utils.data.Dataset):
|
| 25 |
-
"""Reads samples on demand from multiple HDF5 .mat files.
|
| 26 |
-
Uses ~0 RAM regardless of dataset size. Each __getitem__ does 3 small
|
| 27 |
-
HDF5 slice reads (~700 KB total per sample).
|
| 28 |
-
|
| 29 |
-
Returns the same tuple ordering as the original TensorDataset:
|
| 30 |
-
(features_wo_offset, features_w_offset, labels_gaussian_2d)
|
| 31 |
-
"""
|
| 32 |
-
|
| 33 |
-
def __init__(self, file_paths):
|
| 34 |
-
self.file_paths = file_paths
|
| 35 |
-
self._handles = {} # lazy-opened per worker/process
|
| 36 |
-
self.cum_sizes = []
|
| 37 |
-
self.var_names = {} # per file: (w_name, wo_name, lbl_name)
|
| 38 |
-
|
| 39 |
-
total = 0
|
| 40 |
-
for fp in file_paths:
|
| 41 |
-
with h5py.File(fp, 'r') as f:
|
| 42 |
-
keys = list(f.keys())
|
| 43 |
-
w_name = 'features_w_offset' if 'features_w_offset' in keys else 'features_with_offset'
|
| 44 |
-
wo_name = 'features_wo_offset' if 'features_wo_offset' in keys else 'features_without_offset'
|
| 45 |
-
lbl_name = 'labels_gaussian_2d'
|
| 46 |
-
self.var_names[fp] = (w_name, wo_name, lbl_name)
|
| 47 |
-
# HDF5 stores MATLAB dims reversed: last dim = N
|
| 48 |
-
n = f[w_name].shape[-1]
|
| 49 |
-
total += n
|
| 50 |
-
self.cum_sizes.append(total)
|
| 51 |
-
print(f' Indexed {fp}: {n} samples')
|
| 52 |
-
|
| 53 |
-
self.total_len = total
|
| 54 |
-
print(f' Total: {self.total_len} samples (lazy, ~0 MB RAM)')
|
| 55 |
-
|
| 56 |
-
def __len__(self):
|
| 57 |
-
return self.total_len
|
| 58 |
-
|
| 59 |
-
def _get_handle(self, fp):
|
| 60 |
-
if fp not in self._handles:
|
| 61 |
-
self._handles[fp] = h5py.File(fp, 'r')
|
| 62 |
-
return self._handles[fp]
|
| 63 |
-
|
| 64 |
-
def __getitem__(self, idx):
|
| 65 |
-
# Find which file this idx belongs to
|
| 66 |
-
file_idx = 0
|
| 67 |
-
for i, cs in enumerate(self.cum_sizes):
|
| 68 |
-
if idx < cs:
|
| 69 |
-
file_idx = i
|
| 70 |
-
break
|
| 71 |
-
|
| 72 |
-
offset = self.cum_sizes[file_idx - 1] if file_idx > 0 else 0
|
| 73 |
-
local_idx = idx - offset
|
| 74 |
-
|
| 75 |
-
fp = self.file_paths[file_idx]
|
| 76 |
-
f = self._get_handle(fp)
|
| 77 |
-
w_name, wo_name, lbl_name = self.var_names[fp]
|
| 78 |
-
|
| 79 |
-
# HDF5 dims are reversed vs MATLAB:
|
| 80 |
-
# features: MATLAB [N,4,161,361] -> HDF5 [361,161,4,N]
|
| 81 |
-
# labels: MATLAB [N,161,361] -> HDF5 [361,161,N]
|
| 82 |
-
feat_w = np.array(f[w_name][:, :, :, local_idx], dtype=np.float32).transpose() # -> [4,161,361]
|
| 83 |
-
feat_wo = np.array(f[wo_name][:, :, :, local_idx], dtype=np.float32).transpose() # -> [4,161,361]
|
| 84 |
-
label = np.array(f[lbl_name][:, :, local_idx], dtype=np.float32).transpose() # -> [161,361]
|
| 85 |
-
label = label[np.newaxis, :, :] # -> [1,161,361]
|
| 86 |
-
|
| 87 |
-
return (
|
| 88 |
-
torch.from_numpy(feat_wo),
|
| 89 |
-
torch.from_numpy(feat_w),
|
| 90 |
-
torch.from_numpy(label)
|
| 91 |
-
)
|
| 92 |
-
|
| 93 |
-
def __del__(self):
|
| 94 |
-
for f in self._handles.values():
|
| 95 |
-
try:
|
| 96 |
-
f.close()
|
| 97 |
-
except Exception:
|
| 98 |
-
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|