关于测试自己的单张图片所需要的lenet.prototxt(deploy)文件

本文介绍如何从训练测试配置文件cifar10_quick_train_test.prototxt转换为部署文件deploy.prototxt的过程。主要包括删除测试层信息、修改训练层数据源为输入层并调整最后一层为Softmax层。

Depoly文件其实就是在刚刚定义定义层的参数的那个文件基础上进行修改的,

delpoy文件只记录了网络结构,而并没有里面的反向计算,也不需要计算误差。

我使用cifar10_quick_train_test.ptototxt进行修改,修改成对应的deploy文件.

cifar10_quick_train_test.prototxt文件内容如下:

name: "CIFAR10_quick"
layer {
  name: "cifar"
  type: "Data"
  top: "data"
  top: "label"
  include {
    phase: TRAIN
  }
  transform_param {
    mean_file: "C:/Users/Administrator/Desktop/data12345/mean.binaryproto"
  }
  data_param {
    source: "C:/Users/Administrator/Desktop/data12345/mtrainldb"
    batch_size: 50
    backend: LEVELDB
  }
}
layer {
  name: "cifar"
  type: "Data"
  top: "data"
  top: "label"
  include {
    phase: TEST
  }
  transform_param {
    mean_file: "C:/Users/Administrator/Desktop/data12345/mean.binaryproto"
  }
  data_param {
    source: "C:/Users/Administrator/Desktop/data12345/mtestldb"
    batch_size: 2
    backend: LEVELDB
  }
}
layer {
  name: "conv1"
  type: "Convolution"
  bottom: "data"
  top: "conv1"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  convolution_param {
    num_output: 16
    pad: 2
    kernel_size: 5
    stride: 1
    weight_filler {
      type: "gaussian"
      std: 0.0001
    }
    bias_filler {
      type: "constant"
    }
  }
}
layer {
  name: "pool1"
  type: "Pooling"
  bottom: "conv1"
  top: "pool1"
  pooling_param {
    pool: MAX
    kernel_size: 2
    stride: 2
  }
}
layer {
  name: "relu1"
  type: "ReLU"
  bottom: "pool1"
  top: "pool1"
}
layer {
  name: "conv2"
  type: "Convolution"
  bottom: "pool1"
  top: "conv2"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  convolution_param {
    num_output: 24
    pad: 2
    kernel_size: 5
    stride: 1
    weight_filler {
      type: "gaussian"
      std: 0.01
    }
    bias_filler {
      type: "constant"
    }
  }
}
layer {
  name: "relu2"
  type: "ReLU"
  bottom: "conv2"
  top: "conv2"
}
layer {
  name: "pool2"
  type: "Pooling"
  bottom: "conv2"
  top: "pool2"
  pooling_param {
    pool: AVE
    kernel_size: 2
    stride: 2
  }
}
layer {
  name: "conv3"
  type: "Convolution"
  bottom: "pool2"
  top: "conv3"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  convolution_param {
    num_output: 50
    pad: 2
    kernel_size: 5
    stride: 1
    weight_filler {
      type: "gaussian"
      std: 0.01
    }
    bias_filler {
      type: "constant"
    }
  }
}
layer {
  name: "relu3"
  type: "ReLU"
  bottom: "conv3"
  top: "conv3"
}
layer {
  name: "pool3"
  type: "Pooling"
  bottom: "conv3"
  top: "pool3"
  pooling_param {
    pool: AVE
    kernel_size: 2
    stride: 2
  }
}
layer {
  name: "ip1"
  type: "InnerProduct"
  bottom: "pool3"
  top: "ip1"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  inner_product_param {
    num_output: 786
    weight_filler {
      type: "gaussian"
      std: 0.1
    }
    bias_filler {
      type: "constant"
    }
  }
}
layer {
  name: "ip2"
  type: "InnerProduct"
  bottom: "ip1"
  top: "ip2"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  inner_product_param {
    num_output: 2
    weight_filler {
      type: "gaussian"
      std: 0.1
    }
    bias_filler {
      type: "constant"
    }
  }
}
layer {
  name: "accuracy"
  type: "Accuracy"
  bottom: "ip2"
  bottom: "label"
  top: "accuracy"
  include {
    phase: TEST
  }
}
layer {
  name: "loss"
  type: "SoftmaxWithLoss"
  bottom: "ip2"
  bottom: "label"
  top: "loss"
}

修改方法如下:

(1)删掉有关于TEST层的信息。共有两个地方

第一个是在开头处:

layer {
  name: "cifar"
  type: "Data"
  top: "data"
  top: "label"
  include {
    phase: TEST
  }
  transform_param {
    mean_file: "C:/Users/Administrator/Desktop/data12345/mean.binaryproto"
  }
  data_param {
    source: "C:/Users/Administrator/Desktop/data12345/mtestldb"
    batch_size: 2
    backend: LEVELDB
  }
}

还有一处是结尾处,accuracy

layer {
  name: "accuracy"
  type: "Accuracy"
  bottom: "ip2"
  bottom: "label"
  top: "accuracy"
  include {
    phase: TEST
  }
}

(2)将开头Train层的信息修改:

原来的信息如下:

layer {
  name: "cifar"
  type: "Data"
  top: "data"
  top: "label"
  include {
    phase: TRAIN
  }
  transform_param {
    mean_file: "C:/Users/Administrator/Desktop/data12345/mean.binaryproto"
  }
  data_param {
    source: "C:/Users/Administrator/Desktop/data12345/mtrainldb"
    batch_size: 50
    backend: LEVELDB
  }
}

修改后如下:

layer {
  name: "data"
  type: "Input"
  top: "data"
  input_param { shape: { dim: 64 dim: 1 dim: 28 dim: 28 } }
}

其中
dim: 64 dim: 3 dim: 28 dim: 28
第一个代表了batch_size,第二个代表颜色通道数,像我就是彩色图片,所以是3,黑白是1,后面俩数分别代表图片的width和height。

(3)修改最后一层

原来是这样子的:

layer {
  name: "loss"
  type: "SoftmaxWithLoss"
  bottom: "ip2"
  bottom: "label"
  top: "loss"
}

现在修改如下:
layer {
  name: "prob"
  type: "Softmax"
  bottom: "fc8"
  top: "prob"
}

以上部分参考了:http://blog.youkuaiyun.com/redfivehit/article/details/72902418

<think>我们正在处理用户关于如何获取`face_deploy.prototxt`文件的问题。根据引用[4]中的信息,Caffe模型需要两个文件:`deploy.prototxt`和权重文件(例如`.caffemodel`)。用户特别询问了如何生成或获取`face_deploy.prototxt`。回顾引用[2]和[3],我们注意到在年龄和性别检测的代码中,使用了`.prototxt`文件作为模型的配置文件。例如:-`ageProto="/content/age_deploy.prototxt"`-`genderProto="/content/gender_deploy.prototxt"`同样,对于人脸检测,在引用[3]中使用了:-`faceProto="./model/opencv_face_detector.pbtxt"`(注意这里是`.pbtxt`,但用户询问的是`.prototxt`)不过,引用[4]提到Caffe模型需要`deploy.prototxt`和`.caffemodel`两个文件,而TensorFlow模型则需要`.pb`和`.pbtxt`两个文件。因此,`.prototxt`是Caffe模型的配置文件,而`.pbtxt`是TensorFlow模型的文本格式的配置文件。用户的问题是关于`face_deploy.prototxt`,这应该是用于人脸检测的Caffe模型的配置文件。那么,如何获取这个文件呢?1.**从预训练模型提供方获取**:通常,当我们使用一个预训练的Caffe模型时,发布者会同时提供`deploy.prototxt`文件和`.caffemodel`权重文件。例如,OpenCV提供了一些预训练的人脸检测模型,我们可以从OpenCV的GitHub仓库或官方网站下载。2.**自己编写**:如果用户有自定义的Caffe模型,那么需要自己编写`deploy.prototxt`文件。这需要了解Caffe的网络结构定义语法。3.**从训练过程中生成**:在训练Caffe模型时,我们会有一个`train.prototxt`(用于训练的网络结构)和一个`deploy.prototxt`(用于部署的网络结构)。通常,这两个文件结构相似,但输入和输出层可能不同。训练完成后,我们可以使用训练过程中定义的网络结构,然后根据部署需求调整(例如移除训练专用层)并保存为`deploy.prototxt`。根据引用[4]中的线索,我们可以尝试从OpenCV的GitHub仓库获取。因为引用[4]提到“我查到的很多帖子中都没有详细解释这些文件的来源,好在我找到了”,所以我们可以推测这些文件可能是OpenCV提供的预训练模型的一部分。具体步骤:-访问OpenCV的GitHub仓库:https://github.com/opencv/opencv-在`opencv`仓库中,预训练模型通常存放在`opencv_extra`仓库的`testdata`目录下,但直接搜索可能困难。-或者,我们可以从OpenCV的官方文档或源代码中寻找链接。实际上,OpenCV在GitHub上提供了一个名为`opencv_3rdparty`的仓库,其中包含了一些第三方贡献的模型,但人脸检测模型的配置文件可能直接包含在OpenCV的样本数据中。经过搜索,OpenCV提供的一个常用人脸检测模型是基于Caffe的,其配置文件为`deploy.prototxt`,权重文件为`res10_300x300_ssd_iter_140000.caffemodel`。我们可以从OpenCV的GitHub仓库中获取:-`deploy.prototxt`文件:https://github.com/opencv/opencv/tree/master/samples/dnn/face_detector在这个目录下,有`deploy.prototxt`和`deploy_2.prototxt`等文件。但是,请注意,用户提到的文件名是`face_deploy.prototxt`,而OpenCV提供的文件名为`deploy.prototxt`。实际上,文件名可以自定义,只要与代码中加载的路径一致即可。因此,我们可以直接使用OpenCV提供的`deploy.prototxt`。**下载方法:**1.直接访问OpenCV的GitHub仓库中对应的文件,然后点击"Raw"按钮,右键保存。2.使用wget或curl命令下载(在Linux环境下):```wgethttps://raw.githubusercontent.com/opencv/opencv/master/samples/dnn/face_detector/deploy.prototxt```如果用户需要的是其他特定模型的`face_deploy.prototxt`,可能需要从该模型的发布者处获取。**总结获取途径:**-如果是使用OpenCV的预训练人脸检测模型(如基于ResNet的SSD),则从OpenCV的GitHub仓库下载。-如果是其他模型(如年龄检测、性别检测),则从相应的模型发布者处下载。-对于自定义模型,需要自行编写。**注意**:在代码中加载时,需要确保路径正确,如引用[3]所示。因此,回答用户问题:用户可以通过下载OpenCV提供的预训练模型配置文件来获取`face_deploy.prototxt`(实际上OpenCV提供的文件名为`deploy.prototxt`,但用户可以在代码中将其重命名或直接使用该文件名)。如果用户需要的是其他特定模型的配置文件,则需要从该模型的发布源获取。最后,根据要求,生成相关问题。</think>### 如何获取用于人脸识别的 face_deploy.prototxt 文件 根据 OpenCV 的模型规范,`face_deploy.prototxt` 是 Caffe 模型的结构定义文件,通常与权重文件(`.caffemodel`)配合使用[^4]。以下是获取该文件的三种可靠方法: #### 1. **从 OpenCV 官方资源下载**(推荐) OpenCV 在 GitHub 提供了预训练模型文件: ```bash wget https://raw.githubusercontent.com/opencv/opencv/master/samples/dnn/face_detector/deploy.prototxt ``` 下载后重命名为 `face_deploy.prototxt` 即可[^4][^3]。这是 OpenCV 官方人脸检测模型的标准配置文件。 #### 2. **自定义生成(需 Caffe 环境)** 若需自定义网络结构: ```protobuf # 示例:基础人脸检测网络结构 name: "FaceNet" input: "data" input_shape { dim: 1 dim: 3 dim: 300 dim: 300 } layer { name: "conv1" type: "Convolution" bottom: "data" top: "conv1" convolution_param { num_output: 32 kernel_size: 3 stride: 1 } } # 添加更多层定义... ``` 保存为 `.prototxt` 文件后,需使用 Caffe 编译: ```bash caffe train --solver=your_solver.prototxt ``` 此方法适用于自定义模型,但需深度学习基础[^4]。 #### 3. **从预训练模型包提取** 部分开源项目(如 Age-Gender 检测模型)会提供完整文件包: ```python # 参考引用[3]中的模型加载方式 genderProto = "./model/deploy_gender.prototxt" genderModel = "./model/gender_net.caffemodel" ``` 可在以下资源站查找包含 `.prototxt` 的预训练包: - OpenCV Model Zoo: https://github.com/opencv/opencv_zoo - Caffe Model Gallery: https://caffe.berkeleyvision.org/model_zoo.html --- ### 注意事项 1. **文件匹配**:`.prototxt` 必须与对应的 `.caffemodel` 权重文件版本一致[^3] 2. **TensorFlow 替代方案**:若使用 TensorFlow 模型(如引用[3]中的 `opencv_face_detector.pbtxt`),无需 `.prototxt` 文件 3. **验证文件有效性**:通过 OpenCV 加载测试: ```python import cv2 net = cv2.dnn.readNet('face_deploy.prototxt', 'model.caffemodel') # 无报错即有效[^3] ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值