原来通过np.concatenate方法拼接加载的数据,发现比较耗内存,现在改成通过append方法加载图像数据。
需要注意:通过append方法加载后的数据类型是list,要想喂给卷积神经网络训练,需要转换为np.ndarray类型。
import cv2
import numpy as np
'''ROI数据集'''
open_path = 'C:/Users/panansi/Desktop/easyAI'
read_path = 'C:/Users/panansi/Desktop/easyAI'
x_test = []
y_test = []
for line in open('{}/{}'.format(open_path,'/test/sick/sick.txt'), 'r'):
file_name = line[:-3].replace('img','bmp')
img = cv2.imread('{}/test/sick/{}'.format(read_path, file_name))#, flags=-1)
img = img / 255.0
img = img.astype(np.float32)
x_test.append(img)
y_test.append(1)
#cv2.imwrite('{}/train/sick/{}'.format(write_path, file_name), img)
#############################################################################
i=0
for line in open('{}/{}'.format(open_path,'/test/unsick/unsick.txt'), 'r'):
file_name = line[:-3].replace('img','bmp')
img = cv2.imread('{}/test/unsick/{}'.format(read_path, file_name))#, flags=-1)
img = img / 255.0
img = img.astype(np.float32)
x_test.append(img)
y_test.append(0)
for line in open('{}/{}'.format(open_path,'/train/sick/sick.txt'), 'r'):
file_name = line[:-3].replace('img','bmp')
img = cv2.imread('{}/train/sick/{}'.format(read_path, file_name))#, flags=-1)
img = img / 255.0
img = img.astype(np.float32)
x_test.append(img)
y_test.append(1)
#cv2.imwrite('{}/train/sick/{}'.format(write_path, file_name), img)
#############################################################################
i=0
for line in open('{}/{}'.format(open_path,'/train/unsick/unsick.txt'), 'r'):
file_name = line[:-3].replace('img','bmp')
img = cv2.imread('{}/train/unsick/{}'.format(read_path, file_name))#, flags=-1)
img = img / 255.0
img = img.astype(np.float32)
x_test.append(img)
y_test.append(0)
x_test = np.reshape(x_test, (len(x_test), x_test[1].shape[0], x_test[1].shape[1], x_test[1].shape[2]))
y_test = np.reshape(y_test, (len(y_test), 1))