【问题标题】:Get train data generator and save them获取火车数据生成器并保存它们
【发布时间】: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


【解决方案1】:

查看您的代码,我认为它默默地失败了:

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

您没有提供任何有关异常的信息,因此如果此行失败 (无论出于何种原因,即使是拼写错误或参数数量错误)你会 从未见过会发生什么,脚本将继续运行 for 循环。

首先,您可以删除try/except 并明确查看问题所在, 有了这些信息,您就可以继续调试了。

更多提示:

  1. 不要使用try/except,而是:
try:
    # ..code
except Exception as exc:
    # something went wrong, at least print the exception
    print(exc)
  1. python有一个叫做pdb的调试器,你可以开始调试一个程序 逐行检查变量值。 从python -m pdb yourprogram.py 开始并继续调试。 您应该研究一下pdb 命令才能做到这一点,但这 现在会有用,请查看TutorialOfficial Docs

【讨论】:

  • 当您评论尝试时它可以运行。当我运行 pdb 并到达“X、Y、image_data、debug_img、debug_num_pos = next(data_gen_train)”时,它显示一个空白终端。可能是什么问题
猜你喜欢
  • 2023-03-03
  • 2021-12-30
  • 1970-01-01
  • 2020-01-20
  • 2023-03-11
  • 1970-01-01
  • 1970-01-01
  • 2020-05-10
  • 1970-01-01
相关资源
最近更新 更多