- Feature extractor:
class FeatureExtractor(nn.Module): def __init__(self, submodule, extracted_layers): self.submodule = submodule def forward(self, x): outputs = [] for name, module in self.submodule._modules.items(): x = module(x) if name in self.extracted_layers: outputs += [x] return outputs + [x]
- Feature Perceptual Loss:
- Extracting features from pretrained model:
- class Vgg16_pretrained(nn.Module):
def __init__(self):
super(Vgg16_pretrained, self).__init__()
features = list(vgg19(pretrained = True).features)[:36]
# interested in following layers with corresponding indices
# 1: relu_1_1
# 6: relu_2_1
# 11: relu_3_1
# 20: relu_4_1
# 29: relu_5_1
# change it to eval() mode so that gradient won't be computed
self.features = nn.ModuleList(features).eval()
def forward(self, x):
results = []
for ii,model in enumerate(self.features):
x = model(x)
#pdb.set_trace()
if ii in {1,6,11}:
print(ii, ':', model)
print(x.shape)
results.append(x) - Loading pretrained model
- # init weights
model_v1 = EMNetv2(models)
init_weights = []
[init_weights.append(param.data) for param in model_v1.parameters()]
# pretrained weights
model_v2 = EMNetv2(models)
model_v2.load_state_dict(torch.load(model_ccd_pretrained))
pretrained_weights = []
[pretrained_weights.append(param.data) for param in model_v2.parameters()]
Funfact: artificial-intelligence-salaries-openai
No comments:
Post a Comment