分析一套源代碼的代碼規範和風格並討論如何改進優化代碼python
結合工程實踐選題相關的一套源代碼,根據其編程語言或項目特色,分析其在源代碼目錄結構、文件名/類名/函數名/變量名等命名、接口定義規範和單元測試組織形式等方面的作法和特色;git
使用的代碼爲手寫漢字識別的代碼github
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 caffenet_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=0allimage=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)