1.深度学习中经常要将图像的文件名写入txt目录中
首先 ls -l *.tif >1.txt
然后,用notepad++的多行处理功能处理生成的txt文件
2.用python编程,读取一个文件夹下所有指定的文件的文件名到List,然后操作该List,按照train,test,val划分,将划分后的文件名写入文本中
import glob
import random
imgs_listFull = glob.glob("/media/mlxuan/winD/DeepLearningData/ISPRS_BENCHMARK_DATASETS/Potsdam/imageIRRG/*.tif")#返回所匹配的文件名列表
imgs_list=[i[len("/media/mlxuan/winD/DeepLearningData/ISPRS_BENCHMARK_DATASETS/Potsdam/imageIRRG/"):] for i in imgs_listFull]
train_per = 0.8
valid_per = 0.1
test_per = 0.1
random.seed(666)
random.shuffle(imgs_list)
imgs_num = len(imgs_list)
train_point = int(imgs_num * train_per)
valid_point = int(imgs_num * valid_per)
trainList = imgs_list[0:train_point]
valList = imgs_list[train_point:train_point+valid_point]
testList = imgs_list[train_point+valid_point:imgs_num-1]
fileTrainIRRG = open("/media/mlxuan/winD/DeepLearningData/ISPRS_BENCHMARK_DATASETS/Potsdam/txtForTrain/trainIRRG.txt",'w')
fileValIRRG = open("/media/mlxuan/winD/DeepLearningData/ISPRS_BENCHMARK_DATASETS/Potsdam/txtForTrain/valIRRG.txt",'w')
fileTestIRRG = open("/media/mlxuan/winD/DeepLearningData/ISPRS_BENCHMARK_DATASETS/Potsdam/txtForTrain/testIRRG.txt",'w')
for line in trainList:
line2 = line+' '+line.replace("IRRG","label")
fileTrainIRRG.write(line2)
fileTrainIRRG.write('\n')
for line in valList:
line2 = line+' '+line.replace("IRRG","label")
fileValIRRG.write(line)
fileValIRRG.write('\n')
for line in testList:
line2 = line+' '+line.replace("IRRG","label")
fileTestIRRG.write(line2)
fileTestIRRG.write('\n')
fileTrainIRRG.close()
fileValIRRG.close()
fileTestIRRG.close()
2.不好操作的直接用python编程,比如下面程序,读取源文件中的每一行,对每一行操作后写入新的文件中
file = open("trainLable.txt",'w')
with open("train2.txt", "r") as f:
line = f.readline()
while line:
line2 = line[0:-1]+' '+line.replace("RGB","label")#[0:-1]是为了去除换行键
file.write(line2)
line = f.readline()
file.close
常见的对一行的其他操作:从''-rw-r--r-- 1 mlxuan mlxuan 360154 10月 10 21:15 0.tiff\n'获取最后的0.tiff:
line = f.readline()
imagename = line[0:-1].split()[-1]