Skip to content

Commit

Permalink
[Update] Release semilearn 0.3.1. (#135)
Browse files Browse the repository at this point in the history
* [Update] resolve requirements.txt conflicts

* [Fix] Fix mean teacher bug in #102

* [Fix] Fix DebiasPL bug

* [Fix] Fix potential sample data bug in #119

* [Update] Add auto issue/pr closer

* [Update] Update requirements.txt

* [Fix] Fix bug in #74

* [Fix] Fix amp lighting bug in #123

* [Fix] Fix notebook bugs

* [Update] release semilearn 0.3.1
  • Loading branch information
Hhhhhhao committed Jul 20, 2023
1 parent 6ebb76d commit 4595a7c
Show file tree
Hide file tree
Showing 12 changed files with 721 additions and 349 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/blank.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ jobs:
- name: Run a multi-line script
run: |
echo Add other actions to build,
echo test, and deploy your project.
echo test, and deploy your project.
17 changes: 17 additions & 0 deletions .github/workflows/issue.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: 'Close stale issues and PR'
on:
schedule:
- cron: '30 1 * * *'

jobs:
stale:
runs-on: ubuntu-latest
steps:
- uses: actions/stale@v8
with:
stale-issue-message: 'This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.'
stale-pr-message: 'This PR is stale because it has been open 45 days with no activity. Remove stale label or comment or this will be closed in 10 days.'
close-issue-message: 'This issue was closed because it has been stalled for 5 days with no activity.'
days-before-stale: 30
days-before-close: 5
days-before-pr-close: -1
388 changes: 324 additions & 64 deletions notebooks/Beginner_Example.ipynb

Large diffs are not rendered by default.

367 changes: 318 additions & 49 deletions notebooks/Custom_Algorithm.ipynb

Large diffs are not rendered by default.

251 changes: 38 additions & 213 deletions notebooks/Custom_Dataset.ipynb

Large diffs are not rendered by default.

3 changes: 0 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ joblib==1.2.0
kiwisolver==1.4.3
Markdown==3.3.7
matplotlib==3.5.2
mkl-fft>=1.3.0
mkl-random==1.2.2
mkl-service==2.4.0
networkx==2.6.3
numpy
oauthlib==3.2.1
Expand Down
4 changes: 2 additions & 2 deletions semilearn/algorithms/meanteacher/meanteacher.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ def train_step(self, x_lb, y_lb, x_ulb_w, x_ulb_s):

self.bn_controller.freeze_bn(self.model)
outs_x_ulb_s = self.model(x_ulb_s)
logits_x_ulb_s = outs_x_ulb_w['logits']
feats_x_ulb_s = outs_x_ulb_w['feat']
logits_x_ulb_s = outs_x_ulb_s['logits']
feats_x_ulb_s = outs_x_ulb_s['feat']
self.bn_controller.unfreeze_bn(self.model)

feat_dict = {'x_lb':feats_x_lb, 'x_ulb_w':feats_x_ulb_w, 'x_ulb_s':feats_x_ulb_s}
Expand Down
8 changes: 2 additions & 6 deletions semilearn/datasets/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,6 @@ def split_ssl_data(args, data, targets, num_classes,
return data[lb_idx], targets[lb_idx], data[ulb_idx], targets[ulb_idx]


def sample_labeled_data():
pass


def sample_labeled_unlabeled_data(args, data, target, num_classes,
lb_num_labels, ulb_num_labels=None,
lb_imbalance_ratio=1.0, ulb_imbalance_ratio=1.0,
Expand Down Expand Up @@ -88,7 +84,7 @@ def sample_labeled_unlabeled_data(args, data, target, num_classes,
if ulb_imbalance_ratio == 1.0:
# balanced setting
if ulb_num_labels is None or ulb_num_labels == 'None':
pass # ulb_samples_per_class = [int(len(data) / num_classes) - lb_samples_per_class[c] for c in range(num_classes)] # [int(len(data) / num_classes) - int(lb_num_labels / num_classes)] * num_classes
ulb_samples_per_class = None
else:
assert ulb_num_labels % num_classes == 0, "ulb_num_labels must be dividable by num_classes in balanced setting"
ulb_samples_per_class = [int(ulb_num_labels / num_classes)] * num_classes
Expand All @@ -104,7 +100,7 @@ def sample_labeled_unlabeled_data(args, data, target, num_classes,
idx = np.where(target == c)[0]
np.random.shuffle(idx)
lb_idx.extend(idx[:lb_samples_per_class[c]])
if ulb_num_labels is None or ulb_num_labels == 'None':
if ulb_samples_per_class is None:
ulb_idx.extend(idx[lb_samples_per_class[c]:])
else:
ulb_idx.extend(idx[lb_samples_per_class[c]:lb_samples_per_class[c]+ulb_samples_per_class[c]])
Expand Down
2 changes: 1 addition & 1 deletion semilearn/imb_algorithms/debiaspl/debiaspl.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def set_hooks(self):
def compute_prob(self, logits):
# update p_hat
probs = super().compute_prob(logits)
delta_p = probs.mean()
delta_p = probs.mean(dim=0)
self.p_hat = self.ema_m * self.p_hat + (1 - self.ema_p) * delta_p
return super().compute_prob(logits - self.tau * torch.log(self.p_hat))

Expand Down
2 changes: 1 addition & 1 deletion semilearn/lighting/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def get_config(config):
## core algorithm setting
parser.add_argument('-alg', '--algorithm', type=str, default='fixmatch', help='ssl algorithm')
parser.add_argument('--use_cat', type=str2bool, default=True, help='use cat operation in algorithms')
parser.add_argument('--use_amp', type=str2bool, default=False, help='use mixed precision training or not')
parser.add_argument('--amp', type=str2bool, default=False, help='use mixed precision training or not')
parser.add_argument('--clip_grad', type=float, default=0)

## imbalance algorithm setting
Expand Down
22 changes: 15 additions & 7 deletions semilearn/lighting/trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,17 @@ def __init__(self, config, algorithm, verbose=0):


def fit(self, train_lb_loader, train_ulb_loader, eval_loader):
self.algorithm.loader_dict = {
'trian_lb': train_lb_loader,
'train_ulb': train_ulb_loader,
'eval': eval_loader
}
self.algorithm.model.train()

# EMA Init
self.algorithm.ema = EMA(self.algorithm.model, self.algorithm.ema_m)
self.algorithm.ema.register()

# train
self.algorithm.it = 0
self.algorithm.best_eval_acc = 0.0
self.algorithm.best_epoch = 0
self.algorithm.call_hook("before_run")

for epoch in range(self.config.epoch):
self.algorithm.epoch = epoch
Expand All @@ -48,19 +49,26 @@ def fit(self, train_lb_loader, train_ulb_loader, eval_loader):
bar = Bar('Processing', max=len(train_lb_loader))

self.algorithm.model.train()
self.algorithm.call_hook("before_train_epoch")

for data_lb, data_ulb in zip(train_lb_loader, train_ulb_loader):

if self.algorithm.it > self.config.num_train_iter:
break

result = self.algorithm.train_step(**self.algorithm.process_batch(**data_lb, **data_ulb))

self.algorithm.call_hook("before_train_step")
out_dict, log_dict = self.algorithm.train_step(**self.algorithm.process_batch(**data_lb, **data_ulb))
self.algorithm.out_dict = out_dict
self.algorithm.log_dict = log_dict
self.algorithm.call_hook("after_train_step")

bar.suffix = ("Iter: {batch:4}/{iter:4}.".format(batch=self.algorithm.it, iter=len(train_lb_loader)))
bar.next()
self.algorithm.it += 1
bar.finish()

self.algorithm.call_hook("after_train_epoch")

# validate
result = self.evaluate(eval_loader)

Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

setup(
name='semilearn',
version='0.3.0',
version='0.3.1',
description='Unfied Semi-Supervised Learning Benchmark',
long_description=long_description,
long_description_content_type='text/markdown',
Expand All @@ -41,5 +41,5 @@
include_package_data=True,
# install_requires=['torch >= 1.8', 'torchvision', 'torchaudio', 'transformers', 'timm', 'progress', 'ruamel.yaml', 'scikit-image', 'scikit-learn', 'tensorflow', ''],
install_requires=install_requires,
python_requires='>=3.8',
python_requires='>=3.9',
)

0 comments on commit 4595a7c

Please sign in to comment.