读文件,29行list index out of range
本帖最后由 猪猪虾 于 2020-8-17 21:00 编辑train_path = 'E:/BaiduNetdiskDownload/rootplanning/data_cnn/train_data/train_img/' #图片路径
train_txt ='E:/BaiduNetdiskDownload/rootplanning/data_cnn/train_data/train_img_to_label.txt'#配对好的标签文件
x_train_savepath ='E:/BaiduNetdiskDownload/rootplanning/data_cnn/train_data/train_img_datasets.npy'#输入(特征)图像数据集
y_train_savepath ='E:/BaiduNetdiskDownload/rootplanning/data_cnn/train_data/train_label_datasets.npy'#生成的标签数据集
test_path = 'E:/BaiduNetdiskDownload/rootplanning/data_cnn/test_data/test_img/'
test_txt ='E:/BaiduNetdiskDownload/rootplanning/data_cnn/test_data/test_img_to_label.txt'
x_test_savepath = 'E:/BaiduNetdiskDownload/rootplanning/data_cnn/test_data/test_img_datasets.npy'
y_test_savepath = 'E:/BaiduNetdiskDownload/rootplanning/data_cnn/test_data/test_label_datasets.npy'
'''
**********************************************************************************
目的:该函数返回输入特征(输入小块)和对应的输入标签,及其路径
一个病人的数据对应n张图片,每张图片又对应n个标签
11-31,像素范围 3763.557 ~-1551.5474
1-5,像素范围3977.636962890625 ~ -1530.5186767578125
*********************************************************************************
'''
def generateds(path,txt,pixel_range):
f = open(txt,'r') #只读形式打开文件
contents = f.readlines() #读取文件中所有的行
f.close()
x,y_ = [],[] #建立空列表,分别存储输入特征和输入特征对应的实际标签
for content in contents:
value = content.split()
img_path = path+value #图片路径+图片名,获得图片索引路径
img = Image.open(img_path) # 读入图片
img = np.array(img.convert('L'))
img = img/pixel_range #归一化数据
x.append(img) #存放图像
y_.append(value)#存放标签数据
print('loading : ' + content)
x = np.array(x)
y_ = np.array(y_)
y_ = y_.astype(np.int64)
return x,y_
if os.path.exists(x_train_savepath) and os.path.exists(y_train_savepath) \
and os.path.exists(x_test_savepath) and os.path.exists(y_test_savepath):
#判断所需要的文件路劲是否已经存在
print('**************load datasets********************')
x_train_save = np.load(x_train_savepath)
y_train = np.load(y_train_savepath)
x_test_save = np.load(x_test_savepath)
y_test = np.load(y_test_savepath)
x_train = np.reshape(x_train_save,(len(x_train_save),32,32))
x_test = np.reshape(x_test_save,(len(x_test_save),32,32))
else:
print('**************Generate datasets********************')
x_train,y_train = generateds(train_path,train_txt, 3763.6 + 1551.5 )
x_test,y_test = generateds(test_path,test_txt, 3977.6 + 1530.5 )
print('**************save datasets********************')
x_train_save = np.reshape(x_train,(len(x_train),-1))
x_test_save = np.reshape(x_test,(len(x_test),-1))
np.save(x_train_savepath,x_train_save)
np.save(y_train_savepath,y_train)
np.save(x_test_savepath,x_test_save)
np.save(y_test_savepath,y_test)
明明是读文件,一行一行的读,怎么会存在索引超出范围的问题,并没有涉及到下标索引啊 给个报错信息…… 永恒的蓝色梦想 发表于 2020-8-17 22:18
给个报错信息……
list index out of range 猪猪虾 发表于 2020-8-17 22:25
list index out of range
给个完整的行不行?
页:
[1]