-
Notifications
You must be signed in to change notification settings - Fork 6
/
pretrained.py
1513 lines (1254 loc) · 47.6 KB
/
pretrained.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
This module contains entrypoints to loading pretrained models.
Notes
-----
Doing it this way allows:
#. a better overview of the available weights
#. less coupling of model definition code and our config system
#. easy integration with torch.hub
There is obviously a tradeoff here, as this requires slightly more code
but given the advantages I think it's worth it.
"""
from typing import Any, Callable, Dict, List
import torch
from bcos.experiments.utils import Experiment
__all__ = ["list_available"] # entrypoints filled by `register` decorator
_entrypoint_registry = {}
def register(
entrypoint_fn: Callable[..., torch.nn.Module]
) -> Callable[..., torch.nn.Module]:
"""Decorator to register a function as an entrypoint."""
_entrypoint_registry[entrypoint_fn.__name__] = entrypoint_fn
__all__.append(entrypoint_fn.__name__)
return entrypoint_fn
def list_available() -> List[str]:
"""List all available pretrained models."""
return [k for k in _entrypoint_registry.keys()]
BASE = "https://github.com/B-cos/B-cos-v2/releases/download/v0.0.1-weights"
# map (base -> (model_name -> url))
URLS: Dict[str, Dict[str, str]] = {
"bcos_final": {
# resnets and resnext
"resnet_18": f"{BASE}/resnet_18-68b4160fff.pth",
"resnet_34": f"{BASE}/resnet_34-a63425a03e.pth",
"resnet_50": f"{BASE}/resnet_50-ead259efe4.pth",
"resnet_101": f"{BASE}/resnet_101-84c3658278.pth",
"resnet_152": f"{BASE}/resnet_152-42051a77c1.pth",
"resnext_50_32x4d": f"{BASE}/resnext_50_32x4d-57af241ab9.pth",
# densenets
"densenet_121": f"{BASE}/densenet_121-b8daf96afb.pth",
"densenet_161": f"{BASE}/densenet_161-9e9ea51353.pth",
"densenet_169": f"{BASE}/densenet_169-7037ee0604.pth",
"densenet_201": f"{BASE}/densenet_201-00ac87066f.pth",
# other
"vgg_11_bnu": f"{BASE}/vgg_11_bnu-34036029f0.pth",
},
"bcos_final_long": {
"convnext_tiny_pn": f"{BASE}/convnext_tiny_pn-539b1bfb37.pth",
"convnext_base_pn": f"{BASE}/convnext_base_pn-b0495852c6.pth",
"convnext_tiny_bnu": f"{BASE}/convnext_tiny_bnu-dbd7f5ef9d.pth",
"convnext_base_bnu": f"{BASE}/convnext_base_bnu-7c32a704b3.pth",
"densenet_121": f"{BASE}/densenet_121_long-5175461597.pth",
"resnet_50": f"{BASE}/resnet_50_long-ef38a88533.pth",
"resnet_152": f"{BASE}/resnet_152_long-0b4b434939.pth",
},
"vit_final": {
"bcos_simple_vit_ti_patch16_224": f"{BASE}/bcos_simple_vit_ti_patch16_224-4b0824b1c1.pth",
"bcos_simple_vit_s_patch16_224": f"{BASE}/bcos_simple_vit_s_patch16_224-75e99d1f73.pth",
"bcos_simple_vit_b_patch16_224": f"{BASE}/bcos_simple_vit_b_patch16_224-1fc4750806.pth",
"bcos_simple_vit_l_patch16_224": f"{BASE}/bcos_simple_vit_l_patch16_224-9613b2ad0a.pth",
"bcos_vitc_ti_patch1_14": f"{BASE}/bcos_vitc_ti_patch1_14-ddd6193a77.pth",
"bcos_vitc_s_patch1_14": f"{BASE}/bcos_vitc_s_patch1_14-cf55c88f0c.pth",
"bcos_vitc_b_patch1_14": f"{BASE}/bcos_vitc_b_patch1_14-a13c46397b.pth",
"bcos_vitc_l_patch1_14": f"{BASE}/bcos_vitc_l_patch1_14-8739e18b8d.pth",
# standard! ie non-B-cos
"simple_vit_ti_patch16_224": f"{BASE}/standard_simple_vit_ti_patch16_224-2ae8c65a39.pth",
"simple_vit_s_patch16_224": f"{BASE}/standard_simple_vit_s_patch16_224-f2934fcdcf.pth",
"simple_vit_b_patch16_224": f"{BASE}/standard_simple_vit_b_patch16_224-87074200ed.pth",
"simple_vit_l_patch16_224": f"{BASE}/standard_simple_vit_l_patch16_224-62dc536e03.pth",
"vitc_ti_patch1_14": f"{BASE}/standard_vitc_ti_patch1_14-a5d6bded37.pth",
"vitc_s_patch1_14": f"{BASE}/standard_vitc_s_patch1_14-34ecd7288e.pth",
"vitc_b_patch1_14": f"{BASE}/standard_vitc_b_patch1_14-4d374b0220.pth",
"vitc_l_patch1_14": f"{BASE}/standard_vitc_l_patch1_14-560e48f246.pth",
},
}
def _get_model(
experiment_name: str,
pretrained: bool,
progress: bool,
base_network: str = "bcos_final",
dataset: str = "ImageNet",
**model_kwargs: Any,
) -> torch.nn.Module:
"""
Helper that loads the model and attaches its config and
transform to it as `config` and `transform` respectively.
"""
# load empty model
exp = Experiment(dataset, base_network, experiment_name)
bcos_args = {}
if "bcos_args" in model_kwargs:
bcos_args = dict(bcos_args=model_kwargs.pop("bcos_args"))
model = exp.get_model(args=model_kwargs, **bcos_args)
# attach stuff
assert not hasattr(model, "config")
assert not hasattr(model, "transform")
model.config = exp.config
model.transform = model.config["data"]["test_transform"]
# load weights if needed
if pretrained:
url = URLS[base_network][experiment_name]
state_dict = torch.hub.load_state_dict_from_url(
url,
progress=progress,
check_hash=True,
)
model.load_state_dict(state_dict)
return model
# ------------------------------- [model entrypoints] -------------------------------------
@register
def resnet18(
pretrained: bool = False, progress: bool = True, **kwargs
) -> "torch.nn.Module":
r"""B-cos ResNet-18.
B-cos version of a ResNet-18 model.
+---------+---------------+
| Name | Value |
+=========+===============+
| Acc@1 | 68.736% |
+---------+---------------+
| Acc@5 | 87.430% |
+---------+---------------+
| #Params | 11.69M |
+---------+---------------+
References
----------
`B-cos Networks: Alignment is All We Need for Interpretability <https://arxiv.org/abs/2205.10268>`_
`Deep Residual Learning for Image Recognition <https://arxiv.org/pdf/1512.03385.pdf>`_
Parameters
----------
pretrained : bool
If True, returns a model pre-trained on ImageNet
progress : bool
If True, displays a progress bar of the download to stderr
**kwargs : Any, optional
Additional arguments passed to the model constructor
Please see source code for details.
"""
return _get_model("resnet_18", pretrained, progress, **kwargs)
@register
def resnet34(pretrained: bool = False, progress: bool = True, **kwargs):
"""B-cos ResNet-34
B-cos version of a ResNet-34 model.
+---------+---------------+
| Name | Value |
+=========+===============+
| Acc@1 | 72.284% |
+---------+---------------+
| Acc@5 | 90.052% |
+---------+---------------+
| #Params | 21.80M |
+---------+---------------+
References
----------
`B-cos Networks: Alignment is All We Need for Interpretability <https://arxiv.org/abs/2205.10268>`_
`Deep Residual Learning for Image Recognition <https://arxiv.org/pdf/1512.03385.pdf>`_
Parameters
----------
pretrained : bool
If True, returns a model pre-trained on ImageNet
progress : bool
If True, displays a progress bar of the download to stderr
**kwargs : Any, optional
Additional arguments passed to the model constructor
Please see source code for details.
"""
return _get_model("resnet_34", pretrained, progress, **kwargs)
@register
def resnet50(pretrained: bool = False, progress: bool = True, **kwargs):
"""B-cos ResNet-50
B-cos version of a ResNet-50 model.
+---------+---------------+
| Name | Value |
+=========+===============+
| Acc@1 | 75.882% |
+---------+---------------+
| Acc@5 | 92.064% |
+---------+---------------+
| #Params | 25.54M |
+---------+---------------+
References
----------
`B-cos Networks: Alignment is All We Need for Interpretability <https://arxiv.org/abs/2205.10268>`_
`Deep Residual Learning for Image Recognition <https://arxiv.org/pdf/1512.03385.pdf>`_
Parameters
----------
pretrained : bool
If True, returns a model pre-trained on ImageNet
progress : bool
If True, displays a progress bar of the download to stderr
**kwargs : Any, optional
Additional arguments passed to the model constructor
Please see source code for details.
"""
return _get_model("resnet_50", pretrained, progress, **kwargs)
@register
def resnet101(pretrained: bool = False, progress: bool = True, **kwargs):
"""B-cos ResNet-101
B-cos version of a ResNet-101 model.
+---------+---------------+
| Name | Value |
+=========+===============+
| Acc@1 | 76.532% |
+---------+---------------+
| Acc@5 | 92.538% |
+---------+---------------+
| #Params | 44.50M |
+---------+---------------+
References
----------
.. [1] `B-cos Networks: Alignment is All We Need for Interpretability <https://arxiv.org/abs/2205.10268>`_
.. [2] `Deep Residual Learning for Image Recognition <https://arxiv.org/pdf/1512.03385.pdf>`_
Parameters
----------
pretrained : bool
If True, returns a model pre-trained on ImageNet
progress : bool
If True, displays a progress bar of the download to stderr
**kwargs : Any, optional
Additional arguments passed to the model constructor
Please see source code for details.
"""
return _get_model("resnet_101", pretrained, progress, **kwargs)
@register
def resnet152(pretrained: bool = False, progress: bool = True, **kwargs):
"""B-cos ResNet-152
B-cos version of a ResNet-152 model.
+---------+---------------+
| Name | Value |
+=========+===============+
| Acc@1 | 76.484% |
+---------+---------------+
| Acc@5 | 92.398% |
+---------+---------------+
| #Params | 60.13M |
+---------+---------------+
References
----------
`B-cos Networks: Alignment is All We Need for Interpretability <https://arxiv.org/abs/2205.10268>`_
`Deep Residual Learning for Image Recognition <https://arxiv.org/pdf/1512.03385.pdf>`_
Parameters
----------
pretrained : bool
If True, returns a model pre-trained on ImageNet
progress : bool
If True, displays a progress bar of the download to stderr
**kwargs : Any, optional
Additional arguments passed to the model constructor
Please see source code for details.
"""
return _get_model("resnet_152", pretrained, progress, **kwargs)
@register
def resnext50_32x4d(pretrained: bool = False, progress: bool = True, **kwargs):
"""B-cos ResNeXt-50 32x4d
B-cos version of a ResNeXt-50 32x4d model.
+---------+---------------------+
| Name | Value |
+=========+=====================+
| Acc@1 | 75.820% |
+---------+---------------------+
| Acc@5 | 91.810% |
+---------+---------------------+
| #Params | 25.00M |
+---------+---------------------+
References
----------
`B-cos Networks: Alignment is All We Need for Interpretability <https://arxiv.org/abs/2205.10268>`_
`Aggregated Residual Transformations for Deep Neural Networks <https://arxiv.org/pdf/1611.05431.pdf>`_
Parameters
----------
pretrained : bool
If True, returns a model pre-trained on ImageNet
progress : bool
If True, displays a progress bar of the download to stderr
**kwargs : Any, optional
Additional arguments passed to the model constructor
Please see source code for details.
"""
return _get_model("resnext_50_32x4d", pretrained, progress, **kwargs)
@register
def densenet121(pretrained: bool = False, progress: bool = True, **kwargs):
"""B-cos DenseNet-121
B-cos version of a DenseNet-121 model.
+---------+---------------+
| Name | Value |
+=========+===============+
| Acc@1 | 73.612% |
+---------+---------------+
| Acc@5 | 91.106% |
+---------+---------------+
| #Params | 7.95M |
+---------+---------------+
References
----------
`B-cos Networks: Alignment is All We Need for Interpretability <https://arxiv.org/abs/2205.10268>`_
`Densely Connected Convolutional Networks <https://arxiv.org/pdf/1608.06993.pdf>`_
Parameters
----------
pretrained : bool
If True, returns a model pre-trained on ImageNet
progress : bool
If True, displays a progress bar of the download to stderr
**kwargs : Any, optional
Additional arguments passed to the model constructor
Please see source code for details.
"""
return _get_model("densenet_121", pretrained, progress, **kwargs)
@register
def densenet161(pretrained: bool = False, progress: bool = True, **kwargs):
"""B-cos DenseNet-161
B-cos version of a DenseNet-161 model.
+---------+---------------+
| Name | Value |
+=========+===============+
| Acc@1 | 76.622% |
+---------+---------------+
| Acc@5 | 92.554% |
+---------+---------------+
| #Params | 28.58M |
+---------+---------------+
References
----------
`B-cos Networks: Alignment is All We Need for Interpretability <https://arxiv.org/abs/2205.10268>`_
`Densely Connected Convolutional Networks <https://arxiv.org/pdf/1608.06993.pdf>`_
Parameters
----------
pretrained : bool
If True, returns a model pre-trained on ImageNet
progress : bool
If True, displays a progress bar of the download to stderr
**kwargs : Any, optional
Additional arguments passed to the model constructor
Please see source code for details.
"""
return _get_model("densenet_161", pretrained, progress, **kwargs)
@register
def densenet169(pretrained: bool = False, progress: bool = True, **kwargs):
"""B-cos DenseNet-169
B-cos version of a DenseNet-169 model.
+---------+---------------+
| Name | Value |
+=========+===============+
| Acc@1 | 75.186% |
+---------+---------------+
| Acc@5 | 91.786% |
+---------+---------------+
| #Params | 14.08M |
+---------+---------------+
References
----------
`B-cos Networks: Alignment is All We Need for Interpretability <https://arxiv.org/abs/2205.10268>`_
`Densely Connected Convolutional Networks <https://arxiv.org/pdf/1608.06993.pdf>`_
Parameters
----------
pretrained : bool
If True, returns a model pre-trained on ImageNet
progress : bool
If True, displays a progress bar of the download to stderr
**kwargs : Any, optional
Additional arguments passed to the model constructor
Please see source code for details.
"""
return _get_model("densenet_169", pretrained, progress, **kwargs)
@register
def densenet201(pretrained: bool = False, progress: bool = True, **kwargs):
"""B-cos DenseNet-201
B-cos version of a DenseNet-201 model.
+---------+-----------------+
| Name | Value |
+=========+=================+
| Acc@1 | 75.480% |
+---------+-----------------+
| Acc@5 | 91.992% |
+---------+-----------------+
| #Params | 19.91M |
+---------+-----------------+
References
----------
`B-cos Networks: Alignment is All We Need for Interpretability <https://arxiv.org/abs/2205.10268>`_
`Densely Connected Convolutional Networks <https://arxiv.org/pdf/1608.06993.pdf>`_
Parameters
----------
pretrained : bool
If True, returns a model pre-trained on ImageNet
progress : bool
If True, displays a progress bar of the download to stderr
**kwargs : Any, optional
Additional arguments passed to the model constructor
Please see source code for details.
"""
return _get_model("densenet_201", pretrained, progress, **kwargs)
@register
def vgg11_bnu(pretrained: bool = False, progress: bool = True, **kwargs):
"""B-cos VGG-11 BNU
B-cos version of a VGG-11 model with Batch Normalization without centering.
+---------+---------------+
| Name | Value |
+=========+===============+
| Acc@1 | 69.310% |
+---------+---------------+
| Acc@5 | 88.388% |
+---------+---------------+
| #Params | 132.86M |
+---------+---------------+
References
----------
`B-cos Networks: Alignment is All We Need for Interpretability <https://arxiv.org/abs/2205.10268>`_
`Very Deep Convolutional Networks for Large-Scale Image Recognition <https://arxiv.org/pdf/1409.1556.pdf>`_
Parameters
----------
pretrained : bool
If True, returns a model pre-trained on ImageNet
progress : bool
If True, displays a progress bar of the download to stderr
**kwargs : Any, optional
Additional arguments passed to the model constructor
Please see source code for details.
"""
return _get_model("vgg_11_bnu", pretrained, progress, **kwargs)
# ----------------------------------------------------------------------------------------------------------------------
# Models trained much longer (600 epochs) with better accuracies.
# ----------------------------------------------------------------------------------------------------------------------
@register
def convnext_tiny(pretrained: bool = False, progress: bool = True, **kwargs):
"""B-cos ConvNeXt-T
B-cos version of a ConvNeXt-Tiny model with HW-normalization.
The weights are from epoch 596's (starting from 0) EMA checkpoint weights.
The model was trained with AMP.
+---------+------------------+
| Name | Value |
+=========+==================+
| Acc@1 | 77.488% |
+---------+------------------+
| Acc@5 | 93.192% |
+---------+------------------+
| #Params | 28.54M |
+---------+------------------+
References
----------
`B-cos Networks: Alignment is All We Need for Interpretability <https://arxiv.org/abs/2205.10268>`_
`A ConvNet for the 2020s <https://arxiv.org/abs/2201.03545>`_
`TorchVision's new recipe
<https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives>`_
Parameters
----------
pretrained : bool
If True, returns a model pre-trained on ImageNet
progress : bool
If True, displays a progress bar of the download to stderr
**kwargs : Any, optional
Additional arguments passed to the model constructor
Please see source code for details.
"""
return _get_model(
"convnext_tiny_pn",
pretrained,
progress,
base_network="bcos_final_long",
**kwargs,
)
@register
def convnext_base(pretrained: bool = False, progress: bool = True, **kwargs):
"""B-cos ConvNeXt-B
B-cos version of a ConvNeXt-Base model with HW-normalization.
The weights are from epoch 581's (starting from 0) non-EMA checkpoint weights.
The model was trained with AMP.
+---------+------------------+
| Name | Value |
+=========+==================+
| Acc@1 | 79.650% |
+---------+------------------+
| Acc@5 | 94.614% |
+---------+------------------+
| #Params | 88.47M |
+---------+------------------+
References
----------
`B-cos Networks: Alignment is All We Need for Interpretability <https://arxiv.org/abs/2205.10268>`_
`A ConvNet for the 2020s <https://arxiv.org/abs/2201.03545>`_
`TorchVision's new recipe
<https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives>`_
Parameters
----------
pretrained : bool
If True, returns a model pre-trained on ImageNet
progress : bool
If True, displays a progress bar of the download to stderr
**kwargs : Any, optional
Additional arguments passed to the model constructor
Please see source code for details.
"""
return _get_model(
"convnext_base_pn",
pretrained,
progress,
base_network="bcos_final_long",
**kwargs,
)
@register
def convnext_tiny_bnu(pretrained: bool = False, progress: bool = True, **kwargs):
"""B-cos ConvNeXt-T with BNU
B-cos version of a ConvNeXt-Tiny model with Batch Normalization without centering.
The weights are from epoch 456's (starting from 0) EMA checkpoint weights.
The model was trained with AMP.
+---------+------------------+
| Name | Value |
+=========+==================+
| Acc@1 | 76.826% |
+---------+------------------+
| Acc@5 | 93.090% |
+---------+------------------+
| #Params | 28.54M |
+---------+------------------+
References
----------
`B-cos Networks: Alignment is All We Need for Interpretability <https://arxiv.org/abs/2205.10268>`_
`A ConvNet for the 2020s <https://arxiv.org/abs/2201.03545>`_
`TorchVision's new recipe
<https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives>`_
Parameters
----------
pretrained : bool
If True, returns a model pre-trained on ImageNet
progress : bool
If True, displays a progress bar of the download to stderr
**kwargs : Any, optional
Additional arguments passed to the model constructor
Please see source code for details.
"""
return _get_model(
"convnext_tiny_bnu",
pretrained,
progress,
base_network="bcos_final_long",
**kwargs,
)
@register
def convnext_base_bnu(pretrained: bool = False, progress: bool = True, **kwargs):
"""B-cos ConvNeXt-B with BNU
B-cos version of a ConvNeXt-Base model with Batch Normalization without centering.
The weights are from epoch 541's (starting from 0) EMA checkpoint weights.
The model was trained with AMP.
+---------+------------------+
| Name | Value |
+=========+==================+
| Acc@1 | 80.142% |
+---------+------------------+
| Acc@5 | 94.834% |
+---------+------------------+
| #Params | 88.47M |
+---------+------------------+
References
----------
`B-cos Networks: Alignment is All We Need for Interpretability <https://arxiv.org/abs/2205.10268>`_
`A ConvNet for the 2020s <https://arxiv.org/abs/2201.03545>`_
`TorchVision's new recipe
<https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives>`_
Parameters
----------
pretrained : bool
If True, returns a model pre-trained on ImageNet
progress : bool
If True, displays a progress bar of the download to stderr
**kwargs : Any, optional
Additional arguments passed to the model constructor
Please see source code for details.
"""
return _get_model(
"convnext_base_bnu",
pretrained,
progress,
base_network="bcos_final_long",
**kwargs,
)
@register
def densenet121_long(pretrained: bool = False, progress: bool = True, **kwargs):
"""B-cos DenseNet-121 just trained longer.
The model architecture is the same as the one from `densenet121`.
The weights are from epoch 596's (starting from 0) non-EMA checkpoint weights.
The model was trained with AMP.
+---------+---------------------+
| Name | Value |
+=========+=====================+
| Acc@1 | 77.302% |
+---------+---------------------+
| Acc@5 | 93.234% |
+---------+---------------------+
| #Params | 7.95M |
+---------+---------------------+
References
----------
`B-cos Networks: Alignment is All We Need for Interpretability <https://arxiv.org/abs/2205.10268>`_
`Densely Connected Convolutional Networks <https://arxiv.org/abs/1608.06993>`_
`TorchVision's new recipe
<https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives>`_
Parameters
----------
pretrained : bool
If True, returns a model pre-trained on ImageNet
progress : bool
If True, displays a progress bar of the download to stderr
**kwargs : Any, optional
Additional arguments passed to the model constructor
Please see source code for details.
"""
return _get_model(
"densenet_121", pretrained, progress, base_network="bcos_final_long", **kwargs
)
@register
def resnet50_long(pretrained: bool = False, progress: bool = True, **kwargs):
"""B-cos ResNet-50 just trained longer.
The model architecture is the same as the one from `resnet50`.
The weights are from epoch 580's (starting from 0) non-EMA checkpoint weights.
The model was trained with AMP.
+---------+------------------+
| Name | Value |
+=========+==================+
| Acc@1 | 79.468% |
+---------+------------------+
| Acc@5 | 94.452% |
+---------+------------------+
| #Params | 25.54M |
+---------+------------------+
References
----------
`B-cos Networks: Alignment is All We Need for Interpretability <https://arxiv.org/abs/2205.10268>`_
`Deep Residual Learning for Image Recognition <https://arxiv.org/abs/1512.03385>`_
`TorchVision's new recipe
<https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives>`_
Parameters
----------
pretrained : bool
If True, returns a model pre-trained on ImageNet
progress : bool
If True, displays a progress bar of the download to stderr
**kwargs : Any, optional
Additional arguments passed to the model constructor
Please see source code for details.
"""
return _get_model(
"resnet_50", pretrained, progress, base_network="bcos_final_long", **kwargs
)
@register
def resnet152_long(pretrained: bool = False, progress: bool = True, **kwargs):
"""B-cos ResNet-152 just trained longer.
The model architecture is the same as the one from `resnet152`.
The weights are from epoch 433's (starting from 0) non-EMA checkpoint weights.
The model was trained with AMP.
+---------+-------------------+
| Name | Value |
+=========+===================+
| Acc@1 | 80.144% |
+---------+-------------------+
| Acc@5 | 94.116% |
+---------+-------------------+
| #Params | 60.13M |
+---------+-------------------+
References
----------
`B-cos Networks: Alignment is All We Need for Interpretability <https://arxiv.org/abs/2205.10268>`_
`Deep Residual Learning for Image Recognition <https://arxiv.org/abs/1512.03385>`_
`TorchVision's new recipe
<https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives>`_
Parameters
----------
pretrained : bool
If True, returns a model pre-trained on ImageNet
progress : bool
If True, displays a progress bar of the download to stderr
**kwargs : Any, optional
Additional arguments passed to the model constructor
Please see source code for details.
"""
return _get_model(
"resnet_152", pretrained, progress, base_network="bcos_final_long", **kwargs
)
# ----------------------------------------------------------------------------------------------------------------------
# ViT models (both B-cos and non-B-cos)
# non-B-cos i.e. standard models are prefixed with "standard_"
# ----------------------------------------------------------------------------------------------------------------------
def _requires_einops():
"""Checks if einops is installed."""
try:
import einops # noqa: F401
except ImportError:
raise RuntimeError(
"This model requires einops to be installed. "
"To fix this, run `pip install einops`."
)
def _get_vit_model(*args, **kwargs):
"""Gets a ViT model, which requires einops. Hence, it checks for it."""
_requires_einops()
return _get_model(*args, **kwargs)
@register
def simple_vit_ti_patch16_224(
pretrained: bool = False, progress: bool = True, **kwargs
):
"""B-cos Simple ViT-Ti with 16x16 patch size and 224x224 image size.
+---------+-------------------+
| Name | Value |
+=========+===================+
| Acc@1 | 59.960% |
+---------+-------------------+
| Acc@5 | 81.838% |
+---------+-------------------+
| #Params | 5.80M |
+---------+-------------------+
References
----------
`B-cos Networks: Alignment is All We Need for Interpretability <https://arxiv.org/abs/2205.10268>`_
`Better plain ViT baselines for ImageNet-1k <https://arxiv.org/abs/2205.01580>`_
Parameters
----------
pretrained : bool
If True, returns a model pre-trained on ImageNet
progress : bool
If True, displays a progress bar of the download to stderr
**kwargs : Any, optional
Additional arguments passed to the model constructor
Please see source code for details.
"""
return _get_vit_model(
"bcos_simple_vit_ti_patch16_224",
pretrained,
progress,
base_network="vit_final",
**kwargs,
)
@register
def simple_vit_s_patch16_224(pretrained: bool = False, progress: bool = True, **kwargs):
"""B-cos Simple ViT-S with 16x16 patch size and 224x224 image size.
+---------+-------------------+
| Name | Value |
+=========+===================+
| Acc@1 | 69.246% |
+---------+-------------------+
| Acc@5 | 88.096% |
+---------+-------------------+
| #Params | 22.28M |
+---------+-------------------+
References
----------
`B-cos Networks: Alignment is All We Need for Interpretability <https://arxiv.org/abs/2205.10268>`_
`Better plain ViT baselines for ImageNet-1k <https://arxiv.org/abs/2205.01580>`_
Parameters
----------
pretrained : bool
If True, returns a model pre-trained on ImageNet
progress : bool
If True, displays a progress bar of the download to stderr
**kwargs : Any, optional
Additional arguments passed to the model constructor
Please see source code for details.
"""
return _get_vit_model(
"bcos_simple_vit_s_patch16_224",
pretrained,
progress,
base_network="vit_final",
**kwargs,
)
@register
def simple_vit_b_patch16_224(pretrained: bool = False, progress: bool = True, **kwargs):
"""B-cos Simple ViT-B with 16x16 patch size and 224x224 image size.
+---------+-------------------+
| Name | Value |
+=========+===================+
| Acc@1 | 74.408% |
+---------+-------------------+
| Acc@5 | 91.156% |
+---------+-------------------+
| #Params | 86.90M |
+---------+-------------------+
References
----------
`B-cos Networks: Alignment is All We Need for Interpretability <https://arxiv.org/abs/2205.10268>`_
`Better plain ViT baselines for ImageNet-1k <https://arxiv.org/abs/2205.01580>`_
Parameters
----------
pretrained : bool
If True, returns a model pre-trained on ImageNet
progress : bool
If True, displays a progress bar of the download to stderr
**kwargs : Any, optional
Additional arguments passed to the model constructor
Please see source code for details.
"""
return _get_vit_model(
"bcos_simple_vit_b_patch16_224",
pretrained,
progress,
base_network="vit_final",
**kwargs,
)
@register
def simple_vit_l_patch16_224(pretrained: bool = False, progress: bool = True, **kwargs):
"""B-cos Simple ViT-L with 16x16 patch size and 224x224 image size.
+---------+-------------------+
| Name | Value |
+=========+===================+
| Acc@1 | 75.060% |
+---------+-------------------+
| Acc@5 | 91.378% |
+---------+-------------------+
| #Params | 178.79M |
+---------+-------------------+
References
----------
`B-cos Networks: Alignment is All We Need for Interpretability <https://arxiv.org/abs/2205.10268>`_
`Better plain ViT baselines for ImageNet-1k <https://arxiv.org/abs/2205.01580>`_
Parameters
----------
pretrained : bool
If True, returns a model pre-trained on ImageNet