-
Notifications
You must be signed in to change notification settings - Fork 1
/
vgg_svhn.py
191 lines (167 loc) · 5.74 KB
/
vgg_svhn.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# -*- coding: utf8 -*-
import numpy as np
import torch
import torchvision
import torch.nn as nn
from scipy import io
import torch.utils.data as Data
import torchvision.transforms as transforms
from torch.autograd import Variable
torch.manual_seed(2)
BATCH_SIZE = 1
# 读取数据集
DOWNLOAD_SVHN = True # 如果你已经下载好了svhn数据就写上False
train_data = torchvision.datasets.SVHN(
root='./svhn/', # 保存或者提取位置
split='train',
transform=transforms.Compose([transforms.Resize(224),transforms.ToTensor()]),
download=DOWNLOAD_SVHN,
)
test_data = torchvision.datasets.SVHN(
root='./svhn/',
split='test',
transform=transforms.Compose([transforms.Resize(224),transforms.ToTensor()]),
)
train_loader = Data.DataLoader(dataset=train_data, batch_size=BATCH_SIZE, shuffle=False)
test_loader = Data.DataLoader(dataset=test_data, batch_size=BATCH_SIZE, shuffle=False)
# 读取模型及参数
vgg = torchvision.models.vgg16(pretrained=True)
vgg.classifier[6].out_features = 10
vgg.load_state_dict(torch.load('vgg.pkl'))
# 剔除后面的全连接层,保留之前的卷积层用于提取特征
vgg = nn.Sequential(*list(vgg.children())[:-1])
vgg = vgg.cuda()
# 训练集1提取
'''
train_features = torch.zeros((1, 512, 7, 7))
train_features = train_features.cuda()
train_labels = []
for i, (batch_data, batch_label) in enumerate(train_loader):
if i >= 20000:
break
batch_data = Variable(batch_data.cuda())
out = vgg(batch_data)
out_np = out.data
train_labels.append(int(batch_label[0]))
train_features = torch.cat((train_features, out_np), 0)
print(train_features.shape)
train_feature = train_features[1:,:]
train_feature = train_feature.cpu().numpy()
print(train_feature.shape)
train_label = np.matrix(train_labels)
io.savemat('svhn_vgg_train_features_1.mat', {'svhn_train_feature': train_feature})
io.savemat('svhn_vgg_train_labels_1.mat', {'svhn_train_label': train_label})
'''
# 训练集2提取
'''
train_features = torch.zeros((1, 512, 7, 7))
train_features = train_features.cuda()
train_labels = []
for i, (batch_data, batch_label) in enumerate(train_loader):
if i < 20000:
continue
if i >= 40000:
break
batch_data = Variable(batch_data.cuda())
out = vgg(batch_data)
out_np = out.data
train_labels.append(int(batch_label[0]))
train_features = torch.cat((train_features, out_np), 0)
print(train_features.shape)
train_feature = train_features[1:,:]
train_feature = train_feature.cpu().numpy()
print(train_feature.shape)
train_label = np.matrix(train_labels)
io.savemat('svhn_vgg_train_features_2.mat', {'svhn_train_feature': train_feature})
io.savemat('svhn_vgg_train_labels_2.mat', {'svhn_train_label': train_label})
'''
# 训练集3提取
'''
train_features = torch.zeros((1, 512, 7, 7))
train_features = train_features.cuda()
train_labels = []
for i, (batch_data, batch_label) in enumerate(train_loader):
if i < 40000:
continue
if i >= 60000:
break
batch_data = Variable(batch_data.cuda())
out = vgg(batch_data)
out_np = out.data
train_labels.append(int(batch_label[0]))
train_features = torch.cat((train_features, out_np), 0)
print(train_features.shape)
train_feature = train_features[1:,:]
train_feature = train_feature.cpu().numpy()
print(train_feature.shape)
train_label = np.matrix(train_labels)
io.savemat('svhn_vgg_train_features_3.mat', {'svhn_train_feature': train_feature})
io.savemat('svhn_vgg_train_labels_3.mat', {'svhn_train_label': train_label})
'''
# 训练集4提取
'''
train_features = torch.zeros((1, 512, 7, 7))
train_features = train_features.cuda()
train_labels = []
for i, (batch_data, batch_label) in enumerate(train_loader):
if i < 60000:
continue
if i >= 80000:
break
batch_data = Variable(batch_data.cuda())
out = vgg(batch_data)
out_np = out.data
train_labels.append(int(batch_label[0]))
train_features = torch.cat((train_features, out_np), 0)
print(train_features.shape)
train_feature = train_features[1:,:]
train_feature = train_feature.cpu().numpy()
print(train_feature.shape)
train_label = np.matrix(train_labels)
io.savemat('svhn_vgg_train_features_4.mat', {'svhn_train_feature': train_feature})
io.savemat('svhn_vgg_train_labels_4.mat', {'svhn_train_label': train_label})
'''
# 测试集1提取
'''
test_features = torch.zeros((1, 512, 7, 7))
test_features = test_features.cuda()
test_labels = []
for i, (batch_data, batch_label) in enumerate(test_loader):
if i >= 20000:
break
batch_data = Variable(batch_data.cuda())
out = vgg(batch_data)
out_np = out.data
test_labels.append(int(batch_label[0]))
test_features = torch.cat((test_features, out_np), 0)
print(test_features.shape)
test_feature = test_features[1:,:]
test_feature = test_feature.cpu().numpy()
print(test_feature.shape)
test_label = np.matrix(test_labels)
io.savemat('svhn_vgg_test_features_1.mat', {'svhn_test_feature': test_feature})
io.savemat('svhn_vgg_test_labels_1.mat', {'svhn_test_label': test_label})
'''
# 测试集2提取
'''
test_features = torch.zeros((1, 512, 7, 7))
test_features = test_features.cuda()
test_labels = []
for i, (batch_data, batch_label) in enumerate(test_loader):
if i < 20000:
continue
if i >= 40000:
break
batch_data = Variable(batch_data.cuda())
out = vgg(batch_data)
out_np = out.data
test_labels.append(int(batch_label[0]))
test_features = torch.cat((test_features, out_np), 0)
print(test_features.shape)
test_feature = test_features[1:,:]
test_feature = test_feature.cpu().numpy()
print(test_feature.shape)
test_label = np.matrix(test_labels)
io.savemat('svhn_vgg_test_features_2.mat', {'svhn_test_feature': test_feature})
io.savemat('svhn_vgg_test_labels_2.mat', {'svhn_test_label': test_label})
'''