1. 程式人生 > >caffe影象標籤生成

caffe影象標籤生成

   程式碼:執行環境spyder,python3.6

# -*- coding: utf-8 -*-
"""
Spyder Editor

This is a temporary script file.
"""

#<pre class="python" name="code">#coding:utf-8
'''
Created on Jul 29, 2016
@author: sgg
'''
 
"<span style=""font-family:Arial;font-size:18px;"">"
"<span style=""font-size:18px;"">"
"<span style=""font-size:18px;"">" 
import os
 
def IsSubString(SubStrList,Str):
    flag=True
    for substr in SubStrList:
        if not(substr in Str):
            flag=False
    
    return flag
 
#掃面檔案
def GetFileList(FindPath,FlagStr=[]):
    FileList=[]
    FileNames=os.listdir(FindPath)
    if len(FileNames)>0:
        for fn in FileNames:
            if len(FlagStr)>0:
                if IsSubString(FlagStr,fn):
                    fullfilename=os.path.join(FindPath,fn)
                    FileList.append(fullfilename)
            else:
                fullfilename=os.path.join(FindPath,fn)
                FileList.append(fullfilename)
    
    if len(FileList)>0:
        FileList.sort()
        
    return FileList
 
 
 
train_txt=open('E:/caffe/MRI/MRI_yihang/train.txt','w')
#製作標籤資料,如果是狗的,標籤設定為0,如果是貓的標籤為1
imgfile=GetFileList('E:/caffe/MRI/MRI_yihang/train_normal')#將資料集放在與.py檔案相同目錄下
for img in imgfile:
    img=img.replace('_normal','') #將_資料夾名中的_normal刪掉
    img=img.replace('/','\\')
    str1=img+' '+'0'+'\n'        #用空格代替轉義字元 \t 
    train_txt.writelines(str1)
    
 
imgfile=GetFileList('E:/caffe/MRI/MRI_yihang/train_tumor')
for img in imgfile:
    img=img.replace('_tumor','')
    img=img.replace('/','\\')
    str2=img+' '+'1'+'\n'
    train_txt.writelines(str2)
train_txt.close()
 
 
#測試集檔案列表
test_txt=open('E:/caffe/MRI/MRI_yihang/test.txt','w')
#製作標籤資料,如果是男的,標籤設定為0,如果是女的標籤為1
imgfile=GetFileList('E:/caffe/MRI/MRI_yihang/test_normal')#將資料集放在與.py檔案相同目錄下
for img in imgfile:
    img=img.replace('_normal','')
    img=img.replace('/','\\')
    str3=img+' '+'0'+'\n'
    test_txt.writelines(str3)
    
 
imgfile=GetFileList('E:/caffe/MRI/MRI_yihang/test_tumor')
for img in imgfile:
    img=img.replace('_tumor','')
    img=img.replace('/','\\')
    str4=img+' '+'1'+'\n'
    test_txt.writelines(str4)
test_txt.close()
 
print("成功生成檔案列表")

結果:

自動建立txt檔案

txt內容:前面沒有截的是路徑部分

但是這樣會出問題。因為每個標籤都是用整個檔案路徑(e:picture.jpg  0),後面轉換資料時,程式會自動幫你加上路徑,變成:e:picture.jpge:picture.jpg 0 ,重複了。所以要吧前面的路徑刪掉,只保留檔名和標籤(注意檔名最前面要加\)

最終程式碼:

# -*- coding: utf-8 -*-
"""
Spyder Editor

This is a temporary script file.
"""

#<pre class="python" name="code">#coding:utf-8
'''
Created on Jul 29, 2016
@author: sgg
'''
 
"<span style=""font-family:Arial;font-size:18px;"">"
"<span style=""font-size:18px;"">"
"<span style=""font-size:18px;"">" 
import os
 
def IsSubString(SubStrList,Str):
    flag=True
    for substr in SubStrList:
        if not(substr in Str):
            flag=False
    
    return flag
 
#掃面檔案
def GetFileList(FindPath,FlagStr=[]):
    FileList=[]
    FileNames=os.listdir(FindPath)
    if len(FileNames)>0:
        for fn in FileNames:
            if len(FlagStr)>0:
                if IsSubString(FlagStr,fn):
                    fullfilename=os.path.join(FindPath,fn)
                    FileList.append(fullfilename)
            else:
                fullfilename=os.path.join(FindPath,fn)
                FileList.append(fullfilename)
    
    if len(FileList)>0:
        FileList.sort()
        
    return FileList
 
 
 
train_txt=open('E:/caffe/MRI/MRI_yihang/train.txt','w')
#製作標籤資料,如果是狗的,標籤設定為0,如果是貓的標籤為1
imgfile=GetFileList('E:/caffe/MRI/MRI_yihang/train_normal')#將資料集放在與.py檔案相同目錄下
for img in imgfile:
#    img=img.replace('_normal','') #將_資料夾名中的_normal刪掉
#    img=img.replace('/','\\')
    str1=img
    str1=str1.split('\\')[-1]
    str1=str1+' '+'0'+'\n'        #用空格代替轉義字元 \t 
    str0='\\'
    str0=str0+str1
    train_txt.writelines(str0)
    
 
imgfile=GetFileList('E:/caffe/MRI/MRI_yihang/train_tumor')
for img in imgfile:
#    img=img.replace('_tumor','')
#    img=img.replace('/','\\')
    str2=img
    str2=str2.split('\\')[-1]
    str2=str2+' '+'1'+'\n'
    str0='\\'
    str0=str0+str2
    train_txt.writelines(str0)
train_txt.close()
 
 
#測試集檔案列表
test_txt=open('E:/caffe/MRI/MRI_yihang/test.txt','w')
#製作標籤資料,如果是男的,標籤設定為0,如果是女的標籤為1
imgfile=GetFileList('E:/caffe/MRI/MRI_yihang/test_normal')#將資料集放在與.py檔案相同目錄下
for img in imgfile:
#    img=img.replace('_normal','')
#    img=img.replace('/','\\')
    str3=img
    str3=str3.split('\\')[-1]
    str3=str3+' '+'0'+'\n'
    str0='\\'
    str0=str0+str3
    test_txt.writelines(str0)
    
 
imgfile=GetFileList('E:/caffe/MRI/MRI_yihang/test_tumor')
for img in imgfile:
#    img=img.replace('_tumor','')
#    img=img.replace('/','\\')
    str4=img
    str4=str4.split('\\')[-1]
    str4=str4+' '+'1'+'\n'
    str0='\\'
    str0=str0+str4
    test_txt.writelines(str0)
test_txt.close()
 
print("成功生成檔案列表")