用其他數(shù)據(jù)集訓(xùn)練KittiSeg

在win10上配置成功KittiSeg后,隨便從網(wǎng)上download了一張圖片測試一下效果,結(jié)果發(fā)現(xiàn)不work
隨意數(shù)據(jù)上的結(jié)果:




kitti數(shù)據(jù)上的結(jié)果:



估計是kitti數(shù)據(jù)集太小了,模型過擬合,所以泛化性能低

打算自己訓(xùn)練一個model,用百度road_seg的數(shù)據(jù)集。用的是seg_3的數(shù)據(jù)
對數(shù)據(jù)進(jìn)行修改,把label變成2分類問題

import numpy as np

import cv2

#test for one image
#img = cv2.imread('Label/Record057/Camera 6/171206_042426425_Camera_6_bin.png')
#print(img.shape)      
#img[np.where((img!=[49,49,49]).all(axis = 2))] = [255,0,0]
#img[np.where((img==[49,49,49]).all(axis = 2))] = [255,0,255]
#cv2.imwrite('a.png',img)
#image = scipy.misc.imresize(img,0.6)
#print(image.shape)

files = [line for line in open('all2.txt')]
file = files[1000:]
num = 0
for path in file:
    path = path[:-1] #這里要去掉最后的/n,這也是一個字符,不然返回空
    img = cv2.imread(path)
    print(img.shape)    
    img[np.where((img!=[49,49,49]).all(axis = 2))] = [255,0,0]
    img[np.where((img==[49,49,49]).all(axis = 2))] = [255,0,255]
    cv2.imwrite(path,img)
    print(num)
    num=num+1
    

制作并劃分train.txt val.txt
a.先制作all.txt
分別讀取圖片和label,分成兩個txt(all1.txt all2.txt),再把兩個txt合并。

import os
import glob as gb
#import cv2
all_file = 'all2.txt'
with open (all_file, 'w') as a:
        #for root, dirs,files in os.walk('ColorImage'):
        for root, dirs,files in os.walk('Label'):
            if len(dirs)  == 0:
                for i in range(len(files)):
                    if files[i][-3:] =='png':
                        file_path = root+'/'+files[i]
                        a.writelines(file_path+'\n')

合并兩個txt文件,此時發(fā)現(xiàn)圖片30331,label30350,兩者不一致,所以要對兩個txt做比較,去除不匹配的。

#這種是按行來比較,同行之間才比較,所以不符合自己想要的
之前每行后面都加了/n換行,在這里,也算一個字符,所以要去掉
#with open('all.txt','w') as a:
#    with open('all1.txt','r') as a1, open('all2.txt') as a2:
#        for line1, line2 in zip(a1,a2):
#            #print(line1[10:-5],line2[5:-9])
#            if line1[10:-5]==line2[5:-9]:
#                print('o')
#                a.writelines(line1[:-1]+' '+line2[:-1]+'\n')

with open('all.txt','w') as a:
    with open('all1.txt','r') as a1, open('all2.txt') as a2:
        lines1 = [line for line in a1]
        lines2 = [line for line in a2]
        for line1 in lines1:
            #print(line1+'a')
            for line2 in lines2:
                if line1[10:-5]==line2[5:-9]:
                    
                    a.writelines(line1[:-1]+' '+line2[:-1]+'\n')
            #print(line1[10:-5],line2[5:-9])

隨機(jī)劃分成訓(xùn)練集驗證集 由于數(shù)據(jù)量太大,自己想要先驗證一下算法,所以,就先用1000樣本,900訓(xùn)練,100驗證。

from random import shuffle
def make_val_split():
    """
    Splits the Images in train and test.
    Assumes a File all.txt in data_folder.
    """

    all_file = "alls.txt"
    train_file = "trains.txt"
    test_file = "vals.txt"
    test_num = 100

    
    files = [line for line in open(all_file)]

    shuffle(files)

    train = files[:-test_num]
    test = files[-test_num:]

    #train_file = os.path.join(data_folder, train_file)
    #test_file = os.path.join(data_folder, test_file)

    with open(train_file, 'w') as file:
        for label in train:
            file.write(label)

    with open(test_file, 'w') as file:
        for label in test:
            file.write(label)
def main():
    make_val_split()

if __name__ == '__main__':
    main()

運行train.py遇到的問題:
報錯1

    image_file, gt_image_file = file.split(" ")
ValueError: too many values to unpack (expected 2)

查看了一下自己的trains.txt 發(fā)現(xiàn) ColorImage\Record008\Camera 5/171206_030617006_Camera_5.jpg Label\Record008\Camera 5/171206_030617006_Camera_5_bin.png 確實每行都有三個空格
所以需要對代碼進(jìn)行修改
對kitti_seg_input.py的114行進(jìn)行修改,修改后如下

  #image_file, gt_image_file = file.split(" ")
   image_file1,image_file2,gt_image_file1,gt_image_file2 = file.split(" ")
   image_file = image_file1+" "+image_file2
   gt_image_file = gt_image_file1+" "+gt_image_file2

報錯2:

ResourceExhaustedError (see above for traceback): OOM when allocating tensor with shape[1,64,2701,3367] and type float on /job:localhost/replica:0/task:0/device:GPU:0 by allocator GPU_0_bfc
         [[Node: conv1_2/Conv2D = Conv2D[T=DT_FLOAT, data_format="NCHW", dilations=[1, 1, 1, 1], padding="SAME", strides=[1, 1, 1, 1], use_cudnn_on_gpu=true, _device="/job:localhost/replica:0/task:0/device:GPU:0"](conv1_1/Relu, conv1_2/filter/read)]]
 Allocator (GPU_0_bfc) ran out of memory trying to allocate 2.19GiB. Current allocation summary follows.

根據(jù)https://github.com/tensorflow/models/issues/3393估計可能確實是batchsize的問題,但是,自己的batchsize已經(jīng)是1了,gg 內(nèi)存不夠的話,要么resize圖片,要么換簡單一點的網(wǎng)絡(luò),
鑒于自己的圖片較大,有(2710, 3384),所以先resize一下
根據(jù)https://docs.scipy.org/doc/scipy/reference/generated/scipy.misc.imresize.html 使用scipy.misc.imresize(img,0.8) 把圖像縮小為原來的0.8,0.8還是跑不起來,所以改為0.6

 img = scipy.misc.imread(image_file, mode='RGB')
 image = scipy.misc.imresize(img,0.8)
 # Please update Scipy, if mode='RGB' is not avaible
 gt_img = scp.misc.imread(gt_image_file, mode='RGB')
  gy_image = scipy.misc.imresize(gt_img,0.8)

報錯3:
train中間evaluate的時候報錯

  hypes, sess, tv_graph['image_pl'], tv_graph['inf_out'])
  File "D:\work\KittiSeg-hotfix-python3.5_support\hypes\../evals/kitti_eval.py", line 71, in evaluate
    image_file, gt_file = datum.split(" ")
ValueError: too many values to unpack (expected 2)

對kitti_eval.py進(jìn)行修改 ,和輸入的類似 就ok了

至此,開始訓(xùn)練過程。

對昨天訓(xùn)練好的model在自己的數(shù)據(jù)上進(jìn)行測試,結(jié)果發(fā)現(xiàn)mask全白,也就是說將所有的像素預(yù)測為road,排查原因。將驗證集的圖片輸入亦如此,遂將訓(xùn)練集圖片輸入,還是如此。說明擬合的時候就出了問題,所以,應(yīng)該是標(biāo)注出了問題。

在驗證集,訓(xùn)練集上的結(jié)果:

查看.json文件

自己指定的是

"road_color" : [255,0,255],

"background_color" : [255,0,0],

沒有更改,和kitti的一樣,但是聯(lián)想到自己處理出來的數(shù)據(jù)是藍(lán)色的,kitti的是紅色的,肯定有哪里不對,

查看當(dāng)時自己改數(shù)據(jù)的代碼


是按照kitti的改的啊,分別用取色器查看了kitti和自己的標(biāo)定,發(fā)現(xiàn)藍(lán)色分量和紅色分量的值是反的,kitti的是紅色分量為255,自己的是藍(lán)色分量255,查了一下,opencv讀取圖片是BGR,所以BR兩個分量反了,將.json文件中background_color改為[0,0,255]

重新開始訓(xùn)練

以后訓(xùn)練前還是要細(xì)細(xì)檢查,這種bug白白浪費了一天時間。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容