Python image normalization job code generation programming write Graph Python job
From PIL import Image
Import OS
Import Sys
Import NumPy as NP
Import time
From Sklearn import SVM
# gets all. png files under the specified path
def get_file_list (path):
return [Os.path.join (path, F) for F in Os.listdir (path) if F.endswith (". png")]
# parse the name of the. png Map file
def get_img_name_str (Imgpath):
Return Imgpath.split (OS.PATH.SEP) [-1]
# convert 20px * 20px image data to 1*400 numpy vector
# parameter: imgfile--image name such as: 0_1.png
# return: 1*400 numpy vector
def img2vector (imgfile):
#print ("In Img2vector func--para:{}". Format (Imgfile))
img = Image.open (imgfile). Convert (' L ')
Img_arr = Np.array (img, ' i ') # 20px * 20px grayscale image
Img_normalization = Np.round (img_arr/255) # Normalization of grayscale values
IMG_ARR2 = Np.reshape (Img_normalization, (1,-1)) # 1 * 400 matrix
Return IMG_ARR2
# Read all data from a category and convert it into a matrix
Parameters
# BasePath: The basic path where the image data resides
# mnist-image/train/
# mnist-image/test/
# CLA: Category name
# 0,1,2,..., 9
# return: All data for a category----[Sample quantity * (image width × image height)] Matrix
def read_and_convert (imgfilelist):
DataLabel = [] # Store class label
Datanum = Len (imgfilelist)
Datamat = Np.zeros ((datanum, +)) # Datanum * 400 matrix
For I in Range (Datanum):
IMGNAMESTR = Imgfilelist[i]
Imgname = Get_img_name_str (imgnamestr) # Gets the number _ instance number. png
#print ("Imgname: {}". Format (imgname))
Classtag = Imgname.split (".") [0].split ("_") [0] # Get Class label (number)
#print ("Classtag: {}". Format (Classtag))
Datalabel.append (Classtag)
Datamat[i,:] = Img2vector (IMGNAMESTR)
Return Datamat, DataLabel
# Read Training data
Def read_all_data ():
CName = [' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ']
Train_data_path = "Mnist-image\\train\\0"
Flist = Get_file_list (Train_data_path)
Datamat, DataLabel = Read_and_convert (flist)
For C in CName:
Train_data_path_ = "mnist-image\\train\\" + C
Flist_ = Get_file_list (train_data_path_)
Datamat_, Datalabel_ = Read_and_convert (flist_)
Datamat = Np.concatenate ((Datamat, Datamat_), axis=0)
DataLabel = Np.concatenate ((DataLabel, Datalabel_), axis=0)
#print (Datamat.shape)
#print (Len (DataLabel))
Return Datamat, DataLabel
# Create Model
def CREATE_SVM (Datamat, DataLabel, decision= ' OVR '):
CLF = SVM. SVC (decision_function_shape=decision)
Clf.fit (Datamat, DataLabel)
Return CLF
#clf = SVM. SVC (decision_function_shape= ' OVR ')
st = Time.clock ()
CLF = CREATE_SVM (Datamat, DataLabel, decision= ' OVR ')
ET = Time.clock ()
Print ("Training spent {:. 4f}s."). Format ((et-st)))
# 10 numbers for the classification test
def main ():
Tbasepath = "mnist-image\\test\\"
Tcname = [' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ']
TST = Time.clock ()
Allerrcount = 0
Allerrorrate = 0.0
Allscore = 0.0
For TCN in Tcname:
Testpath = "mnist-image\\test\\" + TCN
#print ("class" + TCN + "path is: {}."). Format (Testpath))
Tflist = Get_file_list (Testpath)
#tflist
Tdatamat, Tdatalabel = Read_and_convert (tflist)
Print ("Test Datamat shape: {0}, test DataLabel len: {1}". Format (Tdatamat.shape, Len (Tdatalabel)))
#print ("Test DataLabel: {}". Format (len (Tdatalabel)))
Pre_st = Time.clock ()
Preresult = Clf.predict (Tdatamat)
Pre_et = Time.clock ()
Print ("recognition" + TCN + "spent {:. 4f}s."). Format ((Pre_et-pre_st)))
#print ("predict result: {}". Format (len (Preresult)))
ErrCount = Len ([x for x in Preresult if X!=TCN])
Print ("Errorcount: {}.". Format (ErrCount))
Allerrcount + = ErrCount
Score_st = Time.clock ()
Score = Clf.score (Tdatamat, Tdatalabel)
Score_et = Time.clock ()
Print ("Computing score spent {:. 6f}s.". Format (Score_et-score_st))
Allscore + = Score
Print ("Score: {:. 6f}.". Format (score))
Print ("error rate is {:. 6f}.".) Format ((1-score)))
Print ("---------------------------------------------------------")
Tet = Time.clock ()
Print ("Testing all class Total spent {:. 6f}s."). Format (TET-TST))
Print ("All error Count is: {}.".) Format (Allerrcount))
Avgaccuracy = allscore/10.0
Print ("Average accuracy is: {:. 6f}.". Format (avgaccuracy))
Print ("Average error rate is: {:. 6f}.". Format (1-avgscore))
Http://www.6daixie.com/contents/3/1375.html
The core staff of the team mainly include Silicon Valley engineers, bat front-line engineers, domestic TOP5 master, PhD students, proficient in German English! Our main business scope is to do programming big homework, curriculum design and so on.
Our Direction field: Window Programming numerical algorithm AI Artificial Intelligence financial statistical Metrology analysis Big Data network programming Web programming Communication Programming game Programming Multimedia Linux plug-in programming API image processing embedded/Microcontroller database programming console process and thread Network security assembly language Hardware programming software Design Engineering Standard Rules. The generation of programming languages or tools including, but not limited to, the following ranges:
C/c++/c# Write
Java Write generation
It generation
Python writes
Tutoring Programming Jobs
The MATLAB Generation writes
Haskell writes
Processing Write
Linux Environment Setup
Rust Generation Write
Data Structure assginment Data structure generation
MIPS Generation Writing
Machine Learning Job Writing
Oracle/sql/postgresql/pig database Generation/Generation/Coaching
Web development, Web development, Web site jobs
Asp. NET Web site development
Finance insurace Statistics Statistics, regression, iteration
Prolog write
Computer Computational Method Generation
Because of professional, so trustworthy. If necessary, please add qq:99515681 or e-mail:[email protected]
: Codinghelp
Python image normalization job code generation programming write Graph Python job