如何在python中加速caffe分类器

詹姆

我正在使用python使用caffe分类器。我从相机中获取了图像,并从训练集中预测了图像。它工作正常,但问题是速度非常慢。我认为只有4帧/秒。您能建议我一些方法来改善代码中的计算时间吗?该问题可以解释如下。我必须age_net.caffemodel通过以下代码重新加载大小约为80MB的网络模型

age_net_pretrained='./age_net.caffemodel'
age_net_model_file='./deploy_age.prototxt'
age_net = caffe.Classifier(age_net_model_file, age_net_pretrained,
           mean=mean,
           channel_swap=(2,1,0),
           raw_scale=255,
           image_dims=(256, 256))

对于每个输入图像(caffe_input),我都调用了预测函数

prediction = age_net.predict([caffe_input])

我认为由于网络规模很大。然后,预测功能需要很长时间才能预测图像。我认为这是缓慢的时间。
这是我的完整参考代码。它被我改变了。

from conv_net import *

import matplotlib.pyplot as plt
import numpy as np
import cv2
import glob
import os
caffe_root = './caffe' 
import sys
sys.path.insert(0, caffe_root + 'python')
import caffe
DATA_PATH = './face/'
cnn_params = './params/gender_5x5_5_5x5_10.param'
face_params = './params/haarcascade_frontalface_alt.xml'
def format_frame(frame):
    img = frame.astype(np.float32)/255.
    img = img[...,::-1]
    return img   

if __name__ == '__main__':    
    files = glob.glob(os.path.join(DATA_PATH, '*.*'))

    # This is the configuration of the full convolutional part of the CNN
    # `d` is a list of dicts, where each dict represents a convolution-maxpooling
    # layer. 
    # Eg c1 - first layer, convolution window size
    # p1 - first layer pooling window size
    # f_in1 - first layer no. of input feature arrays
    # f_out1 - first layer no. of output feature arrays
    d = [{'c1':(5,5),
          'p1':(2,2),
          'f_in1':1, 'f_out1':5},
         {'c2':(5,5),
          'p2':(2,2),
          'f_in2':5, 'f_out2':10}]

    # This is the configuration of the mlp part of the CNN
    # first tuple has the fan_in and fan_out of the input layer
    # of the mlp and so on.
    nnet =  [(800,256),(256,2)]    
    c = ConvNet(d,nnet, (45,45))
    c.load_params(cnn_params)        
    face_cascade = cv2.CascadeClassifier(face_params)
    cap = cv2.VideoCapture(0)
    cv2.namedWindow("Image", cv2.WINDOW_NORMAL)

    plt.rcParams['figure.figsize'] = (10, 10)
    plt.rcParams['image.interpolation'] = 'nearest'
    plt.rcParams['image.cmap'] = 'gray'
    mean_filename='./mean.binaryproto'
    proto_data = open(mean_filename, "rb").read()
    a = caffe.io.caffe_pb2.BlobProto.FromString(proto_data)
    mean  = caffe.io.blobproto_to_array(a)[0]
    age_net_pretrained='./age_net.caffemodel'
    age_net_model_file='./deploy_age.prototxt'
    age_net = caffe.Classifier(age_net_model_file, age_net_pretrained,
               mean=mean,
               channel_swap=(2,1,0),
               raw_scale=255,
               image_dims=(256, 256))
    age_list=['(0, 2)','(4, 6)','(8, 12)','(15, 20)','(25, 32)','(38, 43)','(48, 53)','(60, 100)']
    while(True):

        val, image = cap.read()        
        if image is None:
            break
        image = cv2.resize(image, (320,240))
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        faces = face_cascade.detectMultiScale(gray, 1.3, 5, minSize=(30,30))

        for f in faces:
            x,y,w,h = f
            cv2.rectangle(image, (x,y), (x+w,y+h), (0,255,255))            
            face_image_rgb = image[y:y+h, x:x+w]
            caffe_input = cv2.resize(face_image_rgb, (256, 256)).astype(np.float32)
            prediction = age_net.predict([caffe_input]) 
            print 'predicted age:', age_list[prediction[0].argmax()]       
        cv2.imshow('Image', image)
        ch = 0xFF & cv2.waitKey(1)
        if ch == 27:
            break
        #break
hai

尝试调用age_net.predict([caffe_input])oversmaple=False

prediction = age_net.predict([caffe_input], oversample=False)

的默认行为predict是创建10个略有不同的输入图像裁切并将其馈送到网络以进行分类,通过禁用此选项,您应该获得x10加速。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在 Python 中加速 for 循环的执行?

来自分类Dev

如何在python中加速多个内部产品

来自分类Dev

如何在python中加速数组分配?

来自分类Dev

如何在python中加速numpy数组的乘积

来自分类Dev

如何在 Python 中加速斯坦福 NLP?

来自分类Dev

如何在Servlet中加载分类器(Weka)

来自分类Dev

如何在Servlet中加载分类器(Weka)

来自分类Dev

如何在StanfordCoreNLP中加载特定分类器

来自分类Dev

如何在Matlab中加速双循环

来自分类Dev

如何在 Python 中加速慢 (.6s) 掩码分配?

来自分类Dev

如何在SymPy中加速慢矩阵乘法?

来自分类Dev

如何在R中加速此简单功能

来自分类Dev

如何在Matlab中加速此三重循环

来自分类Dev

如何在CABasicAnimation中加速/减慢按钮的旋转

来自分类Dev

如何在R中加速类似循环的函数

来自分类Dev

如何在R中加速CSV文件读取

来自分类Dev

如何在 Rails 中加速 find_or_initialize?

来自分类Dev

如何在python中从sklearn训练多次SVM分类器?

来自分类Dev

Python - 如何在网页中使用 sklearn 分类器

来自分类Dev

如何在ssd-caffe中加载图像而不是LMDB

来自分类Dev

分类器是如何分类的?

来自分类Dev

为什么Apache / WSGI将HEAD映射到GET?如何在Flask中加速HEAD?

来自分类Dev

如何在Emacs中加速自定义模式行面部更改功能

来自分类Dev

如何在R中加速此正则表达式?

来自分类Dev

如何在tensorflow_datasets中加速将张量转换为numpy数组的代码?

来自分类Dev

如何在Google cloud-ndb库中加速或执行上下文管理?

来自分类Dev

如何在R中加速此正则表达式?

来自分类Dev

Caffe:概率输出大于 1 的分类器

来自分类Dev

如何在Python中对数字进行分类和分类?

Related 相关文章

热门标签

归档