基于卷积神经网络的物体分类识别(三)---模型微调和优化

版权声明:学无止境,好好学习 https://blog.csdn.net/m0_38116269/article/details/88371477

1.学会使用.caffemodel and .solverstate进行恢复训练

参考博客:https://blog.csdn.net/yh1226/article/details/85772198
博客中介绍了两种方法,一种是调用.solverstate文件,另一种是调用.caffemodel文件,这种事完全按照新的solver.prototxt方案进行训练,权重就是借用之前的权重。都差不多吧,目前。目前知道的,这两个模型文件的作用,断点续训,模型微调,和做测试用这三总用途。

2.学会利用训练好的模型对实际图片进行测试,要循环使用,进一步要读取视频帧进行测试。

参考博客:caffe—测试模型分类结果并输出(python )
生成deploy文件用于测试,其实这个就是建议你手动修改,主要就是把train_test.prototxt文件头和尾增删一部分就可以了。
参考博客:https://www.cnblogs.com/k7k8k91/p/7806232.html

3.学习看看能不能输出测试集预测的标签

做统计看看那些种类那些图片容易出现误判,进而优化训练集提高学习效率。

4.Code

# -*- coding: utf-8 -*

import numpy as np
import matplotlib.pyplot as plt

import sys
caffe_root = '/media/xxxxx/Linux-doc/xxx/caffe/'
sys.path.insert(0, caffe_root + 'python')
import caffe

import os
if os.path.isfile(caffe_root + 'models/animal_classifier/the_resnet_18_iter_10000.caffemodel'):
    print 'CaffeNet found.'
else:
    print 'Not found'

deploy = caffe_root + 'models/animal_classifier/deploy.prototxt'
caffemodel = caffe_root + 'models/animal_classifier/the_resnet_18_iter_10000.caffemodel'
caffe.set_mode_gpu()
net = caffe.Net(deploy,      # defines the structure of the model
                caffemodel,  # contains the trained weights
                caffe.TEST)     # use test mode (e.g., don't perform dropout)

mu = np.load(caffe_root + 'data/animal_picture/animal_mean.npy')
mu = mu.mean(1).mean(1)  # average over pixels to obtain the mean (BGR) pixel values
print 'mean-subtracted values:', zip('BGR', mu)


# create transformer for the input called 'data'
transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})

transformer.set_transpose('data', (2,0,1))  # move image channels to outermost dimension
transformer.set_mean('data', mu)            # subtract the dataset-mean value in each channel
transformer.set_raw_scale('data', 255)      # rescale from [0, 1] to [0, 255]
transformer.set_channel_swap('data', (2,1,0))  # swap channels from RGB to BGR

net.blobs['data'].reshape(10,        # batch size
                          3,         # 3-channel (BGR) images
                          224, 224)  # image size is 227x227


dir = caffe_root + 'data/animal_picture/test/'
filelist = []
filenames= os.listdir(dir)
# for fn in filenames:
#     fullfilename = os.path.join(dir, fn)
#     filelist.append(fullfilename)


labels_filename = caffe_root + 'data/animal_picture/labels.txt' # 类别名称文件,将数字标签转换回类别名称
labels = np.loadtxt(labels_filename, str, delimiter='\n')  # 读取类别名称文件
# print(labels[0])
#
for img in range(len(filenames)):
    image = caffe.io.load_image(dir + filenames[img])
    transformed_image = transformer.preprocess('data', image)
    # copy the image data into the memory allocated for the net
    net.blobs['data'].data[...] = transformed_image
    ### perform classification
    output = net.forward()

    output_prob = output['prob'][0]  # the output probability vector for the first image in the batch
    print 'the', filenames[img], 'predicted class is:', labels[output_prob.argmax()]
    f = file(caffe_root + 'data/animal_picture/predictedlabel.txt', "a+")
    f.writelines([str(filenames[img]), " ", str(labels[output_prob.argmax()]), "\n"])

f.close()

##read the txt

Result show:
在这里插入图片描述

做的时候可以参考:caffe中python接口的使用

猜你喜欢

转载自blog.csdn.net/m0_38116269/article/details/88371477
今日推荐