MCPcopy Create free account
hub / github.com/Xiaobin-Rong/gtcrn / convert_to_stream

Function convert_to_stream

stream/modules/convert.py:4–37  ·  view source on GitHub ↗
(stream_model, model)

Source from the content-addressed store, hash-verified

2
3
4def convert_to_stream(stream_model, model):
5 state_dict = model.state_dict()
6 new_state_dict = stream_model.state_dict()
7
8 for key in stream_model.state_dict().keys():
9 if key in state_dict.keys():
10 new_state_dict[key] = state_dict[key]
11
12 elif key.replace('Conv1d.', '') in state_dict.keys():
13 new_state_dict[key] = state_dict[key.replace('Conv1d.', '')]
14
15 elif key.replace('Conv2d.', '') in state_dict.keys():
16 new_state_dict[key] = state_dict[key.replace('Conv2d.', '')]
17
18 ## For StreamConvTranspose2d Version 1:
19 # elif key.replace('ConvTranspose2d.', '') in state_dict.keys():
20 # new_state_dict[key] = state_dict[key.replace('ConvTranspose2d.', '')]
21
22 ## For StreamConvTranspose2d Version 2:
23 elif key.replace('ConvTranspose2d.', '') in state_dict.keys():
24 if key.endswith('weight'):
25 if new_state_dict[key].shape != state_dict[key.replace('ConvTranspose2d.', '')].shape:
26 new_state_dict[key] = torch.flip(state_dict[key.replace('ConvTranspose2d.', '')].permute([1,0,2,3]), dims=[-2,-1])
27 else:
28 new_state_dict[key] = torch.flip(state_dict[key.replace('ConvTranspose2d.', '')], dims=[-2,-1])
29
30 else:
31 new_state_dict[key] = state_dict[key.replace('ConvTranspose2d.', '')]
32
33
34 else:
35 raise(ValueError('key error!'))
36
37 stream_model.load_state_dict(new_state_dict)

Callers 2

gtcrn_stream.pyFile · 0.90
convolution.pyFile · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected