Caffe Windows系列(4): 中文识别初探(续)

0. 预备

上一篇文章中,我们测试了100个中文字符使用Lenet5模型的正确率,其正确率达到了100%。(不达到100%就有问题了,我的训练集和测试集是极其近似的,仅仅是为了自己练手,而并未达到实际用途。) 接下来要做的事情是,用C++ API读取一个图像,并对其进行识别。在《Caffe Windows系列(2): 使用C++ API进行分类》一文中,谈到了如果要能够进行识别,需要准备五个文件。

  1. deploy.prototxt:这个文件目前还没有。
  2. network.caffemodel:这个文件已经有了,即lenet_iter_10000.caffemodel
  3. mean.binaryproto:这个文件还没有。
  4. labels.txt:这个文件就是一个分类文件。
  5. img.jpg:要测试的图像

我的labels.txt文件类似于如下的列表

丁
万
严
于
任
何
余
侯
傅
冯
刘
卢
史
叶
向
吕
吴

...
马
高
魏
黄
黎
龙
龚

1. deploy.prototxt

在mnist的例子中,lenet.prototxt就相当于deploy.txt。它与真正用于训练的lenet_train_test.prototxt有一些差别,但好在差别也不大。

  • 差别1:输入层。lenet_train_test.prototxt的输入是lmdb文件。而deploy.txt中是没有使用lmdb文件的。因此,lenet.prototxt将输入层简化为:
layer {
  name: "data"
  type: "Input"
  top: "data"
  input_param { shape: { dim: 64 dim: 1 dim: 28 dim: 28 } }
}

不过我认为,使用中,shape的第一个dim应当写为1才对。因为64是batch_size。而我们测试的时候,是一个一个测试的,因此,可以设置为1。

  • 差别2:作为TEST阶段的accuracy层可以不再需要了。
  • 差别3:loss层变为prob层。因为不需要进行损失函数的计算了。
    loss层:
layer {
  name: "loss"
  type: "SoftmaxWithLoss"
  bottom: "ip2"
  bottom: "label"
  top: "loss"
}

prob层:

layer {
  name: "prob"
  type: "Softmax"
  bottom: "ip2"
  top: "prob"
}

依样画葫芦,我们就可以写出自己的deploy.prototxt文件来。

name: "LeNet"
layer {
  name: "data"
  type: "Input"
  top: "data"
  input_param { shape: { dim: 1 dim: 1 dim: 40 dim: 40 } }
}
layer {
  name: "conv1"
  type: "Convolution"
  bottom: "data"
  top: "conv1"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  convolution_param {
    num_output: 20
    kernel_size: 5
    stride: 1
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
    }
  }
}
layer {
  name: "pool1"
  type: "Pooling"
  bottom: "conv1"
  top: "pool1"
  pooling_param {
    pool: MAX
    kernel_size: 2
    stride: 2
  }
}
layer {
  name: "conv2"
  type: "Convolution"
  bottom: "pool1"
  top: "conv2"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  convolution_param {
    num_output: 50
    kernel_size: 5
    stride: 1
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
    }
  }
}
layer {
  name: "pool2"
  type: "Pooling"
  bottom: "conv2"
  top: "pool2"
  pooling_param {
    pool: MAX
    kernel_size: 2
    stride: 2
  }
}
layer {
  name: "ip1"
  type: "InnerProduct"
  bottom: "pool2"
  top: "ip1"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  inner_product_param {
    num_output: 1000
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
    }
  }
}
layer {
  name: "relu1"
  type: "ReLU"
  bottom: "ip1"
  top: "ip1"
}
layer {
  name: "ip2"
  type: "InnerProduct"
  bottom: "ip1"
  top: "ip2"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  inner_product_param {
    num_output: 100
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
    }
  }
}
layer {
  name: "prob"
  type: "Softmax"
  bottom: "ip2"
  top: "prob"
}

2. 绕开均值文件

由于Lenet-5的训练并未使用到均值文件,因此,可以生成一个全零的均值文件来代替。如果使用python接口,生成全零的均值文件比较方便,网上有很多文章。但如果使用C++接口,它使用的均值文件是binaryproto后缀的,不是直接可视化的那种,因此,生成全零的均值文件并不是那么容易的事。相比之下,可能在cpp_classification代码的基础上进行修改,从而绕过这个均值文件会更容易一些。

之前对于mean_file的处理,主要是SetMean这个函数:

/* Load the mean file in binaryproto format. */
void Classifier::SetMean(const string& mean_file) {
  BlobProto blob_proto;
  ReadProtoFromBinaryFileOrDie(mean_file.c_str(), &blob_proto);
 
  /* Convert from BlobProto to Blob<float> */
  Blob<float> mean_blob;
  mean_blob.FromProto(blob_proto);
  CHECK_EQ(mean_blob.channels(), num_channels_)
    << "Number of channels of mean file doesn't match input layer.";
 
  /* The format of the mean file is planar 32-bit float BGR or grayscale. */
  std::vector<cv::Mat> channels;
  float* data = mean_blob.mutable_cpu_data();
  for (int i = 0; i < num_channels_; ++i) {
    /* Extract an individual channel. */
    cv::Mat channel(mean_blob.height(), mean_blob.width(), CV_32FC1, data);
    channels.push_back(channel);
    data += mean_blob.height() * mean_blob.width();
  }
 
  /* Merge the separate channels into a single image. */
  cv::Mat mean;
  cv::merge(channels, mean);
 
  /* Compute the global mean pixel value and create a mean image
   * filled with this value. */
  cv::Scalar channel_mean = cv::mean(mean);
  mean_ = cv::Mat(input_geometry_, mean.type(), channel_mean);
}

现在,我将SetMean函数改为:

void Classifier::SetMean(const string& mean_file) {
    mean_ = cv::Mat::zeros(input_geometry_, CV_32F);
}

这样的话,不管输入的mean_file是啥,我都会让mean_成为一个全零矩阵。

3. 正式测试

将examples\classification设为启动项,配置调试参数:

《Caffe Windows系列(4): 中文识别初探(续)》 配置调试参数

命令参数为:

deploy.prototxt lenet_iter_10000.caffemodel mean.binaryproto labels.txt test.bmp

test.bmp为:

《Caffe Windows系列(4): 中文识别初探(续)》 测试图像

运行后的结果为:

---------- Prediction for test.bmp ----------
1.0000 - "潘"
0.0000 - "万"
0.0000 - "于"
0.0000 - "任"
0.0000 - "丁"

也就是说,这个图,100%像“潘”字,而0%像后面的“万”、“于”、“任”、“丁”。这个结果是一个非常不错的鼓励。有理由相信,CNN完全可以胜任印刷体汉字的识别。接下来,我会去尝试身份证信息的识别~

    原文作者:行之与亦安
    原文地址: https://www.jianshu.com/p/7b22180263f9
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞