-
Notifications
You must be signed in to change notification settings - Fork 41
/
nets.py
executable file
·59 lines (51 loc) · 1.9 KB
/
nets.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from __future__ import division
from utils import *
from data import MyDatasetSTFT
from sklearn.model_selection import train_test_split
# from utils import *
import torch
import torch.nn as nn
import argparse
import copy
import random
from torchvision import transforms
# import time
import torch.backends.cudnn as cudnn
import os, sys
from time import time, strftime
class MyResNet(nn.Module):
def __init__(self, depth, num_classes, pretrained = True):
super(MyResNet, self).__init__()
if depth == 18:
model = models.resnet18(pretrained)
elif depth == 34:
model = models.resnet34(pretrained)
elif depth == 50:
model = models.resnet50(pretrained)
elif depth == 152:
model = models.resnet152(pretrained)
self.num_ftrs = model.fc.in_features
# self.num_classes = num_classes
self.shared = nn.Sequential(*list(model.children())[:-1])
self.target = nn.Sequential(nn.Linear(self.num_ftrs, num_classes))
def forward(self, x):
# pdb.set_trace()
x = self.shared(x)
x = torch.squeeze(x)
return self.target(x)
def frozen_until(self, to_layer):
print('Frozen shared part until %d-th layer, inclusive'%to_layer)
# if to_layer = -1, frozen all
child_counter = 0
for child in self.shared.children():
if child_counter <= to_layer:
print("child ", child_counter, " was frozen")
for param in child.parameters():
param.requires_grad = False
# frozen deeper children? check
# https://spandan-madan.github.io/A-Collection-of-important-tasks-in-pytorch/
else:
print("child ", child_counter, " was not frozen")
for param in child.parameters():
param.requires_grad = True
child_counter += 1