# -*- coding: UTF-8 -*-
__author__ = 'xuy'
import os
import numpy as np
import caffe
caffe_root='/home/xuy/caffe'
os.chdir(caffe_root)
"""
对于caffe的硬件进行设置:是使用cpu还是使用gpu
"""
USE_GPU=True
if USE_GPU:
caffe.set_device(0)
caffe.set_mode_gpu()
else:
caffe.set_mode_cpu()
net=caffe.Net("examples/mnist/lenet_train_test.prototxt",caffe.TRAIN)
"""
用来查看网络结构层次:查看层的名字以及层的类型
"""
for name,layer in zip(net._layer_names,net.layers):#用来查看层的名字+层的类型
print "%s : %s : %d blobs"%(name,layer.type,len(layer.blobs))
"""
前面还有一些读取lenet_train_test.prototxt的内容
输出结果
mnist : Data : 0 blobs
conv1 : Convolution : 2 blobs
pool1 : Pooling : 0 blobs
conv2 : Convolution : 2 blobs
pool2 : Pooling : 0 blobs
ip1 : InnerProduct : 2 blobs
relu1 : ReLU : 0 blobs
ip2 : InnerProduct : 2 blobs
loss : SoftmaxWithLoss : 0 blobs
"""
"""
输出某一个具体层的blobs的数量,这里就输出了ip层(全连接层)的blobs的数量
"""
print len(net.params["ip1"])
"""
用来输出blobs数据的内容
----------------------------------------------------------
blobs.data输出的是若干的矩阵, 用于存储原始数据(see forward propagation)
blobs.data.shape输出的是对于该矩阵的大小的描述
blobs.diff输出的是若干矩阵,用于存储反向传播的梯度的更新值(see backward propagation)
blobs.diff.shape的输出和blobs.data.shape相同
输出内容是:
data : (64, 1, 28, 28)
label : (64,)
conv1 : (64, 20, 24, 24)
pool1 : (64, 20, 12, 12)
conv2 : (64, 50, 8, 8)
pool2 : (64, 50, 4, 4)
ip1 : (64, 500)
ip2 : (64, 10)
loss : ()
这里如果没有输出结果的话,默认为1
-----------------------------------------------------------------------
blobs.num指的是:元素的个数,通常来说是batch_size
channels, height, width,再结合上面的num属性,结合起来就相当于blobs.data.shape
print "%s %s %s %s"%(blob.num,blob.channels, blob.height, blob.width)
输出内容是:
64 1 28 28
64 1 1 1
64 20 24 24
64 20 12 12
64 50 8 8
64 50 4 4
64 500 1 1
64 10 1 1
1 1 1 1
------------------------------------------------------------------------
blob.count:是指的是:输出num * channels * height * width的值
输出内容是:
50176
64
737280
184320
204800
51200
32000
640
1
"""
print "\n\n"
print "Blobs:"
print "\n\n"
for name,blob in net.blobs.iteritems():
# print "%s %s %s %s"%(blob.num,blob.channels, blob.height, blob.width)
# print "%s %s %s "%(blob.num,blob.count,blob.diff)
print blob.diff.shape
"""
读取blob attributes的内容
"""
print "blob attributes"
for e in dir(net.blobs['label']):
if not e.startswith("__"):
print "%s"%e,
"""
输出结果:
channels count data diff height num reshape shape width
"""
print "\n"
"""
输出mnist的top的内容
"""
print net.top_names["mnist"]
pycaffe使用(第三篇)——对于blob数据类型的说明
最新推荐文章于 2020-01-12 22:16:56 发布