日本黄色一级经典视频|伊人久久精品视频|亚洲黄色色周成人视频九九九|av免费网址黄色小短片|黄色Av无码亚洲成年人|亚洲1区2区3区无码|真人黄片免费观看|无码一级小说欧美日免费三级|日韩中文字幕91在线看|精品久久久无码中文字幕边打电话

當(dāng)前位置:首頁 > > 充電吧
[導(dǎo)讀]一、學(xué)習(xí)路線個(gè)人感覺對(duì)于任何一個(gè)深度學(xué)習(xí)庫,如mxnet、tensorflow、theano、caffe等,基本上我都采用同樣的一個(gè)學(xué)習(xí)流程,大體流程如下:(1)訓(xùn)練階段:數(shù)據(jù)打包-》網(wǎng)絡(luò)構(gòu)建、訓(xùn)練-


一、學(xué)習(xí)路線

個(gè)人感覺對(duì)于任何一個(gè)深度學(xué)習(xí)庫,如mxnet、tensorflow、theano、caffe等,基本上我都采用同樣的一個(gè)學(xué)習(xí)流程,大體流程如下:

(1)訓(xùn)練階段:數(shù)據(jù)打包-》網(wǎng)絡(luò)構(gòu)建、訓(xùn)練-》模型保存-》可視化查看損失函數(shù)、驗(yàn)證精度

(2)測(cè)試階段:模型加載-》測(cè)試圖片讀取-》預(yù)測(cè)顯示結(jié)果

(3)移植階段:量化、壓縮加速-》微調(diào)-》C++移植打包-》上線

這邊我就以tensorflow為例子,講解整個(gè)流程的大體架構(gòu),完成一個(gè)深度學(xué)習(xí)項(xiàng)目所需要熟悉的過程代碼。

二、訓(xùn)練、測(cè)試階段

1、tensorflow打包數(shù)據(jù)

這一步對(duì)于tensorflow來說,也可以直接自己在線讀?。?jpg圖片、標(biāo)簽文件等,然后通過phaceholder變量,把數(shù)據(jù)送入網(wǎng)絡(luò)中,進(jìn)行計(jì)算。

不過這種效率比較低,對(duì)于大規(guī)模訓(xùn)練數(shù)據(jù)來說,我們需要一個(gè)比較高效的方式,tensorflow建議我們采用tfrecoder進(jìn)行高效數(shù)據(jù)讀取。學(xué)習(xí)tensorflow一定要學(xué)會(huì)tfrecoder文件寫入、讀取,具體示例代碼如下:


[python]view plaincopy#coding=utf-8 #tensorflow高效數(shù)據(jù)讀取訓(xùn)練 importtensorflowastf importcv2 #把train.txt文件格式,每一行:圖片路徑名類別標(biāo)簽 #獎(jiǎng)數(shù)據(jù)打包,轉(zhuǎn)換成tfrecords格式,以便后續(xù)高效讀取 defencode_to_tfrecords(lable_file,data_root,new_name='data.tfrecords',resize=None): writer=tf.python_io.TFRecordWriter(data_root+'/'+new_name) num_example=0 withopen(lable_file,'r')asf: forlinf.readlines(): l=l.split() image=cv2.imread(data_root+"/"+l[0]) ifresizeisnotNone: image=cv2.resize(image,resize)#為了 height,width,nchannel=image.shape label=int(l[1]) example=tf.train.Example(features=tf.train.Features(feature={ 'height':tf.train.Feature(int64_list=tf.train.Int64List(value=[height])), 'width':tf.train.Feature(int64_list=tf.train.Int64List(value=[width])), 'nchannel':tf.train.Feature(int64_list=tf.train.Int64List(value=[nchannel])), 'image':tf.train.Feature(bytes_list=tf.train.BytesList(value=[image.tobytes()])), 'label':tf.train.Feature(int64_list=tf.train.Int64List(value=[label])) })) serialized=example.SerializeToString() writer.write(serialized) num_example+=1 printlable_file,"樣本數(shù)據(jù)量:",num_example writer.close() #讀取tfrecords文件 defdecode_from_tfrecords(filename,num_epoch=None): filename_queue=tf.train.string_input_producer([filename],num_epochs=num_epoch)#因?yàn)橛械挠?xùn)練數(shù)據(jù)過于龐大,被分成了很多個(gè)文件,所以第一個(gè)參數(shù)就是文件列表名參數(shù) reader=tf.TFRecordReader() _,serialized=reader.read(filename_queue) example=tf.parse_single_example(serialized,features={ 'height':tf.FixedLenFeature([],tf.int64), 'width':tf.FixedLenFeature([],tf.int64), 'nchannel':tf.FixedLenFeature([],tf.int64), 'image':tf.FixedLenFeature([],tf.string), 'label':tf.FixedLenFeature([],tf.int64) }) label=tf.cast(example['label'],tf.int32) image=tf.decode_raw(example['image'],tf.uint8) image=tf.reshape(image,tf.pack([ tf.cast(example['height'],tf.int32), tf.cast(example['width'],tf.int32), tf.cast(example['nchannel'],tf.int32)])) #label=example['label'] returnimage,label #根據(jù)隊(duì)列流數(shù)據(jù)格式,解壓出一張圖片后,輸入一張圖片,對(duì)其做預(yù)處理、及樣本隨機(jī)擴(kuò)充 defget_batch(image,label,batch_size,crop_size): #數(shù)據(jù)擴(kuò)充變換 distorted_image=tf.random_crop(image,[crop_size,crop_size,3])#隨機(jī)裁剪 distorted_image=tf.image.random_flip_up_down(distorted_image)#上下隨機(jī)翻轉(zhuǎn) #distorted_image=tf.image.random_brightness(distorted_image,max_delta=63)#亮度變化 #distorted_image=tf.image.random_contrast(distorted_image,lower=0.2,upper=1.8)#對(duì)比度變化 #生成batch #shuffle_batch的參數(shù):capacity用于定義shuttle的范圍,如果是對(duì)整個(gè)訓(xùn)練數(shù)據(jù)集,獲取batch,那么capacity就應(yīng)該夠大 #保證數(shù)據(jù)打的足夠亂 images,label_batch=tf.train.shuffle_batch([distorted_image,label],batch_size=batch_size, num_threads=16,capacity=50000,min_after_dequeue=10000) #images,label_batch=tf.train.batch([distorted_image,label],batch_size=batch_size) #調(diào)試顯示 #tf.image_summary('images',images) returnimages,tf.reshape(label_batch,[batch_size]) #這個(gè)是用于測(cè)試階段,使用的get_batch函數(shù) defget_test_batch(image,label,batch_size,crop_size): #數(shù)據(jù)擴(kuò)充變換 distorted_image=tf.image.central_crop(image,39./45.) distorted_image=tf.random_crop(distorted_image,[crop_size,crop_size,3])#隨機(jī)裁剪 images,label_batch=tf.train.batch([distorted_image,label],batch_size=batch_size) returnimages,tf.reshape(label_batch,[batch_size]) #測(cè)試上面的壓縮、解壓代碼 deftest(): encode_to_tfrecords("data/train.txt","data",(100,100)) image,label=decode_from_tfrecords('data/data.tfrecords') batch_image,batch_label=get_batch(image,label,3)#batch生成測(cè)試 init=tf.initialize_all_variables() withtf.Session()assession: session.run(init) coord=tf.train.Coordinator() threads=tf.train.start_queue_runners(coord=coord) forlinrange(100000):#每run一次,就會(huì)指向下一個(gè)樣本,一直循環(huán) #image_np,label_np=session.run([image,label])#每調(diào)用run一次,那么 '''''cv2.imshow("temp",image_np) cv2.waitKey()''' #printlabel_np #printimage_np.shape batch_image_np,batch_label_np=session.run([batch_image,batch_label]) printbatch_image_np.shape printbatch_label_np.shape coord.request_stop()#queue需要關(guān)閉,否則報(bào)錯(cuò) coord.join(threads) #test()


2、網(wǎng)絡(luò)架構(gòu)與訓(xùn)練

經(jīng)過上面的數(shù)據(jù)格式處理,接著我們只要寫一寫網(wǎng)絡(luò)結(jié)構(gòu)、網(wǎng)絡(luò)優(yōu)化方法,把數(shù)據(jù)搞進(jìn)網(wǎng)絡(luò)中就可以了,具體示例代碼如下:


[python]view plaincopy#coding=utf-8 importtensorflowastf fromdata_encoder_decoederimportencode_to_tfrecords,decode_from_tfrecords,get_batch,get_test_batch importcv2 importos classnetwork(object): def__init__(self): withtf.variable_scope("weights"): self.weights={ #39*39*3->36*36*20->18*18*20 'conv1':tf.get_variable('conv1',[4,4,3,20],initializer=tf.contrib.layers.xavier_initializer_conv2d()), #18*18*20->16*16*40->8*8*40 'conv2':tf.get_variable('conv2',[3,3,20,40],initializer=tf.contrib.layers.xavier_initializer_conv2d()), #8*8*40->6*6*60->3*3*60 'conv3':tf.get_variable('conv3',[3,3,40,60],initializer=tf.contrib.layers.xavier_initializer_conv2d()), #3*3*60->120 'fc1':tf.get_variable('fc1',[3*3*60,120],initializer=tf.contrib.layers.xavier_initializer()), #120->6 'fc2':tf.get_variable('fc2',[120,6],initializer=tf.contrib.layers.xavier_initializer()), } withtf.variable_scope("biases"): self.biases={ 'conv1':tf.get_variable('conv1',[20,],initializer=tf.constant_initializer(value=0.0,dtype=tf.float32)), 'conv2':tf.get_variable('conv2',[40,],initializer=tf.constant_initializer(value=0.0,dtype=tf.float32)), 'conv3':tf.get_variable('conv3',[60,],initializer=tf.constant_initializer(value=0.0,dtype=tf.float32)), 'fc1':tf.get_variable('fc1',[120,],initializer=tf.constant_initializer(value=0.0,dtype=tf.float32)), 'fc2':tf.get_variable('fc2',[6,],initializer=tf.constant_initializer(value=0.0,dtype=tf.float32)) } definference(self,images): #向量轉(zhuǎn)為矩陣 images=tf.reshape(images,shape=[-1,39,39,3])#[batch,in_height,in_width,in_channels] images=(tf.cast(images,tf.float32)/255.-0.5)*2#歸一化處理 #第一層 conv1=tf.nn.bias_add(tf.nn.conv2d(images,self.weights['conv1'],strides=[1,1,1,1],padding='VALID'), self.biases['conv1']) relu1=tf.nn.relu(conv1) pool1=tf.nn.max_pool(relu1,ksize=[1,2,2,1],strides=[1,2,2,1],padding='VALID') #第二層 conv2=tf.nn.bias_add(tf.nn.conv2d(pool1,self.weights['conv2'],strides=[1,1,1,1],padding='VALID'), self.biases['conv2']) relu2=tf.nn.relu(conv2) pool2=tf.nn.max_pool(relu2,ksize=[1,2,2,1],strides=[1,2,2,1],padding='VALID') #第三層 conv3=tf.nn.bias_add(tf.nn.conv2d(pool2,self.weights['conv3'],strides=[1,1,1,1],padding='VALID'), self.biases['conv3']) relu3=tf.nn.relu(conv3) pool3=tf.nn.max_pool(relu3,ksize=[1,2,2,1],strides=[1,2,2,1],padding='VALID') #全連接層1,先把特征圖轉(zhuǎn)為向量 flatten=tf.reshape(pool3,[-1,self.weights['fc1'].get_shape().as_list()[0]]) drop1=tf.nn.dropout(flatten,0.5) fc1=tf.matmul(drop1,self.weights['fc1'])+self.biases['fc1'] fc_relu1=tf.nn.relu(fc1) fc2=tf.matmul(fc_relu1,self.weights['fc2'])+self.biases['fc2'] returnfc2 definference_test(self,images): #向量轉(zhuǎn)為矩陣 images=tf.reshape(images,shape=[-1,39,39,3])#[batch,in_height,in_width,in_channels] images=(tf.cast(images,tf.float32)/255.-0.5)*2#歸一化處理 #第一層 conv1=tf.nn.bias_add(tf.nn.conv2d(images,self.weights['conv1'],strides=[1,1,1,1],padding='VALID'), self.biases['conv1']) relu1=tf.nn.relu(conv1) pool1=tf.nn.max_pool(relu1,ksize=[1,2,2,1],strides=[1,2,2,1],padding='VALID') #第二層 conv2=tf.nn.bias_add(tf.nn.conv2d(pool1,self.weights['conv2'],strides=[1,1,1,1],padding='VALID'), self.biases['conv2']) relu2=tf.nn.relu(conv2) pool2=tf.nn.max_pool(relu2,ksize=[1,2,2,1],strides=[1,2,2,1],padding='VALID') #第三層 conv3=tf.nn.bias_add(tf.nn.conv2d(pool2,self.weights['conv3'],strides=[1,1,1,1],padding='VALID'), self.biases['conv3']) relu3=tf.nn.relu(conv3) pool3=tf.nn.max_pool(relu3,ksize=[1,2,2,1],strides=[1,2,2,1],padding='VALID') #全連接層1,先把特征圖轉(zhuǎn)為向量 flatten=tf.reshape(pool3,[-1,self.weights['fc1'].get_shape().as_list()[0]]) fc1=tf.matmul(flatten,self.weights['fc1'])+self.biases['fc1'] fc_relu1=tf.nn.relu(fc1) fc2=tf.matmul(fc_relu1,self.weights['fc2'])+self.biases['fc2'] returnfc2 #計(jì)算softmax交叉熵?fù)p失函數(shù) defsorfmax_loss(self,predicts,labels): predicts=tf.nn.softmax(predicts) labels=tf.one_hot(labels,self.weights['fc2'].get_shape().as_list()[1]) loss=-tf.reduce_mean(labels*tf.log(predicts))#tf.nn.softmax_cross_entropy_with_logits(predicts,labels) self.cost=loss returnself.cost #梯度下降 defoptimer(self,loss,lr=0.001): train_optimizer=tf.train.GradientDescentOptimizer(lr).minimize(loss) returntrain_optimizer deftrain(): encode_to_tfrecords("data/train.txt","data",'train.tfrecords',(45,45)) image,label=decode_from_tfrecords('data/train.tfrecords') batch_image,batch_label=get_batch(image,label,batch_size=50,crop_size=39)#batch生成測(cè)試 #網(wǎng)絡(luò)鏈接,訓(xùn)練所用 net=network() inf=net.inference(batch_image) loss=net.sorfmax_loss(inf,batch_label) opti=net.optimer(loss) #驗(yàn)證集所用 encode_to_tfrecords("data/val.txt","data",'val.tfrecords',(45,45)) test_image,test_label=decode_from_tfrecords('data/val.tfrecords',num_epoch=None) test_images,test_labels=get_test_batch(test_image,test_label,batch_size=120,crop_size=39)#batch生成測(cè)試 test_inf=net.inference_test(test_images) correct_prediction=tf.equal(tf.cast(tf.argmax(test_inf,1),tf.int32),test_labels) accuracy=tf.reduce_mean(tf.cast(correct_prediction,tf.float32)) init=tf.initialize_all_variables() withtf.Session()assession: session.run(init) coord=tf.train.Coordinator() threads=tf.train.start_queue_runners(coord=coord) max_iter=100000 iter=0 ifos.path.exists(os.path.join("model",'model.ckpt'))isTrue: tf.train.Saver(max_to_keep=None).restore(session,os.path.join("model",'model.ckpt')) whileiter<max_iter: loss_np,_,label_np,image_np,inf_np=session.run([loss,opti,batch_label,batch_image,inf]) #printimage_np.shape #cv2.imshow(str(label_np[0]),image_np[0]) #printlabel_np[0] #cv2.waitKey() #printlabel_np ifiter%50==0: print'trainloss:',loss_np ifiter%500==0: accuracy_np=session.run([accuracy]) print'***************testaccruacy:',accuracy_np,'*******************' tf.train.Saver(max_to_keep=None).save(session,os.path.join('model','model.ckpt')) iter+=1 coord.request_stop()#queue需要關(guān)閉,否則報(bào)錯(cuò) coord.join(threads) train()

3、可視化顯示

(1)首先再源碼中加入需要跟蹤的變量:


[python]view plaincopy

本站聲明: 本文章由作者或相關(guān)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點(diǎn),本站亦不保證或承諾內(nèi)容真實(shí)性等。需要轉(zhuǎn)載請(qǐng)聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請(qǐng)及時(shí)聯(lián)系本站刪除。
換一批
延伸閱讀

LED驅(qū)動(dòng)電源的輸入包括高壓工頻交流(即市電)、低壓直流、高壓直流、低壓高頻交流(如電子變壓器的輸出)等。

關(guān)鍵字: 驅(qū)動(dòng)電源

在工業(yè)自動(dòng)化蓬勃發(fā)展的當(dāng)下,工業(yè)電機(jī)作為核心動(dòng)力設(shè)備,其驅(qū)動(dòng)電源的性能直接關(guān)系到整個(gè)系統(tǒng)的穩(wěn)定性和可靠性。其中,反電動(dòng)勢(shì)抑制與過流保護(hù)是驅(qū)動(dòng)電源設(shè)計(jì)中至關(guān)重要的兩個(gè)環(huán)節(jié),集成化方案的設(shè)計(jì)成為提升電機(jī)驅(qū)動(dòng)性能的關(guān)鍵。

關(guān)鍵字: 工業(yè)電機(jī) 驅(qū)動(dòng)電源

LED 驅(qū)動(dòng)電源作為 LED 照明系統(tǒng)的 “心臟”,其穩(wěn)定性直接決定了整個(gè)照明設(shè)備的使用壽命。然而,在實(shí)際應(yīng)用中,LED 驅(qū)動(dòng)電源易損壞的問題卻十分常見,不僅增加了維護(hù)成本,還影響了用戶體驗(yàn)。要解決這一問題,需從設(shè)計(jì)、生...

關(guān)鍵字: 驅(qū)動(dòng)電源 照明系統(tǒng) 散熱

根據(jù)LED驅(qū)動(dòng)電源的公式,電感內(nèi)電流波動(dòng)大小和電感值成反比,輸出紋波和輸出電容值成反比。所以加大電感值和輸出電容值可以減小紋波。

關(guān)鍵字: LED 設(shè)計(jì) 驅(qū)動(dòng)電源

電動(dòng)汽車(EV)作為新能源汽車的重要代表,正逐漸成為全球汽車產(chǎn)業(yè)的重要發(fā)展方向。電動(dòng)汽車的核心技術(shù)之一是電機(jī)驅(qū)動(dòng)控制系統(tǒng),而絕緣柵雙極型晶體管(IGBT)作為電機(jī)驅(qū)動(dòng)系統(tǒng)中的關(guān)鍵元件,其性能直接影響到電動(dòng)汽車的動(dòng)力性能和...

關(guān)鍵字: 電動(dòng)汽車 新能源 驅(qū)動(dòng)電源

在現(xiàn)代城市建設(shè)中,街道及停車場(chǎng)照明作為基礎(chǔ)設(shè)施的重要組成部分,其質(zhì)量和效率直接關(guān)系到城市的公共安全、居民生活質(zhì)量和能源利用效率。隨著科技的進(jìn)步,高亮度白光發(fā)光二極管(LED)因其獨(dú)特的優(yōu)勢(shì)逐漸取代傳統(tǒng)光源,成為大功率區(qū)域...

關(guān)鍵字: 發(fā)光二極管 驅(qū)動(dòng)電源 LED

LED通用照明設(shè)計(jì)工程師會(huì)遇到許多挑戰(zhàn),如功率密度、功率因數(shù)校正(PFC)、空間受限和可靠性等。

關(guān)鍵字: LED 驅(qū)動(dòng)電源 功率因數(shù)校正

在LED照明技術(shù)日益普及的今天,LED驅(qū)動(dòng)電源的電磁干擾(EMI)問題成為了一個(gè)不可忽視的挑戰(zhàn)。電磁干擾不僅會(huì)影響LED燈具的正常工作,還可能對(duì)周圍電子設(shè)備造成不利影響,甚至引發(fā)系統(tǒng)故障。因此,采取有效的硬件措施來解決L...

關(guān)鍵字: LED照明技術(shù) 電磁干擾 驅(qū)動(dòng)電源

開關(guān)電源具有效率高的特性,而且開關(guān)電源的變壓器體積比串聯(lián)穩(wěn)壓型電源的要小得多,電源電路比較整潔,整機(jī)重量也有所下降,所以,現(xiàn)在的LED驅(qū)動(dòng)電源

關(guān)鍵字: LED 驅(qū)動(dòng)電源 開關(guān)電源

LED驅(qū)動(dòng)電源是把電源供應(yīng)轉(zhuǎn)換為特定的電壓電流以驅(qū)動(dòng)LED發(fā)光的電壓轉(zhuǎn)換器,通常情況下:LED驅(qū)動(dòng)電源的輸入包括高壓工頻交流(即市電)、低壓直流、高壓直流、低壓高頻交流(如電子變壓器的輸出)等。

關(guān)鍵字: LED 隧道燈 驅(qū)動(dòng)電源
關(guān)閉