Skip to content

Commit

Permalink
Add class filtering to LoadImagesAndLabels() dataloader (#5172)
Browse files Browse the repository at this point in the history
* Add train class filter feature to datasets.py

Allows for training on a subset of total classes if `include_class` list is defined on datasets.py L448:
```python
        include_class = []  # filter labels to include only these classes (optional)
```

* segments fix
  • Loading branch information
glenn-jocher committed Oct 13, 2021
1 parent b754525 commit a346926
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions utils/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,17 +437,27 @@ def __init__(self, path, img_size=640, batch_size=16, augment=False, hyp=None, r
self.shapes = np.array(shapes, dtype=np.float64)
self.img_files = list(cache.keys()) # update
self.label_files = img2label_paths(cache.keys()) # update
if single_cls:
for x in self.labels:
x[:, 0] = 0

n = len(shapes) # number of images
bi = np.floor(np.arange(n) / batch_size).astype(np.int) # batch index
nb = bi[-1] + 1 # number of batches
self.batch = bi # batch index of image
self.n = n
self.indices = range(n)

# Update labels
include_class = [] # filter labels to include only these classes (optional)
include_class_array = np.array(include_class).reshape(1, -1)
for i, (label, segment) in enumerate(zip(self.labels, self.segments)):
if include_class:
j = (label[:, 0:1] == include_class_array).any(1)
self.labels[i] = label[j]
if segment:
self.segments[i] = segment[j]
if single_cls: # single-class training, merge all classes into 0
self.labels[i][:, 0] = 0
if segment:
self.segments[i][:, 0] = 0

# Rectangular Training
if self.rect:
# Sort by aspect ratio
Expand Down

0 comments on commit a346926

Please sign in to comment.