From b41cbe2a49e76fccf05852c4d365c554e2cb9d5f Mon Sep 17 00:00:00 2001 From: kleinicke Date: Fri, 2 Sep 2022 14:58:47 +0200 Subject: [PATCH] reduced numbers of pylint warnings (#304) --- glasses/models/AutoModel.py | 23 +++++++++++++---- glasses/models/AutoTransform.py | 10 ++++---- glasses/models/base/protocols.py | 2 +- glasses/nn/att/CBAM.py | 2 +- glasses/nn/blocks/__init__.py | 25 +++++++++---------- glasses/nn/pool/SpatialPyramidPool.py | 5 +++- glasses/utils/ModuleTransfer.py | 1 - glasses/utils/storage/Storage.py | 1 - .../utils/storage/hugging_face/HFModelHub.py | 2 +- .../storage/hugging_face/ModelCardProducer.py | 8 +++--- setup.py | 1 + 11 files changed, 47 insertions(+), 33 deletions(-) diff --git a/glasses/models/AutoModel.py b/glasses/models/AutoModel.py index 375ac33b..b61704a6 100755 --- a/glasses/models/AutoModel.py +++ b/glasses/models/AutoModel.py @@ -2,8 +2,23 @@ from glasses.utils.storage import HuggingFaceStorage from typing import List, Optional, OrderedDict from torch import nn -from .classification import * -from .segmentation import * + +from .classification import ( + ResNetXt, + WideResNet, + DenseNet, + SEResNet, + RegNet, + ResNeSt, + VGG, + MobileNet, + EfficientNet, + EfficientNetLite, + FishNet, + ViT, + DeiT, +) +from .segmentation import UNet from rich.table import Table from .classification.resnet import ( ResNet, @@ -273,9 +288,7 @@ def models_table(storage: Optional[Storage] = HuggingFaceStorage()) -> Table: table.add_column("Name", justify="left", no_wrap=True) table.add_column("Pretrained", justify="left", no_wrap=True) - [ + for k in AutoModel.zoo.keys(): table.add_row(k, "true" if k in storage.models else "false") - for k in AutoModel.zoo.keys() - ] return table diff --git a/glasses/models/AutoTransform.py b/glasses/models/AutoTransform.py index f7c1750d..88aacb8b 100755 --- a/glasses/models/AutoTransform.py +++ b/glasses/models/AutoTransform.py @@ -1,10 +1,11 @@ import torch import torchvision.transforms as T -from glasses.models import * from typing import Tuple, List, Callable from torchvision.transforms import InterpolationMode from functools import partial +# from glasses.models import * + IMAGENET_DEFAULT_MEAN = torch.Tensor([0.485, 0.456, 0.406]) IMAGENET_DEFAULT_STD = torch.Tensor([0.229, 0.224, 0.225]) @@ -22,8 +23,10 @@ def __init__( std: Tuple[float], mean: Tuple[float], interpolation: str = "bilinear", - transforms: List[Callable] = list(), + transforms: List[Callable] = None, ): + if transforms is None: + transforms = [] self.input_size: int = input_size base_transforms = [ T.Resize(resize, Transform.interpolations[interpolation]), @@ -56,9 +59,6 @@ class AutoTransform: "eca_resnet26t": ImageNetTransform( resize=320, input_size=320, interpolation="bicubic" ), - "eca_resnet26t": ImageNetTransform( - resize=320, input_size=320, interpolation="bicubic" - ), "eca_resnet50t": ImageNetTransform( resize=320, input_size=320, interpolation="bicubic" ), diff --git a/glasses/models/base/protocols.py b/glasses/models/base/protocols.py index 272e1596..e80249d5 100755 --- a/glasses/models/base/protocols.py +++ b/glasses/models/base/protocols.py @@ -37,7 +37,7 @@ def unfreeze(self, who: Optional[nn.Module] = None): class Interpretable: - """Protocol that allows the clas that subclass it to interpret an input using and instance of `Interpretability`""" + """Protocol that allows the class that subclass it to interpret an input using and instance of `Interpretability`""" def interpret(self, x: torch.Tensor, using: Interpretability, *args, **kwargs): return using(x, self, *args, **kwargs) diff --git a/glasses/nn/att/CBAM.py b/glasses/nn/att/CBAM.py index be7f3e28..35c8808a 100644 --- a/glasses/nn/att/CBAM.py +++ b/glasses/nn/att/CBAM.py @@ -1,7 +1,7 @@ import torch import torch.nn as nn from torch import Tensor -from einops.layers.torch import Rearrange, Reduce +from einops.layers.torch import Reduce class CBAMChannelAtt(nn.Module): diff --git a/glasses/nn/blocks/__init__.py b/glasses/nn/blocks/__init__.py index 6537f3c0..6f4649b0 100755 --- a/glasses/nn/blocks/__init__.py +++ b/glasses/nn/blocks/__init__.py @@ -1,25 +1,24 @@ -import torch.nn as nn import math -from functools import partial from typing import Callable, Tuple, Optional -from torch import Tensor +from functools import partial +from torch import nn, Tensor from torch.nn import functional as F from torchvision.ops import StochasticDepth -from glasses.nn import regularization - class Lambda(nn.Module): - def __init__(self, lambd: Callable[[Tensor], Tensor]): - """An utility Module, it allows custom function to be passed + """An utility Module, it allows custom function to be passed - Args: - lambd (Callable[Tensor]): A function that does something on a tensor + Args: + lambd (Callable[Tensor]): A function that does something on a tensor + + Examples: + >>> add_two = Lambda(lambd x: x + 2) + >>> add_two(Tensor([0])) // 2 + """ + + def __init__(self, lambd: Callable[[Tensor], Tensor]): - Examples: - >>> add_two = Lambda(lambd x: x + 2) - >>> add_two(Tensor([0])) // 2 - """ super().__init__() self.lambd = lambd diff --git a/glasses/nn/pool/SpatialPyramidPool.py b/glasses/nn/pool/SpatialPyramidPool.py index e2f641f1..f3c1beb1 100755 --- a/glasses/nn/pool/SpatialPyramidPool.py +++ b/glasses/nn/pool/SpatialPyramidPool.py @@ -24,10 +24,13 @@ class SpatialPyramidPool(nn.Module): """ def __init__( - self, num_pools: List[int] = [1, 4, 16], pool: nn.Module = nn.AdaptiveMaxPool2d + self, num_pools: List[int] = None, pool: nn.Module = nn.AdaptiveMaxPool2d ): super().__init__() + if num_pools is None: + num_pools = [1, 4, 16] + self.pools = nn.ModuleList([]) for p in num_pools: # the output of 2d pool is B X C X P X P diff --git a/glasses/utils/ModuleTransfer.py b/glasses/utils/ModuleTransfer.py index a4088aea..e4eae162 100755 --- a/glasses/utils/ModuleTransfer.py +++ b/glasses/utils/ModuleTransfer.py @@ -2,7 +2,6 @@ from torch import Tensor from dataclasses import dataclass, field from .Tracker import Tracker -from pprint import pprint from typing import List diff --git a/glasses/utils/storage/Storage.py b/glasses/utils/storage/Storage.py index 16f07090..32e71cad 100644 --- a/glasses/utils/storage/Storage.py +++ b/glasses/utils/storage/Storage.py @@ -1,4 +1,3 @@ -from re import L from typing import Any, List from abc import ABC, abstractmethod diff --git a/glasses/utils/storage/hugging_face/HFModelHub.py b/glasses/utils/storage/hugging_face/HFModelHub.py index 8d6e27a8..a3126161 100644 --- a/glasses/utils/storage/hugging_face/HFModelHub.py +++ b/glasses/utils/storage/hugging_face/HFModelHub.py @@ -1,9 +1,9 @@ import json import logging import os +from typing import Dict, Optional import torch import requests -from typing import Dict, Optional from torch import nn from torch import Tensor diff --git a/glasses/utils/storage/hugging_face/ModelCardProducer.py b/glasses/utils/storage/hugging_face/ModelCardProducer.py index 1c9d1bb1..27edc684 100644 --- a/glasses/utils/storage/hugging_face/ModelCardProducer.py +++ b/glasses/utils/storage/hugging_face/ModelCardProducer.py @@ -1,10 +1,10 @@ import os -import time -from glasses.models import AutoModel -from huggingface_hub import Repository, HfFolder -from .HuggingFaceStorage import HuggingFaceStorage from pathlib import Path from torch import nn +from huggingface_hub import Repository +from glasses.models import AutoModel +from .HuggingFaceStorage import HuggingFaceStorage + # --- # tags: diff --git a/setup.py b/setup.py index b7348d36..acd6f4ca 100755 --- a/setup.py +++ b/setup.py @@ -28,6 +28,7 @@ "matplotlib", "einops", "rich", + "huggingface-hub", ], python_requires=">=3.8", extras_require={