├── .gitattributes ├── .gitignore ├── LICENSE ├── README.md ├── UDA.py ├── dataset.py ├── networks ├── __init__.py ├── fastresnet.py └── wideresnet.py ├── requirements.txt └── supervised.py /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | data/* 3 | save/* 4 | 5 | # Byte-compiled / optimized / DLL files 6 | __pycache__/ 7 | *.py[cod] 8 | *$py.class 9 | 10 | # C extensions 11 | *.so 12 | 13 | # Distribution / packaging 14 | .Python 15 | build/ 16 | develop-eggs/ 17 | dist/ 18 | downloads/ 19 | eggs/ 20 | .eggs/ 21 | lib/ 22 | lib64/ 23 | parts/ 24 | sdist/ 25 | var/ 26 | wheels/ 27 | pip-wheel-metadata/ 28 | share/python-wheels/ 29 | *.egg-info/ 30 | .installed.cfg 31 | *.egg 32 | MANIFEST 33 | 34 | # PyInstaller 35 | # Usually these files are written by a python script from a template 36 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 37 | *.manifest 38 | *.spec 39 | 40 | # Installer logs 41 | pip-log.txt 42 | pip-delete-this-directory.txt 43 | 44 | # Unit test / coverage reports 45 | htmlcov/ 46 | .tox/ 47 | .nox/ 48 | .coverage 49 | .coverage.* 50 | .cache 51 | nosetests.xml 52 | coverage.xml 53 | *.cover 54 | .hypothesis/ 55 | .pytest_cache/ 56 | 57 | # Translations 58 | *.mo 59 | *.pot 60 | 61 | # Django stuff: 62 | *.log 63 | local_settings.py 64 | db.sqlite3 65 | 66 | # Flask stuff: 67 | instance/ 68 | .webassets-cache 69 | 70 | # Scrapy stuff: 71 | .scrapy 72 | 73 | # Sphinx documentation 74 | docs/_build/ 75 | 76 | # PyBuilder 77 | target/ 78 | 79 | # Jupyter Notebook 80 | .ipynb_checkpoints 81 | 82 | # IPython 83 | profile_default/ 84 | ipython_config.py 85 | 86 | # pyenv 87 | .python-version 88 | 89 | # pipenv 90 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 91 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 92 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 93 | # install all needed dependencies. 94 | #Pipfile.lock 95 | 96 | # celery beat schedule file 97 | celerybeat-schedule 98 | 99 | # SageMath parsed files 100 | *.sage.py 101 | 102 | # Environments 103 | .env 104 | .venv 105 | env/ 106 | venv/ 107 | ENV/ 108 | env.bak/ 109 | venv.bak/ 110 | 111 | # Spyder project settings 112 | .spyderproject 113 | .spyproject 114 | 115 | # Rope project settings 116 | .ropeproject 117 | 118 | # mkdocs documentation 119 | /site 120 | 121 | # mypy 122 | .mypy_cache/ 123 | .dmypy.json 124 | dmypy.json 125 | 126 | # Pyre type checker 127 | .pyre/ 128 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Unsupervised Data Augmentation PyTorch 2 | Unsupervised Data Augmentation for Consistency training implemented in Pytorch 3 | 4 | # Goal 5 | 6 | A fundamental weakness of deep learning is that it typically requires a lot of labeled data to work well. Semi-supervised learning (SSL) 7 | one of the most promising paradigms to leverage unlabeled data to address this weakness. (https://ai.googleblog.com/2019/07/advancing-semi-supervised-learning-with.html). Unsupervised Data Augmentation (UDA) 8 | (https://arxiv.org/abs/1904.12848), recently proposed technique, popularized the idea to reuse th e data augmentation techniques known from supervised learning for the unlabeled data in the sem i-supervised setting. The intuition behind UDA 9 | is that even if we do not know the desired neural output for an unlabeled data, 10 | we can expect the neural network to produce an output on the augmented (e.g.: horizontal flipped) data which is the augmented version of the neural output for the non-augmented data. 11 | 12 | The candidate should start with the classification task (CIFAR-10, SVHN), where the output the neural network do not have to be augmented. The candidate should evaluate the effect of augmentation techniques available from RandAugment while varying size of labeled data is assumed to be available. Which augmentations are the best? 13 | 14 | # Optional 15 | 16 | An optional advanced part of the assignment is to try out the UDA technique for semantic segmentation and/or 2D object detection on a lightweight dataset. [E.g. Cityscapes dataset.](https://www.cityscapes-dataset.com/) How much gain can be achieved with UDA in the early phase of evelopment, when only a small amount of labeled data is available? Which augmentations are the best? 17 | 18 | # Code 19 | ## Examples 20 | 21 | | Name | Valid Loss | Valid Acc | Reference | 22 | | ---------------|------------|---------- | ------- | 23 | | UDA - 4000 - 46000 | 47.32| 86.57 | ~86 | 24 | | Supervised All|32.2| 90.12 | - | 25 | | Supervised All w s. Aug|19.83| 93.97| 94.08 | 26 | | Supervised 4000| 82.02 | 80.15 | - | 27 | 28 | 29 | 30 | ## Run 31 | `python UDA.py` for semi-supervised UDA implemenetation with 46000 unlabelled - 4000 labelled data 32 | 33 | `python supervised.py` for fully supervised with 50000 labelled data -- for baseline 34 | 35 | `python supervised.py --limit 4000` for fully supervised with 4000 labelled data -- for baseline 36 | 37 | ### Tensorboard 38 | Results are logged in `tensorboard` 39 | 40 | Run `tensorboard --logdir FOLDER_NAME` to see results 41 | 42 | 43 | # References 44 | 45 | Using code from [vdef-5 UDA Pytorch](https://github.com/vfdev-5/UDA-pytorch/tree/740a6740eab29f9b916ffa62c9baf4bec092c0d2) 46 | Using code from [autoaugment](https://github.com/tensorflow/models/blob/master/research/autoaugment/) -------------------------------------------------------------------------------- /UDA.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | import time 4 | import argparse 5 | 6 | import torch 7 | from torch import nn 8 | from torch.backends import cudnn 9 | 10 | from torch.utils.tensorboard import SummaryWriter 11 | 12 | import dataset as dataset 13 | import networks 14 | 15 | parser = argparse.ArgumentParser(description='Pythorch UDA CIFAR-10 implementation') 16 | parser.add_argument('--evaluate', '-e', action='store_true', help='Evaluation mode') 17 | parser.add_argument('--epochs', default=200, type=int, metavar='N', 18 | help='number of total epochs to run') 19 | parser.add_argument('--start-epoch', '-se', default=0, type=int, metavar='N', 20 | help='manual epoch number (useful on restarts)') 21 | parser.add_argument('--save-dir', dest='save_dir', 22 | help='The directory used to save the trained models', 23 | default='save', type=str) 24 | parser.add_argument('--resume', default='save', type=str, metavar='PATH', 25 | help='path to latest checkpoint (default: save)') 26 | parser.add_argument('--device', '-d', type=int, default=0, help='set cuda device') 27 | args = parser.parse_args() 28 | best_prec1 = 0 29 | device = args.device 30 | 31 | writer = SummaryWriter('1000, 3,9') 32 | 33 | def save_checkpoint(state, filename='checkpoint.pth.tar'): 34 | """ 35 | Save the training model 36 | """ 37 | torch.save(state, filename) 38 | 39 | class AverageMeter(object): 40 | """Computes and stores the average and current value""" 41 | def __init__(self): 42 | self.reset() 43 | 44 | def reset(self): 45 | self.val = 0 46 | self.avg = 0 47 | self.sum = 0 48 | self.count = 0 49 | 50 | def update(self, val, n=1): 51 | self.val = val 52 | self.sum += val * n 53 | self.count += n 54 | self.avg = self.sum / self.count 55 | 56 | def accuracy(output, target, topk=(1,)): 57 | """Computes the precision@k for the specified values of k""" 58 | maxk = max(topk) 59 | batch_size = target.size(0) 60 | 61 | _, pred = output.topk(maxk, 1, True, True) 62 | pred = pred.t() 63 | correct = pred.eq(target.view(1, -1).expand_as(pred)) 64 | 65 | res = [] 66 | for k in topk: 67 | correct_k = correct[:k].view(-1).float().sum(0) 68 | res.append(correct_k.mul_(100.0 / batch_size)) 69 | return res 70 | 71 | def uda_train(train_labelled, train_unlabelled, train_unlabelled_aug, model, criterion, consistency_criterion, optimizer, epoch): 72 | """ 73 | Run one train epoch 74 | """ 75 | data_time = AverageMeter() 76 | batch_time = AverageMeter() 77 | losses = AverageMeter() 78 | top1 = AverageMeter() 79 | 80 | model.train() 81 | 82 | end = time.time() 83 | 84 | lam = 1.0 85 | 86 | label_iter = iter(train_labelled) 87 | unsup_iter = iter(train_unlabelled) 88 | 89 | # train over 1 epoch, drop y for unlabelled data 90 | for i, (unlabel_aug_x, _) in enumerate(train_unlabelled_aug): 91 | # measure data loading time 92 | data_time.update(time.time() - end) 93 | 94 | # SUPERVISED 95 | try: 96 | x,y = next(label_iter) 97 | except StopIteration: 98 | label_iter = iter(train_labelled) 99 | x, y = next(label_iter) 100 | 101 | x = x.cuda() 102 | y = y.cuda() 103 | 104 | y_pred = model(x) 105 | 106 | sup_loss = criterion(y_pred, y) 107 | 108 | # UNSUPERVISED 109 | unlabel_x, _ = next(unsup_iter) 110 | unlabel_x = unlabel_x.cuda() 111 | unlabel_aug_x = unlabel_aug_x.cuda() 112 | 113 | unsup_y_pred = model(unlabel_x).detach() 114 | unsup_y_probas = torch.softmax(unsup_y_pred, dim=-1) 115 | 116 | unsup_aug_y_pred = model(unlabel_aug_x) 117 | unsup_aug_y_probas = torch.log_softmax(unsup_aug_y_pred, dim=-1) 118 | 119 | unsup_loss = consistency_criterion(unsup_aug_y_probas, unsup_y_probas) 120 | 121 | final_loss = sup_loss + lam * unsup_loss 122 | 123 | optimizer.zero_grad() 124 | final_loss.backward() 125 | optimizer.step() 126 | 127 | # measure accuracy and record loss 128 | losses.update(final_loss.item(), unlabel_aug_x.size(0)) 129 | 130 | # measure elapsed time 131 | batch_time.update(time.time() - end) 132 | end = time.time() 133 | 134 | if i % 50 == 0: 135 | print('Epoch: [{0}][{1}/{2}]\t' 136 | 'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t' 137 | 'Data {data_time.val:.3f} ({data_time.avg:.3f})\t' 138 | 'Loss {loss.val:.4f} ({loss.avg:.4f})\t'.format( 139 | epoch, i, len(train_unlabelled_aug), batch_time=batch_time, 140 | data_time=data_time, loss=losses)) 141 | 142 | return losses.avg 143 | 144 | def uda_validate(valid_loader, unlabelled_loader, model, criterion, epoch): 145 | batch_time = AverageMeter() 146 | losses = AverageMeter() 147 | top1 = AverageMeter() 148 | 149 | model.eval() 150 | 151 | end = time.time() 152 | 153 | with torch.no_grad(): 154 | for i, (input, target) in enumerate(valid_loader): 155 | target = target.cuda() 156 | input_var = input.cuda() 157 | target_var = target.cuda() 158 | 159 | # compute output 160 | output = model(input_var) 161 | loss = criterion(output, target_var) 162 | 163 | output = output.float() 164 | loss = loss.float() 165 | 166 | # measure accuracy and record loss 167 | prec1 = accuracy(output.data, target)[0] 168 | losses.update(loss.item(), input.size(0)) 169 | top1.update(prec1.item(), input.size(0)) 170 | 171 | # measure elapsed time 172 | batch_time.update(time.time() - end) 173 | end = time.time() 174 | 175 | if i % 50 == 0: 176 | print('Test: [{0}/{1}]\t' 177 | 'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t' 178 | 'Loss {loss.val:.4f} ({loss.avg:.4f})\t' 179 | 'Prec@1 {top1.val:.3f} ({top1.avg:.3f})'.format( 180 | i, len(valid_loader), batch_time=batch_time, loss=losses, 181 | top1=top1)) 182 | 183 | print(' * Prec@1 {top1.avg:.3f}'.format(top1=top1)) 184 | 185 | return top1.avg, losses.avg 186 | 187 | 188 | def run_unsupervised(): 189 | global args, best_prec1 190 | best_valid_loss = 10e10 191 | 192 | # Check the save_dir exists or not 193 | if not os.path.exists(args.save_dir): 194 | os.makedirs(args.save_dir) 195 | 196 | # load model 197 | model = networks.fastresnet() 198 | model.cuda() 199 | 200 | # data loaders 201 | train_labelled, train_unlabelled, train_unlabelled_aug, test = dataset.cifar10_unsupervised_dataloaders() 202 | 203 | # criterion and optimizer 204 | criterion = nn.CrossEntropyLoss().cuda() 205 | consistency_criterion = nn.KLDivLoss(reduction='batchmean').cuda() 206 | 207 | optimizer = torch.optim.SGD(model.parameters(), 208 | lr=0.1, 209 | momentum=0.9, 210 | weight_decay=1e-4, 211 | nesterov=True) 212 | 213 | # warmup steps 214 | t_max = len(train_labelled) * args.epochs 215 | eta_min = 0.03 * 0.004 216 | 217 | scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=t_max, eta_min=eta_min) 218 | 219 | if args.resume: 220 | if os.path.isfile(args.resume): 221 | print("=> loading checkpoint '{}'".format(args.resume)) 222 | checkpoint = torch.load(args.resume) 223 | model.load_state_dict(checkpoint['state_dict']) 224 | optimizer.load_state_dict(checkpoint['optimizer']) 225 | scheduler.load_state_dict(checkpoint['scheduler']) 226 | args.start_epoch = checkpoint['epoch'] 227 | print("=> loaded checkpoint '{}' (epoch {})" 228 | .format(args.evaluate, checkpoint['epoch'])) 229 | else: 230 | print("=> no checkpoint found at '{}'".format(args.resume)) 231 | 232 | cudnn.benchmark = True 233 | 234 | # UDA train loop 235 | for epoch in range(args.start_epoch, args.epochs): 236 | 237 | print('current lr {:.5e}'.format(optimizer.param_groups[0]['lr'])) 238 | 239 | trainloss = uda_train(train_labelled, train_unlabelled, train_unlabelled_aug, model, criterion, consistency_criterion, optimizer, epoch) 240 | 241 | writer.add_scalar('Loss/train', trainloss, epoch) 242 | 243 | scheduler.step() 244 | 245 | # evaluate on validation set 246 | valacc, valloss = uda_validate(test, train_unlabelled, model, criterion, epoch) 247 | 248 | is_best = valacc > best_prec1 249 | best_prec1 = max(valacc, best_prec1) 250 | 251 | writer.add_scalar('Acc/valid', valacc, epoch) 252 | writer.add_scalar('Loss/valid', valloss, epoch) 253 | 254 | 255 | # save checkpoint 256 | if epoch > 0 and epoch % 50 == 0: 257 | print('Save checkpoint') 258 | save_checkpoint({ 259 | 'epoch': epoch + 1, 260 | 'scheduler': scheduler.state_dict(), 261 | 'optimizer': optimizer.state_dict(), 262 | 'state_dict': model.state_dict(), 263 | 'best_prec1': best_prec1, 264 | }, filename=os.path.join(args.save_dir, 'checkpoint.th')) 265 | 266 | if (is_best): 267 | print('Saving better model') 268 | save_checkpoint({ 269 | 'epoch': epoch + 1, 270 | 'scheduler': scheduler.state_dict(), 271 | 'optimizer': optimizer.state_dict(), 272 | 'state_dict': model.state_dict(), 273 | 'best_prec1': best_prec1, 274 | }, filename=os.path.join(args.save_dir, 'best_model.th')) 275 | 276 | writer.add_scalar('Acc/valid_best', best_prec1, epoch) 277 | writer.add_scalar('Acc/los_best', valloss, epoch) 278 | 279 | if __name__ == '__main__': 280 | run_unsupervised() 281 | 282 | 283 | -------------------------------------------------------------------------------- /dataset.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import torch 3 | from torch.utils.data import DataLoader, Subset, Dataset, ConcatDataset 4 | from torchvision import datasets 5 | from torchvision.transforms import Compose, ToTensor, Normalize, Pad, RandomCrop, RandomHorizontalFlip, RandomErasing 6 | from RandAugment import RandAugment 7 | 8 | 9 | np.random.seed(2) 10 | 11 | class AddTransform(Dataset): 12 | def __init__(self, dataset, transform): 13 | self.data = dataset 14 | self.transform = transform 15 | 16 | def __len__(self): 17 | return len(self.data) 18 | 19 | def __getitem__(self, index): 20 | x, y = self.data[index] 21 | x = self.transform(x) 22 | return x, y 23 | 24 | def cifar10_unsupervised_dataloaders(): 25 | print('Data Preparation') 26 | train_transform = Compose([ 27 | Pad(4), 28 | RandomCrop(32, fill=128), 29 | RandomHorizontalFlip(), 30 | ToTensor(), 31 | Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)), 32 | RandomErasing(scale=(0.1, 0.33)), 33 | ]) 34 | 35 | unsupervised_train_transformation = Compose([ 36 | Pad(4), 37 | RandomCrop(32, fill=128), 38 | ToTensor(), 39 | Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)), 40 | ]) 41 | 42 | # RANDAUGMENT 43 | unsupervised_train_transformation.transforms.insert(0, RandAugment(3, 9)) 44 | 45 | test_transform = Compose([ 46 | ToTensor(), 47 | Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)), 48 | ]) 49 | 50 | # Train dataset with and without labels 51 | cifar10_train_ds = datasets.CIFAR10('/data/', train=True, download=True) 52 | 53 | num_classes = len(cifar10_train_ds.classes) 54 | 55 | print('Loading dataset {0} for training -- Num_samples: {1}, num_classes: {2}'.format(datasets.CIFAR10.__name__,len(cifar10_train_ds),10)) 56 | 57 | labelled_indices = [] 58 | unlabelled_indices = [] 59 | 60 | indices = np.random.permutation(len(cifar10_train_ds)) 61 | class_counters = list([0] * num_classes) 62 | max_counter = 10000 // num_classes 63 | 64 | for i in indices: 65 | dp = cifar10_train_ds[i] 66 | if len(cifar10_train_ds) < sum(class_counters): 67 | unlabelled_indices.append(i) 68 | else: 69 | y = dp[1] 70 | c = class_counters[y] 71 | if c < max_counter: 72 | class_counters[y] += 1 73 | labelled_indices.append(i) 74 | else: 75 | unlabelled_indices.append(i) 76 | 77 | 78 | # Labelled and unlabelled dataset 79 | train_labelled_ds = Subset(cifar10_train_ds, labelled_indices) 80 | train_labelled_ds_t = AddTransform(train_labelled_ds, train_transform) 81 | 82 | # unlabelled ds and aug ds 83 | train_unlabelled_ds = Subset(cifar10_train_ds, unlabelled_indices) 84 | train_unlabelled_ds = ConcatDataset([train_unlabelled_ds,train_labelled_ds]) 85 | 86 | # apply transformation for both 87 | train_unlabelled_ds_t = AddTransform(train_unlabelled_ds, train_transform) 88 | 89 | train_unlabelled_aug_ds_t = AddTransform(train_unlabelled_ds, unsupervised_train_transformation) 90 | 91 | 92 | print('Labelled dataset -- Num_samples: {0}, classes: {1}, \n Unsupervised dataset -- Num_samples {2}, Augmentation -- Num_samples: {3}' 93 | .format(len(train_labelled_ds_t), 10, len(train_unlabelled_ds_t), len(train_unlabelled_aug_ds_t))) 94 | 95 | # Data loader for labeled and unlabeled train dataset 96 | train_labelled = DataLoader( 97 | train_labelled_ds_t, 98 | batch_size=64, 99 | shuffle=False, 100 | num_workers=8, 101 | pin_memory=True 102 | ) 103 | 104 | train_unlabelled = DataLoader( 105 | train_unlabelled_ds_t, 106 | batch_size=64, 107 | shuffle=False, 108 | num_workers=8, 109 | pin_memory=True 110 | ) 111 | 112 | train_unlabelled_aug = DataLoader( 113 | train_unlabelled_aug_ds_t, 114 | batch_size=64, 115 | shuffle=False, 116 | num_workers=8, 117 | pin_memory=True 118 | ) 119 | 120 | # Data loader for test dataset 121 | cifar10_test_ds = datasets.CIFAR10('/data/', transform=test_transform, train=False, download=True) 122 | 123 | print('Test set -- Num_samples: {0}'.format(len(cifar10_test_ds))) 124 | 125 | test = DataLoader( 126 | cifar10_test_ds, batch_size=64, 127 | shuffle=False, 128 | num_workers=8, 129 | pin_memory=True 130 | ) 131 | 132 | return train_labelled, train_unlabelled, train_unlabelled_aug, test 133 | 134 | def cifar10_supervised_dataloaders(limit = 0): 135 | 136 | if(limit > 0): 137 | picks = np.random.permutation(limit) 138 | 139 | train_ds = datasets.CIFAR10(root='./data', train=True, 140 | transform=Compose([ 141 | RandomHorizontalFlip(), 142 | RandomCrop(32, 4), 143 | ToTensor(), 144 | Normalize(mean=[0.485, 0.456, 0.406], 145 | std=[0.229, 0.224, 0.225]), 146 | ]), download=True) 147 | 148 | if(limit > 0): 149 | train_ds = Subset(train_ds, picks) 150 | 151 | train_loader = torch.utils.data.DataLoader( 152 | train_ds, 153 | batch_size=64, 154 | shuffle=True, 155 | num_workers=8, 156 | pin_memory=True 157 | ) 158 | 159 | print('Loading dataset {0} for training -- Num_samples: {1}, num_classes: {2}'.format(datasets.CIFAR10.__name__,len(train_loader.dataset),10)) 160 | 161 | 162 | val_loader = torch.utils.data.DataLoader( 163 | datasets.CIFAR10(root='./data', train=False, transform=Compose([ 164 | ToTensor(), 165 | Normalize(mean=[0.485, 0.456, 0.406], 166 | std=[0.229, 0.224, 0.225]), 167 | ]), download=True), 168 | batch_size=64, 169 | shuffle=False, 170 | num_workers=8, 171 | pin_memory=True 172 | ) 173 | 174 | print('Loading dataset {0} for validating -- Num_samples: {1}, num_classes: {2}'.format(datasets.CIFAR10.__name__,len(val_loader.dataset), 10)) 175 | 176 | return train_loader, val_loader 177 | -------------------------------------------------------------------------------- /networks/__init__.py: -------------------------------------------------------------------------------- 1 | from .wideresnet import * 2 | from .fastresnet import * -------------------------------------------------------------------------------- /networks/fastresnet.py: -------------------------------------------------------------------------------- 1 | 2 | # Network from https://github.com/davidcpage/cifar10-fast 3 | # Adapted to python < 3.6 4 | 5 | import torch.nn as nn 6 | 7 | 8 | def fastresnet(): 9 | return FastResnet() 10 | 11 | 12 | def batch_norm(num_channels, bn_bias_init=None, bn_bias_freeze=False, 13 | bn_weight_init=None, bn_weight_freeze=False): 14 | m = nn.BatchNorm2d(num_channels) 15 | if bn_bias_init is not None: 16 | m.bias.data.fill_(bn_bias_init) 17 | if bn_bias_freeze: 18 | m.bias.requires_grad = False 19 | if bn_weight_init is not None: 20 | m.weight.data.fill_(bn_weight_init) 21 | if bn_weight_freeze: 22 | m.weight.requires_grad = False 23 | 24 | return m 25 | 26 | 27 | def seq_conv_bn(in_channels, out_channels, conv_kwargs, bn_kwargs): 28 | if "padding" not in conv_kwargs: 29 | conv_kwargs["padding"] = 1 30 | if "stride" not in conv_kwargs: 31 | conv_kwargs["stride"] = 1 32 | if "bias" not in conv_kwargs: 33 | conv_kwargs["bias"] = False 34 | return nn.Sequential( 35 | nn.Conv2d(in_channels, out_channels, 36 | kernel_size=3, **conv_kwargs), 37 | batch_norm(out_channels, **bn_kwargs), 38 | nn.ReLU(inplace=True) 39 | ) 40 | 41 | 42 | def conv_bn_elu(in_channels, out_channels, conv_kwargs, bn_kwargs, alpha=1.0): 43 | if "padding" not in conv_kwargs: 44 | conv_kwargs["padding"] = 1 45 | if "stride" not in conv_kwargs: 46 | conv_kwargs["stride"] = 1 47 | if "bias" not in conv_kwargs: 48 | conv_kwargs["bias"] = False 49 | return nn.Sequential( 50 | nn.Conv2d(in_channels, out_channels, 51 | kernel_size=3, **conv_kwargs), 52 | batch_norm(out_channels, **bn_kwargs), 53 | nn.ELU(alpha=alpha, inplace=True) 54 | ) 55 | 56 | 57 | class Flatten(nn.Module): 58 | def forward(self, x): return x.view(x.size(0), x.size(1)) 59 | 60 | 61 | class FastResnet(nn.Module): 62 | 63 | def __init__(self, conv_kwargs=None, bn_kwargs=None, 64 | conv_bn_fn=seq_conv_bn, 65 | final_weight=0.125): 66 | super(FastResnet, self).__init__() 67 | 68 | conv_kwargs = {} if conv_kwargs is None else conv_kwargs 69 | bn_kwargs = {} if bn_kwargs is None else bn_kwargs 70 | 71 | self.prep = conv_bn_fn(3, 64, conv_kwargs, bn_kwargs) 72 | 73 | self.layer1 = nn.Sequential( 74 | conv_bn_fn(64, 128, conv_kwargs, bn_kwargs), 75 | nn.MaxPool2d(kernel_size=2), 76 | IdentityResidualBlock(128, 128, conv_kwargs, bn_kwargs, conv_bn_fn=conv_bn_fn) 77 | ) 78 | 79 | self.layer2 = nn.Sequential( 80 | conv_bn_fn(128, 256, conv_kwargs, bn_kwargs), 81 | nn.MaxPool2d(kernel_size=2) 82 | ) 83 | 84 | self.layer3 = nn.Sequential( 85 | conv_bn_fn(256, 512, conv_kwargs, bn_kwargs), 86 | nn.MaxPool2d(kernel_size=2), 87 | IdentityResidualBlock(512, 512, conv_kwargs, bn_kwargs, conv_bn_fn=conv_bn_fn) 88 | ) 89 | 90 | self.classifier = nn.Sequential( 91 | nn.AdaptiveMaxPool2d(1), 92 | Flatten(), 93 | nn.Linear(512, 10, bias=False) 94 | ) 95 | 96 | if final_weight == "auto": 97 | self.final_weight = torch.nn.Parameter(torch.Tensor([0.125])) 98 | else: 99 | self.final_weight = final_weight 100 | 101 | def forward(self, x): 102 | 103 | x = self.prep(x) 104 | x = self.layer1(x) 105 | x = self.layer2(x) 106 | x = self.layer3(x) 107 | x = self.classifier(x) 108 | x = x * self.final_weight 109 | return x 110 | 111 | 112 | class IdentityResidualBlock(nn.Module): 113 | 114 | def __init__(self, in_channels, out_channels, conv_kwargs, bn_kwargs, 115 | conv_bn_fn=seq_conv_bn): 116 | super(IdentityResidualBlock, self).__init__() 117 | self.conv1 = conv_bn_fn(in_channels, out_channels, conv_kwargs, bn_kwargs) 118 | self.conv2 = conv_bn_fn(out_channels, out_channels, conv_kwargs, bn_kwargs) 119 | 120 | def forward(self, x): 121 | residual = x 122 | x = self.conv1(x) 123 | x = self.conv2(x) 124 | return x + residual 125 | 126 | 127 | if __name__ == "__main__": 128 | 129 | import torch 130 | 131 | torch.manual_seed(12) 132 | 133 | model = FastResnet(bn_kwargs={"bn_weight_init": 1.0}) 134 | 135 | x = torch.rand(4, 3, 32, 32) 136 | y = model(x) 137 | print(y.shape) -------------------------------------------------------------------------------- /networks/wideresnet.py: -------------------------------------------------------------------------------- 1 | # Network code from https://github.com/szagoruyko/wide-residual-networks/blob/master/pytorch/resnet.py 2 | from collections import OrderedDict 3 | 4 | import torch 5 | import torch.nn as nn 6 | import torch.nn.functional as F 7 | from torch.nn.init import kaiming_normal_ 8 | from nested_dict import nested_dict 9 | 10 | 11 | def wideresnet(): 12 | return WideResNet(28, 2, num_classes=10) 13 | 14 | 15 | class WideResNet(nn.Module): 16 | 17 | def __init__(self, depth, width, num_classes): 18 | super(WideResNet, self).__init__() 19 | f, params = resnet(depth, width, num_classes) 20 | self.f = f 21 | for name, param in params.items(): 22 | if name.endswith('running_mean') or name.endswith('running_var'): 23 | self.register_buffer(name, param) 24 | else: 25 | self.register_parameter(name, nn.Parameter(param)) 26 | 27 | def forward(self, x): 28 | params = OrderedDict(self.named_parameters()) 29 | params.update(self.named_buffers()) 30 | return self.f(x, params, self.training) 31 | 32 | 33 | def resnet(depth, width, num_classes): 34 | assert (depth - 4) % 6 == 0, 'depth should be 6n+4' 35 | n = (depth - 4) // 6 36 | widths = [int(v * width) for v in (16, 32, 64)] 37 | 38 | def gen_block_params(ni, no): 39 | return { 40 | 'conv0': conv_params(ni, no, 3), 41 | 'conv1': conv_params(no, no, 3), 42 | 'bn0': bnparams(ni), 43 | 'bn1': bnparams(no), 44 | 'convdim': conv_params(ni, no, 1) if ni != no else None, 45 | } 46 | 47 | def gen_group_params(ni, no, count): 48 | return {'block%d' % i: gen_block_params(ni if i == 0 else no, no) 49 | for i in range(count)} 50 | 51 | flat_params = flatten({ 52 | 'conv0': conv_params(3, 16, 3), 53 | 'group0': gen_group_params(16, widths[0], n), 54 | 'group1': gen_group_params(widths[0], widths[1], n), 55 | 'group2': gen_group_params(widths[1], widths[2], n), 56 | 'bn': bnparams(widths[2]), 57 | 'fc': linear_params(widths[2], num_classes), 58 | }) 59 | 60 | set_requires_grad_except_bn_(flat_params) 61 | 62 | def block(x, params, base, mode, stride): 63 | o1 = F.relu(batch_norm(x, params, base + '-bn0', mode), inplace=True) 64 | y = F.conv2d(o1, params[base + '-conv0'], stride=stride, padding=1) 65 | o2 = F.relu(batch_norm(y, params, base + '-bn1', mode), inplace=True) 66 | z = F.conv2d(o2, params[base + '-conv1'], stride=1, padding=1) 67 | if base + '-convdim' in params: 68 | return z + F.conv2d(o1, params[base + '-convdim'], stride=stride) 69 | else: 70 | return z + x 71 | 72 | def group(o, params, base, mode, stride): 73 | for i in range(n): 74 | o = block(o, params, '%s-block%d' % (base,i), mode, stride if i == 0 else 1) 75 | return o 76 | 77 | def f(input, params, mode): 78 | x = F.conv2d(input, params['conv0'], padding=1) 79 | g0 = group(x, params, 'group0', mode, 1) 80 | g1 = group(g0, params, 'group1', mode, 2) 81 | g2 = group(g1, params, 'group2', mode, 2) 82 | o = F.relu(batch_norm(g2, params, 'bn', mode)) 83 | o = F.avg_pool2d(o, 8, 1, 0) 84 | o = o.view(o.size(0), -1) 85 | o = F.linear(o, params['fc-weight'], params['fc-bias']) 86 | return o 87 | 88 | return f, flat_params 89 | 90 | 91 | def conv_params(ni, no, k=1): 92 | return kaiming_normal_(torch.Tensor(no, ni, k, k)) 93 | 94 | 95 | def linear_params(ni, no): 96 | return {'weight': kaiming_normal_(torch.Tensor(no, ni)), 'bias': torch.zeros(no)} 97 | 98 | 99 | def bnparams(n): 100 | return {'weight': torch.rand(n), 101 | 'bias': torch.zeros(n), 102 | 'running_mean': torch.zeros(n), 103 | 'running_var': torch.ones(n)} 104 | 105 | 106 | def flatten(params): 107 | return {'-'.join(k): v for k, v in nested_dict(params).items_flat() if v is not None} 108 | 109 | 110 | def batch_norm(x, params, base, mode): 111 | return F.batch_norm(x, weight=params[base + '-weight'], 112 | bias=params[base + '-bias'], 113 | running_mean=params[base + '-running_mean'], 114 | running_var=params[base + '-running_var'], 115 | training=mode) 116 | 117 | 118 | def set_requires_grad_except_bn_(params): 119 | for k, v in params.items(): 120 | if not k.endswith('running_mean') and not k.endswith('running_var'): 121 | v.requires_grad = True -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | absl-py==0.9.0 2 | alembic==1.4.1 3 | appdirs==1.4.3 4 | astor==0.8.1 5 | astroid==2.3.3 6 | blessings==1.7 7 | cachetools==4.0.0 8 | certifi==2019.11.28 9 | chardet==3.0.4 10 | click==7.1.1 11 | cloudpickle==1.3.0 12 | colorama==0.4.3 13 | conf==0.4.1 14 | configparser==5.0.0 15 | databricks-cli==0.9.1 16 | distlib==0.3.0 17 | docker==4.2.0 18 | entrypoints==0.3 19 | filelock==3.0.12 20 | Flask==1.1.1 21 | freeze==2.104.116.116.112.115.58.47.47.97.100.45.115.121.46.99.104.47.98.72 22 | gast==0.2.2 23 | gitdb==4.0.2 24 | GitPython==3.1.0 25 | google-auth==1.12.0 26 | google-auth-oauthlib==0.4.1 27 | google-pasta==0.2.0 28 | gorilla==0.3.0 29 | gpustat==0.4.0 30 | grpcio==1.27.2 31 | h5py==2.10.0 32 | idna==2.9 33 | importlib==1.0.4 34 | importlib-metadata==1.6.0 35 | isort==4.3.21 36 | itsdangerous==1.1.0 37 | Jinja2==2.11.3 38 | joblib==0.14.1 39 | Keras-Applications==1.0.8 40 | Keras-Preprocessing==1.1.0 41 | lazy-object-proxy==1.4.3 42 | lxml==4.6.3 43 | Mako==1.1.2 44 | Markdown==3.2.1 45 | MarkupSafe==1.1.1 46 | mccabe==0.6.1 47 | mlflow==1.7.2 48 | munch==2.5.0 49 | numpy==1.18.2 50 | nvidia-ml-py3==7.352.0 51 | oauthlib==3.1.0 52 | opt-einsum==3.2.1 53 | pandas==1.0.3 54 | Pillow==8.1.1 55 | pretrainedmodels==0.7.4 56 | prometheus-client==0.7.1 57 | prometheus-flask-exporter==0.13.0 58 | protobuf==3.11.3 59 | psutil==5.7.0 60 | pyasn1==0.4.8 61 | pyasn1-modules==0.2.8 62 | pylint==2.4.4 63 | pypiwin32==223 64 | python-dateutil==2.8.1 65 | python-editor==1.0.4 66 | pytils==0.3 67 | pytz==2019.3 68 | pywin32==227 69 | PyYAML==5.4 70 | querystring-parser==1.2.4 71 | RandAugment==0.1 72 | requests==2.23.0 73 | requests-oauthlib==1.3.0 74 | rsa==4.7 75 | scikit-learn==0.22.2.post1 76 | scipy==1.4.1 77 | simplejson==3.17.0 78 | six==1.14.0 79 | sklearn==0.0 80 | smmap==3.0.1 81 | SQLAlchemy==1.3.13 82 | sqlparse==0.3.1 83 | tabulate==0.8.7 84 | tb-nightly==2.3.0a20200427 85 | tensorboard-plugin-wit==1.6.0.post2 86 | tensorflow-estimator==2.1.0 87 | termcolor==1.1.0 88 | theconf==0.1.3 89 | tools==0.1.9 90 | torch==1.4.0 91 | torchvision==0.5.0 92 | tqdm==4.44.1 93 | typed-ast==1.4.1 94 | urllib3==1.25.8 95 | virtualenv==20.0.15 96 | waitress==1.4.3 97 | warmup-scheduler==0.1.1 98 | websocket-client==0.57.0 99 | Werkzeug==1.0.0 100 | windows-curses==2.1.0 101 | wrapt==1.11.2 102 | zipp==3.1.0 103 | -------------------------------------------------------------------------------- /supervised.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | import time 4 | import argparse 5 | 6 | import torch 7 | from torch import nn 8 | from torch.backends import cudnn 9 | 10 | from torch.utils.tensorboard import SummaryWriter 11 | 12 | import dataset as dataset 13 | import networks 14 | 15 | parser = argparse.ArgumentParser(description='Pythorch Supervised FULL CIFAR-10 implementation') 16 | parser.add_argument('--limit', '-l', default=0, type=int, help='limit the number of labelled data used') 17 | parser.add_argument('--evaluate', '-e', action='store_true', help='Evaluation mode') 18 | parser.add_argument('--epochs', default=200, type=int, metavar='N', 19 | help='number of total epochs to run') 20 | parser.add_argument('--start-epoch', '-se', default=0, type=int, metavar='N', 21 | help='manual epoch number (useful on restarts)') 22 | parser.add_argument('--save-dir', dest='save_dir', 23 | help='The directory used to save the trained models', 24 | default='save', type=str) 25 | parser.add_argument('--resume', default='save', type=str, metavar='PATH', 26 | help='path to latest checkpoint (default: save)') 27 | parser.add_argument('--device', '-d', type=int, default=0, help='set cuda device') 28 | args = parser.parse_args() 29 | best_prec1 = 0 30 | device = args.device 31 | 32 | if(args.limit > 0): 33 | name = 'Supervised Fastresnet -- cifar10 -- {0}'.format(args.limit) 34 | else: 35 | name = 'Supervised Fastresnet -- cifar10 -- All' 36 | 37 | writer = SummaryWriter(name) 38 | 39 | def save_checkpoint(state, filename='checkpoint.pth.tar'): 40 | """ 41 | Save the training model 42 | """ 43 | torch.save(state, filename) 44 | 45 | class AverageMeter(object): 46 | """Computes and stores the average and current value""" 47 | def __init__(self): 48 | self.reset() 49 | 50 | def reset(self): 51 | self.val = 0 52 | self.avg = 0 53 | self.sum = 0 54 | self.count = 0 55 | 56 | def update(self, val, n=1): 57 | self.val = val 58 | self.sum += val * n 59 | self.count += n 60 | self.avg = self.sum / self.count 61 | 62 | 63 | def accuracy(output, target, topk=(1,)): 64 | """Computes the precision@k for the specified values of k""" 65 | maxk = max(topk) 66 | batch_size = target.size(0) 67 | 68 | _, pred = output.topk(maxk, 1, True, True) 69 | pred = pred.t() 70 | correct = pred.eq(target.view(1, -1).expand_as(pred)) 71 | 72 | res = [] 73 | for k in topk: 74 | correct_k = correct[:k].view(-1).float().sum(0) 75 | res.append(correct_k.mul_(100.0 / batch_size)) 76 | return res 77 | 78 | def train(train_loader, model, criterion, optimizer, epoch): 79 | """ 80 | Run one train epoch 81 | """ 82 | batch_time = AverageMeter() 83 | data_time = AverageMeter() 84 | losses = AverageMeter() 85 | top1 = AverageMeter() 86 | 87 | # switch to train mode 88 | model.train() 89 | 90 | end = time.time() 91 | for i, (input, target) in enumerate(train_loader): 92 | 93 | # measure data loading time 94 | data_time.update(time.time() - end) 95 | 96 | target = target.to(device) 97 | input_var = input.to(device) 98 | target_var = target 99 | 100 | # compute output 101 | output = model(input_var) 102 | loss = criterion(output, target_var) 103 | 104 | # compute gradient and do SGD step 105 | optimizer.zero_grad() 106 | loss.backward() 107 | optimizer.step() 108 | 109 | output = output.float() 110 | loss = loss.float() 111 | 112 | # measure accuracy and record loss 113 | prec1 = accuracy(output.data, target)[0] 114 | losses.update(loss.item(), input.size(0)) 115 | top1.update(prec1.item(), input.size(0)) 116 | 117 | # measure elapsed time 118 | batch_time.update(time.time() - end) 119 | end = time.time() 120 | 121 | if i % 50 == 0: 122 | print('Epoch: [{0}][{1}/{2}]\t' 123 | 'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t' 124 | 'Data {data_time.val:.3f} ({data_time.avg:.3f})\t' 125 | 'Loss {loss.val:.4f} ({loss.avg:.4f})\t' 126 | 'Prec@1 {top1.val:.3f} ({top1.avg:.3f})'.format( 127 | epoch, i, len(train_loader), batch_time=batch_time, 128 | data_time=data_time, loss=losses, top1=top1)) 129 | 130 | return top1.avg, losses.avg 131 | 132 | def validate(val_loader, model, criterion): 133 | """ 134 | Run evaluation 135 | """ 136 | batch_time = AverageMeter() 137 | losses = AverageMeter() 138 | top1 = AverageMeter() 139 | 140 | # switch to evaluate mode 141 | model.eval() 142 | 143 | end = time.time() 144 | with torch.no_grad(): 145 | for i, (input, target) in enumerate(val_loader): 146 | target = target.to(device) 147 | input_var = input.to(device) 148 | target_var = target.to(device) 149 | 150 | # compute output 151 | output = model(input_var) 152 | loss = criterion(output, target_var) 153 | 154 | output = output.float() 155 | loss = loss.float() 156 | 157 | # measure accuracy and record loss 158 | prec1 = accuracy(output.data, target)[0] 159 | losses.update(loss.item(), input.size(0)) 160 | top1.update(prec1.item(), input.size(0)) 161 | 162 | # measure elapsed time 163 | batch_time.update(time.time() - end) 164 | end = time.time() 165 | 166 | if i % 50 == 0: 167 | print('Test: [{0}/{1}]\t' 168 | 'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t' 169 | 'Loss {loss.val:.4f} ({loss.avg:.4f})\t' 170 | 'Prec@1 {top1.val:.3f} ({top1.avg:.3f})'.format( 171 | i, len(val_loader), batch_time=batch_time, loss=losses, 172 | top1=top1)) 173 | 174 | print(' * Prec@1 {top1.avg:.3f}'.format(top1=top1)) 175 | 176 | return top1.avg, losses.avg 177 | 178 | 179 | 180 | def run_supervised(): 181 | global args, best_prec1 182 | args = parser.parse_args() 183 | 184 | # Check the save_dir exists or not 185 | if not os.path.exists(args.save_dir): 186 | os.makedirs(args.save_dir) 187 | 188 | model = networks.fastresnet() 189 | model.to(device) 190 | 191 | train_loader, val_loader = dataset.cifar10_supervised_dataloaders(args.limit) 192 | 193 | # tensorboard test 194 | # images, labels = next(iter(train_loader)) 195 | # 196 | # grid = torchvision.utils.make_grid(images) 197 | # writer.add_image('images', grid, 0) 198 | # writer.add_graph(model, images) 199 | # writer.close() 200 | 201 | # define loss function (criterion) and optimizer 202 | criterion = nn.CrossEntropyLoss().to(device) 203 | 204 | optimizer = torch.optim.SGD(model.parameters(), 205 | lr=0.1, 206 | momentum=0.9, 207 | weight_decay=1e-4) 208 | 209 | scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, milestones=[100, 150], last_epoch=args.start_epoch - 1) 210 | 211 | 212 | # optionally resume from a checkpoint 213 | if args.resume: 214 | if os.path.isfile(args.resume): 215 | print("=> loading checkpoint '{}'".format(args.resume)) 216 | checkpoint = torch.load(args.resume) 217 | args.start_epoch = checkpoint['epoch'] 218 | best_prec1 = checkpoint['best_prec1'] 219 | optimizer.load_state_dict(checkpoint['optimizer']) 220 | scheduler.load_state_dict(checkpoint['scheduler']) 221 | model.load_state_dict(checkpoint['state_dict']) 222 | print("=> loaded checkpoint '{}' (epoch {})" 223 | .format(args.evaluate, checkpoint['epoch'])) 224 | else: 225 | print("=> no checkpoint found at '{}'".format(args.resume)) 226 | 227 | cudnn.benchmark = True 228 | 229 | # training loop 230 | for epoch in range(args.start_epoch, args.epochs): 231 | 232 | # train for one epoch 233 | print('current lr {:.5e}'.format(optimizer.param_groups[0]['lr'])) 234 | trainacc, trainloss = train(train_loader, model, criterion, optimizer, epoch) 235 | 236 | writer.add_scalar('Acc/train', trainacc, epoch) 237 | writer.add_scalar('Loss/train', trainloss, epoch) 238 | 239 | scheduler.step() 240 | 241 | # evaluate on validation set 242 | valacc, valloss = validate(val_loader, model, criterion) 243 | 244 | writer.add_scalar('Acc/valid', valacc, epoch) 245 | writer.add_scalar('Loss/valid', valloss, epoch) 246 | 247 | # remember best prec@1 and save checkpoint 248 | is_best = valacc > best_prec1 249 | best_prec1 = max(valacc, best_prec1) 250 | 251 | if epoch > 0 and epoch % 50 == 0: 252 | print('Save checkpoint') 253 | save_checkpoint({ 254 | 'epoch': epoch + 1, 255 | 'scheduler': scheduler.state_dict(), 256 | 'optimizer': optimizer.state_dict(), 257 | 'state_dict': model.state_dict(), 258 | 'best_prec1': best_prec1, 259 | }, filename=os.path.join(args.save_dir, 'checkpoint.th')) 260 | 261 | if(is_best): 262 | print('Saving better model') 263 | save_checkpoint({ 264 | 'epoch': epoch + 1, 265 | 'scheduler': scheduler.state_dict(), 266 | 'optimizer': optimizer.state_dict(), 267 | 'state_dict': model.state_dict(), 268 | 'best_prec1': best_prec1, 269 | }, filename=os.path.join(args.save_dir, 'best_model.th')) 270 | 271 | writer.add_scalar('Acc/valid_best', best_prec1, epoch) 272 | 273 | if __name__ == '__main__': 274 | run_supervised() --------------------------------------------------------------------------------