Repository navigation
Expand file tree
/
Copy pathresnet_load_pretrained_weights.py
More file actions
188 lines (164 loc) · 6.69 KB
/
Copy pathresnet_load_pretrained_weights.py
File metadata and controls
188 lines (164 loc) · 6.69 KB
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
# IMSL Lab - University of Notre Dame
# Author: Clemens JS Schaefer
import jax
from flax import core
from flax.core import freeze, unfreeze
import optax
import jax.numpy as jnp
from flax.training import dynamic_scale as dynamic_scale_lib
from flax import struct
from typing import Any, Callable
import torch
class TrainState(struct.PyTreeNode):
step: int
apply_fn: Callable = struct.field(pytree_node=False)
params: core.FrozenDict[str, Any] = struct.field(pytree_node=True)
quant_params: core.FrozenDict[str, Any] = struct.field(pytree_node=True)
batch_stats: Any
tx: optax.GradientTransformation = struct.field(pytree_node=False)
opt_state: optax.OptState = struct.field(pytree_node=True)
dynamic_scale: dynamic_scale_lib.DynamicScale = None
@classmethod
def create(cls, *, apply_fn, params, quant_params, tx, **kwargs):
"""Creates a new instance with `step=0` and initialized `opt_state`."""
return cls(
step=0,
apply_fn=apply_fn,
params=params,
quant_params=quant_params,
tx=None,
opt_state=None,
**kwargs,
)
map_dict = {
'11': '0',
'12': '1',
'21': '2',
'22': '3',
'31': '4',
'32': '5',
'41': '6',
'42': '7',
}
def resnet_load_pretrained_weights(state, location):
# Load torch style resnet18.
torch_state = torch.load(location, map_location=torch.device('cpu'))
torch_weights = unfreeze(jax.tree_util.tree_map(
lambda x: jnp.zeros(x.shape), state.params))
torch_bn_stats = unfreeze(jax.tree_util.tree_map(
lambda x: jnp.zeros(x.shape), state.batch_stats))
for key, value in torch_state.items():
# init block
if key == 'features.init_block.conv.conv.weight':
torch_weights['conv_init']['kernel'] = jnp.moveaxis(
jnp.array(value), (0, 1, 2, 3), (3, 2, 0, 1))
continue
if key == 'features.init_block.conv.bn.weight':
torch_weights['bn_init']['scale'] = jnp.array(value)
continue
if key == 'features.init_block.conv.bn.bias':
torch_weights['bn_init']['bias'] = jnp.array(value)
continue
if key == 'features.init_block.conv.bn.running_mean':
torch_bn_stats['bn_init']['mean'] = jnp.array(value)
continue
if key == 'features.init_block.conv.bn.running_var':
torch_bn_stats['bn_init']['var'] = jnp.array(value)
continue
# output block
if key == 'output.weight':
torch_weights['Dense_0']['kernel'] = jnp.transpose(jnp.array(value))
if key == 'output.bias':
torch_weights['Dense_0']['bias'] = jnp.array(value)
# body blocks
if 'features.stage' in key:
stage_id = key.split('.')[1][-1]
unit_id = key.split('.')[2][-1]
map_k = map_dict[stage_id + unit_id]
if key == 'features.stage' + stage_id + '.unit' + unit_id + \
'.body.conv1.conv.weight':
torch_weights['ResNetBlock_' + map_k]['Conv_0']['kernel'
] = jnp.moveaxis(
jnp.array(value), (0, 1, 2, 3), (3, 2, 0, 1))
continue
if key == 'features.stage' + stage_id + '.unit' + unit_id + \
'.body.conv1.bn.weight':
torch_weights['ResNetBlock_'
+ map_k]['BatchNorm_0']['scale'] = jnp.array(value)
continue
if key == 'features.stage' + stage_id + '.unit' + unit_id + \
'.body.conv1.bn.bias':
torch_weights['ResNetBlock_'
+ map_k]['BatchNorm_0']['bias'] = jnp.array(value)
continue
if key == 'features.stage' + stage_id + '.unit' + unit_id + \
'.body.conv1.bn.running_mean':
torch_bn_stats['ResNetBlock_'
+ map_k]['BatchNorm_0']['mean'] = jnp.array(value)
continue
if key == 'features.stage' + stage_id + '.unit' + unit_id + \
'.body.conv1.bn.running_var':
torch_bn_stats['ResNetBlock_'
+ map_k]['BatchNorm_0']['var'] = jnp.array(value)
continue
if key == 'features.stage' + stage_id + '.unit' + unit_id + \
'.body.conv2.conv.weight':
torch_weights['ResNetBlock_' + map_k]['Conv_1']['kernel'
] = jnp.moveaxis(
jnp.array(value), (0, 1, 2, 3), (3, 2, 0, 1))
continue
if key == 'features.stage' + stage_id + '.unit' + unit_id + \
'.body.conv2.bn.weight':
torch_weights['ResNetBlock_'
+ map_k]['BatchNorm_1']['scale'] = jnp.array(value)
continue
if key == 'features.stage' + stage_id + '.unit' + unit_id + \
'.body.conv2.bn.bias':
torch_weights['ResNetBlock_'
+ map_k]['BatchNorm_1']['bias'] = jnp.array(value)
continue
if key == 'features.stage' + stage_id + '.unit' + unit_id + \
'.body.conv2.bn.running_mean':
torch_bn_stats['ResNetBlock_'
+ map_k]['BatchNorm_1']['mean'] = jnp.array(value)
continue
if key == 'features.stage' + stage_id + '.unit' + unit_id + \
'.body.conv2.bn.running_var':
torch_bn_stats['ResNetBlock_'
+ map_k]['BatchNorm_1']['var'] = jnp.array(value)
continue
if key == 'features.stage' + stage_id + '.unit' + unit_id + \
'.identity_conv.conv.weight':
torch_weights['ResNetBlock_' + map_k]['conv_proj']['kernel'
] = jnp.moveaxis(
jnp.array(value), (0, 1, 2, 3), (3, 2, 0, 1))
continue
if key == 'features.stage' + stage_id + '.unit' + unit_id + \
'.identity_conv.bn.weight':
torch_weights['ResNetBlock_'
+ map_k]['norm_proj']['scale'] = jnp.array(value)
continue
if key == 'features.stage' + stage_id + '.unit' + unit_id + \
'.identity_conv.bn.bias':
torch_weights['ResNetBlock_'
+ map_k]['norm_proj']['bias'] = jnp.array(value)
continue
if key == 'features.stage' + stage_id + '.unit' + unit_id + \
'.identity_conv.bn.running_mean':
torch_bn_stats['ResNetBlock_'
+ map_k]['norm_proj']['mean'] = jnp.array(value)
continue
if key == 'features.stage' + stage_id + '.unit' + unit_id + \
'.identity_conv.bn.running_var':
torch_bn_stats['ResNetBlock_'
+ map_k]['norm_proj']['var'] = jnp.array(value)
continue
general_params = torch_weights
batch_stats = torch_bn_stats
return TrainState.create(
apply_fn=state.apply_fn,
params=freeze(general_params),
quant_params=state.quant_params,
tx=state.tx,
batch_stats=freeze(batch_stats),
)