加载神经网络时如何解释文件mean.binaryproto?

问题描述 投票:0回答:2

我想加载一个经过caffe训练的神经网络用于图像分类。

NN包含文件mean.binaryproto,其具有在输入要分类的图像之前被减去的手段。

我正在尝试了解此文件中包含的内容,因此我使用Google Colab查看其中的内容。

加载它的代码如下:

# Load the Drive helper and mount
from google.colab import drive

# This will prompt for authorization.
drive.mount('/content/drive')
!ls "/content/drive/My Drive"

#install packages
!apt install -y caffe-cuda
!apt update
!apt upgrade
!apt dist-upgrade
!ls "/content/drive/My Drive/NeuralNetwork/CNRPark-Trained-Models/mAlexNet-on-CNRPark/"
import caffe
import numpy as np
with open('/content/drive/My Drive/NeuralNetwork/CNRPark-Trained-Models/mAlexNet-on-CNRPark/mean.binaryproto', 'rb') as f:
    blob = caffe.proto.caffe_pb2.BlobProto()
    blob.ParseFromString(f.read())
    arr = np.array( caffe.io.blobproto_to_array(blob) )
    print(arr.shape)
    out = arr[0]
    data = np.array(blob.data).reshape([blob.channels, blob.height, blob.width])
    print (data.shape)
    print(data[0])
 #display the mean image
 from PIL import Image
 from IPython.display import Image as Im, display
 display(Image.fromarray(data[0], 'RGB'))

哪个输出:

(1, 3, 256, 256)
(3, 256, 256)

我所理解的是该文件包含手段,我们正在谈论的图像是3通道图像,因此每个通道都有一个平均值。

但是我期望每个通道只有一个值,而我找到了一个256x256阵列:这是否意味着每个通道的每个像素都取了一个平均值?

另一个问题如下:我想在OpenCV中使用这样的NN而不是RGB使用BGR:如何知道3x256x256的平均值是使用RGB还是BGR?

该模型的链接是this。我正在查看的模型包含在文件夹中的zip文件CNRPark-Trained-Models.zip中:mAlexNet-on-CNRPark

python neural-network caffe pycaffe
2个回答
1
投票

但是我期望每个通道只有一个值,而我找到了一个256x256阵列:这是否意味着每个通道的每个像素都取了一个平均值?

究竟。根据mean.binaryproto的形状,该文件是某些数据集的平均图像,这意味着它取每个通道的每个像素(特征)的平均值。

这不应该与平均像素相混淆,正如您所说,平均像素是每个通道的单个值。

例如,Very Deep Convolutional Networks for Large-Scale Image Recognition采用了平均像素。根据他们的论文:

我们所做的唯一预处理是从每个像素中减去在训练集上计算的平均RGB值

换句话说,如果您将RGB图像视为大小为N x N的3个要素数组,则平均图像将是每个要素的平均值,平均像素将是所有要素的平均值。


另一个问题如下:我想在OpenCV中使用这样的NN而不是RGB使用BGR:如何知道3x256x256的平均值是使用RGB还是BGR?

我怀疑你正在阅读的二进制文件存储有关其颜色格式的任何信息,但实际的方法是使用matplotlib绘制此图像并查看颜色是否有意义。

例如,面部图像。如果交换红色和蓝色通道,肤色将显得偏蓝。

enter image description here

事实上,上面的图像是一个平均图像(脸部图像)的例子:)

你也可以假设它是BGR,因为OpenCV使用这种颜色格式。

但是,找出这个mean.binaryproto是如何生成的正确方法是查看它们的存储库或询问模型的所有者。


0
投票
import os, sys, glob, caffe
import numpy as np
mean_file= "path/to/file/mean.binaryproto"
#convert mean file to image
blob= caffe.proto.caffe_pb2.BlobProto()
try:
    data = open( mean_file, 'rb' ).read()
except:
    data = open( mean_file, 'r' ).read()
blob.ParseFromString(data)
arr = np.uint8(np.array( caffe.io.blobproto_to_array(blob) )[0])
#a= arr[0];    b= arr[1];    c= arr[2]
img= np.zeros([128,200,3])
img[:,:,0]= arr[0];    img[:,:,1]= arr[1];    img[:,:,2]= arr[2]
import cv2
cv2.imwrite(mean_file.replace(".binaryproto", ".bmp"), img)
© www.soinside.com 2019 - 2024. All rights reserved.