【问题标题】:How to interpret the file mean.binaryproto when loading a Neural Network?加载神经网络时如何解释文件 mean.binaryproto?
【发布时间】:2019-03-29 04:13:27
【问题描述】:

我想加载一个经过 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 数组:这是否意味着每个通道的每个像素都已取平均值?

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

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

【问题讨论】:

    标签: python neural-network caffe pycaffe


    【解决方案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 绘制此图像并查看颜色是否有意义。

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

    其实上图是平均图片(人脸图片)的一个例子:)

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

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

    【讨论】:

      【解决方案2】:
      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)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-05-10
        • 1970-01-01
        • 1970-01-01
        • 2013-10-25
        • 2018-05-24
        • 2015-06-08
        • 1970-01-01
        • 2019-11-23
        相关资源
        最近更新 更多