def calc_tab3(): ddir = sys.argv[1] inds = os.listdir(ddir) outs = [] if len(sys.argv) > 2: inds = inds[:int(sys.argv[2])] for ind in tqdm(inds): if '.txt' in ind: hp = utils.loadHPFromFile(f'{ddir}/{ind}') verts, faces = hier_execute(hp) else: verts, faces = utils.loadObj(f'{ddir}/{ind}') verts = torch.tensor(verts) faces = torch.tensor(faces) outs.append((verts, faces)) misses = 0. results = { 'num_parts': [], 'rootedness': [], 'stability': [], } samples = [] for (verts, faces) in tqdm(outs): results['num_parts'].append(verts.shape[0] / 8.0) samples.append((verts, faces)) if check_rooted(verts, faces): results['rootedness'].append(1.) else: results['rootedness'].append(0.) if check_stability(verts, faces): results['stability'].append(1.) else: results['stability'].append(0.) for key in results: if len(results[key]) > 0: res = torch.tensor(results[key]).mean().item() else: res = 0. results[key] = res results['variance'] = eval_get_var(samples) for key in results: print(f"Result {key} : {results[key]}")
def main(ind): sa = ShapeAssembly() root_lines, has_mid = make_skel() prog_lines = ['Assembly Program_0 {'] for b in root_lines: prog_lines.append('\t'+b) prog_lines.append('}') RP = Program() for l in root_lines: RP.execute(l) base_par = 'mid' if has_mid else 'top' base_lines = make_base_prog( RP.cuboids['base'], RP.cuboids[base_par] ) for i in range(len(prog_lines)): prog_lines[i] = prog_lines[i].replace('base', 'Program_1') prog_lines += base_lines #for b in base_lines: # prog_lines.append('\t'+b) #prog_lines.append('}') cube_count = -1 switches = [] for line in prog_lines: if 'Cuboid' in line: if not ('Program_' in line or "bbox" in line): switches.append(( f'cube{cube_count}', line.split()[0] )) if "bbox" in line: cube_count = -1 cube_count += 1 for a, b in switches: prog_lines = [line.replace(b,a) for line in prog_lines] hier_prog = make_hier_prog(prog_lines) verts, faces = hier_execute(hier_prog) if check_rooted(verts, faces) and check_stability(verts, faces): # writeObj(verts, faces, f'out_{ind}.obj') writeHierProg(hier_prog, f"random_hier_data/{ind}.txt") return True return False
def main(dataset_path, outdir): indices, progs = load_progs(dataset_path) os.system(f'mkdir {outdir}') os.system(f'mkdir {outdir}/valid') os.system(f'mkdir {outdir}/non_valid') count = 0 for ind, prog in tqdm(list(zip(indices, progs))): count += 1 if count < 0: continue lc = simplifyHP(prog) verts, faces = hier_execute(prog) bbdims = torch.tensor([ float(a) for a in re.split(r'[()]', prog['prog'][0])[1].split(',')[:3] ]) bb_viol = (verts.abs().max(dim=0).values / (bbdims / 2)).max() try: rooted = check_rooted(verts, faces) except Exception as e: print(f"Failed rooted check with {e}") rooted = False if DO_STABLE: stable = check_stability(verts, faces) else: stable = True if lc <= MAX_LEAVES and lc >= MIN_LEAVES and bb_viol < BB_THRESH and rooted and stable: utils.writeHierProg(prog, f'{outdir}/valid/{ind}.txt') utils.writeObj(verts, faces, f'{outdir}/valid/{ind}.obj') else: utils.writeHierProg(prog, f'{outdir}/non_valid/{ind}.txt') utils.writeObj(verts, faces, f'{outdir}/non_valid/{ind}.obj')
def gen_metrics(gen_progs, outpath, exp_name, epoch, VERBOSE, write_progs=True): misses = 0. results = { 'num_parts': [], 'rootedness': [], 'stability': [], } samples = [] for i, prog in enumerate(gen_progs): try: verts, faces = hier_execute(prog) assert not torch.isnan(verts).any(), 'saw nan vert' if write_progs: utils.writeObj( verts, faces, f"{outpath}/{exp_name}/objs/gen/{epoch}_{i}.obj") utils.writeHierProg( prog, f"{outpath}/{exp_name}/programs/gen/{epoch}_{i}.txt") results['num_parts'].append(verts.shape[0] / 8.0) samples.append((verts, faces)) except Exception as e: misses += 1. if VERBOSE: print(f"failed gen metrics for {i} with {e}") continue try: if check_rooted(verts, faces): results['rootedness'].append(1.) else: results['rootedness'].append(0.) if check_stability(verts, faces): results['stability'].append(1.) else: results['stability'].append(0.) except Exception as e: if VERBOSE: print(f"failed rooted/stable with {e}") for key in results: if len(results[key]) > 0: res = torch.tensor(results[key]).mean().item() else: res = 0. results[key] = res try: results['variance'] = eval_get_var(samples) except Exception as e: results['variance'] = 0. if VERBOSE: print(f"failed getting variance with {e}") return results, misses