forked from wkcn/CaffeSVD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
improve_ip1_new.py
163 lines (137 loc) · 4.57 KB
/
improve_ip1_new.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
#coding=utf-8
# decompress ip1 layer
import caffe
from caffe import layers as L, params as P, to_proto
from caffe.proto import caffe_pb2
import lmdb
import numpy as np
import os
import sys
from numpy import linalg as la
import matplotlib.pyplot as plt
from base import *
CAFFE_HOME = "/opt/caffe/"
RESULT_DIR = "./result/"
SVD_R = 6 # 64 x 1024
deploySVD = GetIP1SVDProto(SVD_R)
USE_WEIGHT = True
deploy = "./proto/cifar10_quick.prototxt"
caffe_model = CAFFE_HOME + "/examples/cifar10/cifar10_quick_iter_5000.caffemodel.h5"
train_db = CAFFE_HOME + "examples/cifar10/cifar10_train_lmdb"
test_db = CAFFE_HOME + "examples/cifar10/cifar10_test_lmdb"
mean_proto = CAFFE_HOME + "examples/cifar10/mean.binaryproto"
mean_npy = "./mean.npy"
mean_pic = np.load(mean_npy)
def read_db(db_name):
lmdb_env = lmdb.open(db_name)
lmdb_txn = lmdb_env.begin()
lmdb_cursor = lmdb_txn.cursor()
datum = caffe.proto.caffe_pb2.Datum()
X = []
y = []
cnts = {}
for key, value in lmdb_cursor:
datum.ParseFromString(value)
label = datum.label
data = caffe.io.datum_to_array(datum)
#data = data.swapaxes(0, 2).swapaxes(0, 1)
X.append(data)
y.append(label)
if label not in cnts:
cnts[label] = 0
cnts[label] += 1
#plt.imshow(data)
#plt.show()
return X, np.array(y), cnts
testX, testy, cnts = read_db(test_db)
#testX, testy, cnts = read_db(train_db)
print ("#train set: ", len(testX))
print ("the size of sample:", testX[0].shape)
print ("kinds: ", cnts)
if not os.path.exists("label.npy"):
np.save("label.npy", testy)
# Load model and network
net = caffe.Net(deploy, caffe_model, caffe.TEST)
for layer_name, param in net.params.items():
# 0 is weight, 1 is biases
print (layer_name, param[0].data.shape,net.blobs[layer_name].data.shape)
if SVD_R > 0:
netSVD = caffe.Net(deploySVD, caffe_model, caffe.TEST)
print ("SVD NET:")
for layer_name, param in netSVD.params.items():
# 0 is weight, 1 is biases
print (layer_name, param[0].data.shape)
print (type(net.params))
print (net.params.keys())
print ("layer ip1:")
print ("WEIGHT:")
print (net.params["ip1"][0].data.shape)
print ("BIASES:")
print (net.params["ip1"][1].data.shape)
data, label = L.Data(source = test_db, backend = P.Data.LMDB, batch_size = 100, ntop = 2, mean_file = mean_proto)
model_file = "model/net_SVD%d.caffemodel" % SVD_R
if SVD_R > 0:
# SVD
print ("SVD %d" % SVD_R)
u, sigma, vt = la.svd(net.params["ip1"][0].data)
print ("Sigma: ", sigma)
if SVD_R > len(sigma):
print ("SVD_R is too large :-(")
sys.exit()
U = np.matrix(u[:, :SVD_R])
S = np.matrix(np.diag(sigma[:SVD_R]))
VT = np.matrix(vt[:SVD_R, :])
print ("IP1", net.params["ip1"][0].data.shape) # 10, 64
print ("U", U.shape)
print ("S", S.shape)
print ("VT", VT.shape)
# y = Wx + b
# y = U * S * VT * x + b
# y = U * ((S * VT) * x) + b
# y = U * (Z * x) + b
Z = S * VT
np.copyto(netSVD.params["ipZ"][0].data, Z)
np.copyto(netSVD.params["ipU"][0].data, U)
np.copyto(netSVD.params["ipU"][1].data, net.params["ip1"][1].data)
nn = netSVD
nn.save(model_file)
else:
print ("NORMAL")
nn = net
# 生成配置文件
# CAFFE_HOME
print ("CONFIG")
example_dir = CAFFE_HOME + "examples/cifar10/"
build_dir = "./build_ip1/"
cwd = os.getcwd()
proto = build_dir + "train_test_SVD_ip1.prototext"
BuildFile([("$", "%d" % SVD_R)], proto, "./proto/train_test_SVD_ip1.template")
ps = [("$prototxt", proto), ("$snap_pre", "ip1_SVD%d" % (SVD_R))]
BuildFile(ps, build_dir + "solver.prototxt","./proto/solver.template")
model_file = build_dir + "ip1_SVD%d.caffemodel" % (SVD_R)
rp = [("$proto", build_dir + "solver.prototxt"), ("$model", model_file)]
BuildFile(rp, build_dir + "train_imp.sh", "./proto/train_imp.sh")
nn.save(model_file)
print ("OK")
sys.exit(0)
print ("train..")
os.system("cd %s && sudo optirun sh ./examples/cifar10/train_imp.sh" % CAFFE_HOME)
raw_input("aha")
# 加载新的模型
new_model = CAFFE_HOME + "examples/cifar10/net_improve_ip2_SVD%d_iter_500.caffemodel.h5" % SVD_R
nn = caffe.Net(deploySVD, new_model, caffe.TEST)
n = len(testX)
pre = np.zeros(testy.shape)
print ("N = %d" % n)
for i in range(n):
nn.blobs["data"].data[...] = testX[i] - mean_pic
nn.forward()
prob = nn.blobs["prob"].data
pre[i] = prob.argmax()
print ("%d / %d" % (i + 1, n))
right = np.sum(pre == testy)
print ("Accuracy: %f" % (right * 1.0 / n))
if SVD_R > 0:
np.save(RESULT_DIR + "net_imp_SVD%d.npy" % SVD_R, pre)
else:
np.save(RESULT_DIR + "net_imp_normal.npy", pre)