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設為啟動項,配置調試參數:

配置調試參數

命令參數為:

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

test.bmp為:

測試圖像

運行后的結果為:

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

也就是說,這個圖,100%像“潘”字,而0%像后面的“萬”、“于”、“任”、“丁”。這個結果是一個非常不錯的鼓勵。有理由相信,CNN完全可以勝任印刷體漢字的識別。接下來,我會去嘗試身份證信息的識別~

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容