-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathperceptual_model.py
41 lines (28 loc) · 1.25 KB
/
perceptual_model.py
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
import torch
from torchvision import models
import torch.nn as nn
class VGG16_for_Perceptual(torch.nn.Module):
def __init__(self,requires_grad=False,n_layers=[2,4,14,21]):
super(VGG16_for_Perceptual,self).__init__()
vgg_pretrained_features=models.vgg16(pretrained=True).features
self.slice0=torch.nn.Sequential()
self.slice1=torch.nn.Sequential()
self.slice2=torch.nn.Sequential()
self.slice3=torch.nn.Sequential()
for x in range(n_layers[0]):#relu1_1
self.slice0.add_module(str(x),vgg_pretrained_features[x])
for x in range(n_layers[0],n_layers[1]): #relu1_2
self.slice1.add_module(str(x),vgg_pretrained_features[x])
for x in range(n_layers[1],n_layers[2]): #relu3_2
self.slice2.add_module(str(x),vgg_pretrained_features[x])
for x in range(n_layers[2],n_layers[3]):#relu4_2
self.slice3.add_module(str(x),vgg_pretrained_features[x])
if not requires_grad:
for param in self.parameters():
param.requires_grad=False
def forward(self,x):
h0=self.slice0(x)
h1=self.slice1(h0)
h2=self.slice2(h1)
h3=self.slice3(h2)
return h0,h1,h2,h3