#!/usr/bin/env python """Out-of-domain check: scanned household objects are not what users upload. Generates 12 transparent RGBA subjects with the base model's text-to-image mode (cartoon mascot, sneaker, armchair, robot toy, potted plant, game character, perfume bottle, rubber duck, desk lamp, backpack, coffee mug, headphones), then runs an 8-view eye-level turntable on each with ckpt2000. Every edit takes the ORIGINAL generated image as source (never a generated one), so errors cannot accumulate across the turntable. Cumulative angles that the 23-instruction grammar cannot express in one move are expressed as the equivalent opposite-side move: 225R=135L, 270R=90L, 315R=45L. Also measures output alpha coverage per view so we can say honestly where transparency breaks. Self-contained: fetches eval_edit.py (pipeline shared with all eval runs), resolves ckpt2000 via hf_hub_download, uploads everything at the end. Usage: python ood_orbit.py --out /out/ood """ import argparse, json, os, sys _here = os.path.dirname(os.path.abspath(__file__)) if _here not in sys.path: sys.path.insert(0, _here) if not os.path.exists(os.path.join(_here, 'eval_edit.py')): from huggingface_hub import hf_hub_download import shutil p = hf_hub_download(repo_id='ysharma/gso-orbit-rgba', repo_type='dataset', filename='eval_edit.py') dst = os.path.join(_here, 'eval_edit.py') if os.path.abspath(p) != os.path.abspath(dst): shutil.copy(p, dst) print('fetched eval_edit.py', flush=True) import eval_edit as E from PIL import Image SUBJECTS = [ ('cartoon_mascot', 'a cute cartoon mascot character, a round orange creature with big eyes and tiny arms, full body, 3d render, centered, isolated on transparent background'), ('sneaker', 'a single red and white running sneaker, side view, product photo, 3d render, centered, isolated on transparent background'), ('armchair', 'a mid-century modern armchair with teal upholstery and wooden legs, 3d render, centered, isolated on transparent background'), ('robot_toy', 'a small friendly retro robot toy with a round head and antenna, glossy plastic, 3d render, centered, isolated on transparent background'), ('potted_plant', 'a potted monstera plant in a terracotta pot, 3d render, centered, isolated on transparent background'), ('game_character', 'a stylized video game character, a knight with a sword and round shield, full body, 3d render, centered, isolated on transparent background'), ('perfume_bottle', 'an elegant glass perfume bottle with a gold cap, 3d render, centered, isolated on transparent background'), ('rubber_duck', 'a yellow rubber duck toy, 3d render, centered, isolated on transparent background'), ('desk_lamp', 'an articulated desk lamp with a white shade, 3d render, centered, isolated on transparent background'), ('backpack', 'a modern hiking backpack in green and black, 3d render, centered, isolated on transparent background'), ('coffee_mug', 'a ceramic coffee mug with a simple geometric pattern, 3d render, centered, isolated on transparent background'), ('headphones', 'a pair of over-ear wireless headphones in matte black, 3d render, centered, isolated on transparent background'), ] # 8 eye-level views: original + 7 edits, each sourced from the ORIGINAL image. # Cumulative clockwise angles 45..315 expressed with grammar-legal moves. ORBIT = [ (45, ' rotate the camera 45 degrees to the right, eye level'), (90, ' rotate the camera 90 degrees to the right, eye level'), (135, ' rotate the camera 135 degrees to the right, eye level'), (180, ' rotate the camera 180 degrees, eye level'), (225, ' rotate the camera 135 degrees to the left, eye level'), (270, ' rotate the camera 90 degrees to the left, eye level'), (315, ' rotate the camera 45 degrees to the left, eye level'), ] STEPS = 40 def alpha_cov(img): a = img.split()[3] h = a.histogram() return sum(h[16:]) / (img.size[0] * img.size[1]) def on_gray(im, size=256): bg = Image.new('RGB', im.size, (128, 128, 128)) bg.paste(im, mask=im.split()[3]) return bg.resize((size, size)) def main(): ap = argparse.ArgumentParser() ap.add_argument('--out', required=True) args = ap.parse_args() os.makedirs(args.out, exist_ok=True) lora_path = hf_lora() pipe = E.load_pipe() print('--- generating 12 subjects (T2I, 40 steps, suffix caption) ---', flush=True) for i, (name, prompt) in enumerate(SUBJECTS): out_p = os.path.join(args.out, name) os.makedirs(out_p, exist_ok=True) orig_p = os.path.join(out_p, 'view000_original.png') if not os.path.exists(orig_p): g = __import__('torch').Generator('cuda').manual_seed(1000 + i) img = pipe(prompt=prompt + ' ' + E.ALPHA_SUFFIX, width=768, height=768, num_inference_steps=STEPS, true_cfg_scale=1.0, generator=g ).images[0] if img.mode != 'RGBA': print(f' WARN {name}: T2I returned mode {img.mode}, compositing alpha=255', flush=True) img = img.convert('RGBA') img.save(orig_p) print(f'{name}: generated, mode={img.mode}, alpha_cov={alpha_cov(img):.3f}', flush=True) pipe.load_lora_weights(lora_path) print('--- ckpt2000 loaded, running turntables ---', flush=True) import torch results = [] for i, (name, _) in enumerate(SUBJECTS): out_p = os.path.join(args.out, name) orig_p = os.path.join(out_p, 'view000_original.png') views = [orig_p] for j, (deg, instr) in enumerate(ORBIT): vp = os.path.join(out_p, f'view{j+1:03d}_{deg}deg.png') if not os.path.exists(vp): g = torch.Generator('cuda').manual_seed(2000 + i * 10 + j) img = pipe(image=Image.open(orig_p).convert('RGBA'), prompt=instr + ' ' + E.ALPHA_SUFFIX, width=768, height=768, num_inference_steps=STEPS, true_cfg_scale=1.0, generator=g ).images[0] if img.mode != 'RGBA': img = img.convert('RGBA') img.save(vp) views.append(vp) cov = alpha_cov(Image.open(vp)) results.append({'subject': name, 'deg': deg, 'alpha_cov': round(cov, 4)}) print(f'{name} +{deg}deg: alpha_cov={cov:.3f}', flush=True) # contact sheet: 8 views on mid-gray sheet = Image.new('RGB', (256 * 8, 256), (30, 30, 30)) for k, vp in enumerate(views): sheet.paste(on_gray(Image.open(vp)), (k * 256, 0)) sheet.save(os.path.join(args.out, f'sheet_{name}.png')) # animated turntable GIF frames = [on_gray(Image.open(vp), 320) for vp in views] frames += frames[1:-1][::-1] # ping-pong back to start frames[0].save(os.path.join(args.out, f'turntable_{name}.gif'), save_all=True, append_images=frames[1:], duration=350, loop=0) print(f'{name}: sheet + turntable done', flush=True) json.dump(results, open(os.path.join(args.out, 'alpha_cov.json'), 'w'), indent=2) lows = [r for r in results if r['alpha_cov'] < 0.05] print(f'alpha coverage summary: {len(results)} views, {len(lows)} below 5% coverage', flush=True) from huggingface_hub import HfApi HfApi().upload_folder(folder_path=args.out, repo_id='ysharma/gso-orbit-rgba', repo_type='dataset', path_in_repo='eval/ood', commit_message='OOD 12-subject orbit check (ckpt2000)') print('uploaded -> ysharma/gso-orbit-rgba/eval/ood', flush=True) print('DONE', flush=True) def hf_lora(): from huggingface_hub import hf_hub_download p = hf_hub_download(repo_id='ysharma/orbit-alpha-lora', filename='checkpoints/steps2000res768/orbit_alpha_lora/orbit_alpha_lora.safetensors') print(f'using lora: {p}', flush=True) return p if __name__ == '__main__': main()