"""Export a joint checkpoint to files accepted by StereoHand SharedHeads.""" import argparse,json from pathlib import Path import torch def unpack(checkpoint,output,model_config): bundle=torch.load(checkpoint,map_location='cpu',mmap=True,weights_only=True) if bundle.get('format')!='stereohand-joint-v1':raise ValueError('Unsupported checkpoint format') root=Path(output).resolve();root.mkdir(parents=True,exist_ok=False) for branch in ('mask','mano','matching'): torch.save({'model':bundle['states'][branch]},root/(branch+'.pt')) config=json.loads(Path(model_config).read_text()) model=config.get('model',config) model.pop('inference_cache',None) for branch in ('mask','mano'): model[branch+'_initial']=model[branch+'_checkpoint']=str(root/(branch+'.pt')) model[branch+'_step']=bundle['steps'][branch]['step'] model['matching_checkpoint']=str(root/'matching.pt') target=root/'reconstruct.json';target.write_text(json.dumps(config,indent=2)) print(target) if __name__=='__main__': p=argparse.ArgumentParser();p.add_argument('checkpoint');p.add_argument('--output',required=True);p.add_argument('--config',required=True) a=p.parse_args();unpack(a.checkpoint,a.output,a.config)