Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Weighted Random Sampler for Multi-class detection #792

Open
bw4sz opened this issue Sep 25, 2024 · 0 comments
Open

Weighted Random Sampler for Multi-class detection #792

bw4sz opened this issue Sep 25, 2024 · 0 comments
Labels
Feature Request New feature or request Ideas for Machine Learning! These are machine learning ideas and papers that could be useful for DeepForest models. High level.

Comments

@bw4sz
Copy link
Collaborator

bw4sz commented Sep 25, 2024

The vast majority of ecological data are imbalanced. We should have some default weighted random sampler options

from torch.utils.data import WeightedRandomSampler
from deepforest import main

m = main.deepforest()

[setup and load data]

dataset = m.train_ds

# Assuming 'dataset' is your PyTorch Dataset object
class_counts = [0] * len(dataset.classes)
for _, label in dataset:
    class_counts[label] += 1

class_weights = 1. / torch.tensor(class_counts, dtype=torch.float)
sample_weights = [class_weights[label] for _, label in dataset]
sampler = WeightedRandomSampler(sample_weights, len(dataset))

and would get synced up through load dataset

loader = self.load_dataset(csv_file=self.config["train"]["csv_file"],

and into the dataloader object

data_loader = torch.utils.data.DataLoader(

@bw4sz bw4sz added Feature Request New feature or request Ideas for Machine Learning! These are machine learning ideas and papers that could be useful for DeepForest models. High level. labels Sep 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature Request New feature or request Ideas for Machine Learning! These are machine learning ideas and papers that could be useful for DeepForest models. High level.
Projects
None yet
Development

No branches or pull requests

1 participant