DLR-RM / BlenderProc

A procedural Blender pipeline for photorealistic training image generation
GNU General Public License v3.0
2.85k stars 454 forks source link

loop slows over time #1129

Open soans1994 opened 4 months ago

soans1994 commented 4 months ago

Describe the issue

I am facing the issue of loop becomes slower over time when the list length is over 1000. Can you please check my code below and see if it is problem of ubuntu system or code.

Thank you

Minimal code example

import blenderproc as bproc
import argparse
import os
import numpy as np
import random
import bpy
import math
from tqdm import tqdm
from random import shuffle
random.seed = 42

def ListArranger(lst):
    lst_copy = lst[:]
    shuffle(lst_copy)
    return lst_copy

parser = argparse.ArgumentParser()
parser.add_argument('camera', nargs='?', default="camera_positions", help="Path to the camera file")
parser.add_argument('scene', nargs='?', default="3.blend", help="Path to the scene.blend file")
parser.add_argument('output_dir', nargs='?', default="output_poi", help="Path to where the final files will be saved ")
parser.add_argument('output_dir2', nargs='?', default="output_poi/depth", help="Path to where the final files will be saved ")
args = parser.parse_args()

bproc.init()
bproc.renderer.enable_depth_output(activate_antialiasing=False)
import_path = "renamed_all"#combined_all
file_list = sorted(os.listdir(import_path))
file_list = ListArranger(file_list)
blend_list = file_list[0::15]#50, 5 train, 15 val

# load the objects into the scene

hdr_path = "hdr"
hdr_list = sorted(os.listdir(hdr_path))

for single in tqdm(blend_list):  
    # remove objects Will collect meshes from delete objects
    meshes = set()
    #remove curves
    for obj in bpy.data.objects:
        if obj.type == 'MESH':
            # Delete the object
            bpy.data.objects.remove(obj)
    # Look at meshes that are orphean after objects removal
    for mesh in [m for m in meshes if m.users == 0]:
        # Delete the meshes
        bpy.data.meshes.remove(mesh)

    objs = bproc.loader.load_blend("renamed_all/"+single)#combined_all

    for j, obj in enumerate(objs):
        if obj.get_attr("name").startswith("Curve"):
             obj.set_cp("category_id", 0)
        elif obj.get_attr("name").startswith("misc"):
            obj.set_cp("category_id", 1)
        elif obj.get_attr("name").startswith("ramp"):#remove later
            obj.set_cp("category_id", 1)

    bproc.world.set_world_background_hdr_img("hdr/"+hdr_list[random.randrange(len(hdr_list))]) # ,strength=1.0 #0.5?

    # define the camera intrinsics
    bproc.camera.set_resolution(512, 512)#512,512

    poi = bproc.object.compute_poi(objs)

    for i in range(1): #30position
        position, euler_rotation = (random.uniform(30,10),random.uniform(-50,-70),random.uniform(0,0)),(random.uniform(0,0),random.uniform(0,0),random.uniform(0,0))
        rotation_matrix = bproc.camera.rotation_from_forward_vec(poi - position, inplane_rot=np.random.uniform(0, 0))
        cam2world_matrix = bproc.math.build_transformation_mat(position, rotation_matrix)#final used for paper
        bproc.camera.add_camera_pose(cam2world_matrix)

    bproc.renderer.enable_segmentation_output(map_by=["category_id", "instance", "name"])

    # Enable motion blur
    bproc.renderer.enable_motion_blur(motion_blur_length=random.uniform(0,0.1))#0.5 too much but annot remain insitial position
    # render the whole pipeline
    bproc.renderer.set_output_format(enable_transparency=True) # hdr only light remove background
    data = bproc.renderer.render()

    bproc.writer.write_hdf5(args.output_dir2, data, append_to_existing_output=True)                   
    # Write data to coco file
    bproc.writer.write_coco_annotations(os.path.join(args.output_dir, 'coco_data'),
                            instance_segmaps=data["instance_segmaps"],
                            instance_attribute_maps=data["instance_attribute_maps"],
                            colors=data["colors"],
                            color_file_format="PNG")

    bproc.utility.reset_keyframes()
    objs.clear()
    data.clear()

Files required to run the code

No response

Expected behavior

When the list of blend files is less, the code finishes completetly. If list legth is large, the loop time goes from average 3 seconds to 4,5,6,......60 seconds per image

BlenderProc version

main

cornerfarmer commented 4 months ago

Probably there are some leftover data blocks when loading new objects in each iteration. Could you check whether adding bproc.clean_up() in the beginning of each loop solves your problem?

soans1994 commented 3 months ago

@cornerfarmer i tried it but doesnt slow the problem

soans1994 commented 3 months ago

please check out my paper here, I used BlenderProc as the first reference https://www.mdpi.com/2076-3417/14/14/6352

cornerfarmer commented 3 months ago

Hey @soans1994,

could you then provide a minimal example together with the required meshes which reproduces your issue?