-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
52 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import os | ||
|
||
import torch | ||
|
||
|
||
class SubsetSequentialSampler(torch.utils.data.Sampler): | ||
""" | ||
Samples elements sequentially from a given list of indices, without replacement. | ||
Args: | ||
indices (sequence): a sequence of indices | ||
""" | ||
def __init__(self, indices): | ||
self.indices = indices | ||
|
||
def __iter__(self): | ||
return (i for i in self.indices) | ||
|
||
def __len__(self): | ||
return len(self.indices) | ||
|
||
|
||
def get_indices_by_names(dataset, filenames): | ||
""" | ||
Retreive the indices of inputs by file names | ||
Args: | ||
dataset (benzina.torch.dataset.Dataset): dataset from which to fetch the indices. | ||
filenames (sequence): a sequence of file names | ||
""" | ||
filenames_indices = {} | ||
filenames_lookup = set(filenames) | ||
|
||
with open(os.path.join(dataset.root, "data.filenames"), 'r') as names: | ||
for i, name in enumerate(names): | ||
# skip the end line following the filename | ||
name = name.rstrip() | ||
if name in filenames_lookup: | ||
filenames_indices[name] = i | ||
|
||
return [filenames_indices.get(filename, None) for filename in filenames] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters