使用Google colab 路徑問題報錯記錄

使用Google colab 路徑問題報錯記錄

遇到問題

當使用colab訓練yolov4代碼時遇到了一個錯誤(其實之前也遇到過同樣的錯誤,不過忘記怎麼解決了)python

FileNotFoundError: [Errno 2] No such file or directory: '/content/drive/My'

錯誤嘗試

最初我是以爲多是因爲哪些庫的版本有問題,致使導入和發現對應的路徑。由於我出現了以下錯誤:
在這裏插入圖片描述
因爲顯示了torch、PIL等庫的錯誤,我覺得是這些庫版本存在問題,或者是環境不匹配。花了大半天去調試環境,試了不少torch、torchvison、pillow的版本,可報錯依然發生。後來看見了一個博主的博客,解決了這個問題
解決經驗


算法

問題總結

其實緣由很簡單,就是由於Google的網盤叫My Drive,中間存在空格,在程序的讀取和執行的過程當中沒法識別爲整個總體,因此報了找不到路徑的錯誤。我這裏的代碼是這個樣子:app

def convert_annotation(year, image_id, list_file):    
	in_file = open('VOCdevkit/VOC%s/Annotations/%s.xml'%(year, image_id))    	
	tree=ET.parse(in_file)    
	root = tree.getroot()    	
	list_file.write('%s/VOCdevkit/VOC%s/JPEGImages/%s.png'%(wd,year, image_id))    
	for obj in root.iter('object'):        
		difficult = obj.find('difficult').text        
		cls = obj.find('name').text        
		if cls not in classes or int(difficult)==1:           
			continue        
		cls_id = classes.index(cls)        
		xmlbox = obj.find('bndbox')        
		b = (int(xmlbox.find('xmin').text), int(xmlbox.find('ymin').text), int(xmlbox.find('xmax').text), int(xmlbox.find('ymax').text))        list_file.write(" " + ",".join([str(a) for a in b]) + ',' + str(cls_id))
    	list_file.write('\n')

只需將 list_file.write(’%s/VOCdevkit/VOC%s/JPEGImages/%s.png’%(wd,year, image_id))改爲list_file.write(’./VOCdevkit/VOC%s/JPEGImages/%s.png’%(year, image_id)),把原來的絕對路徑化成相對路徑就能直接運行了。其實不止是yolov4,像yolov3,ssd,efficientnet等目標檢測算法在colab中使用時,遇到相關報錯時均可以這樣解決。
修改後個人2007_train.txt文件變成了這樣:
在這裏插入圖片描述
最後再運行train.py就能成功運行了。介紹下本身吧,做爲一個通訊專業的本科生,學習深度學習也快一年了,第一次寫博客,但願可以幫助到你們。


學習

相關文章
相關標籤/搜索