标签:图片 top tps 一个 ima main file div ESS
分析一套源代码的代码规范和风格并讨论如何改进优化代码
结合工程实践选题相关的一套源代码,根据其编程语言或项目特点,分析其在源代码目录结构、文件名/类名/函数名/变量名等命名、接口定义规范和单元测试组织形式等方面的做法和特点;
使用的代码为手写汉字识别的代码
https://github.com/chongyangtao/DeepHCCR
目录结构为:
images包括汉字图片
meanfiles包括主要的文件
models包括google的lenet模型
util包括各种文本文件,和两种网络的准确率图
目录结构清晰明了,文件名符合命名规范,容易使人知道各个文件的作用。
部分代码如下
#coding=utf-8 import numpy as np import pickle import os import time import sys import shutil import skimage caffe_root = ‘/home/cscl/caffe-master/‘ sys.path.insert(0, caffe_root + ‘python‘) import caffe net_file = ‘googlenet_deploy.prototxt‘ caffe_model = ‘models/googlenet_hccr.caffemodel‘ mean_file = ‘meanfiles/CASIA1.0_1.1_1.2_mean_112.npy‘ unicode_index = np.loadtxt(‘util/unicode_index.txt‘, delimiter = ‘,‘,dtype = np.int) #7534 net = caffe.Net(net_file,caffe_model,caffe.TEST) def get_crop_image(imagepath, img_name): img=skimage.io.imread(imagepath + img_name,as_grey=True) black_index = np.where(img < 255 ) min_x = min(black_index[0]) max_x = max(black_index[0]) min_y = min(black_index[1]) max_y = max(black_index[1]) #print(min_x,max_x,min_y,max_y) image = caffe.io.load_image(imagepath+"//"+img_name) return image[min_x:max_x, min_y:max_y,:] def evaluate(imagepath, top_k): transformer = caffe.io.Transformer({‘data‘: net.blobs[‘data‘].data.shape}) transformer.set_transpose(‘data‘, (2,0,1)) transformer.set_raw_scale(‘data‘, 255) rightcount=0 allcount=0 allimage=os.listdir(imagepath) for img_name in allimage: allcount = allcount + 1 label_truth = img_name.split(‘.‘)[0] print "----------------------" image = get_crop_image(imagepath,img_name) net.blobs[‘data‘].data[...] = transformer.preprocess(‘data‘,image) out = net.forward() label_index = net.blobs[‘loss‘].data[0].flatten().argsort()[-1:-top_k-1:-1] labels = unicode_index[label_index.astype(np.int)] # output unicode #print ‘Index: ‘,label_index print ‘Top-‘ + str(top_k) + ‘ Label: ‘,labels print ‘label_truth: ‘,label_truth for i in range(0,top_k): if labels[i] == int(label_truth): rightcount=rightcount+1 break print(rightcount,allcount,(float)(rightcount)/(float)(allcount)) if __name__==‘__main__‘: imagepath=‘images/‘ top_k = 1; evaluate(imagepath,top_k)
函数,变量名等命名采用XX_XX的方式,清晰的表示了各变量和函数的作用。
同时每一部分功能中间用空行隔开,便于区分每一部分功能。
代码注释偏少,可能部分代码较难理解。
总结同类编程语言或项目在代码规范和风格的一般要求。
头部需加 #coding=utf-8
每一行尽可能不超过80个字符。
缩进使用4个空格。
在二元运算符左右各有一个空格。
对于函数名和变量名均使用小写。
#coding=utf-8import numpy as npimport pickleimport osimport timeimport sysimport shutilimport skimage
caffe_root = ‘/home/cscl/caffe-master/‘ sys.path.insert(0, caffe_root + ‘python‘)import caffe
net_file = ‘googlenet_deploy.prototxt‘caffe_model = ‘models/googlenet_hccr.caffemodel‘ mean_file = ‘meanfiles/CASIA1.0_1.1_1.2_mean_112.npy‘unicode_index = np.loadtxt(‘util/unicode_index.txt‘, delimiter = ‘,‘,dtype = np.int) #7534net = caffe.Net(net_file,caffe_model,caffe.TEST)
def get_crop_image(imagepath, img_name):img=skimage.io.imread(imagepath + img_name,as_grey=True)black_index = np.where(img < 255 )min_x = min(black_index[0])max_x = max(black_index[0])min_y = min(black_index[1])max_y = max(black_index[1])#print(min_x,max_x,min_y,max_y)image = caffe.io.load_image(imagepath+"//"+img_name)return image[min_x:max_x, min_y:max_y,:]
def evaluate(imagepath, top_k):transformer = caffe.io.Transformer({‘data‘: net.blobs[‘data‘].data.shape})transformer.set_transpose(‘data‘, (2,0,1))transformer.set_raw_scale(‘data‘, 255)
rightcount=0allcount=0
allimage=os.listdir(imagepath)
for img_name in allimage: allcount = allcount + 1 label_truth = img_name.split(‘.‘)[0]
print "----------------------" image = get_crop_image(imagepath,img_name) net.blobs[‘data‘].data[...] = transformer.preprocess(‘data‘,image) out = net.forward() label_index = net.blobs[‘loss‘].data[0].flatten().argsort()[-1:-top_k-1:-1] labels = unicode_index[label_index.astype(np.int)] # output unicode
#print ‘Index: ‘,label_index print ‘Top-‘ + str(top_k) + ‘ Label: ‘,labels print ‘label_truth: ‘,label_truth
for i in range(0,top_k):if labels[i] == int(label_truth):rightcount=rightcount+1breakprint(rightcount,allcount,(float)(rightcount)/(float)(allcount))
if __name__==‘__main__‘:imagepath=‘images/‘top_k = 1;evaluate(imagepath,top_k)
标签:图片 top tps 一个 ima main file div ESS
原文地址:https://www.cnblogs.com/lclclclc/p/11660752.html