computer vision - How to speed up caffe classifer in python -
i using python use caffe classifier. got image camera , peform predict image training set. work problem speed slow. thinks 4 frames/second. suggest me way improve computational time in code? problem can explained following. have reload network model age_net.caffemodel
size 80mb following code
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))
and each input image (caffe_input
), call predict function
prediction = age_net.predict([caffe_input])
i think due size of network large. predict function takes long time predict image. think slow time it.
full reference code. changed me.
from conv_net import * import matplotlib.pyplot plt import numpy 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, '*.*')) # configuration of full convolutional part of cnn # `d` list of dicts, each dict represents 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}] # configuration of mlp part of cnn # first tuple has fan_in , fan_out of input layer # of mlp , 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() = 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 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)) 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
try calling age_net.predict([caffe_input])
oversmaple=false
:
prediction = age_net.predict([caffe_input], oversample=false)
the default behavior of predict
create 10, different, crops of input image , feed them network classify, disabling option should x10 speedup.
Comments
Post a Comment