我使用cv2
和plt
使用嵌套循环和函数来生成图像。在程序运行期间,直到计算机崩溃之前,内存使用量一直在增加。
我在循环和函数结束时尝试了del
和gc.collection()
,但是没有用。我想知道问题出在哪里?
def seg(mat_data):
data = np.array(mat_data)
signals = []
peaks = biosppy.signals.ecg.christov_segmenter(signal=data, sampling_rate = 500)[0]
for i in range(2,len(peaks)-1):
left_diff = abs(peaks[i-1]-peaks[i])//2
right_diff = abs(peaks[i+1]-peaks[i])//2
x = peaks[i]-left_diff
y = peaks[i]+right_diff
signal = data[x:y]
signals.append(signal)
return signals,peaks
def sig2img(signals,file_name,label,channel):
if label == 'null':
for i, signal in enumerate(signals):
fig = plt.figure(frameon=False)
plt.plot(signal, linewidth=3.5)
plt.xticks([]), plt.yticks([])
for spine in plt.gca().spines.values():
spine.set_visible(False)
filename = 'test_img' + '/' +file_name+'_'+str(label)+'_'+str(channel)+'_'+str(i)+'.png'
fig.savefig(filename)
plt.close(fig)
im_gray = cv2.imread(filename, cv2.IMREAD_GRAYSCALE)
im_gray = cv2.resize(im_gray, (128, 128), interpolation = cv2.INTER_LANCZOS4)
cv2.imwrite(filename, im_gray)
return
for i, signal in enumerate(signals):
fig = plt.figure(frameon=False)
plt.plot(signal, linewidth=3.5)
plt.xticks([]), plt.yticks([])
for spine in plt.gca().spines.values():
spine.set_visible(False)
filename = 'train_img' + '/' +file_name+'_'+str(label)+'_'+str(channel)+'_'+str(i)+'.png'
fig.savefig(filename)
plt.close(fig)
im_gray = cv2.imread(filename, cv2.IMREAD_GRAYSCALE)
im_gray = cv2.resize(im_gray, (128, 128), interpolation = cv2.INTER_LANCZOS4)
cv2.imwrite(filename, im_gray)
def generate_images(train = True,cut=5000):
if train == False:
data_path = 'data/test'
test_name = os.listdir(data_path)
file_raw = [(scio.loadmat(data_path+'/'+m),) for m in test_name]
data = [ normalize(d[0]['data']) for d in file_raw]
for i in range(len(test_name)):
print(test_name[i])
print(data[i].shape)
label = 'null'
for j in range(data[i].shape[0]):
print(data[i][j,:cut].shape)
signals,peaks = seg(data[i][j,:cut])
channel = j+1
sig2img(signals,test_name[i],label,channel)
data_path = 'data/train'
reference_path = 'data/reference.txt'
mat_label = pd.read_csv(reference_path,sep='\t',header = None)
mat = mat_label[0]
file_raw = [(scio.loadmat(data_path+'/'+m),) for m in mat]
train_name = [m for m in mat]
label = mat_label[1]
data = [ normalize(d[0]['data']) for d in file_raw] #data shape: 12x5000
for i in range(len(label)):
print(train_name[i])
print(data[i].shape)
print(label[i])
for j in range(data[i].shape[0]):
print(data[i][j,:cut].shape)
signals,peaks = seg(data[i][j,:cut]) #peaks is a list with length of 6~10
channel = j+1 #signals is numpy array with shape of (500,)
sig2img(signals,train_name[i],label[i],channel)
我想知道应该在循环运行期间在何处添加del
或gc
,或通过其他方式释放痛苦。