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

标签 python neural-network caffe pycaffe

我想加载一个用 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 channel 图像,因此每个 channel 都有一个平均值。

但是我原以为每个 channel 只有一个值,但我发现了一个 256x256 数组:这是否意味着每个 channel 的每个像素都取了平均值?

另一个问题如下:我想将这样的 NN 与 OpenCV 一起使用,而不是 RGB 使用 BGR:How to know if the mean 3x256x256 uses RGB or BGR?

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

最佳答案

However I was expecting a single value per channel instead I found a 256x256 array: does it mean that the took a mean on each pixel of each channel?

没错。根据mean.binaryproto的形状,该文件是某个数据集的平均图像,这意味着它对每个 channel 的每个像素(特征)取平均值。

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

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

The only pre-processing we do is subtracting the mean RGB value, computed on the training set, from each pixel

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


Another question is the following: I want to use such NN with OpenCV which instead of RGB uses BGR: How to know if the mean 3x256x256 uses RGB or BGR?

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

例如,人脸图像。如果交换红色和蓝色 channel ,肤色将看起来偏蓝。

enter image description here

其实上图是平均图像(人脸图像)的例子:)

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

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

关于python - 加载神经网络时如何解释文件 mean.binaryproto?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52974196/

相关文章:

python - 查找功能的参数

machine-learning - 为什么 Adam 的学习率会增加?

deep-learning - 如何使用深度神经网络提高验证准确性?

deep-learning - Faster-RCNN bbox/图像标准化

caffe - 如何理解caffe的双线性上采样

python - 从 JSON 加载类变量?

python - 在 forms.py 的 admin.py 中包含一个 ModelForm

当项目作为服务启动时,Python 模块导入失败

tensorflow - 从损失函数记录标量

javascript - 为什么我的 Brain.js 神经网络卡在中间?