日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

运行caffe识别数字的模型mnist

發布時間:2024/9/5 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 运行caffe识别数字的模型mnist 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

<!---title:運行caffe識別數字的模型mnist-->
<!---keywords:填寫關鍵字, 以半角逗號分割-->
原始引用地址: ?[運行caffe識別數字的模型mnist](http://yangkuncn.cn/caffe_mnist.html) ?
time: ?2020.5.17 23:28

## mnist是啥?

mnist是一個運用神經網絡識別數字的模型,可以識別數字0到9.

### 獲取mnist模塊與數據

對于mnist(就是數字識別)例子,參考以下地址,獲取數據訓練相關數據:

<http://caffe.berkeleyvision.org/gathered/examples/mnist.html>

準備數據:

```
cd $CAFFE_ROOT?
./data/mnist/get_mnist.sh?
./examples/mnist/create_mnist.sh
```

### 運用多核,加速訓練

caffe在純CPU模式下,使用多核運行

<https://blog.csdn.net/b876144622/article/details/80009877>

1. sudo apt-get install -y libopenblas-dev

2、修改caffe目錄下的Makefile.config文件,將BLAS: =atlas修改為BLAS: =open

3、再編譯caffe,首先make clean,清除之前的編譯結果,再依次執行make all -j16, make test -j16, make runtest -j16,編譯caffe。-j16是指用16個核并行編譯caffe,可以大大加快編譯速度。

4、編譯完成后,執行訓練前,需要export OPENBLAS_NUM_THREADS=4, 即使用4個核進行訓練

### 開始訓練

因為編譯時,選擇使用cpu,使用要更改文件:

examples/mnist/lenet_solver.prototxt

把:solver_mode: GPU ? ? ?改為:solver_mode: CPU?

訓練命令:

```
export PYTHONPATH=/home/xy/works/caffe/python/

time ./examples/mnist/train_lenet.sh ? #vm用時17m36s ?j1900 42m55

```

### 進行圖片識別

#### 用軟件手寫一個圖片

(注意:識別的圖片,**一定要是黑底, 數字用白色寫**)

安裝畫圖軟件gimp:

```
sudo apt-get install gimp
```

gimp 創建27*27的bmp圖:

1. 新建file -> new 選width 27 ?height27 單位為像素, 底色選黑

2. 畫圖:選畫筆,左邊size ,可以選2,size太大,27*27的畫布不夠畫的。

3. 導出:file->export as, 格式先jpg,?

#### 識別手寫的圖片

在運行python/calssify前要運行安裝 protobuf:

```sudo ?pip install protobuf```

在識別圖片前,需要對classify進行更改:

```
git diff python/classify.py
diff --git a/python/classify.py b/python/classify.py
index 4544c51..446c55e 100755
--- a/python/classify.py
+++ b/python/classify.py
@@ -105,9 +105,9 @@ def main(argv):
?
? ? ?# Make classifier.
? ? ?classifier = caffe.Classifier(args.model_def, args.pretrained_model,
- ? ? ? ? ? ?image_dims=image_dims, mean=mean,
- ? ? ? ? ? ?input_scale=args.input_scale, raw_scale=args.raw_scale,
- ? ? ? ? ? ?channel_swap=channel_swap)
+ ? ? ? ? ? ?image_dims=None, mean=None,
+ ? ? ? ? ? ?input_scale=None, raw_scale=None,
+ ? ? ? ? ? ?channel_swap=None)
?
? ? ?# Load numpy array (.npy), directory glob (*.jpg), or image file.
? ? ?args.input_file = os.path.expanduser(args.input_file)
@@ -116,11 +116,11 @@ def main(argv):
? ? ? ? ?inputs = np.load(args.input_file)
? ? ?elif os.path.isdir(args.input_file):
? ? ? ? ?print("Loading folder: %s" % args.input_file)
- ? ? ? ?inputs =[caffe.io.load_image(im_f)
+ ? ? ? ?inputs =[caffe.io.load_image(im_f, False)
? ? ? ? ? ? ? ? ? for im_f in glob.glob(args.input_file + '/*.' + args.ext)]
? ? ?else:
? ? ? ? ?print("Loading file: %s" % args.input_file)
- ? ? ? ?inputs = [caffe.io.load_image(args.input_file)]
+ ? ? ? ?inputs = [caffe.io.load_image(args.input_file, False)]
?
? ? ?print("Classifying %d inputs." % len(inputs))
?
@@ -131,6 +131,7 @@ def main(argv):
?
? ? ?# Save
? ? ?print("Saving results into %s" % args.output_file)
+ ? ?print(predictions)
? ? ?np.save(args.output_file, predictions)

git diff ?python/caffe/classifier.py
diff --git a/python/caffe/classifier.py b/python/caffe/classifier.py
index 64d804be..65b0d881 100644
--- a/python/caffe/classifier.py
+++ b/python/caffe/classifier.py
@@ -69,18 +69,18 @@ class Classifier(caffe.Net):
? ? ? ? ?for ix, in_ in enumerate(inputs):
? ? ? ? ? ? ?input_[ix] = caffe.io.resize_image(in_, self.image_dims)
?
- ? ? ? ?if oversample:
- ? ? ? ? ? ?# Generate center, corner, and mirrored crops.
- ? ? ? ? ? ?input_ = caffe.io.oversample(input_, self.crop_dims)
- ? ? ? ?else:
- ? ? ? ? ? ?# Take center crop.
- ? ? ? ? ? ?center = np.array(self.image_dims) / 2.0
- ? ? ? ? ? ?crop = np.tile(center, (1, 2))[0] + np.concatenate([
- ? ? ? ? ? ? ? ?-self.crop_dims / 2.0,
- ? ? ? ? ? ? ? ?self.crop_dims / 2.0
- ? ? ? ? ? ?])
- ? ? ? ? ? ?crop = crop.astype(int)
- ? ? ? ? ? ?input_ = input_[:, crop[0]:crop[2], crop[1]:crop[3], :]
+ ? ? ? ?#if oversample:
+ ? ? ? ?# ? ?# Generate center, corner, and mirrored crops.
+ ? ? ? ?# ? ?input_ = caffe.io.oversample(input_, self.crop_dims)
+ ? ? ? ?#else:
+ ? ? ? ?# ? ?# Take center crop.
+ ? ? ? ?# ? ?center = np.array(self.image_dims) / 2.0
+ ? ? ? ?# ? ?crop = np.tile(center, (1, 2))[0] + np.concatenate([
+ ? ? ? ?# ? ? ? ?-self.crop_dims / 2.0,
+ ? ? ? ?# ? ? ? ?self.crop_dims / 2.0
+ ? ? ? ?# ? ?])
+ ? ? ? ?# ? ?crop = crop.astype(int)
+ ? ? ? ?# ? ?input_ = input_[:, crop[0]:crop[2], crop[1]:crop[3], :]
?
? ? ? ? ?# Classify
? ? ? ? ?caffe_in = np.zeros(np.array(input_.shape)[[0, 3, 1, 2]],
@@ -91,8 +91,8 @@ class Classifier(caffe.Net):
? ? ? ? ?predictions = out[self.outputs[0]]
?
? ? ? ? ?# For oversampling, average predictions across crops.
- ? ? ? ?if oversample:
- ? ? ? ? ? ?predictions = predictions.reshape((len(predictions) // 10, 10, -1))
- ? ? ? ? ? ?predictions = predictions.mean(1)
+ ? ? ? ?#if oversample:
+ ? ? ? ?# ? ?predictions = predictions.reshape((len(predictions) // 10, 10, -1))
+ ? ? ? ?# ? ?predictions = predictions.mean(1)
?
? ? ? ? ?return predictions
```

最后,真正到運行命令的時候了:

```
使用命令計算圖片:
python python/classify.py ? ?--model_def examples/mnist/lenet.prototxt ? --pretrained_model examples/mnist/lenet_iter_10000.caffemodel ? --center_only ? ? ?--images_dim 28,28 /home/user/Desktop/2.jpg ?FOO
```

更改上在py程序后,會輸出以下內容 :

```
Saving results into FOO
[[2.63039285e-10 1.69570372e-10 1.00000000e+00 3.37297190e-10
? 1.04435086e-16 6.86246951e-15 1.50223258e-14 4.68932055e-12
? 6.54263449e-11 1.28875165e-14]]

```

由于輸入的是2.jpg,所以第2個位置(從0開始)的概率最大,幾乎是1.可以分別手寫0到9圖片,進行測試。

我分別在vm(i53230), j1900(真機), i737**m(真機)進行測試,cpu運行過,識別率還可以,速度感覺都比較慢在1s以上吧。

[首頁](http://yangkuncn.cn/index.html)

總結

以上是生活随笔為你收集整理的运行caffe识别数字的模型mnist的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。