【问题标题】:How to implement batching on a folder by folder basis如何逐个文件夹实现批处理
【发布时间】:2020-04-13 13:15:38
【问题描述】:

我有一个使用 MTCNN 人脸检测库的脚本,它遍历大量目录,总共有数千张图像。我在使用此脚本时遇到的一个问题是在处理所有这些图像时内存使用过多,这最终会导致我的 MacBook(16gb 的 RAM)内存不足。我要做的是逐个文件夹实现批处理,而不是特定的批处理限制,因为没有一个文件夹单独包含足够的图像,这会使系统内存不足。

# open up the csv file
with open(csv_path, 'w', newline='') as file:
    writer = csv.writer(file)
    writer.writerow(['Index', 'Threshhold', 'Path'])

for path, subdirs, files in os.walk(path):
    for name in files:
        if name == '.DS_Store':
            print("Skipping .DS_Store")
            continue
        else:
            try:
                image = os.path.join(path, name)
                pixels = pyplot.imread(image)

                print("Processing " + image)
                print("Count: " + str(inc))

                # calculate the area of the image
                total_height = pixels.shape[0]
                total_width = pixels.shape[1]

                total_area = total_height * total_width

                # create the detector, using default weights
                detector = MTCNN()
                faces = detector.detect_faces(pixels)

                ax = pyplot.gca()

                face_total_area = 0

                if faces == []:
                    print("No faces detected.")
                    # pass in 0 for the threshold becuase there's no faces
                    #write_to_csv(inc, 0, image)
                    print()
                else:
                    for face in faces:
                        # get dimensions from the face
                        x, y, width, height = face['box']

                        # calculate the area of the face
                        face_area = width * height
                        face_total_area += face_area

                    threshold = face_total_area / total_area

                    # write to csv only if the threshold is less than the limit 
                    # change back to this eventually ^^^^^^^^^
                    if threshold > threshhold_limit:
                        print("Facial area is over the threshold - writing file path to csv.")
                        write_to_csv(inc, threshold, image)
                    else:
                        print("Image threshold is under the limit - good")

                    print(threshold)
                    print()

                inc += 1
            except:
                print("Processing error - skipping image")

这样的事情可以做吗?还是应该以不同的方式完成?这个想法是,像这样的批处理将允许 mtcnn 在处理完该文件夹时释放它所持有的内存。

【问题讨论】:

    标签: python-3.x batching


    【解决方案1】:

    此程序不应增加内存使用量,因为它不会将数据从一张图像累积到下一张图像。因此,您所要求的将无效。您是否尝试过在 Python 笔记本之外运行相同的代码?作为一个独立的程序?可能是笔记本保留了对所有已读图像的引用。 要么,要么找到一个真正重置 pyplot 在最内层循环内的内部状态的调用。 (也许pyplot.clf())。

    正如您所说的“批处理”是在第一个 for 循环中发生的,它将为您树中的每个文件夹运行一次。您可能拥有的唯一好处是在第一个循环内重置内部状态,但在第二个循环之外(for name in ...),您必须找到完全相同的调用来重置内部状态。

    (另外,附带说明一下,您在 with 块中创建了一个 csv 写入器,该块在块末尾无效 - 您应该重构此代码,而不是为每个新行重新打开 CSV 文件 - (这发生在未显示的write_to_csv 函数中))

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-02-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-28
      相关资源
      最近更新 更多