spikezoo 0.1.1__py3-none-any.whl → 0.2__py3-none-any.whl
Sign up to get free protection for your applications and to get access to all the features.
- spikezoo/__init__.py +13 -0
- spikezoo/archs/__pycache__/__init__.cpython-39.pyc +0 -0
- spikezoo/archs/base/__pycache__/nets.cpython-39.pyc +0 -0
- spikezoo/archs/base/nets.py +34 -0
- spikezoo/archs/bsf/README.md +92 -0
- spikezoo/archs/bsf/datasets/datasets.py +328 -0
- spikezoo/archs/bsf/datasets/ds_utils.py +64 -0
- spikezoo/archs/bsf/main.py +398 -0
- spikezoo/archs/bsf/metrics/psnr.py +22 -0
- spikezoo/archs/bsf/metrics/ssim.py +54 -0
- spikezoo/archs/bsf/models/bsf/__pycache__/align.cpython-39.pyc +0 -0
- spikezoo/archs/bsf/models/bsf/__pycache__/bsf.cpython-39.pyc +0 -0
- spikezoo/archs/bsf/models/bsf/__pycache__/rep.cpython-39.pyc +0 -0
- spikezoo/archs/bsf/models/bsf/align.py +154 -0
- spikezoo/archs/bsf/models/bsf/bsf.py +105 -0
- spikezoo/archs/bsf/models/bsf/dsft_convert.py +96 -0
- spikezoo/archs/bsf/models/bsf/rep.py +44 -0
- spikezoo/archs/bsf/models/get_model.py +7 -0
- spikezoo/archs/bsf/prepare_data/DSFT.py +62 -0
- spikezoo/archs/bsf/prepare_data/crop_dataset_train.py +135 -0
- spikezoo/archs/bsf/prepare_data/crop_dataset_val.py +139 -0
- spikezoo/archs/bsf/prepare_data/crop_train.sh +4 -0
- spikezoo/archs/bsf/prepare_data/crop_val.sh +4 -0
- spikezoo/archs/bsf/prepare_data/io_utils.py +64 -0
- spikezoo/archs/bsf/requirements.txt +9 -0
- spikezoo/archs/bsf/test.py +16 -0
- spikezoo/archs/bsf/utils.py +154 -0
- spikezoo/archs/spikeclip/__pycache__/nets.cpython-39.pyc +0 -0
- spikezoo/archs/spikeclip/nets.py +40 -0
- spikezoo/archs/spikeformer/CheckPoints/readme +1 -0
- spikezoo/archs/spikeformer/DataProcess/DataExtactor.py +60 -0
- spikezoo/archs/spikeformer/DataProcess/DataLoader.py +115 -0
- spikezoo/archs/spikeformer/DataProcess/LoadSpike.py +39 -0
- spikezoo/archs/spikeformer/EvalResults/readme +1 -0
- spikezoo/archs/spikeformer/LICENSE +21 -0
- spikezoo/archs/spikeformer/Metrics/Metrics.py +50 -0
- spikezoo/archs/spikeformer/Metrics/__init__.py +0 -0
- spikezoo/archs/spikeformer/Model/Loss.py +89 -0
- spikezoo/archs/spikeformer/Model/SpikeFormer.py +230 -0
- spikezoo/archs/spikeformer/Model/__init__.py +0 -0
- spikezoo/archs/spikeformer/Model/__pycache__/SpikeFormer.cpython-39.pyc +0 -0
- spikezoo/archs/spikeformer/Model/__pycache__/__init__.cpython-39.pyc +0 -0
- spikezoo/archs/spikeformer/README.md +30 -0
- spikezoo/archs/spikeformer/evaluate.py +87 -0
- spikezoo/archs/spikeformer/recon_real_data.py +97 -0
- spikezoo/archs/spikeformer/requirements.yml +95 -0
- spikezoo/archs/spikeformer/train.py +173 -0
- spikezoo/archs/spikeformer/utils.py +22 -0
- spikezoo/archs/spk2imgnet/.github/workflows/pylint.yml +23 -0
- spikezoo/archs/spk2imgnet/.gitignore +150 -0
- spikezoo/archs/spk2imgnet/DCNv2.py +135 -0
- spikezoo/archs/spk2imgnet/__pycache__/DCNv2.cpython-39.pyc +0 -0
- spikezoo/archs/spk2imgnet/__pycache__/align_arch.cpython-39.pyc +0 -0
- spikezoo/archs/spk2imgnet/__pycache__/nets.cpython-39.pyc +0 -0
- spikezoo/archs/spk2imgnet/align_arch.py +159 -0
- spikezoo/archs/spk2imgnet/dataset.py +144 -0
- spikezoo/archs/spk2imgnet/nets.py +230 -0
- spikezoo/archs/spk2imgnet/readme.md +86 -0
- spikezoo/archs/spk2imgnet/test_gen_imgseq.py +118 -0
- spikezoo/archs/spk2imgnet/train.py +189 -0
- spikezoo/archs/spk2imgnet/utils.py +64 -0
- spikezoo/archs/ssir/README.md +87 -0
- spikezoo/archs/ssir/configs/SSIR.yml +37 -0
- spikezoo/archs/ssir/configs/yml_parser.py +78 -0
- spikezoo/archs/ssir/datasets/dataset_sreds.py +170 -0
- spikezoo/archs/ssir/datasets/ds_utils.py +66 -0
- spikezoo/archs/ssir/losses.py +21 -0
- spikezoo/archs/ssir/main.py +326 -0
- spikezoo/archs/ssir/metrics/psnr.py +22 -0
- spikezoo/archs/ssir/metrics/ssim.py +54 -0
- spikezoo/archs/ssir/models/Vgg19.py +42 -0
- spikezoo/archs/ssir/models/__pycache__/layers.cpython-39.pyc +0 -0
- spikezoo/archs/ssir/models/__pycache__/networks.cpython-39.pyc +0 -0
- spikezoo/archs/ssir/models/layers.py +110 -0
- spikezoo/archs/ssir/models/networks.py +61 -0
- spikezoo/archs/ssir/requirements.txt +8 -0
- spikezoo/archs/ssir/shells/eval_SREDS.sh +6 -0
- spikezoo/archs/ssir/shells/train_SSIR.sh +12 -0
- spikezoo/archs/ssir/test.py +3 -0
- spikezoo/archs/ssir/utils.py +154 -0
- spikezoo/archs/ssml/__pycache__/cbam.cpython-39.pyc +0 -0
- spikezoo/archs/ssml/__pycache__/model.cpython-39.pyc +0 -0
- spikezoo/archs/ssml/cbam.py +224 -0
- spikezoo/archs/ssml/model.py +290 -0
- spikezoo/archs/ssml/res.png +0 -0
- spikezoo/archs/ssml/test.py +67 -0
- spikezoo/archs/stir/.git-credentials +0 -0
- spikezoo/archs/stir/README.md +65 -0
- spikezoo/archs/stir/ckpt_outputs/Descriptions.txt +1 -0
- spikezoo/archs/stir/configs/STIR.yml +37 -0
- spikezoo/archs/stir/configs/utils.py +155 -0
- spikezoo/archs/stir/configs/yml_parser.py +78 -0
- spikezoo/archs/stir/datasets/dataset_sreds.py +180 -0
- spikezoo/archs/stir/datasets/ds_utils.py +66 -0
- spikezoo/archs/stir/eval_SREDS.sh +5 -0
- spikezoo/archs/stir/main.py +397 -0
- spikezoo/archs/stir/metrics/losses.py +219 -0
- spikezoo/archs/stir/metrics/psnr.py +22 -0
- spikezoo/archs/stir/metrics/ssim.py +54 -0
- spikezoo/archs/stir/models/Vgg19.py +42 -0
- spikezoo/archs/stir/models/__pycache__/networks_STIR.cpython-39.pyc +0 -0
- spikezoo/archs/stir/models/__pycache__/submodules.cpython-39.pyc +0 -0
- spikezoo/archs/stir/models/__pycache__/transformer_new.cpython-39.pyc +0 -0
- spikezoo/archs/stir/models/networks_STIR.py +361 -0
- spikezoo/archs/stir/models/submodules.py +86 -0
- spikezoo/archs/stir/models/transformer_new.py +151 -0
- spikezoo/archs/stir/package_core/build/lib/package_core/__init__.py +0 -0
- spikezoo/archs/stir/package_core/build/lib/package_core/convertions.py +721 -0
- spikezoo/archs/stir/package_core/build/lib/package_core/disp_netS.py +133 -0
- spikezoo/archs/stir/package_core/build/lib/package_core/flow_utils.py +167 -0
- spikezoo/archs/stir/package_core/build/lib/package_core/generic_train_test.py +76 -0
- spikezoo/archs/stir/package_core/build/lib/package_core/geometry.py +458 -0
- spikezoo/archs/stir/package_core/build/lib/package_core/image_proc.py +183 -0
- spikezoo/archs/stir/package_core/build/lib/package_core/linalg.py +40 -0
- spikezoo/archs/stir/package_core/build/lib/package_core/losses.py +198 -0
- spikezoo/archs/stir/package_core/build/lib/package_core/metrics.py +51 -0
- spikezoo/archs/stir/package_core/build/lib/package_core/model_base.py +53 -0
- spikezoo/archs/stir/package_core/build/lib/package_core/net_basics.py +100 -0
- spikezoo/archs/stir/package_core/build/lib/package_core/resnet.py +333 -0
- spikezoo/archs/stir/package_core/build/lib/package_core/transforms.py +123 -0
- spikezoo/archs/stir/package_core/build/lib/package_core/utils.py +72 -0
- spikezoo/archs/stir/package_core/dist/package_core-0.0.0-py3.9.egg +0 -0
- spikezoo/archs/stir/package_core/package_core/__init__.py +0 -0
- spikezoo/archs/stir/package_core/package_core/__pycache__/__init__.cpython-39.pyc +0 -0
- spikezoo/archs/stir/package_core/package_core/__pycache__/net_basics.cpython-39.pyc +0 -0
- spikezoo/archs/stir/package_core/package_core/convertions.py +721 -0
- spikezoo/archs/stir/package_core/package_core/disp_netS.py +133 -0
- spikezoo/archs/stir/package_core/package_core/flow_utils.py +167 -0
- spikezoo/archs/stir/package_core/package_core/generic_train_test.py +76 -0
- spikezoo/archs/stir/package_core/package_core/geometry.py +458 -0
- spikezoo/archs/stir/package_core/package_core/image_proc.py +183 -0
- spikezoo/archs/stir/package_core/package_core/linalg.py +40 -0
- spikezoo/archs/stir/package_core/package_core/losses.py +198 -0
- spikezoo/archs/stir/package_core/package_core/metrics.py +51 -0
- spikezoo/archs/stir/package_core/package_core/model_base.py +53 -0
- spikezoo/archs/stir/package_core/package_core/net_basics.py +100 -0
- spikezoo/archs/stir/package_core/package_core/resnet.py +333 -0
- spikezoo/archs/stir/package_core/package_core/transforms.py +123 -0
- spikezoo/archs/stir/package_core/package_core/utils.py +72 -0
- spikezoo/archs/stir/package_core/package_core.egg-info/PKG-INFO +3 -0
- spikezoo/archs/stir/package_core/package_core.egg-info/SOURCES.txt +20 -0
- spikezoo/archs/stir/package_core/package_core.egg-info/dependency_links.txt +1 -0
- spikezoo/archs/stir/package_core/package_core.egg-info/top_level.txt +1 -0
- spikezoo/archs/stir/package_core/setup.py +5 -0
- spikezoo/archs/stir/requirements.txt +12 -0
- spikezoo/archs/stir/train_STIR.sh +9 -0
- spikezoo/archs/tfi/__pycache__/nets.cpython-39.pyc +0 -0
- spikezoo/archs/tfi/nets.py +43 -0
- spikezoo/archs/tfp/__pycache__/nets.cpython-39.pyc +0 -0
- spikezoo/archs/tfp/nets.py +13 -0
- spikezoo/archs/wgse/README.md +64 -0
- spikezoo/archs/wgse/__pycache__/dwtnets.cpython-39.pyc +0 -0
- spikezoo/archs/wgse/__pycache__/submodules.cpython-39.pyc +0 -0
- spikezoo/archs/wgse/dataset.py +59 -0
- spikezoo/archs/wgse/demo.png +0 -0
- spikezoo/archs/wgse/demo.py +83 -0
- spikezoo/archs/wgse/dwtnets.py +145 -0
- spikezoo/archs/wgse/eval.py +133 -0
- spikezoo/archs/wgse/logs/WGSE-Dwt1dNet-db8-5-ks3/log.txt +11 -0
- spikezoo/archs/wgse/submodules.py +68 -0
- spikezoo/archs/wgse/train.py +261 -0
- spikezoo/archs/wgse/transform.py +139 -0
- spikezoo/archs/wgse/utils.py +128 -0
- spikezoo/archs/wgse/weights/demo.png +0 -0
- spikezoo/data/base/test/gt/200_part1_key_id151.png +0 -0
- spikezoo/data/base/test/gt/200_part3_key_id151.png +0 -0
- spikezoo/data/base/test/gt/203_part1_key_id151.png +0 -0
- spikezoo/data/base/test/spike/200_part1_key_id151.dat +0 -0
- spikezoo/data/base/test/spike/200_part3_key_id151.dat +0 -0
- spikezoo/data/base/test/spike/203_part1_key_id151.dat +0 -0
- spikezoo/data/base/train/gt/203_part2_key_id151.png +0 -0
- spikezoo/data/base/train/gt/203_part3_key_id151.png +0 -0
- spikezoo/data/base/train/gt/203_part4_key_id151.png +0 -0
- spikezoo/data/base/train/spike/203_part2_key_id151.dat +0 -0
- spikezoo/data/base/train/spike/203_part3_key_id151.dat +0 -0
- spikezoo/data/base/train/spike/203_part4_key_id151.dat +0 -0
- spikezoo/datasets/base_dataset.py +2 -3
- spikezoo/metrics/__init__.py +1 -1
- spikezoo/models/base_model.py +1 -3
- spikezoo/pipeline/base_pipeline.py +7 -5
- spikezoo/pipeline/train_pipeline.py +1 -1
- spikezoo/utils/other_utils.py +16 -6
- spikezoo/utils/spike_utils.py +33 -29
- spikezoo/utils/vidar_loader.cpython-39-x86_64-linux-gnu.so +0 -0
- spikezoo-0.2.dist-info/METADATA +163 -0
- spikezoo-0.2.dist-info/RECORD +211 -0
- spikezoo/models/spcsnet_model.py +0 -19
- spikezoo-0.1.1.dist-info/METADATA +0 -39
- spikezoo-0.1.1.dist-info/RECORD +0 -36
- {spikezoo-0.1.1.dist-info → spikezoo-0.2.dist-info}/LICENSE.txt +0 -0
- {spikezoo-0.1.1.dist-info → spikezoo-0.2.dist-info}/WHEEL +0 -0
- {spikezoo-0.1.1.dist-info → spikezoo-0.2.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,333 @@
|
|
1
|
+
import torch.nn as nn
|
2
|
+
try:
|
3
|
+
from torch.hub import load_state_dict_from_url
|
4
|
+
except ImportError:
|
5
|
+
from torch.utils.model_zoo import load_url as load_state_dict_from_url
|
6
|
+
|
7
|
+
|
8
|
+
__all__ = ['ResNet', 'resnet18', 'resnet34', 'resnet50', 'resnet101',
|
9
|
+
'resnet152', 'resnext50_32x4d', 'resnext101_32x8d',
|
10
|
+
'wide_resnet50_2', 'wide_resnet101_2']
|
11
|
+
|
12
|
+
|
13
|
+
model_urls = {
|
14
|
+
'resnet18': 'https://download.pytorch.org/models/resnet18-5c106cde.pth',
|
15
|
+
'resnet34': 'https://download.pytorch.org/models/resnet34-333f7ec4.pth',
|
16
|
+
'resnet50': 'https://download.pytorch.org/models/resnet50-19c8e357.pth',
|
17
|
+
'resnet101': 'https://download.pytorch.org/models/resnet101-5d3b4d8f.pth',
|
18
|
+
'resnet152': 'https://download.pytorch.org/models/resnet152-b121ed2d.pth',
|
19
|
+
'resnext50_32x4d': 'https://download.pytorch.org/models/resnext50_32x4d-7cdf4587.pth',
|
20
|
+
'resnext101_32x8d': 'https://download.pytorch.org/models/resnext101_32x8d-8ba56ff5.pth',
|
21
|
+
'wide_resnet50_2': 'https://download.pytorch.org/models/wide_resnet50_2-95faca4d.pth',
|
22
|
+
'wide_resnet101_2': 'https://download.pytorch.org/models/wide_resnet101_2-32ee1156.pth',
|
23
|
+
}
|
24
|
+
|
25
|
+
|
26
|
+
def conv3x3(in_planes, out_planes, stride=1, groups=1, dilation=1):
|
27
|
+
"""3x3 convolution with padding"""
|
28
|
+
return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride,
|
29
|
+
padding=dilation, groups=groups, bias=False, dilation=dilation)
|
30
|
+
|
31
|
+
|
32
|
+
def conv1x1(in_planes, out_planes, stride=1):
|
33
|
+
"""1x1 convolution"""
|
34
|
+
return nn.Conv2d(in_planes, out_planes, kernel_size=1, stride=stride, bias=False)
|
35
|
+
|
36
|
+
|
37
|
+
class BasicBlock(nn.Module):
|
38
|
+
expansion = 1
|
39
|
+
|
40
|
+
def __init__(self, inplanes, planes, stride=1, downsample=None, groups=1,
|
41
|
+
base_width=64, dilation=1, norm_layer=None):
|
42
|
+
super(BasicBlock, self).__init__()
|
43
|
+
if norm_layer is None:
|
44
|
+
norm_layer = nn.BatchNorm2d
|
45
|
+
if groups != 1 or base_width != 64:
|
46
|
+
raise ValueError('BasicBlock only supports groups=1 and base_width=64')
|
47
|
+
if dilation > 1:
|
48
|
+
raise NotImplementedError("Dilation > 1 not supported in BasicBlock")
|
49
|
+
# Both self.conv1 and self.downsample layers downsample the input when stride != 1
|
50
|
+
self.conv1 = conv3x3(inplanes, planes, stride)
|
51
|
+
self.bn1 = norm_layer(planes)
|
52
|
+
self.relu = nn.ReLU(inplace=True)
|
53
|
+
self.conv2 = conv3x3(planes, planes)
|
54
|
+
self.bn2 = norm_layer(planes)
|
55
|
+
self.downsample = downsample
|
56
|
+
self.stride = stride
|
57
|
+
|
58
|
+
def forward(self, x):
|
59
|
+
identity = x
|
60
|
+
|
61
|
+
out = self.conv1(x)
|
62
|
+
out = self.bn1(out)
|
63
|
+
out = self.relu(out)
|
64
|
+
|
65
|
+
out = self.conv2(out)
|
66
|
+
out = self.bn2(out)
|
67
|
+
|
68
|
+
if self.downsample is not None:
|
69
|
+
identity = self.downsample(x)
|
70
|
+
|
71
|
+
out += identity
|
72
|
+
out = self.relu(out)
|
73
|
+
|
74
|
+
return out
|
75
|
+
|
76
|
+
|
77
|
+
class Bottleneck(nn.Module):
|
78
|
+
expansion = 4
|
79
|
+
|
80
|
+
def __init__(self, inplanes, planes, stride=1, downsample=None, groups=1,
|
81
|
+
base_width=64, dilation=1, norm_layer=None):
|
82
|
+
super(Bottleneck, self).__init__()
|
83
|
+
if norm_layer is None:
|
84
|
+
norm_layer = nn.BatchNorm2d
|
85
|
+
width = int(planes * (base_width / 64.)) * groups
|
86
|
+
# Both self.conv2 and self.downsample layers downsample the input when stride != 1
|
87
|
+
self.conv1 = conv1x1(inplanes, width)
|
88
|
+
self.bn1 = norm_layer(width)
|
89
|
+
self.conv2 = conv3x3(width, width, stride, groups, dilation)
|
90
|
+
self.bn2 = norm_layer(width)
|
91
|
+
self.conv3 = conv1x1(width, planes * self.expansion)
|
92
|
+
self.bn3 = norm_layer(planes * self.expansion)
|
93
|
+
self.relu = nn.ReLU(inplace=True)
|
94
|
+
self.downsample = downsample
|
95
|
+
self.stride = stride
|
96
|
+
|
97
|
+
def forward(self, x):
|
98
|
+
identity = x
|
99
|
+
|
100
|
+
out = self.conv1(x)
|
101
|
+
out = self.bn1(out)
|
102
|
+
out = self.relu(out)
|
103
|
+
|
104
|
+
out = self.conv2(out)
|
105
|
+
out = self.bn2(out)
|
106
|
+
out = self.relu(out)
|
107
|
+
|
108
|
+
out = self.conv3(out)
|
109
|
+
out = self.bn3(out)
|
110
|
+
|
111
|
+
if self.downsample is not None:
|
112
|
+
identity = self.downsample(x)
|
113
|
+
|
114
|
+
out += identity
|
115
|
+
out = self.relu(out)
|
116
|
+
|
117
|
+
return out
|
118
|
+
|
119
|
+
|
120
|
+
class ResNet(nn.Module):
|
121
|
+
|
122
|
+
def __init__(self, block, layers, num_classes=1000, zero_init_residual=False,
|
123
|
+
groups=1, width_per_group=64, replace_stride_with_dilation=None,
|
124
|
+
norm_layer=None):
|
125
|
+
super(ResNet, self).__init__()
|
126
|
+
if norm_layer is None:
|
127
|
+
norm_layer = nn.BatchNorm2d
|
128
|
+
self._norm_layer = norm_layer
|
129
|
+
|
130
|
+
self.inplanes = 64
|
131
|
+
self.dilation = 1
|
132
|
+
if replace_stride_with_dilation is None:
|
133
|
+
# each element in the tuple indicates if we should replace
|
134
|
+
# the 2x2 stride with a dilated convolution instead
|
135
|
+
replace_stride_with_dilation = [False, False, False]
|
136
|
+
if len(replace_stride_with_dilation) != 3:
|
137
|
+
raise ValueError("replace_stride_with_dilation should be None "
|
138
|
+
"or a 3-element tuple, got {}".format(replace_stride_with_dilation))
|
139
|
+
self.groups = groups
|
140
|
+
self.base_width = width_per_group
|
141
|
+
self.conv1 = nn.Conv2d(3, self.inplanes, kernel_size=7, stride=2, padding=3,
|
142
|
+
bias=False)
|
143
|
+
self.bn1 = norm_layer(self.inplanes)
|
144
|
+
self.relu = nn.ReLU(inplace=True)
|
145
|
+
self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
|
146
|
+
self.layer1 = self._make_layer(block, 64, layers[0])
|
147
|
+
self.layer2 = self._make_layer(block, 128, layers[1], stride=2,
|
148
|
+
dilate=replace_stride_with_dilation[0])
|
149
|
+
self.layer3 = self._make_layer(block, 256, layers[2], stride=2,
|
150
|
+
dilate=replace_stride_with_dilation[1])
|
151
|
+
self.layer4 = self._make_layer(block, 512, layers[3], stride=2,
|
152
|
+
dilate=replace_stride_with_dilation[2])
|
153
|
+
self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
|
154
|
+
self.fc = nn.Linear(512 * block.expansion, num_classes)
|
155
|
+
|
156
|
+
for m in self.modules():
|
157
|
+
if isinstance(m, nn.Conv2d):
|
158
|
+
nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
|
159
|
+
elif isinstance(m, (nn.BatchNorm2d, nn.GroupNorm)):
|
160
|
+
nn.init.constant_(m.weight, 1)
|
161
|
+
nn.init.constant_(m.bias, 0)
|
162
|
+
|
163
|
+
# Zero-initialize the last BN in each residual branch,
|
164
|
+
# so that the residual branch starts with zeros, and each residual block behaves like an identity.
|
165
|
+
# This improves the model by 0.2~0.3% according to https://arxiv.org/abs/1706.02677
|
166
|
+
if zero_init_residual:
|
167
|
+
for m in self.modules():
|
168
|
+
if isinstance(m, Bottleneck):
|
169
|
+
nn.init.constant_(m.bn3.weight, 0)
|
170
|
+
elif isinstance(m, BasicBlock):
|
171
|
+
nn.init.constant_(m.bn2.weight, 0)
|
172
|
+
|
173
|
+
def _make_layer(self, block, planes, blocks, stride=1, dilate=False):
|
174
|
+
norm_layer = self._norm_layer
|
175
|
+
downsample = None
|
176
|
+
previous_dilation = self.dilation
|
177
|
+
if dilate:
|
178
|
+
self.dilation *= stride
|
179
|
+
stride = 1
|
180
|
+
if stride != 1 or self.inplanes != planes * block.expansion:
|
181
|
+
downsample = nn.Sequential(
|
182
|
+
conv1x1(self.inplanes, planes * block.expansion, stride),
|
183
|
+
norm_layer(planes * block.expansion),
|
184
|
+
)
|
185
|
+
|
186
|
+
layers = []
|
187
|
+
layers.append(block(self.inplanes, planes, stride, downsample, self.groups,
|
188
|
+
self.base_width, previous_dilation, norm_layer))
|
189
|
+
self.inplanes = planes * block.expansion
|
190
|
+
for _ in range(1, blocks):
|
191
|
+
layers.append(block(self.inplanes, planes, groups=self.groups,
|
192
|
+
base_width=self.base_width, dilation=self.dilation,
|
193
|
+
norm_layer=norm_layer))
|
194
|
+
|
195
|
+
return nn.Sequential(*layers)
|
196
|
+
|
197
|
+
def forward(self, x):
|
198
|
+
x = self.conv1(x)
|
199
|
+
x = self.bn1(x)
|
200
|
+
x = self.relu(x)
|
201
|
+
x = self.maxpool(x)
|
202
|
+
|
203
|
+
x = self.layer1(x)
|
204
|
+
x = self.layer2(x)
|
205
|
+
x = self.layer3(x)
|
206
|
+
x = self.layer4(x)
|
207
|
+
|
208
|
+
#x = self.avgpool(x)
|
209
|
+
#x = x.reshape(x.size(0), -1)
|
210
|
+
#x = self.fc(x)
|
211
|
+
|
212
|
+
return x
|
213
|
+
|
214
|
+
|
215
|
+
def _resnet(arch, block, layers, pretrained, progress, **kwargs):
|
216
|
+
model = ResNet(block, layers, **kwargs)
|
217
|
+
if pretrained:
|
218
|
+
state_dict = load_state_dict_from_url(model_urls[arch],
|
219
|
+
progress=progress)
|
220
|
+
model.load_state_dict(state_dict)
|
221
|
+
return model
|
222
|
+
|
223
|
+
|
224
|
+
def resnet18(pretrained=False, progress=True, **kwargs):
|
225
|
+
r"""ResNet-18 model from
|
226
|
+
`"Deep Residual Learning for Image Recognition" <https://arxiv.org/pdf/1512.03385.pdf>'_
|
227
|
+
Args:
|
228
|
+
pretrained (bool): If True, returns a model pre-trained on ImageNet
|
229
|
+
progress (bool): If True, displays a progress bar of the download to stderr
|
230
|
+
"""
|
231
|
+
return _resnet('resnet18', BasicBlock, [2, 2, 2, 2], pretrained, progress,
|
232
|
+
**kwargs)
|
233
|
+
|
234
|
+
|
235
|
+
def resnet34(pretrained=False, progress=True, **kwargs):
|
236
|
+
r"""ResNet-34 model from
|
237
|
+
`"Deep Residual Learning for Image Recognition" <https://arxiv.org/pdf/1512.03385.pdf>'_
|
238
|
+
Args:
|
239
|
+
pretrained (bool): If True, returns a model pre-trained on ImageNet
|
240
|
+
progress (bool): If True, displays a progress bar of the download to stderr
|
241
|
+
"""
|
242
|
+
return _resnet('resnet34', BasicBlock, [3, 4, 6, 3], pretrained, progress,
|
243
|
+
**kwargs)
|
244
|
+
|
245
|
+
|
246
|
+
def resnet50(pretrained=False, progress=True, **kwargs):
|
247
|
+
r"""ResNet-50 model from
|
248
|
+
`"Deep Residual Learning for Image Recognition" <https://arxiv.org/pdf/1512.03385.pdf>'_
|
249
|
+
Args:
|
250
|
+
pretrained (bool): If True, returns a model pre-trained on ImageNet
|
251
|
+
progress (bool): If True, displays a progress bar of the download to stderr
|
252
|
+
"""
|
253
|
+
return _resnet('resnet50', Bottleneck, [3, 4, 6, 3], pretrained, progress,
|
254
|
+
**kwargs)
|
255
|
+
|
256
|
+
|
257
|
+
def resnet101(pretrained=False, progress=True, **kwargs):
|
258
|
+
r"""ResNet-101 model from
|
259
|
+
`"Deep Residual Learning for Image Recognition" <https://arxiv.org/pdf/1512.03385.pdf>'_
|
260
|
+
Args:
|
261
|
+
pretrained (bool): If True, returns a model pre-trained on ImageNet
|
262
|
+
progress (bool): If True, displays a progress bar of the download to stderr
|
263
|
+
"""
|
264
|
+
return _resnet('resnet101', Bottleneck, [3, 4, 23, 3], pretrained, progress,
|
265
|
+
**kwargs)
|
266
|
+
|
267
|
+
|
268
|
+
def resnet152(pretrained=False, progress=True, **kwargs):
|
269
|
+
r"""ResNet-152 model from
|
270
|
+
`"Deep Residual Learning for Image Recognition" <https://arxiv.org/pdf/1512.03385.pdf>'_
|
271
|
+
Args:
|
272
|
+
pretrained (bool): If True, returns a model pre-trained on ImageNet
|
273
|
+
progress (bool): If True, displays a progress bar of the download to stderr
|
274
|
+
"""
|
275
|
+
return _resnet('resnet152', Bottleneck, [3, 8, 36, 3], pretrained, progress,
|
276
|
+
**kwargs)
|
277
|
+
|
278
|
+
|
279
|
+
def resnext50_32x4d(pretrained=False, progress=True, **kwargs):
|
280
|
+
r"""ResNeXt-50 32x4d model from
|
281
|
+
`"Aggregated Residual Transformation for Deep Neural Networks" <https://arxiv.org/pdf/1611.05431.pdf>`_
|
282
|
+
Args:
|
283
|
+
pretrained (bool): If True, returns a model pre-trained on ImageNet
|
284
|
+
progress (bool): If True, displays a progress bar of the download to stderr
|
285
|
+
"""
|
286
|
+
kwargs['groups'] = 32
|
287
|
+
kwargs['width_per_group'] = 4
|
288
|
+
return _resnet('resnext50_32x4d', Bottleneck, [3, 4, 6, 3],
|
289
|
+
pretrained, progress, **kwargs)
|
290
|
+
|
291
|
+
|
292
|
+
def resnext101_32x8d(pretrained=False, progress=True, **kwargs):
|
293
|
+
r"""ResNeXt-101 32x8d model from
|
294
|
+
`"Aggregated Residual Transformation for Deep Neural Networks" <https://arxiv.org/pdf/1611.05431.pdf>`_
|
295
|
+
Args:
|
296
|
+
pretrained (bool): If True, returns a model pre-trained on ImageNet
|
297
|
+
progress (bool): If True, displays a progress bar of the download to stderr
|
298
|
+
"""
|
299
|
+
kwargs['groups'] = 32
|
300
|
+
kwargs['width_per_group'] = 8
|
301
|
+
return _resnet('resnext101_32x8d', Bottleneck, [3, 4, 23, 3],
|
302
|
+
pretrained, progress, **kwargs)
|
303
|
+
|
304
|
+
|
305
|
+
def wide_resnet50_2(pretrained=False, progress=True, **kwargs):
|
306
|
+
r"""Wide ResNet-50-2 model from
|
307
|
+
`"Wide Residual Networks" <https://arxiv.org/pdf/1605.07146.pdf>`_
|
308
|
+
The model is the same as ResNet except for the bottleneck number of channels
|
309
|
+
which is twice larger in every block. The number of channels in outer 1x1
|
310
|
+
convolutions is the same, e.g. last block in ResNet-50 has 2048-512-2048
|
311
|
+
channels, and in Wide ResNet-50-2 has 2048-1024-2048.
|
312
|
+
Args:
|
313
|
+
pretrained (bool): If True, returns a model pre-trained on ImageNet
|
314
|
+
progress (bool): If True, displays a progress bar of the download to stderr
|
315
|
+
"""
|
316
|
+
kwargs['width_per_group'] = 64 * 2
|
317
|
+
return _resnet('wide_resnet50_2', Bottleneck, [3, 4, 6, 3],
|
318
|
+
pretrained, progress, **kwargs)
|
319
|
+
|
320
|
+
|
321
|
+
def wide_resnet101_2(pretrained=False, progress=True, **kwargs):
|
322
|
+
r"""Wide ResNet-101-2 model from
|
323
|
+
`"Wide Residual Networks" <https://arxiv.org/pdf/1605.07146.pdf>`_
|
324
|
+
The model is the same as ResNet except for the bottleneck number of channels
|
325
|
+
which is twice larger in every block. The number of channels in outer 1x1
|
326
|
+
convolutions is the same, e.g. last block in ResNet-50 has 2048-512-2048
|
327
|
+
channels, and in Wide ResNet-50-2 has 2048-1024-2048.
|
328
|
+
Args:
|
329
|
+
pretrained (bool): If True, returns a model pre-trained on ImageNet
|
330
|
+
progress (bool): If True, displays a progress bar of the download to stderr
|
331
|
+
"""
|
332
|
+
kwargs['width_per_group'] = 64 * 2
|
333
|
+
return _resnet('wide_resnet101_2', Bottleneck, [3, 4, 23, 3], pretrained, progress, **kwargs)
|
@@ -0,0 +1,123 @@
|
|
1
|
+
import torch
|
2
|
+
import random
|
3
|
+
import numbers
|
4
|
+
import collections
|
5
|
+
import numpy as np
|
6
|
+
|
7
|
+
class Merge(object):
|
8
|
+
def __init__(self, axis=-1):
|
9
|
+
self.axis = axis
|
10
|
+
|
11
|
+
def __call__(self, images):
|
12
|
+
if isinstance(images, collections.Sequence) or isinstance(images, np.ndarray):
|
13
|
+
assert all([isinstance(i, np.ndarray) for i in images]), 'only numpy array is supported'
|
14
|
+
shapes = [list(i.shape) for i in images]
|
15
|
+
for s in shapes:
|
16
|
+
s[self.axis] = None
|
17
|
+
assert all([s == shapes[0] for s in shapes]), 'shapes must be the same except the merge axis'
|
18
|
+
return np.concatenate(images, axis=self.axis)
|
19
|
+
else:
|
20
|
+
raise Exception("obj is not a sequence (list, tuple, etc)")
|
21
|
+
|
22
|
+
class Normalize(object):
|
23
|
+
def __call__(self, image):
|
24
|
+
image=image/255.
|
25
|
+
return image
|
26
|
+
|
27
|
+
class Crop(object):
|
28
|
+
def __init__(self, size):
|
29
|
+
if isinstance(size, numbers.Number):
|
30
|
+
self.size = (int(size), int(size))
|
31
|
+
else:
|
32
|
+
self.size = size
|
33
|
+
|
34
|
+
def __call__(self, img):
|
35
|
+
h, w = img.shape[:2]
|
36
|
+
|
37
|
+
th, tw = self.size
|
38
|
+
if w == tw and h == th:
|
39
|
+
return img
|
40
|
+
|
41
|
+
return img[:th, :tw, :]
|
42
|
+
|
43
|
+
class Random_crop(object):
|
44
|
+
"""Crops the given numpy array at a random location to have a region of
|
45
|
+
the given size. size can be a tuple (target_height, target_width)
|
46
|
+
or an integer, in which case the target will be of a square shape (size, size)
|
47
|
+
"""
|
48
|
+
def __init__(self, size):
|
49
|
+
if isinstance(size, numbers.Number):
|
50
|
+
self.size = (int(size), int(size))
|
51
|
+
else:
|
52
|
+
self.size = size
|
53
|
+
random.seed()
|
54
|
+
|
55
|
+
def __call__(self, img):
|
56
|
+
h, w = img.shape[:2]
|
57
|
+
|
58
|
+
th, tw = self.size
|
59
|
+
if w == tw and h == th:
|
60
|
+
return img
|
61
|
+
|
62
|
+
w1 = 0 if w==tw else random.randint(0, w - tw)
|
63
|
+
h1 = 0 if h==th else random.randint(0, h - th)
|
64
|
+
|
65
|
+
return img[h1:h1+th, w1:w1+tw, :]
|
66
|
+
|
67
|
+
class Split(object):
|
68
|
+
"""Split images into individual arraies
|
69
|
+
"""
|
70
|
+
def __init__(self, *slices, **kwargs):
|
71
|
+
assert isinstance(slices, collections.Sequence)
|
72
|
+
slices_ = []
|
73
|
+
for s in slices:
|
74
|
+
slices_.append(s)
|
75
|
+
self.slices = slices_
|
76
|
+
|
77
|
+
def __call__(self, image):
|
78
|
+
if isinstance(image, np.ndarray):
|
79
|
+
ret = []
|
80
|
+
for s in self.slices:
|
81
|
+
ret.append(image[:, :, s[0]:s[1]])
|
82
|
+
return ret
|
83
|
+
else:
|
84
|
+
raise Exception("obj is not an numpy array")
|
85
|
+
|
86
|
+
class To_tensor(object):
|
87
|
+
def __call__(self, image):
|
88
|
+
image = image.transpose((2, 0, 1))
|
89
|
+
return torch.from_numpy(image).float()
|
90
|
+
|
91
|
+
class Compose(object):
|
92
|
+
"""Composes several transforms together.
|
93
|
+
Args:
|
94
|
+
transforms (List[Transform]): list of transforms to compose.
|
95
|
+
Example:
|
96
|
+
>>> transforms.Compose([
|
97
|
+
>>> transforms.CenterCrop(10),
|
98
|
+
>>> transforms.ToTensor(),
|
99
|
+
>>> ])
|
100
|
+
"""
|
101
|
+
|
102
|
+
def __init__(self, transforms):
|
103
|
+
self.transforms = transforms
|
104
|
+
|
105
|
+
def __call__(self, img):
|
106
|
+
for t in self.transforms:
|
107
|
+
if isinstance(t, collections.Sequence):
|
108
|
+
assert isinstance(img, collections.Sequence) and len(img) == len(
|
109
|
+
t), "size of image group and transform group does not fit"
|
110
|
+
tmp_ = []
|
111
|
+
for i, im_ in enumerate(img):
|
112
|
+
if callable(t[i]):
|
113
|
+
tmp_.append(t[i](im_))
|
114
|
+
else:
|
115
|
+
tmp_.append(im_)
|
116
|
+
img = tmp_
|
117
|
+
elif callable(t):
|
118
|
+
img = t(img)
|
119
|
+
elif t is None:
|
120
|
+
continue
|
121
|
+
else:
|
122
|
+
raise Exception('unexpected type')
|
123
|
+
return img
|
@@ -0,0 +1,72 @@
|
|
1
|
+
from __future__ import division
|
2
|
+
import shutil
|
3
|
+
import numpy as np
|
4
|
+
import torch
|
5
|
+
import meshzoo
|
6
|
+
from path import Path
|
7
|
+
import datetime
|
8
|
+
from collections import OrderedDict
|
9
|
+
from matplotlib import cm
|
10
|
+
from matplotlib.colors import ListedColormap, LinearSegmentedColormap
|
11
|
+
|
12
|
+
def high_res_colormap(low_res_cmap, resolution=1000, max_value=1):
|
13
|
+
# Construct the list colormap, with interpolated values for higer resolution
|
14
|
+
# For a linear segmented colormap, you can just specify the number of point in
|
15
|
+
# cm.get_cmap(name, lutsize) with the parameter lutsize
|
16
|
+
x = np.linspace(0, 1, low_res_cmap.N)
|
17
|
+
low_res = low_res_cmap(x)
|
18
|
+
new_x = np.linspace(0, max_value, resolution)
|
19
|
+
high_res = np.stack([np.interp(new_x, x, low_res[:, i])
|
20
|
+
for i in range(low_res.shape[1])], axis=1)
|
21
|
+
return ListedColormap(high_res)
|
22
|
+
|
23
|
+
|
24
|
+
def opencv_rainbow(resolution=1000):
|
25
|
+
# Construct the opencv equivalent of Rainbow
|
26
|
+
opencv_rainbow_data = (
|
27
|
+
(0.000, (1.00, 0.00, 0.00)),
|
28
|
+
(0.400, (1.00, 1.00, 0.00)),
|
29
|
+
(0.600, (0.00, 1.00, 0.00)),
|
30
|
+
(0.800, (0.00, 0.00, 1.00)),
|
31
|
+
(1.000, (0.60, 0.00, 1.00))
|
32
|
+
)
|
33
|
+
|
34
|
+
return LinearSegmentedColormap.from_list('opencv_rainbow', opencv_rainbow_data, resolution)
|
35
|
+
|
36
|
+
|
37
|
+
COLORMAPS = {'rainbow': opencv_rainbow(),
|
38
|
+
'magma': high_res_colormap(cm.get_cmap('magma')),
|
39
|
+
'bone': cm.get_cmap('bone', 10000)}
|
40
|
+
|
41
|
+
|
42
|
+
def tensor2array(tensor, max_value=None, colormap='rainbow'):
|
43
|
+
tensor = tensor.detach().cpu()
|
44
|
+
if max_value is None:
|
45
|
+
max_value = tensor.max().item()
|
46
|
+
if tensor.ndimension() == 2 or tensor.size(0) == 1:
|
47
|
+
norm_array = tensor.squeeze().numpy()/max_value
|
48
|
+
array = COLORMAPS[colormap](norm_array).astype(np.float32)
|
49
|
+
array = array.transpose(2, 0, 1)
|
50
|
+
|
51
|
+
elif tensor.ndimension() == 3:
|
52
|
+
assert(tensor.size(0) == 3)
|
53
|
+
array = 0.5 + tensor.numpy()*0.5
|
54
|
+
return array
|
55
|
+
|
56
|
+
def generate_2D_mesh(H, W):
|
57
|
+
_, faces = meshzoo.rectangle(
|
58
|
+
xmin = -1., xmax = 1.,
|
59
|
+
ymin = -1., ymax = 1.,
|
60
|
+
nx = W, ny = H,
|
61
|
+
zigzag=True)
|
62
|
+
|
63
|
+
x = torch.arange(0, W, 1).float().cuda()
|
64
|
+
y = torch.arange(0, H, 1).float().cuda()
|
65
|
+
|
66
|
+
xx = x.repeat(H, 1)
|
67
|
+
yy = y.view(H, 1).repeat(1, W)
|
68
|
+
|
69
|
+
grid = torch.stack([xx, yy], dim=0)
|
70
|
+
|
71
|
+
return grid, faces
|
72
|
+
|
Binary file
|
File without changes
|