mxnet如何查看中间结果

mxnet如何查看权重,中间输出结果

参考:https://blog.csdn.net/u010414386/article/details/55668880

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
import mxnet as mx
import cv2

ctx = mx.cpu()

sym, arg_params, aux_params = mx.model.load_checkpoint('model/light_classify', 299)
args = sym.get_internals().list_outputs() #获得所有中间输出
print("args:", args)
internals = sym.get_internals()
conv1 = internals['conv_1_conv2d_output']
conv1_bn = internals['conv_1_batchnorm_output']
group = mx.symbol.Group([sym, conv1, conv1_bn])

mod = mx.mod.Module(symbol=group, context=ctx, label_names=None)
mod.bind(for_training=False, data_shapes=[('data', (1,3,16,16))],
label_shapes=mod._label_shapes)
mod.set_params(arg_params, aux_params, allow_missing=True)
with open('synset.txt', 'r') as f:
labels = [l.rstrip() for l in f]

import matplotlib.pyplot as plt
import numpy as np
# define a simple data batch
from collections import namedtuple
Batch = namedtuple('Batch', ['data'])

def get_image(url, show=False):
# download and show the image. Remove query string from the file name.
# fname = mx.test_utils.download(url, fname=url.split('/')[-1].split('?')[0])
img = mx.image.imread(url)
if img is None:
return None
if show:
plt.imshow(img.asnumpy())
plt.axis('off')
# convert into format (batch, RGB, width, height)
img = mx.image.imresize(img, 16, 16) # resize
img = img.transpose((2, 0, 1)) # Channel first
# print("+++++++++++", img.asnumpy().__repr__())
img = img.expand_dims(axis=0) # batchify
img = img.astype('float32') # for gpu context
return img

def predict():
# path = 'pic/B1000_1.jpg'
# path = 'pic/black/190.121.11.119_20180101_080207_00124_1.jpg' # black
path = 'pic/green/aa2600000005_1.jpg' # green
# path = 'pic/yellow/wuhe_zhangheng_S_34_9-00_9-30-033_0.jpg'
# path = "pic/red/aa1700000017_0.jpg"
path = '/data0/hhq/data/light/test/yellow/190.201.11.101_20180101_075246_00231_1_0.jpg'

img = get_image(path, show=False)
# compute the predict probabilities
mod.forward(Batch([img]))
prob = mod.get_outputs()[0].asnumpy()
conv1_out = mod.get_outputs()[1]
conv1_bn_out = mod.get_outputs()[2]
# print the top-5
prob = np.squeeze(prob)
a = np.argsort(prob)[::-1]
for i in a[0:5]:
print('probability=%f, class=%s' %(prob[i], labels[i]))

predict()

caffe查看中间结果

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
caffe_root='/data0/hhq/project/caffe-master/'
import sys
sys.path.insert(0, caffe_root+'python')
import caffe
import numpy as np
import struct
import matplotlib.pyplot as plt
import cv2

# caffe.set_mode_gpu()
# caffe.set_device(1)

deploy='model_caffe/light_classify.prototxt'
caffe_model= 'model_caffe/light_classify.caffemodel'

# image_path ='pic/red/aa1700000017_0.jpg' # red

# image_path = 'pic/yellow/wuhe_zhangheng_S_34_9-00_9-30-033_0.jpg' # yellow

# image_path ='pic/black/190.121.11.119_20180101_080207_00134_0.jpg' # black

image_path = 'pic/green/aa2600000006_0.jpg'

net = caffe.Net(deploy, caffe_model, caffe.TEST)

transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})
transformer.set_transpose('data', (2, 0, 1))
transformer.set_raw_scale('data', 225)
# transformer.set_channel_swap('data', (2, 1, 0)) # should not exchange
net.blobs['data'].reshape(1, 3, 16, 16)

im = caffe.io.load_image(image_path)

image_pro = transformer.preprocess('data', im)

net.blobs['data'].data[...] = image_pro

out = net.forward()

fc = net.blobs['fc'].data[0].flatten()
conv1 = net.blobs['conv_1_conv2d'].data[0]
conv1_bn = net.blobs['conv_1_batchnorm'].data[0]
prob = net.blobs['fc'].data[0].flatten()
print(fc)
print(prob)
order = prob.argsort()[3]

print('the class is:', order)