Skip to content

Commit c838c42

Browse files
committed
Add typing to reset_classifier() on other models
1 parent 3e03b2b commit c838c42

35 files changed

+58
-61
lines changed

timm/models/beit.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ def group_matcher(self, coarse=False):
395395
def get_classifier(self):
396396
return self.head
397397

398-
def reset_classifier(self, num_classes, global_pool=None):
398+
def reset_classifier(self, num_classes: int, global_pool: Optional[str] = None):
399399
self.num_classes = num_classes
400400
if global_pool is not None:
401401
self.global_pool = global_pool

timm/models/cait.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ def _matcher(name):
331331
def get_classifier(self):
332332
return self.head
333333

334-
def reset_classifier(self, num_classes, global_pool=None):
334+
def reset_classifier(self, num_classes: int, global_pool: Optional[str] = None):
335335
self.num_classes = num_classes
336336
if global_pool is not None:
337337
assert global_pool in ('', 'token', 'avg')

timm/models/coat.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
88
Modified from timm/models/vision_transformer.py
99
"""
10-
from functools import partial
11-
from typing import Tuple, List, Union
10+
from typing import List, Optional, Union, Tuple
1211

1312
import torch
1413
import torch.nn as nn
@@ -560,7 +559,7 @@ def group_matcher(self, coarse=False):
560559
def get_classifier(self):
561560
return self.head
562561

563-
def reset_classifier(self, num_classes, global_pool=None):
562+
def reset_classifier(self, num_classes: int, global_pool: Optional[str] = None):
564563
self.num_classes = num_classes
565564
if global_pool is not None:
566565
assert global_pool in ('token', 'avg')

timm/models/convit.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
'''These modules are adapted from those of timm, see
2222
https://github.com/rwightman/pytorch-image-models/blob/master/timm/models/vision_transformer.py
2323
'''
24-
25-
from functools import partial
24+
from typing import Optional
2625

2726
import torch
2827
import torch.nn as nn
@@ -349,7 +348,7 @@ def set_grad_checkpointing(self, enable=True):
349348
def get_classifier(self):
350349
return self.head
351350

352-
def reset_classifier(self, num_classes, global_pool=None):
351+
def reset_classifier(self, num_classes: int, global_pool: Optional[str] = None):
353352
self.num_classes = num_classes
354353
if global_pool is not None:
355354
assert global_pool in ('', 'token', 'avg')

timm/models/convmixer.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
""" ConvMixer
22
33
"""
4+
from typing import Optional
5+
46
import torch
57
import torch.nn as nn
68

@@ -75,7 +77,7 @@ def set_grad_checkpointing(self, enable=True):
7577
def get_classifier(self):
7678
return self.head
7779

78-
def reset_classifier(self, num_classes, global_pool=None):
80+
def reset_classifier(self, num_classes: int, global_pool: Optional[str] = None):
7981
self.num_classes = num_classes
8082
if global_pool is not None:
8183
self.pooling = SelectAdaptivePool2d(pool_type=global_pool, flatten=True)

timm/models/convnext.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
# LICENSE file in the root directory of this source tree (Attribution-NonCommercial 4.0 International (CC BY-NC 4.0))
3838
# No code was used directly from ConvNeXt-V2, however the weights are CC BY-NC 4.0 so beware if using commercially.
3939

40-
from collections import OrderedDict
4140
from functools import partial
4241
from typing import Callable, List, Optional, Tuple, Union
4342

timm/models/crossvit.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@
2525
2626
"""
2727
from functools import partial
28-
from typing import List
29-
from typing import Tuple
28+
from typing import List, Optional, Tuple
3029

3130
import torch
3231
import torch.hub
@@ -419,7 +418,7 @@ def set_grad_checkpointing(self, enable=True):
419418
def get_classifier(self):
420419
return self.head
421420

422-
def reset_classifier(self, num_classes, global_pool=None):
421+
def reset_classifier(self, num_classes: int, global_pool: Optional[str] = None):
423422
self.num_classes = num_classes
424423
if global_pool is not None:
425424
assert global_pool in ('token', 'avg')

timm/models/davit.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# All rights reserved.
1313
# This source code is licensed under the MIT license
1414
from functools import partial
15-
from typing import Tuple
15+
from typing import Optional, Tuple
1616

1717
import torch
1818
import torch.nn as nn
@@ -568,7 +568,7 @@ def set_grad_checkpointing(self, enable=True):
568568
def get_classifier(self):
569569
return self.head.fc
570570

571-
def reset_classifier(self, num_classes, global_pool=None):
571+
def reset_classifier(self, num_classes: int, global_pool: Optional[str] = None):
572572
self.head.reset(num_classes, global_pool)
573573

574574
def forward_features(self, x):

timm/models/deit.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# Copyright (c) 2015-present, Facebook, Inc.
1212
# All rights reserved.
1313
from functools import partial
14-
from typing import Sequence, Union
14+
from typing import Optional
1515

1616
import torch
1717
from torch import nn as nn
@@ -20,7 +20,6 @@
2020
from timm.layers import resample_abs_pos_embed
2121
from timm.models.vision_transformer import VisionTransformer, trunc_normal_, checkpoint_filter_fn
2222
from ._builder import build_model_with_cfg
23-
from ._manipulate import checkpoint_seq
2423
from ._registry import generate_default_cfgs, register_model, register_model_deprecations
2524

2625
__all__ = ['VisionTransformerDistilled'] # model_registry will add each entrypoint fn to this
@@ -64,7 +63,7 @@ def group_matcher(self, coarse=False):
6463
def get_classifier(self):
6564
return self.head, self.head_dist
6665

67-
def reset_classifier(self, num_classes, global_pool=None):
66+
def reset_classifier(self, num_classes: int, global_pool: Optional[str] = None):
6867
self.num_classes = num_classes
6968
self.head = nn.Linear(self.embed_dim, num_classes) if num_classes > 0 else nn.Identity()
7069
self.head_dist = nn.Linear(self.embed_dim, self.num_classes) if num_classes > 0 else nn.Identity()

timm/models/edgenext.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
Modifications and additions for timm by / Copyright 2022, Ross Wightman
99
"""
1010
import math
11-
from collections import OrderedDict
1211
from functools import partial
1312
from typing import Tuple
1413

@@ -17,7 +16,7 @@
1716
from torch import nn
1817

1918
from timm.data import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD
20-
from timm.layers import trunc_normal_tf_, DropPath, LayerNorm2d, Mlp, SelectAdaptivePool2d, create_conv2d, \
19+
from timm.layers import trunc_normal_tf_, DropPath, LayerNorm2d, Mlp, create_conv2d, \
2120
use_fused_attn, NormMlpClassifierHead, ClassifierHead
2221
from ._builder import build_model_with_cfg
2322
from ._features_fx import register_notrace_module

0 commit comments

Comments
 (0)