【发布时间】:2021-07-01 19:18:25
【问题描述】:
我正在开发一种更快的 RCNN 算法。我有一个生成 ground_truth 锚点的函数 get_anchor_gt。我在以下代码中调用该函数
# Get train data generator which generate X, Y, image_data
data_gen_train = get_anchor_gt(train_imgs, C, get_img_output_length, mode='train')
当我执行以下代码时
X, Y, image_data, debug_img, debug_num_pos = next(data_gen_train)
它开始训练并将其保存在 jupyter 笔记本中,大小增长到 200 mbs 和笔记本粉碎。有没有办法可以保存在硬盘中的某个地方,然后再加载它们?
这是get_anchor_gt
#Generate the ground_truth anchors
def get_anchor_gt(all_img_data, C, img_length_calc_function, mode='train'):
""" Yield the ground-truth anchors as Y (labels)
Args:
all_img_data: list(filepath, width, height, list(bboxes))
C: config
img_length_calc_function: function to calculate final layer's feature map (of base model) size according to input image size
mode: 'train' or 'test'; 'train' mode need augmentation
Returns:
x_img: image data after resized and scaling (smallest size = 300px)
Y: [y_rpn_cls, y_rpn_regr]
img_data_aug: augmented image data (original image with augmentation)
debug_img: show image for debug
num_pos: show number of positive anchors for debug
"""
while True:
for img_data in all_img_data:
try:
# read in image, and optionally add augmentation
if mode == 'train':
img_data_aug, x_img = augment(img_data, C, augment=True)
else:
img_data_aug, x_img = augment(img_data, C, augment=False)
(width, height) = (img_data_aug['width'], img_data_aug['height'])
(rows, cols, _) = x_img.shape
assert cols == width
assert rows == height
# get image dimensions for resizing
(resized_width, resized_height) = get_new_img_size(width, height, C.im_size)
# resize the image so that smalles side is length = 300px
x_img = cv2.resize(x_img, (resized_width, resized_height), interpolation=cv2.INTER_CUBIC)
debug_img = x_img.copy()
try:
y_rpn_cls, y_rpn_regr, num_pos = calc_rpn(C, img_data_aug, width, height, resized_width, resized_height, img_length_calc_function)
except:
continue
# Zero-center by mean pixel, and preprocess image
x_img = x_img[:,:, (2, 1, 0)] # BGR -> RGB
x_img = x_img.astype(np.float32)
x_img[:, :, 0] -= C.img_channel_mean[0]
x_img[:, :, 1] -= C.img_channel_mean[1]
x_img[:, :, 2] -= C.img_channel_mean[2]
x_img /= C.img_scaling_factor
x_img = np.transpose(x_img, (2, 0, 1))
x_img = np.expand_dims(x_img, axis=0)
y_rpn_regr[:, y_rpn_regr.shape[1]//2:, :, :] *= C.std_scaling
x_img = np.transpose(x_img, (0, 2, 3, 1))
y_rpn_cls = np.transpose(y_rpn_cls, (0, 2, 3, 1))
y_rpn_regr = np.transpose(y_rpn_regr, (0, 2, 3, 1))
yield np.copy(x_img), [np.copy(y_rpn_cls), np.copy(y_rpn_regr)], img_data_aug, debug_img, num_pos
except Exception as e:
print(e)
continue
【问题讨论】:
-
您好,从您的代码中我无法完全理解您正在操作的上下文:您正在导入和使用哪些库,
get_anchor_gt函数来自哪里...请提供更多信息跨度> -
嗨 Piertoni 我添加了“get_anchor_gt”的代码。希望你有更多的信息来解决这个问题
-
只是想给出一些提示:1) 200mb 的内存不是那么多,尝试在普通 python 环境中在 jupyter 之外执行 2) 你可以删除
debug_img创建以消耗更少的内存。 3)从你所说的看来,当你执行指令... next(data_gen_train)时永远不会返回?好像有点奇怪…… -
我执行了一个普通的 python 脚本,它显示一个空白终端,并且在几个小时后没有得到结果。第 1 步不起作用。2 我删除了 debug_img 并且仍然得到空白终端。可能是什么问题
标签: python save conv-neural-network generator