机器学习-学习笔记 MNIST Fine-tuning(一)

来源:互联网 发布:淘宝花卉店铺推荐 编辑:程序博客网 时间:2024/05/16 03:45

Fine-tuning(Finetune|微调)

很多时候,我们的电脑跑不了那么多的数据,就可以去下载别人已经训练好的网络进行第二次训练,达到准确率更高,或者增加一个新的类别,让我们一步一步来。

在原有数据集上进行微调

我们之前训练好了mnist,我们可以修改lenet_solver.prototxt如下

# The train/test net protocol buffer definitionnet: "examples/mnist/lenet_train_test.prototxt"# test_iter specifies how many forward passes the test should carry out.# In the case of MNIST, we have test batch size 100 and 100 test iterations,# covering the full 10,000 testing images.test_iter: 10 # 测试时迭代几次# Carry out testing every 500 training iterations.test_interval: 500 # 每迭代多少次进行测试# The base learning rate, momentum and the weight decay of the network.base_lr: 0.001 #这里是学习速率,调小momentum: 0.9weight_decay: 0.0005# The learning rate policylr_policy: "inv"gamma: 0.0001power: 0.75# Display every 100 iterationsdisplay: 100# The maximum number of iterationsmax_iter: 2000 # 最大迭代次数# snapshot intermediate resultssnapshot: 5000 # 这是快照,即5000次迭代进行一次快照snapshot_prefix: "examples/mnist/lenet" # 快照前面的名字# solver mode: CPU or GPUsolver_mode: CPU

我们这里主要是将学习速率调低,使其更加精确,迭代次数减少(因为也不用那么多次了- -)。

sudo ./build/tools/caffe train --solver ./examples/mnist/lenet_solver.prototxt --weights ./examples/mnist/lenet_iter_10000.caffemodel 
  • 微调前
    这里写图片描述
  • 微调后
    这里写图片描述

增加了0.1%- -,在试试看将学习策略改成step。

继续修改lenet_solver.prototxt文件如下

# The train/test net protocol buffer definitionnet: "examples/mnist/lenet_train_test.prototxt"# test_iter specifies how many forward passes the test should carry out.# In the case of MNIST, we have test batch size 100 and 100 test iterations,# covering the full 10,000 testing images.test_iter: 100# Carry out testing every 500 training iterations.test_interval: 500# The base learning rate, momentum and the weight decay of the network.base_lr: 0.001momentum: 0.9weight_decay: 0.0005# The learning rate policylr_policy: "step"gamma: 0.0001stepsize: 100# Display every 100 iterationsdisplay: 100# The maximum number of iterationsmax_iter: 2000# snapshot intermediate resultssnapshot: 5000snapshot_prefix: "examples/mnist/lenet"# solver mode: CPU or GPUsolver_mode: CPU             
sudo ./build/tools/caffe.bin train -solver examples/mnist/lenet_solver.prototxt -weights examples/mnist/lenet_iter_2000.caffemodel

这里写图片描述

可以看到有进步了- -而且训练时间非常短。

我们试试看将数字0123456剔除,只训练789,接着用789的模型,训练0,看看能不能成功。

我们之前提取了mnist的图片,这次我们根据给出的标签,来进行分类。

首先需要创建文件夹

mkdir mnist_testmkdir ./mnist_test/0mkdir ./mnist_test/1mkdir ./mnist_test/2mkdir ./mnist_test/3mkdir ./mnist_test/4mkdir ./mnist_test/5mkdir ./mnist_test/6mkdir ./mnist_test/7mkdir ./mnist_test/8mkdir ./mnist_test/9

这里写图片描述

import numpy as npimport structimport matplotlib.pyplot as pyplotimport Imagedef unzip(fileName, labelName):    binfile = open(fileName, 'rb')    buf = binfile.read()    binfileOfLabel = open(labelName, 'rb')    bufOfLabel = binfileOfLabel.read()    index = 0    index2 = 0    magic, numImages, numRows, numColumns = struct.unpack_from(        '>IIII', buf, index)    index += struct.calcsize('>IIII')    index2 += struct.calcsize('>II')    n = [0 for x in range(0, 10)];    for image in range(0, numImages):        im = struct.unpack_from('>784B', buf, index)        label = struct.unpack_from('>1B', bufOfLabel, index2)        index += struct.calcsize('>784B')        index2 += struct.calcsize('>1B')        im = np.array(im, dtype='uint8')        im = im.reshape(28, 28)        im = Image.fromarray(im)        im.save('mnist_test/%d/train_%s.bmp' % (label[0], n[label[0]]), 'bmp')        n[label[0]] += 1unzip('./caffe/data/mnist/t10k-images-idx3-ubyte', './caffe/data/mnist/t10k-labels-idx1-ubyte')

这里写图片描述

好了,把数字分割开来以后,我们需要将要测试数据随机存入mdb

原创粉丝点击