资讯详情

【caffe】Caffe: (二) Classification (CPU/GPU设置, feature map可视化,层名,参数输出, Top-1、Top-5输出)

📅 2026/10/22 6:47:46 | 华诺云谱 👁 阅读
【caffe】Caffe: (二) Classification (CPU/GPU设置, feature map可视化,层名,参数输出, Top-1、Top-5输出)
#set up python environment: numpy for numerical routines, matplotlib for plottingimportnumpyasnpimportmatplotlib.pyplotasplt#display plots in this notebook%matplotlib inlineplt.rcParams[figure.figsize](10,10)# large imagesplt.rcParams[image.interpolation]nearest# dont interpolate: show square pixelsplt.rcParams[image.cmap]gray#use grayscale output rather than a (potentially misleading) color hetmap# the caffe modules needs to be on the Python path:importsys# print(sys.path)importcaffeimportos caffe_rootpath to caffeimportosifos.path.exists(caffe_rootmodels/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel):print(CaffeNet found.)else:print(Downloading pre-trained caffenet model...)!path_to_caffe/scripts/download_model_binary.py path_to_caffe/models/bvlc_reference_caffenet/CaffeNet found.importsys# print(sys.path)caffe.set_mode_cpu()model_defcaffe_rootmodels/bvlc_reference_caffenet/deploy.prototxtmodel_weightscaffe_rootmodels/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel#define the structure of the model, trained weights, use test mdoenetcaffe.Net(model_def,model_weights,caffe.TEST)# load the mean ImageNet image (as distributed with Caffe) for subtractionmunp.load(caffe_rootpython/caffe/imagenet/ilsvrc_2012_mean.npy)mumu.mean(1).mean(1)#average over pixels to obtain the mean (BGR) pixel valuesprint(mean-subtracted values: ,list(zip(BGR,mu)))mean-subtracted values: [(B, 104.0069879317889), (G, 116.66876761696767), (R, 122.6789143406786)]# create transformer for the input called datatransformercaffe.io.Transformer({data:net.blobs[data].data.shape})transformer.set_transpose(data,(2,0,1))#move image channels to outermost dimensiontransformer.set_mean(data,mu)#subtract the dataset-mean value in each channeltransformer.set_raw_scale(data,255)#rescale from [0,1] to [0,255]transformer.set_channel_swap(data,(2,1,0))# set the size of the inputnet.blobs[data].reshape(50,3,227,227)imagecaffe.io.load_image(caffe_rootexamples/images/cat.jpg)transformed_imagetransformer.preprocess(data,image)plt.imshow(image)matplotlib.image.AxesImage at 0x7fe5fae03978# copy the image data into the memory allocated for the netnet.blobs[data].data[...]transformed_image# perform classificationoutputnet.forward()output_proboutput[prob][0]# the output probability vector for the first image in the batchprint(predicted class is: ,output_prob.argmax())predicted class is: 281# load ImageNet labelslabels_filecaffe_rootdata/ilsvrc12/synset_words.txtifnotos.path.exists(labels_file):!path_to_caffe/data/ilsvrc12/get_ilsvrc_aux.sh labelsnp.loadtxt(labels_file,str,delimiter\t)print(output label: ,labels[output_prob.argmax()])Downloading... --2020-03-04 13:47:08-- http://dl.caffe.berkeleyvision.org/caffe_ilsvrc12.tar.gz Resolving dl.caffe.berkeleyvision.org (dl.caffe.berkeleyvision.org)... 128.32.162.150 Connecting to dl.caffe.berkeleyvision.org (dl.caffe.berkeleyvision.org)|128.32.162.150|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 17858008 (17M) [application/octet-stream] Saving to: ‘caffe_ilsvrc12.tar.gz’ caffe_ilsvrc12.tar. 100%[] 17.03M 111KB/s in 4m 15s 2020-03-04 13:51:25 (68.3 KB/s) - ‘caffe_ilsvrc12.tar.gz’ saved [17858008/17858008] Unzipping... Done. output label: n02123045 tabby, tabby cat# sort top five predictions from softmax outputtop_indsoutput_prob.argsort()[::-1][:5]print(probabilities and labels: )print(list(zip(output_prob[top_inds],labels[top_inds])))probabilities and labels: [(0.32925108, n02123045 tabby, tabby cat), (0.23463891, n02123159 tiger cat), (0.12834354, n02124075 Egyptian cat), (0.10341559, n02119022 red fox, Vulpes vulpes), (0.06193552, n02127052 lynx, catamount)]%timeit net.forward()768 ms ± 14.4 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)caffe.set_device(0)# if we have multiple GPUs, pick the first onecaffe.set_mode_gpu()net.forward()# run once before timing to set up memory%timeit net.forward()42.6 ms ± 63.2 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)# for each layer, show the outpyt shapeforlayer_name,blobinnet.blobs.items():print(layer_name\tstr(blob.data.shape))data (50, 3, 227, 227) conv1 (50, 96, 55, 55) pool1 (50, 96, 27, 27) norm1 (50, 96, 27, 27) conv2 (50, 256, 27, 27) pool2 (50, 256, 13, 13) norm2 (50, 256, 13, 13) conv3 (50, 384, 13, 13) conv4 (50, 384, 13, 13) conv5 (50, 256, 13, 13) pool5 (50, 256, 6, 6) fc6 (50, 4096) fc7 (50, 4096) fc8 (50, 1000) prob (50, 1000)forlayer_name,paraminnet.params.items():print(layer_name\tstr(param[0].data.shape),str(param[1].data.shape))conv1 (96, 3, 11, 11) (96,) conv2 (256, 48, 5, 5) (256,) conv3 (384, 256, 3, 3) (384,) conv4 (384, 192, 3, 3) (384,) conv5 (256, 192, 3, 3) (256,) fc6 (4096, 9216) (4096,) fc7 (4096, 4096) (4096,) fc8 (1000, 4096) (1000,)defvis_square(data):# Take an array of shape (n, height, width) or (n, height, width, 3)# and visualize each (height, width) thing in a grid of size approx.# sqrt(n) by sqrt(n)print(data.shape[0],data.shape[1],data.shape[2])# normalize data for displaydata(data-data.min())/(data.max()-data.min())# force the number of filters to be squarenint(np.ceil(np.sqrt(data.shape[0])))# add some space between filters# dont pad the last dimension (if there is one)padding(((0,n**2-data.shape[0]),(0,1),(0,1))((0,0),)*(data.ndim-3))print(padding)# pad with ones (white)datanp.pad(data,padding,modeconstant,constant_values1)# tile the filters into an imagedatadata.reshape((n,n)data.shape[1:]).transpose((0,2,1,3)tuple(range(4,data.ndim1)))datadata.reshape((n*data.shape[1],n*data.shape[3])data.shape[4:])plt.imshow(data)plt.axis(off)# the parameters are a list of [weights, bias]filtersnet.params[conv1][0].data vis_square(filters.transpose(0,2,3,1))96 11 11 ((0, 4), (0, 1), (0, 1), (0, 0))featnet.blobs[conv1].data[0,:36]vis_square(feat)36 55 55 ((0, 0), (0, 1), (0, 1))featnet.blobs[pool5].data[0]vis_square(feat)256 6 6 ((0, 0), (0, 1), (0, 1))featnet.blobs[fc6].data[0]plt.subplot(2,1,1)plt.plot(feat.flat)plt.subplot(2,1,2)_plt.hist(feat.flat[feat.flat0],bins100)理解总结部分设置CPU、GPU的方式cpu模式caffe.set_mode_cpu()gpu模式caffe.set_device(0)# if we have multiple GPUs, pick the first onecaffe.set_mode_gpu()分类Top-1 Top-5设置Top-1output_prob output[prob][0] # the output probability vector for the first image in the batchTop-5# sort top five predictions from softmax output top_inds output_prob.argsort()[::-1][:5]输出所有层的shape和对应参数的shape输出对应层的名字和shape,feature map 的方式 (N, C, H, W)# for each layer, show the outpyt shapeforlayer_name,blobinnet.blobs.items():print(layer_name\tstr(blob.data.shape))输出对应层参数的权重weights和biasesweights的存储方式output_channels, input_channels, filter_height, filter_widthforlayer_name,paraminnet.params.items():print(layer_name\tstr(param[0].data.shape),str(param[1].data.shape))可视化卷积核defvis_square(data):# Take an array of shape (n, height, width) or (n, height, width, 3)# and visualize each (height, width) thing in a grid of size approx.# sqrt(n) by sqrt(n)print(data.shape[0],data.shape[1],data.shape[2])# normalize data for displaydata(data-data.min())/(data.max()-data.min())# force the number of filters to be squarenint(np.ceil(np.sqrt(data.shape[0])))# add some space between filters# dont pad the last dimension (if there is one)padding(((0,n**2-data.shape[0]),(0,1),(0,1))((0,0),)*(data.ndim-3))print(padding)# pad with ones (white)datanp.pad(data,padding,modeconstant,constant_values1)# tile the filters into an imagedatadata.reshape((n,n)data.shape[1:]).transpose((0,2,1,3)tuple(range(4,data.ndim1)))datadata.reshape((n*data.shape[1],n*data.shape[3])data.shape[4:])plt.imshow(data)plt.axis(off)
📝

华诺云谱内容团队

资深建站顾问 · 行业研究员

10年+企业数字化服务经验,专注智能建站、SEO优化与品牌营销,持续输出建站技巧、行业洞察与营销干货,已帮助5000+企业实现数字化增长。

你可能需要的服务

订阅华诺云谱资讯周报

每周一封,精选建站技巧、SEO与营销干货,直达邮箱。已有 8,000+ 企业主订阅,助你少走弯路。

↑