-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNetworks.py
138 lines (127 loc) · 4.42 KB
/
Networks.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
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
# coding: utf8
import os
import config as cfg
from sklearn import svm
import joblib
import torch.nn as nn
import torch.utils.model_zoo as model_zoo
import torch
class AlexNet(nn.Module):
"""Alexnet model."""
def __init__(self, num_classes=17):
""" Init
Args:
num_classes (int): The number of output classes
"""
super(AlexNet, self).__init__()
self.features = nn.Sequential(
# [n, 3, 224, 224]
nn.Conv2d(3, 96, kernel_size=11, stride=4, padding=2),
nn.ReLU(inplace=True),
# [n, 96, 55, 55]
nn.MaxPool2d(kernel_size=3, stride=2),
# [n, 96, 27, 27]
nn.Conv2d(96, 256, kernel_size=5, padding=2),
nn.ReLU(inplace=True),
# [n, 256, 27, 27]
nn.MaxPool2d(kernel_size=3, stride=2),
# [n, 256, 13, 13]
nn.Conv2d(256, 384, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
# [n, 384, 13, 13]
nn.Conv2d(384, 384, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
# [n, 384, 13, 13]
nn.Conv2d(384, 256, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
# [n, 256, 13, 13]
nn.MaxPool2d(kernel_size=3, stride=2),
# [n, 256, 6, 6]
)
self.classifier = nn.Sequential(
# [n, 256 * 6 * 6]
nn.Dropout(p=0.5),
nn.Linear(256 * 6 * 6, 4096),
nn.ReLU(inplace=True),
# [n, 4096]
nn.Dropout(p=0.5),
nn.Linear(4096, 4096),
nn.ReLU(inplace=True),
# [n, 4096]
nn.Linear(4096, num_classes),
# [n, num_classes]
)
def forward(self, x):
"""Pytorch forward function implementation."""
x = self.features(x)
x = x.view(x.size(0), 256 * 6 * 6)
x = self.classifier(x)
return x
# def run(self):
# self.load_data()
# self.load_model()
# accuracy = 0
# for epoch in range(1, self.epochs + 1):
# self.scheduler.step(epoch)
# print("\n===> epoch: %d/200" % epoch)
# train_result = self.train()
# print(train_result)
# test_result = self.test()
# accuracy = max(accuracy, test_result[1])
# if epoch == self.epochs:
# print("===> BEST ACC. PERFORMANCE: %.3f%%" % (accuracy * 100))
# self.save()
class SVM :
def __init__(self, data):
self.data = data
self.data_save_path = cfg.SVM_and_Reg_save
self.output = cfg.Out_put
def train(self):
import numpy as np
svms=[]
data_dirs = os.listdir(self.data_save_path)
for data_dir in data_dirs:
images, labels = self.data.get_SVM_data(data_dir)
image_data = [i[0].cpu().detach().numpy() for i in images]
print(np.shape(image_data))
print(np.shape(labels))
clf = svm.LinearSVC()
clf.fit(image_data, labels)
svms.append(clf)
SVM_model_path = os.path.join(self.output, 'SVM_model')
if not os.path.exists(SVM_model_path):
os.makedirs(SVM_model_path)
joblib.dump(clf, os.path.join(SVM_model_path, str(data_dir)+ '_svm.pkl'))
class RegNet(nn.Module):
"""RegNet model."""
def __init__(self, num_classes=5):
""" Init
Args:
num_classes (int): The number of output classes
"""
super(RegNet, self).__init__()
self.features = nn.Sequential(
nn.Linear(4096, 4096),
nn.Tanh(),
# [n, 4096]
nn.Dropout(p=0.5),
nn.Linear(4096, num_classes)
)
def forward(self, x):
"""Pytorch forward function implementation."""
x = self.features(x)
return x
# def run(self):
# self.load_data()
# self.load_model()
# accuracy = 0
# for epoch in range(1, self.epochs + 1):
# self.scheduler.step(epoch)
# print("\n===> epoch: %d/200" % epoch)
# train_result = self.train()
# print(train_result)
# test_result = self.test()
# accuracy = max(accuracy, test_result[1])
# if epoch == self.epochs:
# print("===> BEST ACC. PERFORMANCE: %.3f%%" % (accuracy * 100))
# self.save()