Repository: spytensor/pytorch_img_classification_for_competition Branch: master Commit: 75cdbf998216 Files: 23 Total size: 73.3 KB Directory structure: gitextract_vee5ftx8/ ├── .gitignore ├── LICENSE ├── README.md ├── config.py ├── ensemble.py ├── main.py ├── models/ │ ├── __init__.py │ └── model.py ├── test.py └── utils/ ├── __init__.py ├── logger.py ├── losses/ │ ├── __init__.py │ ├── focalloss.py │ └── label_smoothing.py ├── misc.py ├── optimizers/ │ ├── __init__.py │ ├── lookahead.py │ ├── novograd.py │ ├── over9000.py │ ├── radam.py │ ├── ralamb.py │ └── ranger.py └── reader.py ================================================ FILE CONTENTS ================================================ ================================================ FILE: .gitignore ================================================ # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] *$py.class # C extensions *.so # Distribution / packaging .Python build/ develop-eggs/ dist/ downloads/ eggs/ .eggs/ lib/ lib64/ parts/ sdist/ var/ wheels/ pip-wheel-metadata/ share/python-wheels/ *.egg-info/ .installed.cfg *.egg MANIFEST # PyInstaller # Usually these files are written by a python script from a template # before PyInstaller builds the exe, so as to inject date/other infos into it. *.manifest *.spec # Installer logs pip-log.txt pip-delete-this-directory.txt # Unit test / coverage reports htmlcov/ .tox/ .nox/ .coverage .coverage.* .cache nosetests.xml coverage.xml *.cover *.py,cover .hypothesis/ .pytest_cache/ # Translations *.mo *.pot # Django stuff: *.log local_settings.py db.sqlite3 db.sqlite3-journal # Flask stuff: instance/ .webassets-cache # Scrapy stuff: .scrapy # Sphinx documentation docs/_build/ # PyBuilder target/ # Jupyter Notebook .ipynb_checkpoints # IPython profile_default/ ipython_config.py # pyenv .python-version # pipenv # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. # However, in case of collaboration, if having platform-specific dependencies or dependencies # having no cross-platform support, pipenv may install dependencies that don't work, or not # install all needed dependencies. #Pipfile.lock # PEP 582; used by e.g. github.com/David-OConnor/pyflow __pypackages__/ # Celery stuff celerybeat-schedule celerybeat.pid # SageMath parsed files *.sage.py # Environments .env .venv env/ venv/ ENV/ env.bak/ venv.bak/ # Spyder project settings .spyderproject .spyproject # Rope project settings .ropeproject # mkdocs documentation /site # mypy .mypy_cache/ .dmypy.json dmypy.json # Pyre type checker .pyre/ ================================================ FILE: LICENSE ================================================ Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ================================================ FILE: README.md ================================================ ### pytorch 图像分类竞赛框架 ### 1. 更新日志 - (2020年5月2日) 基础版本上线 ### 2. 依赖库 - pretrainedmodels - progress - efficientnet-pytorch - apex ### 3. 支持功能 - [x] pytorch官网模型 - [x] [pretrained-models.pytorch](https://github.com/Cadene/pretrained-models.pytorch) 复现的部分模型 - [x] [EfficientNet-PyTorch](https://github.com/lukemelas/EfficientNet-PyTorch) - [x] fp16混合精度训练 - [x] TTA - [x] 固定验证集/随机划分验证集 - [x] 多种优化器:adam、radam、novograd、sgd、ranger、ralamb、over9000、lookahead、lamb - [x] OneCycle训练策略 - [x] LabelSmoothLoss - [x] Focal Loss - [ ] AotuAgument ### 4. 使用方法 更改`config.py`中的参数,训练执行 `python main.py`,预测执行`python test.py` ### 5. submit_example.csv 每一行:filename,label 样例: ``` 0001.jpg,dog 0002.jpg,dog 0003.jpg,dog ``` 注:预测图像可能没有label,所以label可以随意给个临时的,但一些比赛平台对都会给个提交样例,随意给个label ### 6.TODO - [ ] 优化模型融合策略 - [ ] 优化online数据增强 - [ ] 优化pytorch官方模型调用接口 - [ ] 增加模型全连接层初始化 - [ ] 增加更多学习率衰减策略 - [ ] 增加find lr - [ ] 增加dali - [ ] 增加wsl模型 - [ ] 增加tensorboardX - [ ] 优化文件夹创建 ================================================ FILE: config.py ================================================ class DefaultConfigs(object): # set default configs, if you don't understand, don't modify seed = 666 # set random seed workers = 4 # set number of data loading workers (default: 4) beta1 = 0.9 # adam parameters beta1 beta2 = 0.999 # adam parameters beta2 mom = 0.9 # momentum parameters wd = 1e-4 # weight-decay resume = None # path to latest checkpoint (default: none),should endswith ".pth" or ".tar" if used evaluate = False # just do evaluate start_epoch = 0 # deault start epoch is zero,if use resume change it split_online = False # split dataset to train and val online or offline # set changeable configs, you can change one during your experiment dataset = "/dataset/df/cloud/data/dataset/" # dataset folder with train and val test_folder = "/dataset/df/cloud/data/test/" # test images' folder submit_example = "/dataset/df/cloud/data/submit_example.csv" # submit example file checkpoints = "./checkpoints/" # path to save checkpoints log_dir = "./logs/" # path to save log files submits = "./submits/" # path to save submission files bs = 32 # batch size lr = 2e-3 # learning rate epochs = 40 # train epochs input_size = 512 # model input size or image resied num_classes = 9 # num of classes gpu_id = "0" # default gpu id model_name = "se_resnext50_32x4d-model-sgd-512" # model name to use optim = "sgd" # "adam","radam","novograd",sgd","ranger","ralamb","over9000","lookahead","lamb" fp16 = True # use float16 to train the model opt_level = "O1" # if use fp16, "O0" means fp32,"O1" means mixed,"O2" means except BN,"O3" means only fp16 keep_batchnorm_fp32 = False # if use fp16,keep BN layer as fp32 loss_func = "CrossEntropy" # "CrossEntropy"、"FocalLoss"、"LabelSmoothCE" lr_scheduler = "step" # lr scheduler method,"adjust","on_loss","on_acc","step" configs = DefaultConfigs() ================================================ FILE: ensemble.py ================================================ import pandas as pd import numpy as np import os from IPython import embed file1 = pd.read_csv("./csvs/efficientnet-b3-model_512-_adam_aug_confidence.csv",header=None) file2 = pd.read_csv("./csvs/efficientnet-b5-model_456_ranger_aug_confidence.csv",header=None) file3 = pd.read_csv("./csvs/efficientnet-b4-model_380_ranger_aug_confidence.csv",header=None) filenames,labels = [],[] # embed() # for (filename1,label1),(filename2,label2),(filename3,label3),(filename4,label4),(filename5,label5) in zip(file1.values,file2.values,file3.values,file4.values,file5.values): for (filename1,label1) ,(filename2,label2),(filename3,label3) in zip(file1.values,file2.values,file3.values): filename = filename1 filenames.append(filename) #embed() label1 = np.array(list(map(float,label1.split("-")))) label2 = np.array(list(map(float,label2.split("-")))) label3 = np.array(list(map(float,label3.split("-")))) # label4 = np.array(list(map(float,label4.split("[")[1].split("]")[0].split(",")))) # label5 = np.array(list(map(float,label5.split("[")[1].split("]")[0].split(",")))) label = np.argmax((label1 + label2 + label3) / 3.0) + 1 labels.append(label) submission = pd.DataFrame({'FileName': filenames, 'type': labels}) submission.to_csv("./ensemble_efficientnets.csv", header=None, index=False) ================================================ FILE: main.py ================================================ import random import time import warnings import torch.nn as nn import torch.nn.parallel import torch.backends.cudnn as cudnn import torch.optim import torch.utils.data import torch.utils.data.distributed import torchvision.transforms as transforms import torchvision.datasets as datasets import numpy as np from PIL import ImageFile from config import configs from models.model import get_model from sklearn.model_selection import train_test_split from utils.misc import * from utils.logger import * from utils.losses import * from progress.bar import Bar from utils.reader import WeatherDataset # for train fp16 if configs.fp16: try: import apex from apex.parallel import DistributedDataParallel as DDP from apex.fp16_utils import * from apex import amp, optimizers from apex.multi_tensor_apply import multi_tensor_applier except ImportError: raise ImportError("Please install apex from https://www.github.com/nvidia/apex to run this example.") ImageFile.LOAD_TRUNCATED_IMAGES = True warnings.filterwarnings("ignore") os.environ['CUDA_VISIBLE_DEVICES'] = configs.gpu_id # set random seed def seed_everything(seed): random.seed(seed) os.environ['PYTHONHASHSEED'] = str(seed) np.random.seed(seed) torch.manual_seed(seed) torch.cuda.manual_seed(seed) torch.backends.cudnn.deterministic = True seed_everything(configs.seed) # make dir for use def makdir(): if not os.path.exists(configs.checkpoints): os.makedirs(configs.checkpoints) if not os.path.exists(configs.log_dir): os.makedirs(configs.log_dir) if not os.path.exists(configs.submits): os.makedirs(configs.submits) makdir() best_acc = 0 # best test accuracy best_loss = 999 # lower loss def main(): global best_acc global best_loss start_epoch = configs.start_epoch # set normalize configs for imagenet normalize_imgnet = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) transform_train = transforms.Compose([ transforms.RandomResizedCrop(configs.input_size), transforms.RandomHorizontalFlip(p=0.5), transforms.RandomVerticalFlip(p=0.5), transforms.ToTensor(), normalize_imgnet ]) transform_val = transforms.Compose([ transforms.Resize(int(configs.input_size * 1.2)), transforms.CenterCrop(configs.input_size), transforms.ToTensor(), normalize_imgnet ]) # Data loading code if configs.split_online: # use online random split dataset method total_files = get_files(configs.dataset,"train") train_files,val_files = train_test_split(total_files,test_size = 0.1,stratify=total_files["label"]) train_dataset = WeatherDataset(train_files,transform_train) val_dataset = WeatherDataset(val_files,transform_val) else: # use offline split dataset train_files = get_files(configs.dataset+"/train/","train") val_files = get_files(configs.dataset+"/val/","train") train_dataset = WeatherDataset(train_files,transform_train) val_dataset = WeatherDataset(val_files,transform_val) train_loader = torch.utils.data.DataLoader( train_dataset, batch_size=configs.bs, shuffle=True, num_workers=configs.workers, pin_memory=True, ) val_loader = torch.utils.data.DataLoader( val_dataset, batch_size=configs.bs, shuffle=False, num_workers=configs.workers, pin_memory=True ) # get model model = get_model() model.cuda() # choose loss func,default is CE if configs.loss_func == "LabelSmoothCE": criterion = LabelSmoothingLoss(0.1, configs.num_classes).cuda() elif configs.loss_func == "CrossEntropy": criterion = nn.CrossEntropyLoss().cuda() elif configs.loss_func == "FocalLoss": criterion = FocalLoss(gamma=2).cuda() else: criterion = nn.CrossEntropyLoss().cuda() optimizer = get_optimizer(model) # set lr scheduler method if configs.lr_scheduler == "step": scheduler = torch.optim.lr_scheduler.StepLR(optimizer,step_size=10,gamma=0.1) elif configs.lr_scheduler == "on_loss": scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, mode='min', factor=0.2, patience=5, verbose=False) elif configs.lr_scheduler == "on_acc": scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, mode='max', factor=0.2, patience=5, verbose=False) else: scheduler = torch.optim.lr_scheduler.StepLR(optimizer,step_size=6,gamma=0.1) # for fp16 if configs.fp16: model, optimizer = amp.initialize(model, optimizer, opt_level=configs.opt_level, keep_batchnorm_fp32= None if configs.opt_level == "O1" else configs.keep_batchnorm_fp32 ) if configs.resume: # Load checkpoint. print('==> Resuming from checkpoint..') assert os.path.isfile(configs.resume), 'Error: no checkpoint directory found!' configs.checkpoint = os.path.dirname(configs.resume) checkpoint = torch.load(configs.resume) best_acc = checkpoint['best_acc'] start_epoch = checkpoint['epoch'] model.module.load_state_dict(checkpoint['state_dict']) optimizer.load_state_dict(checkpoint['optimizer']) logger = Logger(os.path.join(configs.log_dir, '%s_log.txt'%configs.model_name), title=configs.model_name, resume=True) else: logger = Logger(os.path.join(configs.log_dir, '%s_log.txt'%configs.model_name), title=configs.model_name) logger.set_names(['Learning Rate', 'Train Loss', 'Valid Loss', 'Train Acc.', 'Valid Acc.']) if configs.evaluate: print('\nEvaluation only') val_loss, val_acc = validate(val_loader, model, criterion, start_epoch) print(' Test Loss: %.8f, Test Acc: %.2f' % (val_loss, val_acc)) return # Train and val for epoch in range(start_epoch, configs.epochs): print('\nEpoch: [%d | %d] LR: %f' % (epoch + 1, configs.epochs, optimizer.param_groups[0]['lr'])) train_loss, train_acc, train_5 = train(train_loader, model, criterion, optimizer, epoch) val_loss, val_acc, test_5 = validate(val_loader, model, criterion, epoch) # adjust lr if configs.lr_scheduler == "on_loss": scheduler.step(val_loss) elif configs.lr_scheduler == "on_acc": scheduler.step(val_acc) elif configs.lr_scheduler == "step": scheduler.step(epoch) elif configs.lr_scheduler == "adjust": adjust_learning_rate(optimizer,epoch) else: scheduler.step(epoch) # append logger file lr_current = get_lr(optimizer) logger.append([lr_current,train_loss, val_loss, train_acc, val_acc]) print('train_loss:%f, val_loss:%f, train_acc:%f, train_5:%f, val_acc:%f, val_5:%f' % (train_loss, val_loss, train_acc, train_5, val_acc, test_5)) # save model is_best = val_acc > best_acc is_best_loss = val_loss < best_loss best_acc = max(val_acc, best_acc) best_loss = min(val_loss,best_loss) save_checkpoint({ 'fold': 0, 'epoch': epoch + 1, 'state_dict': model.state_dict(), 'train_acc': train_acc, 'acc': val_acc, 'best_acc': best_acc, 'best_loss': best_loss, 'optimizer': optimizer.state_dict(), }, is_best,is_best_loss) logger.close() print('Best acc:') print(best_acc) def train(train_loader, model, criterion, optimizer, epoch): # switch to train mode model.train() batch_time = AverageMeter() data_time = AverageMeter() losses = AverageMeter() top1 = AverageMeter() top5 = AverageMeter() end = time.time() bar = Bar('Training: ', max=len(train_loader)) for batch_idx, (inputs, targets) in enumerate(train_loader): # measure data loading time data_time.update(time.time() - end) inputs, targets = inputs.cuda(), targets.cuda() inputs, targets = torch.autograd.Variable(inputs), torch.autograd.Variable(targets) # compute output outputs = model(inputs) loss = criterion(outputs, targets) # measure accuracy and record loss prec1, prec5 = accuracy(outputs.data, targets.data, topk=(1, 5)) losses.update(loss.item(), inputs.size(0)) top1.update(prec1.item(), inputs.size(0)) top5.update(prec5.item(), inputs.size(0)) # compute gradient and do SGD step optimizer.zero_grad() if configs.fp16: with amp.scale_loss(loss, optimizer) as scaled_loss: scaled_loss.backward() else: loss.backward() # clip gradient torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=5.0, norm_type=2) optimizer.step() # measure elapsed time batch_time.update(time.time() - end) end = time.time() # plot progress bar.suffix = '({batch}/{size}) Data: {data:.3f}s | Batch: {bt:.3f}s | Total: {total:} | ETA: {eta:} | Loss: {loss:.4f} | top1: {top1: .4f} | top5: {top5: .4f}'.format( batch=batch_idx + 1, size=len(train_loader), data=data_time.val, bt=batch_time.val, total=bar.elapsed_td, eta=bar.eta_td, loss=losses.avg, top1=top1.avg, top5=top5.avg, ) bar.next() bar.finish() return (losses.avg, top1.avg, top5.avg) def validate(val_loader, model, criterion, epoch): global best_acc batch_time = AverageMeter() data_time = AverageMeter() losses = AverageMeter() top1 = AverageMeter() top5 = AverageMeter() # switch to evaluate mode model.eval() end = time.time() bar = Bar('Validating: ', max=len(val_loader)) with torch.no_grad(): for batch_idx, (inputs, targets) in enumerate(val_loader): # measure data loading time data_time.update(time.time() - end) inputs, targets = inputs.cuda(), targets.cuda() inputs, targets = torch.autograd.Variable(inputs), torch.autograd.Variable(targets) # compute output outputs = model(inputs) loss = criterion(outputs, targets) # measure accuracy and record loss prec1, prec5 = accuracy(outputs.data, targets.data, topk=(1, 5)) losses.update(loss.item(), inputs.size(0)) top1.update(prec1.item(), inputs.size(0)) top5.update(prec5.item(), inputs.size(0)) # measure elapsed time batch_time.update(time.time() - end) end = time.time() # plot progress bar.suffix = '({batch}/{size}) Data: {data:.3f}s | Batch: {bt:.3f}s | Total: {total:} | ETA: {eta:} | Loss: {loss:.4f} | top1: {top1: .4f} | top5: {top5: .4f}'.format( batch=batch_idx + 1, size=len(val_loader), data=data_time.avg, bt=batch_time.avg, total=bar.elapsed_td, eta=bar.eta_td, loss=losses.avg, top1=top1.avg, top5=top5.avg, ) bar.next() bar.finish() return (losses.avg, top1.avg, top5.avg) if __name__ == '__main__': main() ================================================ FILE: models/__init__.py ================================================ from .model import * ================================================ FILE: models/model.py ================================================ from pretrainedmodels import models as pm import pretrainedmodels from torch import nn from torchvision import models as tm from config import configs from efficientnet_pytorch import EfficientNet import torch from torch.nn.parameter import Parameter import torch.nn.functional as F from torch.nn.parameter import Parameter weights = { "efficientnet-b3":"/data/dataset/detection/pretrainedmodels/efficientnet-b3-c8376fa2.pth", "efficientnet-b4":"/data/dataset/detection/pretrainedmodels/efficientnet-b4-6ed6700e.pth", "efficientnet-b5":"/data/dataset/detection/pretrainedmodels/efficientnet-b5-b6417697.pth", "efficientnet-b6":"/data/dataset/detection/pretrainedmodels/efficientnet-b6-c76e70fd.pth", } def gem(x, p=3, eps=1e-6): return F.avg_pool2d(x.clamp(min=eps).pow(p), (x.size(-2), x.size(-1))).pow(1./p) class GeM(nn.Module): def __init__(self, p=3, eps=1e-6): super(GeM,self).__init__() self.p = Parameter(torch.ones(1)*p) self.eps = eps def forward(self, x): return gem(x, p=self.p, eps=self.eps) def __repr__(self): return self.__class__.__name__ + '(' + 'p=' + '{:.4f}'.format(self.p.data.tolist()[0]) + ', ' + 'eps=' + str(self.eps) + ')' def get_model(): if configs.model_name.startswith("resnext50_32x4d"): model = tm.resnext50_32x4d(pretrained=True) model.avgpool = nn.AdaptiveAvgPool2d(1) model.fc = nn.Linear(2048,configs.num_classes) model.cuda() elif configs.model_name.startswith("efficient"): # efficientNet model_name = configs.model_name[:15] model = EfficientNet.from_name(model_name) model.load_state_dict(torch.load(weights[model_name])) in_features = model._fc.in_features model._fc = nn.Sequential( nn.BatchNorm1d(in_features), nn.Dropout(0.5), nn.Linear(in_features, configs.num_classes), ) model.cuda() else: pretrained = "imagenet+5k" if configs.model_name.startswith("dpn") else "imagenet" model = pretrainedmodels.__dict__[configs.model_name.split("-model")[0]](num_classes=1000, pretrained=pretrained) if configs.model_name.startswith("pnasnet"): model.last_linear = nn.Linear(4320, configs.num_classes) model.avg_pool = nn.AdaptiveAvgPool2d(1) elif configs.model_name.startswith("inception"): model.last_linear = nn.Linear(1536, configs.num_classes) model.avgpool_1a = nn.AdaptiveAvgPool2d(1) else: model.last_linear = nn.Linear(2048, configs.num_classes) model.avg_pool = nn.AdaptiveAvgPool2d(1) model.cuda() return model ================================================ FILE: test.py ================================================ import os import torch import warnings import pandas as pd import numpy as np import torch.backends.cudnn as cudnn from tqdm import tqdm from glob import glob from PIL import Image,ImageFile from config import configs from models.model import get_model from torch.utils.data import Dataset,DataLoader from torchvision import transforms from utils.misc import get_files from IPython import embed ImageFile.LOAD_TRUNCATED_IMAGES = True warnings.filterwarnings("ignore") os.environ['CUDA_VISIBLE_DEVICES'] = configs.gpu_id len_data = 0 class WeatherTTADataset(Dataset): def __init__(self,labels_file,aug): imgs = [] for index, row in labels_file.iterrows(): imgs.append((row["FileName"],row["type"])) self.imgs = imgs self.length = len(imgs) global len_data len_data = self.length self.aug = aug self.Hflip = transforms.RandomHorizontalFlip(p=1) self.Vflip = transforms.RandomVerticalFlip(p=1) self.Rotate = transforms.functional.rotate self.resize = transforms.Resize((configs.input_size,configs.input_size)) self.randomCrop = transforms.Compose([transforms.Resize(int(configs.input_size * 1.2)), transforms.CenterCrop(configs.input_size), ]) def __getitem__(self,index): filename,label_tmp = self.imgs[index] img = Image.open(configs.test_folder + os.sep + filename).convert('RGB') img = self.transform_(img,self.aug) return img,filename def __len__(self): return self.length def transform_(self,data_torch,aug): if aug == 'Ori': data_torch = data_torch data_torch = self.resize(data_torch) if aug == 'Ori_Hflip': data_torch = self.Hflip(data_torch) data_torch = self.resize(data_torch) if aug == 'Ori_Vflip': data_torch = self.Vflip(data_torch) data_torch = self.resize(data_torch) if aug == 'Ori_Rotate_90': data_torch = self.Rotate(data_torch, 90) data_torch = self.resize(data_torch) if aug == 'Ori_Rotate_180': data_torch = self.Rotate(data_torch, 180) data_torch = self.resize(data_torch) if aug == 'Ori_Rotate_270': data_torch = self.Rotate(data_torch, 270) data_torch = self.resize(data_torch) if aug == 'Crop': # print(data_torch.size) data_torch = self.randomCrop(data_torch) data_torch = data_torch if aug == 'Crop_Hflip': data_torch = self.randomCrop(data_torch) data_torch = self.Hflip(data_torch) if aug == 'Crop_Vflip': data_torch = self.randomCrop(data_torch) data_torch = self.Vflip(data_torch) if aug == 'Crop_Rotate_90': data_torch = self.randomCrop(data_torch) data_torch = self.Rotate(data_torch, 90) if aug == 'Crop_Rotate_180': data_torch = self.randomCrop(data_torch) data_torch = self.Rotate(data_torch, 180) if aug == 'Crop_Rotate_270': data_torch = self.randomCrop(data_torch) data_torch = self.Rotate(data_torch, 270) data_torch = transforms.ToTensor()(data_torch) data_torch = transforms.Normalize(mean=[0.485, 0.456, 0.406],std=[0.229, 0.224, 0.225])(data_torch) return data_torch #aug = ['Ori','Ori_Hflip','Ori_Vflip','Ori_Rotate_90','Ori_Rotate_180','Ori_Rotate_270', # 'Crop','Crop_Hflip','Crop_Vflip','Crop_Rotate_90','Crop_Rotate_180','Crop_Rotate_270'] aug = ['Ori_Hflip'] cpk_filename = configs.checkpoints + os.sep + configs.model_name + "-checkpoint.pth.tar" best_cpk = cpk_filename.replace("-checkpoint.pth.tar","-best_model.pth.tar") checkpoint = torch.load(best_cpk) cudnn.benchmark = True model = get_model() model.load_state_dict(checkpoint['state_dict']) model.eval() test_files = pd.read_csv(configs.submit_example) with torch.no_grad(): y_pred_prob = torch.FloatTensor([]) for a in tqdm(aug): print(a) test_set = WeatherTTADataset(test_files, a) test_loader = DataLoader(dataset=test_set, batch_size=configs.bs, shuffle=False, num_workers=4, pin_memory=True, sampler=None) total = 0 correct = 0 for inputs, labels in tqdm(test_loader): inputs = inputs.cuda() outputs = model(inputs) outputs = torch.nn.functional.softmax(outputs, dim=1) # print(outputs.shape) y_pred_prob = torch.cat([y_pred_prob, outputs.to("cpu")], dim=0) #embed() y_pred_prob = y_pred_prob.reshape((len(aug), len_data, configs.num_classes)) y_pred_prob = torch.sum(y_pred_prob, 0) / (len(aug) * 1.0) _, predicted_all = torch.max(y_pred_prob, 1) predicted = predicted_all + 1 # If the category starts with 1 ,else delet 1 test_files.type = predicted.data.cpu().numpy().tolist() test_files.to_csv('./submits/%s_baseline.csv' % configs.model_name, index=False) ================================================ FILE: utils/__init__.py ================================================ from .optimizers import * from .logger import * from .losses import * ================================================ FILE: utils/logger.py ================================================ # A simple torch style logger # (C) Wei YANG 2017 from __future__ import absolute_import import matplotlib.pyplot as plt import os import sys import numpy as np __all__ = ['Logger', 'LoggerMonitor', 'savefig'] def savefig(fname, dpi=None): dpi = 150 if dpi == None else dpi plt.savefig(fname, dpi=dpi) def plot_overlap(logger, names=None): names = logger.names if names == None else names numbers = logger.numbers for _, name in enumerate(names): x = np.arange(len(numbers[name])) plt.plot(x, np.asarray(numbers[name])) return [logger.title + '(' + name + ')' for name in names] class Logger(object): '''Save training process to log file with simple plot function.''' def __init__(self, fpath, title=None, resume=False): self.file = None self.resume = resume self.title = '' if title == None else title if fpath is not None: if resume: self.file = open(fpath, 'r') name = self.file.readline() self.names = name.rstrip().split('\t') self.numbers = {} for _, name in enumerate(self.names): self.numbers[name] = [] for numbers in self.file: numbers = numbers.rstrip().split('\t') for i in range(0, len(numbers)): self.numbers[self.names[i]].append(numbers[i]) self.file.close() self.file = open(fpath, 'a') else: self.file = open(fpath, 'w') def set_names(self, names): if self.resume: pass # initialize numbers as empty list self.numbers = {} self.names = names for _, name in enumerate(self.names): self.file.write(name) self.file.write('\t') self.numbers[name] = [] self.file.write('\n') self.file.flush() def append(self, numbers): assert len(self.names) == len(numbers), 'Numbers do not match names' for index, num in enumerate(numbers): self.file.write("{0:.6f}".format(num)) self.file.write('\t') self.numbers[self.names[index]].append(num) self.file.write('\n') self.file.flush() def plot(self, names=None): names = self.names if names == None else names numbers = self.numbers for _, name in enumerate(names): x = np.arange(len(numbers[name])) plt.plot(x, np.asarray(numbers[name])) plt.legend([self.title + '(' + name + ')' for name in names]) plt.grid(True) def close(self): if self.file is not None: self.file.close() class LoggerMonitor(object): '''Load and visualize multiple logs.''' def __init__ (self, paths): '''paths is a distionary with {name:filepath} pair''' self.loggers = [] for title, path in paths.items(): logger = Logger(path, title=title, resume=True) self.loggers.append(logger) def plot(self, names=None): plt.figure() plt.subplot(121) legend_text = [] for logger in self.loggers: legend_text += plot_overlap(logger, names) plt.legend(legend_text, bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.) plt.grid(True) if __name__ == '__main__': # Example: logger monitor paths = { 'temp':'./logs/efficientnet-b3/log.txt', } field = ['Valid Acc.'] monitor = LoggerMonitor(paths) monitor.plot(names=field) savefig('test.eps') ================================================ FILE: utils/losses/__init__.py ================================================ from .label_smoothing import LabelSmoothingLoss from .focalloss import FocalLoss ================================================ FILE: utils/losses/focalloss.py ================================================ import torch from torch import nn class FocalLoss(nn.Module): def __init__(self, gamma=2., reduction='mean'): super().__init__() self.gamma = gamma self.reduction = reduction def forward(self, inputs, targets): CE_loss = nn.CrossEntropyLoss(reduction='none')(inputs, targets) pt = torch.exp(-CE_loss) F_loss = ((1 - pt)**self.gamma) * CE_loss if self.reduction == 'sum': return F_loss.sum() elif self.reduction == 'mean': return F_loss.mean() ================================================ FILE: utils/losses/label_smoothing.py ================================================ import torch from torch import nn import torch.nn.functional as F class LabelSmoothingLoss(nn.Module): def __init__(self, label_smoothing, class_nums, ignore_index=-100): assert 0.0 < label_smoothing <= 1.0 self.ignore_index = ignore_index super(LabelSmoothingLoss, self).__init__() smoothing_value = label_smoothing / (class_nums - 1) one_hot = torch.full((class_nums,), smoothing_value) if self.ignore_index >= 0: one_hot[self.ignore_index] = 0 self.register_buffer('one_hot', one_hot.unsqueeze(0)) self.confidence = 1.0 - label_smoothing def forward(self, output, target): """ output (FloatTensor): batch_size x n_classes target (LongTensor): batch_size """ log_output = F.log_softmax(output, dim=1) model_prob = self.one_hot.repeat(target.size(0), 1) model_prob.scatter_(1, target.unsqueeze(1), self.confidence) if self.ignore_index >= 0: model_prob.masked_fill_((target == self.ignore_index).unsqueeze(1), 0) # print("model_prob:{}".format(model_prob)) # print("log_output:{}".format(log_output)) return -torch.sum(model_prob * log_output) / target.size(0) ================================================ FILE: utils/misc.py ================================================ import os import torch import shutil import pandas as pd from .optimizers import * from config import configs from torch import optim as optim_t from tqdm import tqdm from glob import glob from itertools import chain def get_optimizer(model): if configs.optim == "adam": return optim_t.Adam(model.parameters(), configs.lr, betas=(configs.beta1,configs.beta2), weight_decay=configs.wd) elif configs.optim == "radam": return RAdam(model.parameters(), configs.lr, betas=(configs.beta1,configs.beta2), weight_decay=configs.wd) elif configs.optim == "ranger": return Ranger(model.parameters(), lr = configs.lr, betas=(configs.beta1,configs.beta2), weight_decay=configs.wd) elif configs.optim == "over9000": return Over9000(model.parameters(), lr = configs.lr, betas=(configs.beta1,configs.beta2), weight_decay=configs.wd) elif configs.optim == "ralamb": return Ralamb(model.parameters(), lr = configs.lr, betas=(configs.beta1,configs.beta2), weight_decay=configs.wd) elif configs.optim == "sgd": return optim_t.SGD(model.parameters(), lr = configs.lr, momentum=configs.mom, weight_decay=configs.wd) else: print("%s optimizer will be add later"%configs.optim) def save_checkpoint(state,is_best,is_best_loss): filename = configs.checkpoints + os.sep + configs.model_name + "-checkpoint.pth.tar" torch.save(state, filename) if is_best: message = filename.replace("-checkpoint.pth.tar","-best_model.pth.tar") shutil.copyfile(filename, message) if is_best_loss: message = filename.replace("-checkpoint.pth.tar","-best_loss.pth.tar") shutil.copyfile(filename, message) def get_lr(optimizer): for param_group in optimizer.param_groups: return param_group['lr'] class AverageMeter(object): """Computes and stores the average and current value Imported from https://github.com/pytorch/examples/blob/master/imagenet/main.py#L247-L262 """ def __init__(self): self.reset() def reset(self): self.val = 0 self.avg = 0 self.sum = 0 self.count = 0 def update(self, val, n=1): self.val = val self.sum += val * n self.count += n self.avg = self.sum / self.count def accuracy(output, target, topk=(1,)): """Computes the precision@k for the specified values of k""" maxk = max(topk) batch_size = target.size(0) _, pred = output.topk(maxk, 1, True, True) pred = pred.t() correct = pred.eq(target.view(1, -1).expand_as(pred)) res = [] for k in topk: correct_k = correct[:k].view(-1).float().sum(0) res.append(correct_k.mul_(100.0 / batch_size)) return res def get_files(root,mode): if mode == "test": files = [] for img in os.listdir(root): files.append(root + img) files = pd.DataFrame({"filename":files}) return files else: all_data_path, labels = [], [] image_folders = list(map(lambda x: root + x, os.listdir(root))) all_images = list(chain.from_iterable(list(map(lambda x: glob(x + "/*"), image_folders)))) print("loading train dataset") for file in tqdm(all_images): all_data_path.append(file) labels.append(int(file.split(os.sep)[-2])) all_files = pd.DataFrame({"filename": all_data_path, "label": labels}) return all_files def adjust_learning_rate(optimizer, epoch): """Sets the learning rate to the initial LR decayed by 10 every 30 epochs""" lrs = [5e-4, 1e-4, 1e-5, 1e-6] if epoch<=10: lr = lrs[0] elif epoch>10 and epoch<=16: lr = lrs[1] elif epoch>16 and epoch<=22: lr = lrs[2] else: lr = lrs[-1] for param_group in optimizer.param_groups: param_group['lr'] = lr ================================================ FILE: utils/optimizers/__init__.py ================================================ from .lookahead import * from .novograd import * from .over9000 import * from .radam import * from .ralamb import * from .ranger import * ================================================ FILE: utils/optimizers/lookahead.py ================================================ # Lookahead implementation from https://github.com/rwightman/pytorch-image-models/blob/master/timm/optim/lookahead.py """ Lookahead Optimizer Wrapper. Implementation modified from: https://github.com/alphadl/lookahead.pytorch Paper: `Lookahead Optimizer: k steps forward, 1 step back` - https://arxiv.org/abs/1907.08610 """ import torch from torch.optim.optimizer import Optimizer from collections import defaultdict class Lookahead(Optimizer): def __init__(self, base_optimizer, alpha=0.5, k=6): if not 0.0 <= alpha <= 1.0: raise ValueError(f'Invalid slow update rate: {alpha}') if not 1 <= k: raise ValueError(f'Invalid lookahead steps: {k}') defaults = dict(lookahead_alpha=alpha, lookahead_k=k, lookahead_step=0) self.base_optimizer = base_optimizer self.param_groups = self.base_optimizer.param_groups self.defaults = base_optimizer.defaults self.defaults.update(defaults) self.state = defaultdict(dict) # manually add our defaults to the param groups for name, default in defaults.items(): for group in self.param_groups: group.setdefault(name, default) def update_slow(self, group): for fast_p in group["params"]: if fast_p.grad is None: continue param_state = self.state[fast_p] if 'slow_buffer' not in param_state: param_state['slow_buffer'] = torch.empty_like(fast_p.data) param_state['slow_buffer'].copy_(fast_p.data) slow = param_state['slow_buffer'] slow.add_(group['lookahead_alpha'], fast_p.data - slow) fast_p.data.copy_(slow) def sync_lookahead(self): for group in self.param_groups: self.update_slow(group) def step(self, closure=None): # print(self.k) #assert id(self.param_groups) == id(self.base_optimizer.param_groups) loss = self.base_optimizer.step(closure) for group in self.param_groups: group['lookahead_step'] += 1 if group['lookahead_step'] % group['lookahead_k'] == 0: self.update_slow(group) return loss def state_dict(self): fast_state_dict = self.base_optimizer.state_dict() slow_state = { (id(k) if isinstance(k, torch.Tensor) else k): v for k, v in self.state.items() } fast_state = fast_state_dict['state'] param_groups = fast_state_dict['param_groups'] return { 'state': fast_state, 'slow_state': slow_state, 'param_groups': param_groups, } def load_state_dict(self, state_dict): fast_state_dict = { 'state': state_dict['state'], 'param_groups': state_dict['param_groups'], } self.base_optimizer.load_state_dict(fast_state_dict) # We want to restore the slow state, but share param_groups reference # with base_optimizer. This is a bit redundant but least code slow_state_new = False if 'slow_state' not in state_dict: print('Loading state_dict from optimizer without Lookahead applied.') state_dict['slow_state'] = defaultdict(dict) slow_state_new = True slow_state_dict = { 'state': state_dict['slow_state'], 'param_groups': state_dict['param_groups'], # this is pointless but saves code } super(Lookahead, self).load_state_dict(slow_state_dict) self.param_groups = self.base_optimizer.param_groups # make both ref same container if slow_state_new: # reapply defaults to catch missing lookahead specific ones for name, default in self.defaults.items(): for group in self.param_groups: group.setdefault(name, default) def LookaheadAdam(params, alpha=0.5, k=6, *args, **kwargs): adam = Adam(params, *args, **kwargs) return Lookahead(adam, alpha, k) ================================================ FILE: utils/optimizers/novograd.py ================================================ # Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import torch from torch.optim import Optimizer import math class AdamW(Optimizer): """Implements AdamW algorithm. It has been proposed in `Adam: A Method for Stochastic Optimization`_. Arguments: params (iterable): iterable of parameters to optimize or dicts defining parameter groups lr (float, optional): learning rate (default: 1e-3) betas (Tuple[float, float], optional): coefficients used for computing running averages of gradient and its square (default: (0.9, 0.999)) eps (float, optional): term added to the denominator to improve numerical stability (default: 1e-8) weight_decay (float, optional): weight decay (L2 penalty) (default: 0) amsgrad (boolean, optional): whether to use the AMSGrad variant of this algorithm from the paper `On the Convergence of Adam and Beyond`_ Adam: A Method for Stochastic Optimization: https://arxiv.org/abs/1412.6980 On the Convergence of Adam and Beyond: https://openreview.net/forum?id=ryQu7f-RZ """ def __init__(self, params, lr=1e-3, betas=(0.9, 0.999), eps=1e-8, weight_decay=0, amsgrad=False): if not 0.0 <= lr: raise ValueError("Invalid learning rate: {}".format(lr)) if not 0.0 <= eps: raise ValueError("Invalid epsilon value: {}".format(eps)) if not 0.0 <= betas[0] < 1.0: raise ValueError("Invalid beta parameter at index 0: {}".format(betas[0])) if not 0.0 <= betas[1] < 1.0: raise ValueError("Invalid beta parameter at index 1: {}".format(betas[1])) defaults = dict(lr=lr, betas=betas, eps=eps, weight_decay=weight_decay, amsgrad=amsgrad) super(AdamW, self).__init__(params, defaults) def __setstate__(self, state): super(AdamW, self).__setstate__(state) for group in self.param_groups: group.setdefault('amsgrad', False) def step(self, closure=None): """Performs a single optimization step. Arguments: closure (callable, optional): A closure that reevaluates the model and returns the loss. """ loss = None if closure is not None: loss = closure() for group in self.param_groups: for p in group['params']: if p.grad is None: continue grad = p.grad.data if grad.is_sparse: raise RuntimeError('Adam does not support sparse gradients, please consider SparseAdam instead') amsgrad = group['amsgrad'] state = self.state[p] # State initialization if len(state) == 0: state['step'] = 0 # Exponential moving average of gradient values state['exp_avg'] = torch.zeros_like(p.data) # Exponential moving average of squared gradient values state['exp_avg_sq'] = torch.zeros_like(p.data) if amsgrad: # Maintains max of all exp. moving avg. of sq. grad. values state['max_exp_avg_sq'] = torch.zeros_like(p.data) exp_avg, exp_avg_sq = state['exp_avg'], state['exp_avg_sq'] if amsgrad: max_exp_avg_sq = state['max_exp_avg_sq'] beta1, beta2 = group['betas'] state['step'] += 1 # Decay the first and second moment running average coefficient exp_avg.mul_(beta1).add_(1 - beta1, grad) exp_avg_sq.mul_(beta2).addcmul_(1 - beta2, grad, grad) if amsgrad: # Maintains the maximum of all 2nd moment running avg. till now torch.max(max_exp_avg_sq, exp_avg_sq, out=max_exp_avg_sq) # Use the max. for normalizing running avg. of gradient denom = max_exp_avg_sq.sqrt().add_(group['eps']) else: denom = exp_avg_sq.sqrt().add_(group['eps']) bias_correction1 = 1 - beta1 ** state['step'] bias_correction2 = 1 - beta2 ** state['step'] step_size = group['lr'] * math.sqrt(bias_correction2) / bias_correction1 p.data.add_(-step_size, torch.mul(p.data, group['weight_decay']).addcdiv_(1, exp_avg, denom) ) return loss class Novograd(Optimizer): """ Implements Novograd algorithm. Args: params (iterable): iterable of parameters to optimize or dicts defining parameter groups lr (float, optional): learning rate (default: 1e-3) betas (Tuple[float, float], optional): coefficients used for computing running averages of gradient and its square (default: (0.95, 0)) eps (float, optional): term added to the denominator to improve numerical stability (default: 1e-8) weight_decay (float, optional): weight decay (L2 penalty) (default: 0) grad_averaging: gradient averaging amsgrad (boolean, optional): whether to use the AMSGrad variant of this algorithm from the paper `On the Convergence of Adam and Beyond`_ (default: False) """ def __init__(self, params, lr=1e-3, betas=(0.95, 0), eps=1e-8, weight_decay=0, grad_averaging=False, amsgrad=False): if not 0.0 <= lr: raise ValueError("Invalid learning rate: {}".format(lr)) if not 0.0 <= eps: raise ValueError("Invalid epsilon value: {}".format(eps)) if not 0.0 <= betas[0] < 1.0: raise ValueError("Invalid beta parameter at index 0: {}".format(betas[0])) if not 0.0 <= betas[1] < 1.0: raise ValueError("Invalid beta parameter at index 1: {}".format(betas[1])) defaults = dict(lr=lr, betas=betas, eps=eps, weight_decay=weight_decay, grad_averaging=grad_averaging, amsgrad=amsgrad) super(Novograd, self).__init__(params, defaults) def __setstate__(self, state): super(Novograd, self).__setstate__(state) for group in self.param_groups: group.setdefault('amsgrad', False) def step(self, closure=None): """Performs a single optimization step. Arguments: closure (callable, optional): A closure that reevaluates the model and returns the loss. """ loss = None if closure is not None: loss = closure() for group in self.param_groups: for p in group['params']: if p.grad is None: continue grad = p.grad.data if grad.is_sparse: raise RuntimeError('Sparse gradients are not supported.') amsgrad = group['amsgrad'] state = self.state[p] # State initialization if len(state) == 0: state['step'] = 0 # Exponential moving average of gradient values state['exp_avg'] = torch.zeros_like(p.data) # Exponential moving average of squared gradient values state['exp_avg_sq'] = torch.zeros([]).to(state['exp_avg'].device) if amsgrad: # Maintains max of all exp. moving avg. of sq. grad. values state['max_exp_avg_sq'] = torch.zeros([]).to(state['exp_avg'].device) exp_avg, exp_avg_sq = state['exp_avg'], state['exp_avg_sq'] if amsgrad: max_exp_avg_sq = state['max_exp_avg_sq'] beta1, beta2 = group['betas'] state['step'] += 1 norm = torch.sum(torch.pow(grad, 2)) if exp_avg_sq == 0: exp_avg_sq.copy_(norm) else: exp_avg_sq.mul_(beta2).add_(1 - beta2, norm) if amsgrad: # Maintains the maximum of all 2nd moment running avg. till now torch.max(max_exp_avg_sq, exp_avg_sq, out=max_exp_avg_sq) # Use the max. for normalizing running avg. of gradient denom = max_exp_avg_sq.sqrt().add_(group['eps']) else: denom = exp_avg_sq.sqrt().add_(group['eps']) grad.div_(denom) if group['weight_decay'] != 0: grad.add_(group['weight_decay'], p.data) if group['grad_averaging']: grad.mul_(1 - beta1) exp_avg.mul_(beta1).add_(grad) p.data.add_(-group['lr'], exp_avg) return loss ================================================ FILE: utils/optimizers/over9000.py ================================================ import torch, math from torch.optim.optimizer import Optimizer import itertools as it from .lookahead import * from .ralamb import * # RAdam + LARS + LookAHead # Lookahead implementation from https://github.com/lonePatient/lookahead_pytorch/blob/master/optimizer.py # RAdam + LARS implementation from https://gist.github.com/redknightlois/c4023d393eb8f92bb44b2ab582d7ec20 def Over9000(params, alpha=0.5, k=6, *args, **kwargs): ralamb = Ralamb(params, *args, **kwargs) return Lookahead(ralamb, alpha, k) RangerLars = Over9000 ================================================ FILE: utils/optimizers/radam.py ================================================ # from https://github.com/LiyuanLucasLiu/RAdam/blob/master/radam.py import math import torch from torch.optim.optimizer import Optimizer, required class RAdam(Optimizer): def __init__(self, params, lr=1e-3, betas=(0.9, 0.999), eps=1e-8, weight_decay=0): defaults = dict(lr=lr, betas=betas, eps=eps, weight_decay=weight_decay) self.buffer = [[None, None, None] for ind in range(10)] super(RAdam, self).__init__(params, defaults) def __setstate__(self, state): super(RAdam, self).__setstate__(state) def step(self, closure=None): loss = None if closure is not None: loss = closure() for group in self.param_groups: for p in group['params']: if p.grad is None: continue grad = p.grad.data.float() if grad.is_sparse: raise RuntimeError('RAdam does not support sparse gradients') p_data_fp32 = p.data.float() state = self.state[p] if len(state) == 0: state['step'] = 0 state['exp_avg'] = torch.zeros_like(p_data_fp32) state['exp_avg_sq'] = torch.zeros_like(p_data_fp32) else: state['exp_avg'] = state['exp_avg'].type_as(p_data_fp32) state['exp_avg_sq'] = state['exp_avg_sq'].type_as(p_data_fp32) exp_avg, exp_avg_sq = state['exp_avg'], state['exp_avg_sq'] beta1, beta2 = group['betas'] exp_avg_sq.mul_(beta2).addcmul_(1 - beta2, grad, grad) exp_avg.mul_(beta1).add_(1 - beta1, grad) state['step'] += 1 buffered = self.buffer[int(state['step'] % 10)] if state['step'] == buffered[0]: N_sma, step_size = buffered[1], buffered[2] else: buffered[0] = state['step'] beta2_t = beta2 ** state['step'] N_sma_max = 2 / (1 - beta2) - 1 N_sma = N_sma_max - 2 * state['step'] * beta2_t / (1 - beta2_t) buffered[1] = N_sma # more conservative since it's an approximated value if N_sma >= 5: step_size = math.sqrt((1 - beta2_t) * (N_sma - 4) / (N_sma_max - 4) * (N_sma - 2) / N_sma * N_sma_max / (N_sma_max - 2)) / (1 - beta1 ** state['step']) else: step_size = 1.0 / (1 - beta1 ** state['step']) buffered[2] = step_size if group['weight_decay'] != 0: p_data_fp32.add_(-group['weight_decay'] * group['lr'], p_data_fp32) # more conservative since it's an approximated value if N_sma >= 5: denom = exp_avg_sq.sqrt().add_(group['eps']) p_data_fp32.addcdiv_(-step_size * group['lr'], exp_avg, denom) else: p_data_fp32.add_(-step_size * group['lr'], exp_avg) p.data.copy_(p_data_fp32) return loss class PlainRAdam(Optimizer): def __init__(self, params, lr=1e-3, betas=(0.9, 0.999), eps=1e-8, weight_decay=0): defaults = dict(lr=lr, betas=betas, eps=eps, weight_decay=weight_decay) super(PlainRAdam, self).__init__(params, defaults) def __setstate__(self, state): super(PlainRAdam, self).__setstate__(state) def step(self, closure=None): loss = None if closure is not None: loss = closure() for group in self.param_groups: for p in group['params']: if p.grad is None: continue grad = p.grad.data.float() if grad.is_sparse: raise RuntimeError('RAdam does not support sparse gradients') p_data_fp32 = p.data.float() state = self.state[p] if len(state) == 0: state['step'] = 0 state['exp_avg'] = torch.zeros_like(p_data_fp32) state['exp_avg_sq'] = torch.zeros_like(p_data_fp32) else: state['exp_avg'] = state['exp_avg'].type_as(p_data_fp32) state['exp_avg_sq'] = state['exp_avg_sq'].type_as(p_data_fp32) exp_avg, exp_avg_sq = state['exp_avg'], state['exp_avg_sq'] beta1, beta2 = group['betas'] exp_avg_sq.mul_(beta2).addcmul_(1 - beta2, grad, grad) exp_avg.mul_(beta1).add_(1 - beta1, grad) state['step'] += 1 beta2_t = beta2 ** state['step'] N_sma_max = 2 / (1 - beta2) - 1 N_sma = N_sma_max - 2 * state['step'] * beta2_t / (1 - beta2_t) if group['weight_decay'] != 0: p_data_fp32.add_(-group['weight_decay'] * group['lr'], p_data_fp32) # more conservative since it's an approximated value if N_sma >= 5: step_size = group['lr'] * math.sqrt((1 - beta2_t) * (N_sma - 4) / (N_sma_max - 4) * (N_sma - 2) / N_sma * N_sma_max / (N_sma_max - 2)) / (1 - beta1 ** state['step']) denom = exp_avg_sq.sqrt().add_(group['eps']) p_data_fp32.addcdiv_(-step_size, exp_avg, denom) else: step_size = group['lr'] / (1 - beta1 ** state['step']) p_data_fp32.add_(-step_size, exp_avg) p.data.copy_(p_data_fp32) return loss class AdamW(Optimizer): def __init__(self, params, lr=1e-3, betas=(0.9, 0.999), eps=1e-8, weight_decay=0, warmup = 0): defaults = dict(lr=lr, betas=betas, eps=eps, weight_decay=weight_decay, warmup = warmup) super(AdamW, self).__init__(params, defaults) def __setstate__(self, state): super(AdamW, self).__setstate__(state) def step(self, closure=None): loss = None if closure is not None: loss = closure() for group in self.param_groups: for p in group['params']: if p.grad is None: continue grad = p.grad.data.float() if grad.is_sparse: raise RuntimeError('Adam does not support sparse gradients, please consider SparseAdam instead') p_data_fp32 = p.data.float() state = self.state[p] if len(state) == 0: state['step'] = 0 state['exp_avg'] = torch.zeros_like(p_data_fp32) state['exp_avg_sq'] = torch.zeros_like(p_data_fp32) else: state['exp_avg'] = state['exp_avg'].type_as(p_data_fp32) state['exp_avg_sq'] = state['exp_avg_sq'].type_as(p_data_fp32) exp_avg, exp_avg_sq = state['exp_avg'], state['exp_avg_sq'] beta1, beta2 = group['betas'] state['step'] += 1 exp_avg_sq.mul_(beta2).addcmul_(1 - beta2, grad, grad) exp_avg.mul_(beta1).add_(1 - beta1, grad) denom = exp_avg_sq.sqrt().add_(group['eps']) bias_correction1 = 1 - beta1 ** state['step'] bias_correction2 = 1 - beta2 ** state['step'] if group['warmup'] > state['step']: scheduled_lr = 1e-8 + state['step'] * group['lr'] / group['warmup'] else: scheduled_lr = group['lr'] step_size = group['lr'] * math.sqrt(bias_correction2) / bias_correction1 if group['weight_decay'] != 0: p_data_fp32.add_(-group['weight_decay'] * scheduled_lr, p_data_fp32) p_data_fp32.addcdiv_(-step_size, exp_avg, denom) p.data.copy_(p_data_fp32) return loss ================================================ FILE: utils/optimizers/ralamb.py ================================================ import torch, math from torch.optim.optimizer import Optimizer # RAdam + LARS class Ralamb(Optimizer): def __init__(self, params, lr=1e-3, betas=(0.9, 0.999), eps=1e-8, weight_decay=0): defaults = dict(lr=lr, betas=betas, eps=eps, weight_decay=weight_decay) self.buffer = [[None, None, None] for ind in range(10)] super(Ralamb, self).__init__(params, defaults) def __setstate__(self, state): super(Ralamb, self).__setstate__(state) def step(self, closure=None): loss = None if closure is not None: loss = closure() for group in self.param_groups: for p in group['params']: if p.grad is None: continue grad = p.grad.data.float() if grad.is_sparse: raise RuntimeError('Ralamb does not support sparse gradients') p_data_fp32 = p.data.float() state = self.state[p] if len(state) == 0: state['step'] = 0 state['exp_avg'] = torch.zeros_like(p_data_fp32) state['exp_avg_sq'] = torch.zeros_like(p_data_fp32) else: state['exp_avg'] = state['exp_avg'].type_as(p_data_fp32) state['exp_avg_sq'] = state['exp_avg_sq'].type_as(p_data_fp32) exp_avg, exp_avg_sq = state['exp_avg'], state['exp_avg_sq'] beta1, beta2 = group['betas'] # Decay the first and second moment running average coefficient # m_t exp_avg.mul_(beta1).add_(1 - beta1, grad) # v_t exp_avg_sq.mul_(beta2).addcmul_(1 - beta2, grad, grad) state['step'] += 1 buffered = self.buffer[int(state['step'] % 10)] if state['step'] == buffered[0]: N_sma, radam_step_size = buffered[1], buffered[2] else: buffered[0] = state['step'] beta2_t = beta2 ** state['step'] N_sma_max = 2 / (1 - beta2) - 1 N_sma = N_sma_max - 2 * state['step'] * beta2_t / (1 - beta2_t) buffered[1] = N_sma # more conservative since it's an approximated value if N_sma >= 5: radam_step_size = math.sqrt((1 - beta2_t) * (N_sma - 4) / (N_sma_max - 4) * (N_sma - 2) / N_sma * N_sma_max / (N_sma_max - 2)) / (1 - beta1 ** state['step']) else: radam_step_size = 1.0 / (1 - beta1 ** state['step']) buffered[2] = radam_step_size if group['weight_decay'] != 0: p_data_fp32.add_(-group['weight_decay'] * group['lr'], p_data_fp32) # more conservative since it's an approximated value radam_step = p_data_fp32.clone() if N_sma >= 5: denom = exp_avg_sq.sqrt().add_(group['eps']) radam_step.addcdiv_(-radam_step_size * group['lr'], exp_avg, denom) else: radam_step.add_(-radam_step_size * group['lr'], exp_avg) radam_norm = radam_step.pow(2).sum().sqrt() weight_norm = p.data.pow(2).sum().sqrt().clamp(0, 10) if weight_norm == 0 or radam_norm == 0: trust_ratio = 1 else: trust_ratio = weight_norm / radam_norm state['weight_norm'] = weight_norm state['adam_norm'] = radam_norm state['trust_ratio'] = trust_ratio if N_sma >= 5: p_data_fp32.addcdiv_(-radam_step_size * group['lr'] * trust_ratio, exp_avg, denom) else: p_data_fp32.add_(-radam_step_size * group['lr'] * trust_ratio, exp_avg) p.data.copy_(p_data_fp32) return loss ================================================ FILE: utils/optimizers/ranger.py ================================================ import math import torch from torch.optim.optimizer import Optimizer, required import itertools as it from .lookahead import * from .radam import * def Ranger(params, alpha=0.5, k=6, *args, **kwargs): radam = RAdam(params, *args, **kwargs) return Lookahead(radam, alpha, k) ================================================ FILE: utils/reader.py ================================================ from torch.utils.data import Dataset from PIL import Image class WeatherDataset(Dataset): # define dataset def __init__(self,label_list,transforms=None,mode="train"): super(WeatherDataset,self).__init__() self.label_list = label_list self.transforms = transforms self.mode = mode imgs = [] if self.mode == "test": for index,row in label_list.iterrows(): imgs.append((row["filename"])) self.imgs = imgs else: for index,row in label_list.iterrows(): imgs.append((row["filename"],row["label"])) self.imgs = imgs def __len__(self): return len(self.imgs) def __getitem__(self,index): if self.mode == "test": filename = self.imgs[index] img = Image.open(filename).convert('RGB') img = self.transforms(img) return img,filename else: filename,label = self.imgs[index] img = Image.open(filename).convert('RGB') img = self.transforms(img) return img,label