Caffe研究实践 二 ------准备数据 训练 测试
发布日期:2021-05-06 19:00:15 浏览次数:30 分类:精选文章

本文共 4230 字,大约阅读时间需要 14 分钟。

一、准备样本数据

获取minist的数据包。这个版本是四个数据包。

learning@learning-virtual-machine:~/caffe/data/mnist$ ./get_mnist.sh

下载过程如下:

Downloading...
--2016-05-11 17:35:10-- http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz
Resolving yann.lecun.com (yann.lecun.com)... 128.122.47.89
Connecting to yann.lecun.com (yann.lecun.com)|128.122.47.89|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 9912422 (9.5M) [application/x-gzip]
Saving to: ‘train-images-idx3-ubyte.gz’
train-images-idx 100%[===========>] 9.45M 225KB/s in 73s 2016-05-11 17:36:24 (133 KB/s) - ‘train-images-idx3-ubyte.gz’ saved [9912422/9912422]
--2016-05-11 17:36:38--  http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz
Resolving yann.lecun.com (yann.lecun.com)... 128.122.47.89
Connecting to yann.lecun.com (yann.lecun.com)|128.122.47.89|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 28881 (28K) [application/x-gzip]
Saving to: ‘train-labels-idx1-ubyte.gz’
train-labels-idx 100%[===========>] 28.20K 1.41KB/s in 7.5s 2016-05-11 17:36:49 (3.75 KB/s) - ‘train-labels-idx1-ubyte.gz’ saved [28881/28881]
--2016-05-11 17:36:49--  http://yann.lecun.com/exdb/mnist/t10k-images-idx3-ubyte.gz
Resolving yann.lecun.com (yann.lecun.com)... 128.122.47.89
Connecting to yann.lecun.com (yann.lecun.com)|128.122.47.89|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1648877 (1.6M) [application/x-gzip]
Saving to: ‘t10k-images-idx3-ubyte.gz’
t10k-images-idx3 100%[===========>] 1.57M 71.9KB/s in 19s 2016-05-11 17:37:08 (85.5 KB/s) - ‘t10k-images-idx3-ubyte.gz’ saved [1648877/1648877]
--2016-05-11 17:37:09--  http://yann.lecun.com/exdb/mnist/t10k-labels-idx1-ubyte.gz
Resolving yann.lecun.com (yann.lecun.com)... 128.122.47.89
Connecting to yann.lecun.com (yann.lecun.com)|128.122.47.89|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 4542 (4.4K) [application/x-gzip]
Saving to: ‘t10k-labels-idx1-ubyte.gz’
t10k-labels-idx1 100%[===========>] 4.44K --.-KB/s in 0s 2016-05-11 17:37:09 (31.5 MB/s) - ‘t10k-labels-idx1-ubyte.gz’ saved [4542/4542]

get_mnist.sh 代码

#!/usr/bin/env sh
# This scripts downloads the mnist data and unzips it.
DIR="$( cd "$(dirname "$0")" ; pwd -P)"
cd $DIR
echo "Downloading..."
for fname in train-images-idx3-ubyte train-labels-idx1-ubyte t10k-images-idx3-ubyte t10k-labels-idx1-ubyte
do
if [ ! -e $fname ]; then
wget --no-check-certificate http://yann.lecun.com/exdb/mnist/${fname}.gz
gunzip ${fname}.gz
fi
done

二、训练

learning@learning-virtual-machine:~/caffe$ ./examples/mnist/train_lenet.sh

出现问题:

I0511 17:52:25.115056 63914 caffe.cpp:185] Using GPUs 0
F0511 17:52:25.116345 63914 common.cpp:66] Cannot use GPU in CPU-only Caffe: check mode.

解决方案:

  • 修改 lenet_solver.prototxt 文件
  • learning@learning-virtual-machine:~/caffe/examples/mnist$ sudo gedit lenet_solver.prototxt

    在文件中添加以下内容:

    # 检查是否有GPU可用
    device: GPU
    1. 运行训练脚本
    2. learning@learning-virtual-machine:~/caffe/examples/mnist$ ./train_lenet.sh

      如果问题依然存在,请检查以下内容:

      • CUDA 驱动是否正确安装
      • CUDA 库文件是否存在
      • 重新启动系统

      三、测试

      learning@learning-virtual-machine:~/caffe$ ./build/tools/caffe.bin test -model=examples/mnist/lenet_train_test.prototxt -weights=examples/mnist/lenet_iter_10000.caffemodel
      I0512 09:18:41.455747  3503 caffe.cpp:275] Batch 44, loss = 0.0137619
      I0512 09:18:41.671058 3503 caffe.cpp:275] Batch 45, accuracy = 0.99
      I0512 09:18:41.671362 3503 caffe.cpp:275] Batch 45, loss = 0.0446652
      I0512 09:18:41.910468 3503 caffe.cpp:275] Batch 46, accuracy = 1
      I0512 09:18:41.910781 3503 caffe.cpp:275] Batch 46, loss = 0.00462838
      I0512 09:18:42:082020 3503 caffe.cpp:275] Batch 47, accuracy = 0.99
      I0512 09:18:42:082260 3503 caffe.cpp:275] Batch 47, loss = 0.0215265
      I0512 09:18:42:297307 3503 caffe.cpp:275] Batch 48, accuracy = 0.96
      I0512 09:18:42:301200 3503 caffe.cpp:275] Batch 48, loss = 0.0964929
      I0512 09:18:42:576354 3503 caffe.cpp:275] Batch 49, accuracy = 1
      I0512 09:18:42:576627 3503 caffe.cpp:275] Batch 49, loss = 0.00345927
      I0512 09:18:42:576843 3503 caffe.cpp:280] Loss: 0.0427004
      I0512 09:18:42:576843 3503 caffe.cpp:292] accuracy = 0.9872
      I0512 09:18:42:576954 3503 caffe.cpp:292] loss = 0.0427004 (* 1 = 0.0427004 loss)

      参考资料

    上一篇:Caffe研究实践 四 -------lenet_train_test.prototxt分析
    下一篇:Ubuntu安装Opencv

    发表评论

    最新留言

    表示我来过!
    [***.240.166.169]2025年04月15日 23时35分56秒