AiuniAI / Unique3D

[NeurIPS 2024] Unique3D: High-Quality and Efficient 3D Mesh Generation from a Single Image
https://wukailu.github.io/Unique3D/
MIT License
3.1k stars 246 forks source link

run it without ui #105

Closed Dan1900 closed 2 months ago

Dan1900 commented 2 months ago

I write a script to reconstruct 3d without UI. I get this error:element 0 of tensors does not require grad and does not have a grad_fn. when I use app ,there is no problem. How can I fix my script?

import os
import sys

sys.path.append(os.curdir)
if 'CUDA_VISIBLE_DEVICES' not in os.environ:
    os.environ['CUDA_VISIBLE_DEVICES'] = '0'
os.environ['TRANSFORMERS_OFFLINE'] = '0'
os.environ['DIFFUSERS_OFFLINE'] = '0'
os.environ['HF_HUB_OFFLINE'] = '0'
os.environ['GRADIO_ANALYTICS_ENABLED'] = 'False'
os.environ['HF_ENDPOINT'] = 'https://hf-mirror.com'
import torch

torch.set_float32_matmul_precision('medium')
torch.backends.cuda.matmul.allow_tf32 = True
torch.set_grad_enabled(False)
from refine_lr_to_sr import run_sr_fast
from PIL import Image
from app.all_models import model_zoo
from app.custom_models.mvimg_prediction import run_mvprediction
from app.custom_models.normal_prediction import predict_normals
from scripts.refine_lr_to_sr import run_sr_fast
from scripts.utils import save_glb_and_video
from scripts.multiview_inference import geo_reconstruct
from pytorch3d.structures import Meshes

def run_unique3d(preview_img):
    seed=-1
    init_type='std'
    expansion_weight=0.1
    do_refine = True
    input_processing=True # remove bg   
    model_zoo.init_models()  
    if isinstance(preview_img, str):
        preview_img = Image.open(preview_img)

    if preview_img.size[0] <= 512:
        preview_img = run_sr_fast([preview_img])[0]
    rgb_pils, front_pil = run_mvprediction(preview_img, remove_bg=input_processing, seed=int(seed))  # 6s
    new_meshes = geo_reconstruct(rgb_pils, None, front_pil, do_refine=do_refine, predict_normal=True,
                                 expansion_weight=expansion_weight, init_type=init_type)
    vertices = new_meshes.verts_packed()
    vertices = vertices / 2 * 1.35
    vertices[..., [0, 2]] = - vertices[..., [0, 2]]
    new_meshes = Meshes(verts=[vertices], faces=new_meshes.faces_list(), textures=new_meshes.textures)

    ret_mesh, video = save_glb_and_video("/tmp/gradio/generated", new_meshes, with_timestamp=True, dist=3.5,
                                         fov_in_degrees=2 / 1.35, cam_type="ortho", export_video=True)
    return ret_mesh, video
josepmy commented 2 months ago

Without more context of the error, I understand you are trying to perform a gradient-based operation on a tensor that doesn't require gradients in torch.set_grad_enabled(False) so I suggest put true or false as you need or not

lzran commented 2 months ago

you can try to comment out the line of code “torch.set_grad_enabled(False)”

Dan1900 commented 2 months ago

you can try to comment out the line of code “torch.set_grad_enabled(False)”

Without more context of the error, I understand you are trying to perform a gradient-based operation on a tensor that doesn't require gradients in torch.set_grad_enabled(False) so I suggest put true or false as you need or not

Thank you!

Dan1900 commented 2 months ago

you can try to comment out the line of code “torch.set_grad_enabled(False)”

Thank you!