Add dataset building scripts
Browse filesFiles changed (10)
- .gitattributes+6
- scripts/assemble_render.pyAdded+37
- scripts/hdri/DayEnvironmentHDRI015_8K-HDR.exrAdded
- scripts/hdri/DaySkyHDRI037B_8K-HDR.exrAdded
- scripts/hdri/IndoorEnvironmentHDRI003_8K-HDR.exrAdded
- scripts/hdri/IndoorEnvironmentHDRI005_8K-HDR.exrAdded
- scripts/hdri/studio_small_02_4k.exrAdded
- scripts/make_crops.pyAdded+107
- scripts/render_ambient.blendAdded+3
- scripts/render_ambient.pyAdded+117
54 unmodified lines55565754 unmodified lines*.webp filter=lfs diff=lfs merge=lfs -textmaps/train/Wood.z01 filter=lfs diff=lfs merge=lfs -textmaps/train/Ground.z01 filter=lfs diff=lfs merge=lfs -text54 unmodified lines55565758596061626354 unmodified lines*.webp filter=lfs diff=lfs merge=lfs -textmaps/train/Wood.z01 filter=lfs diff=lfs merge=lfs -textmaps/train/Ground.z01 filter=lfs diff=lfs merge=lfs -textscripts/hdri/DayEnvironmentHDRI015_8K-HDR.exr filter=lfs diff=lfs merge=lfs -textscripts/hdri/DaySkyHDRI037B_8K-HDR.exr filter=lfs diff=lfs merge=lfs -textscripts/hdri/IndoorEnvironmentHDRI003_8K-HDR.exr filter=lfs diff=lfs merge=lfs -textscripts/hdri/IndoorEnvironmentHDRI005_8K-HDR.exr filter=lfs diff=lfs merge=lfs -textscripts/hdri/studio_small_02_4k.exr filter=lfs diff=lfs merge=lfs -textscripts/render_ambient.blend filter=lfs diff=lfs merge=lfs -text
12345678910111213141516171819202122232425262728293031323334353637import argparsefrom pathlib import Path
import torchvision.transforms.functional as TFfrom PIL import Imagefrom tqdm import tqdm
if __name__ == "__main__": # Create argument parser parser = argparse.ArgumentParser(description="Assemble renders.") parser.add_argument("--source_dir", required=True, help="Directory where the dataset is stored.") args = parser.parse_args()
source_dir = Path(args.source_dir)
# Find all materials for render_dir in tqdm([x for x in source_dir.glob("**/renders/")]): passes_dir = render_dir/"passes" num_renders = len(list(passes_dir.glob("*diffuse.png"))) for i in range(num_renders): diff_path = passes_dir/f"render_{i:02d}_diffuse.png" glossy_path = passes_dir/f"render_{i:02d}_glossy.png"
full_path = render_dir/f"render_{i:02d}.png"
diffuse = TF.to_tensor(Image.open(diff_path)) glossy = TF.to_tensor(Image.open(glossy_path))
diffuse = TF.adjust_gamma(diffuse, 2.2) glossy = TF.adjust_gamma(glossy, 2.2)
render = diffuse + glossy
render = TF.adjust_gamma(render, 1/2.2) render = TF.to_pil_image(render) render.save(full_path)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107import argparseimport mathimport threadingfrom pathlib import Path
import numpy as npimport torchimport torchvision.transforms.functional as TFfrom PIL import Imagefrom tqdm import tqdm
def rotate_normal_map(normal_map, angle_deg): angle_rad = angle_deg * (torch.pi / 180.0)
normal_map = normal_map * 2.0 - 1.0 # Convert to [-1, 1] normal_map = normal_map.unsqueeze(0) # Add batch dimension
# Rotate the Vectors rotation_matrix = torch.tensor([[math.cos(angle_rad), -math.sin(angle_rad), 0], [math.sin(angle_rad), math.cos(angle_rad), 0], [0, 0, 1]], device=normal_map.device)
# Reshape for batch matrix multiplication reshaped_normal_map = normal_map.view(1, 3, -1) # Reshape to [1, 3, H*W] rotation_matrix = rotation_matrix.view(1, 3, 3) # Add batch dimension
# Rotate the vectors rotated_vectors = torch.bmm(rotation_matrix, reshaped_normal_map) rotated_vectors = rotated_vectors.view(1, 3, normal_map.size(2), normal_map.size(3))
rotated_vectors = rotated_vectors / 2.0 + 0.5 # Convert back to [0, 1]
return rotated_vectors[0]
def process_map(map, mat_dest): map_name = map.stem img = Image.open(map) img = TF.to_tensor(img).cuda() img = TF.resize(img, (4096, 4096), antialias=True)
img = img.repeat(1, 3, 3) img = TF.center_crop(img, (5793, 5793))
for rot_angle in range(0, 360, 45): crop_i = 0
if "normal" in map_name: rot_img = rotate_normal_map(img, axis='z', angle_deg=rot_angle) rot_img = TF.rotate(rot_img, rot_angle) else: rot_img = TF.rotate(img, rot_angle)
rot_img = TF.center_crop(rot_img, (4096, 4096)) for crop_res in [4096, 2048, 1024]: # split into crops crops = rot_img.unfold(1, crop_res, crop_res).unfold(2, crop_res, crop_res) crops = crops.permute(1, 2, 0, 3, 4) crops = crops.reshape(-1, crops.size(2), crop_res, crop_res)
for crop in crops: crop_dir = mat_dest / f"rot_{rot_angle:03d}_crop_{crop_i:03d}" crop_dir.mkdir(parents=True, exist_ok=True)
crop = TF.resize(crop, (1024, 1024), antialias=True) if map_name in ["height", "displacement"]: crop = crop.permute(1, 2, 0).cpu().numpy() crop = crop.astype(np.uint16) crop = Image.fromarray(crop[..., 0]) crop.save(crop_dir / f"{map_name}.png") else: TF.to_pil_image(crop).save(crop_dir / f"{map_name}.png")
crop_i += 1
if __name__ == "__main__": # Create argument parser parser = argparse.ArgumentParser(description="Make dataset crops.") parser.add_argument("--source_dir", required=True, help="Directory where the original 4K maps are stored.") parser.add_argument("--dest_dir", required=True , help="Destination directory to store the 1K crops.") args = parser.parse_args()
source_dir = Path(args.source_dir) dest_dir = Path(args.dest_dir)
# Find all materials in the source directory for file in tqdm([x for x in source_dir.glob("**/basecolor.png")]): mat_dir = file.parent
name = mat_dir.stem category = mat_dir.parent.stem split = mat_dir.parent.parent.stem
mat_dest = dest_dir / split / category / name mat_dest.mkdir(parents=True, exist_ok=True)
thread = [] for map in mat_dir.glob("*.png"): t = threading.Thread(target=process_map, args=(map, mat_dest)) t.start()
thread.append(t)
for t in thread: t.join()123version https://git-lfs.github.com/spec/v1oid sha256:09fce0c9a535c86bfa812378c74f826bae8eaa1077a0629b4b878fc1eae2f6casize 1231560123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117import mathimport randomimport sysfrom pathlib import Path
import bpy
argv = sys.argvargv = argv[argv.index("--") + 1:] # get all args after "--"dset_dir = Path(argv[0])
def create_node_once(nodes, name, location=(0,0)): existing = nodes.get(name) if existing is not None: return existing new_node = nodes.new('ShaderNodeTexImage') new_node.name = name new_node.location = location return new_node
def load_map_image(node, img_path, name, colorspace="sRGB"): existing = bpy.data.images.get(name) if existing is not None: bpy.data.images.remove(existing) node.image = bpy.data.images.load(img_path) node.image.colorspace_settings.name = colorspace node.image.name = name
bpy.context.scene.render.image_settings.file_format = "PNG"
material = bpy.data.materials.get("BSDFPlane")
# Set node tree editingmaterial.use_nodes = Truenodes = material.node_tree.nodes
# Compositing nodesscene = bpy.context.scenescene.render.use_compositing = True
# Get output nodesp_out = nodes['Principled Output']
# Get principled BSDF nodebsdf_node = nodes.get("Principled BSDF")
normal_map_node = nodes.get("Normal Map")displacement_node = nodes.get("Displacement")scale_node = nodes.get("Scale")
# Get background nodebg_node = bpy.data.worlds['World'].node_tree.nodes["Background"]
# Get cameraobj_camera = bpy.data.objects["Camera"]# Get the camera data blockcamera_data = obj_camera.data
for item_bc in [x for x in dset_dir.glob("**/basecolor.png")][:]: item = item_bc.parent if (item/"renders/passes").exists() and len(list(Path(item/"renders/passes").glob('*'))) == 10: continue
# Clear orphan data bpy.ops.outliner.orphans_purge()
# Set maps paths basecolor_path = str(item/"basecolor.png") normal_path = str(item/"normal.png") roughness_path = str(item/"roughness.png") metallic_path = str(item/"metallic.png") height_path = str(item/"displacement.png")
# Setup nodes basecolor_node = nodes.get("Base Color") load_map_image(basecolor_node, basecolor_path, name="BaseColor")
normal_node = nodes.get("Normal") load_map_image(normal_node, normal_path, name="Normal", colorspace="Non-Color")
roughness_node = nodes.get("Roughness") load_map_image(roughness_node, roughness_path, name="Roughness", colorspace="Non-Color")
metallic_node = nodes.get("Metallic") load_map_image(metallic_node, metallic_path, name="Metallic", colorspace="Non-Color")
height_node = nodes.get("Height") load_map_image(height_node, height_path, name="Height", colorspace="Non-Color")
scale_node = nodes.get("Scale") # Set render settings render_folder = item/"renders/passes" render_folder.mkdir(exist_ok=True, parents=True)
for hdri_i in range(5): env_node = bpy.data.worlds['World'].node_tree.nodes[f"Env {hdri_i}"] bpy.data.worlds['World'].node_tree.links.new(env_node.outputs['Color'], bg_node.inputs['Color']) bpy.data.worlds['World'].node_tree.nodes[f"HDRIRotation"].outputs[0].default_value = random.random() * math.pi * 2
for render_pass in ["diffuse", "glossy"]: if render_pass == "diffuse": scene.node_tree.nodes.active = scene.node_tree.nodes["DiffusePass"] scale_node.outputs[0].default_value = 1.0 camera_data.type = 'ORTHO' elif render_pass == "glossy": scene.node_tree.nodes.active = scene.node_tree.nodes['GlossyPass'] scale_node.outputs[0].default_value = 0.0 camera_data.type = 'PERSP'
# Render bpy.context.scene.render.filepath = str(render_folder/f"render_{hdri_i:02d}_{render_pass}.png") bpy.ops.render.render(write_still=True)
# Execution command: blender.exe -b .\render_ambient.blend -P .\render_ambient.py -- ..\maps.gitattributes+6
scripts/assemble_render.pyAdded+37
scripts/hdri/DayEnvironmentHDRI015_8K-HDR.exrAdded
Large File Pointer Details
- SHA256: cbb14b550924402ba0132e6e225c11fc62088313e9cea399695c81a9e6f469b9
- Pointer size: 134 Bytes
- Size of remote file: 137 MB
scripts/hdri/DaySkyHDRI037B_8K-HDR.exrAdded
Large File Pointer Details
- SHA256: 36e45a121d7b2d0bfeed0be16e5afb4cfc936e05aeaa72f26e89b5c84ce38ca6
- Pointer size: 133 Bytes
- Size of remote file: 40.6 MB
scripts/hdri/IndoorEnvironmentHDRI003_8K-HDR.exrAdded
Large File Pointer Details
- SHA256: aa2935f36a1ac93ceab58d508721c83fd8f1e9e64c12fe231b47752c9448f80a
- Pointer size: 133 Bytes
- Size of remote file: 96 MB
scripts/hdri/IndoorEnvironmentHDRI005_8K-HDR.exrAdded
Large File Pointer Details
- SHA256: d7ee98faf82aa261ae193661e3834127276d866f6b0efddf7ff3f2f84efd3f47
- Pointer size: 134 Bytes
- Size of remote file: 110 MB
scripts/hdri/studio_small_02_4k.exrAdded
Large File Pointer Details
- SHA256: f1a407bd80d477c482e3a39205d0bbc53905328d77e00a2f024993e07c808215
- Pointer size: 133 Bytes
- Size of remote file: 21.7 MB
scripts/make_crops.pyAdded+107
scripts/render_ambient.blendAdded+3
scripts/render_ambient.pyAdded+117