0%

一些packages

timm

官方文档

Model Summaries - Pytorch Image Models (rwightman.github.io)

安装

1
pip install timm

建立模型

1
2
3
4
import timm

m = timm.create_model('mobilenetv3_large_100', pretrained=True)
m.eval()
1
2
3
4
import timm
from pprint import pprint
model_names = timm.list_models(pretrained=True)
pprint(model_names)
  1. 模块调用

Training Examples - Pytorch Image Models (rwightman.github.io)

Feature Extraction - Pytorch Image Models (rwightman.github.io)

1
2
3
4
5
import torch
import timm
m = timm.create_model('resnet50', pretrained=True, num_classes=0, global_pool='')
o = m(torch.randn(2, 3, 224, 224))
print(f'Unpooled shape: {o.shape}')

pretrainedmodels

https://github.com/Cadene/pretrained-models.pytorch

1
2
3
import pretrainedmodels
print(pretrainedmodels.model_names)
> ['fbresnet152', 'bninception', 'resnext101_32x4d', 'resnext101_64x4d', 'inceptionv4', 'inceptionresnetv2', 'alexnet', 'densenet121', 'densenet169', 'densenet201', 'densenet161', 'resnet18', 'resnet34', 'resnet50', 'resnet101', 'resnet152', 'inceptionv3', 'squeezenet1_0', 'squeezenet1_1', 'vgg11', 'vgg11_bn', 'vgg13', 'vgg13_bn', 'vgg16', 'vgg16_bn', 'vgg19_bn', 'vgg19', 'nasnetalarge', 'nasnetamobile', 'cafferesnet101', 'senet154', 'se_resnet50', 'se_resnet101', 'se_resnet152', 'se_resnext50_32x4d', 'se_resnext101_32x4d', 'cafferesnet101', 'polynet', 'pnasnet5large']
1
2
print(pretrainedmodels.pretrained_settings['nasnetalarge'])
> {'imagenet': {'url': 'http://data.lip6.fr/cadene/pretrainedmodels/nasnetalarge-a1897284.pth', 'input_space': 'RGB', 'input_size': [3, 331, 331], 'input_range': [0, 1], 'mean': [0.5, 0.5, 0.5], 'std': [0.5, 0.5, 0.5], 'num_classes': 1000}, 'imagenet+background': {'url': 'http://data.lip6.fr/cadene/pretrainedmodels/nasnetalarge-a1897284.pth', 'input_space': 'RGB', 'input_size': [3, 331, 331], 'input_range': [0, 1], 'mean': [0.5, 0.5, 0.5], 'std': [0.5, 0.5, 0.5], 'num_classes': 1001}}
1
2
3
model_name = 'nasnetalarge' # could be fbresnet152 or inceptionresnetv2
model = pretrainedmodels.__dict__[model_name](num_classes=1000, pretrained='imagenet')
model.eval()

各种ViT

https://github.com/lucidrains/vit-pytorch

专栏:https://www.zhihu.com/column/c_1260183218026213376

torchsummary可视化

  1. 安装
1
pip install torchsummary
  1. 使用方法
1
2
3
4
5
import torch, torchvision
model = torchvision.models.vgg
model = torchvision.models.vgg16()
from torchsummary import summary
summary(model, (3, 224, 224))

可视化

https://github.com/utkuozbulak/pytorch-cnn-visualizations

https://github.com/JonnesLin/Evison

https://www.jianshu.com/p/0431d6d89d8e

CAM可视化:

https://github.com/jacobgil/pytorch-grad-cam

t-SNE可视化

DmitryUlyanov/Multicore-TSNE: Parallel t-SNE implementation with Python and Torch wrappers. (github.com)

CannyLab/tsne-cuda: GPU Accelerated t-SNE for CUDA with Python bindings (github.com)

优雅地操作张量维度

1
pip install einops

PyTorch 70.einops:优雅地操作张量维度 - 知乎 (zhihu.com)

可微cv库

1
pip install Kornia

官方文档

https://kornia.readthedocs.io/en/latest/index.html

其它

基于 PyTorch 的开源 图像视频复原工具箱, 比如 超分辨率, 去噪, 去模糊, 去 JPEG 压缩噪声等.

https://github.com/xinntao/BasicSR

去雨去噪去模糊

hpcaitech/ColossalAI: Colossal-AI: A Unified Deep Learning System for Large-Scale Parallel Training (github.com)

用于模型训练

fairscale

hpcaitech/ColossalAI: Colossal-AI: A Unified Deep Learning System for Large-Scale Parallel Training (github.com)

GAN

eriklindernoren/PyTorch-GAN: PyTorch implementations of Generative Adversarial Networks. (github.com)

AutoDL

D-X-Y/AutoDL-Projects: Automated deep learning algorithms implemented in PyTorch. (github.com)

攻击&防御

https://github.com/thu-ml/ares

调参

https://github.com/optuna/optuna

用Optuna进行Pytorch超参数调优教程 - 知乎 (zhihu.com)

https://github.com/ray-project/ray

AutoML: 自动调参工具-Ray Tune - 知乎 (zhihu.com)

scikit-optimize: sequential model-based optimization in Python — scikit-optimize 0.8.1 documentation

ml_collections

ML Collections是为ML use cases而设计的一个Python Collections的一个库。它的两个类是ConfigDict和FrozenConfigDict,是"dict-like" 的数据结构,以下是ConfigDict、FrozenConfigDict和FieldReference的示例用法,

https://blog.csdn.net/weixin_47066348/article/details/113954876

Welcome to ml_collections’s documentation! — ml_collections 0.1.0 documentation (ml-collections.readthedocs.io)

https://github.com/google/ml_collections

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
import ml_collections

cfg = ml_collections.ConfigDict()
cfg.float_field = 12.6 #float类型
cfg.integer_field = 123 #int类型
cfg.another_integer_field = 234 #int类型
cfg.nested = ml_collections.ConfigDict() #嵌套了ml_collections.ConfigDict()
cfg.nested.string_field = 'tom' #str类型

print(cfg.integer_field) # 输出结果 123.
print(cfg['integer_field']) # 也输出123.

try:
cfg.integer_field = 'tom' # 输出会报错错误类型是TypeError,因为这你field是整数类型
except TypeError as e:
print(e)

cfg.float_field = 12 # int类型也可以指定给float类型.
cfg.nested.string_field = u'bob' # string可以储存Unicode字符串
print(cfg)
##############################
输出
123
123
Could not override field 'integer_field' (reference). tom is of type <class 'str'> but should be of type <class 'int'> #报错的地方
#以下输出是所有的cfg的结果,以及嵌套的nested
another_integer_field: 234
float_field: 12.0
integer_field: 123
nested:
string_field: bob

不可以改变的ConfigDict

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
import ml_collections
#初始化一个字典
initial_dictionary = {
'int': 1,
'list': [1, 2],
'tuple': (1, 2, 3),
'set': {1, 2, 3, 4},
'dict_tuple_list': {'tuple_list': ([1, 2], 3)}
}

cfg = ml_collections.ConfigDict(initial_dictionary)#把这个字典通过ConfigDict赋值给cfg
frozen_dict = ml_collections.FrozenConfigDict(initial_dictionary)#把这个字典通过FrozenConfigDict赋值给frozen_dict

print(frozen_dict.tuple) # 输出(1, 2, 3)
print(frozen_dict.list) # 输出 (1, 2)
print(frozen_dict.set) # 输出 {1, 2, 3, 4}
print(frozen_dict.dict_tuple_list.tuple_list[0]) # 输出 (1, 2)

frozen_cfg = ml_collections.FrozenConfigDict(cfg)#将cfg变成Forzen类型,即不可再改变其中常量的值
print(frozen_cfg == frozen_dict) # True
print(hash(frozen_cfg) == hash(frozen_dict)) # True

try:
frozen_dict.int = 2 # 会报错,因为FrozenConfigDict是不可以改变其中的值的
except AttributeError as e:
print(e)

# 在`FrozenConfigDict` 与 `ConfigDict`之间进行转换:
thawed_frozen_cfg = ml_collections.ConfigDict(frozen_dict) #将frozen_dict转化为ConfigDict
print(thawed_frozen_cfg == cfg) # True
frozen_cfg_to_cfg = frozen_dict.as_configdict()#将frozen_dict通过as_configdict方法转化为ConfigDict
print(frozen_cfg_to_cfg == cfg) # True

可解释性

https://github.com/pytorch/captum

沙普利值,归因