CatVTON安装教程
特别注意:笔者并没有使用像官网使用conda来安装相关包,而是用绿色版本python创建虚拟环境来安装相关包
一、 克隆代码
git clone源代码
git clone https://github.com/Zheng-Chong/CatVTON.git
二、配置绿色Python版本并创建激活虚拟环境
1.cd到CatVTON目录
2.将准备好的绿色版本Python39放到CatVTON
如何得到绿色版本python请参考文章《绿色版本python》
4.创建虚拟环境
d:\CatVTON\Python39\python -m venv env_name
5.激活环境变量
.\env_name\Scripts\activate
三、在虚拟环境中安装python包
1.手动下载torch-2.1.2和torchvision-0.16.2版本的cuda版本的离线包并安装。
这里下载python3.9.0相关联的torch-2.1.2+cu118-cp39-cp39-win_amd64.whl和torchvision-0.16.2+cu118-cp39-cp39-win_amd64.whl(因两个包都比较大,避免安装失败,这里选择手动下载)
安装命令如下:
python -m pip install torch-2.1.2+cu118-cp39-cp39-win_amd64.whl
python -m pip install torchvision-0.16.2+cu118-cp39-cp39-win_amd64.whl
2.准备requirements.txt在线安装其他包。requirements.txt是源代码中requirements.txt去掉torch2.1.2和torchvision0.16.2内容。如下
accelerate==0.31.0
diffusers==0.29.2
huggingface_hub==0.23.4
matplotlib==3.9.1
numpy==1.26.4
opencv_python==4.10.0.84
pillow==10.3.0
PyYAML==6.0.1
scipy==1.13.1
setuptools==51.0.0
scikit-image==0.24.0
tqdm==4.66.4
transformers==4.27.3
fvcore==0.1.5.post20221221
cloudpickle==3.0.0
omegaconf==2.3.0
pycocotools==2.0.8
av==12.3.0
gradio==4.41.0
安装这些包的命令如下:
python -m pip install -r requirements.txt
在用上述命令安装的时候,安装到matplotlib时会报如下错误
Installing backend dependencies ... done
Preparing wheel metadata ... error
ERROR: Command errored out with exit status 1:
command: 'D:\DevOps\CatVTON\myvenv\Scripts\python.exe' 'D:\DevOps\CatVTON\myvenv\lib\site-packages\pip\_vendor\pep517\_in_process.py' prepare_metadata_for_build_wheel 'C:\Users\CJT\AppData\Local\Temp\tmpvv1p4r17'
cwd: C:\Users\CJT\AppData\Local\Temp\pip-install-l2j287vq\matplotlib
...
这样的问题我们可以到pip官网下载matplotlib 对应的whl包,用pip命令手动安装.比如我们在python官网下载了matplotlib-3.9.1.post1-cp39-cp39-win_amd64.whl包。手动安装命令如下:
python -m pip install matplotlib-3.9.1.post1-cp39-cp39-win_amd64.whl
将 matplotlib==3.9.1从requirements.txt中去掉后调用如下命令继续安装其他包
python -m pip install -r requirements.txt
四、手动模型下载
按官网做法调用命令行参数python app.py --output_dir="resource/demo/output" --mixed_precision="bf16" --allow_tf32 会自动下载模型相关文件,但是过容易报错。我们选中手动下载模型文件
五、运行命令行参数
python app.py --output_dir="resource/demo/output" --mixed_precision="bf16" --allow_tf32
报错信息一:
Traceback (most recent call last):
File "D:\DevOps\CatVTON\app.py", line 104, in <module>
repo_path = snapshot_download(repo_id=args.resume_path)
File "D:\DevOps\CatVTON\myvenv\lib\site-packages\huggingface_hub\utils\_validators.py", line 114, in _inner_fn
return fn(*args, **kwargs)
File "D:\DevOps\CatVTON\myvenv\lib\site-packages\huggingface_hub\_snapshot_download.py", line 236, in snapshot_download
raise LocalEntryNotFoundError(
huggingface_hub.utils._errors.LocalEntryNotFoundError: An error happened while trying to locate the files on the Hub and we cannot find the appropriate snapshot folder for the specified revision on the local disk. Please check your internet connection and try again.
查看app.py中代码snapshot_download,可能是下载导致异常。我们已经下载了模型就不需要重新下载,所以将此处代码
repo_path = snapshot_download(repo_id=args.resume_path)
改为如下代码,明确告诉程序从指定的resume_path获取模型文件
repo_path=args.resume_path
并且在CatVTON目录下创建booksforcharlie和sd-vae-ft-mse目录,并将stable-diffusion-inpainting放到booksforcharlie目录中,将sd-vae-ft-mse放到stabilityai两个目录分别存放如下文件
报错信息二:
我们来分析pipeline.py中代码,attn_ckpt如果存在就不通过 snapshot_download下载方式
class CatVTONPix2PixPipeline(CatVTONPipeline):
def auto_attn_ckpt_load(self, attn_ckpt, version):
# TODO: Temperal fix for the model version
if os.path.exists(attn_ckpt):
load_checkpoint_in_model(self.attn_modules, os.path.join(attn_ckpt, version, 'attention'))
else:
repo_path = snapshot_download(repo_id=attn_ckpt)
print(f"Downloaded {attn_ckpt} to {repo_path}")
load_checkpoint_in_model(self.attn_modules, os.path.join(repo_path, version, 'attention'))
查看pipline.py中auto_attn_ckpt_load方法。主要代码如下:
def auto_attn_ckpt_load(self, attn_ckpt, version):
sub_folder = {
"mix": "mix-48k-1024",
"vitonhd": "vitonhd-16k-512",
"dresscode": "dresscode-16k-512",
}[version]
因为我们已经下载了模型,所以希望指定attn_ckpt目录来,而不是通过下载方式。然后继续跟踪代码
class CatVTONPipeline:
def __init__(
self,
base_ckpt,
attn_ckpt,
attn_ckpt_version="mix",
weight_dtype=torch.float32,
device='cuda',
compile=False,
skip_safety_check=False,
use_tf32=True,
):
...
self.auto_attn_ckpt_load(attn_ckpt, attn_ckpt_version)
继续跟踪代码CatVTONPipeline对象,在app.py中代码如下
pipeline = CatVTONPipeline(
base_ckpt=args.base_model_path,
attn_ckpt=repo_path,
attn_ckpt_version="mix",
weight_dtype=init_weight_dtype(args.mixed_precision),
use_tf32=args.allow_tf32,
device='cuda'
)
由于上面我们将代码改了,repo_path就是resume_path
#repo_path = snapshot_download(repo_id=args.resume_path)
repo_path=args.resume_path
而resume_path代码如下。
def parse_args():
parser = argparse.ArgumentParser(description="Simple example of a training script.")
....
parser.add_argument(
"--resume_path",
type=str,
default="zhengchong/CatVTON",
help=(
"The Path to the checkpoint of trained tryon model."
),
)
我们这里使用--resume_path默认路径,就是zhengchong/CatVTON.我们把下载的模型mix-48k-1024、vitonhd-16k-512、dresscode-16k-512 放到zhengchong/CatVTON目录下。当然你可以用--resume_path指定要给有模型的路径也可以。但是调用python app.py时需要添加这个--resume_path进行调用。如果使用默认参数就不需要另外指定了。
报错信息三
接下来你可能碰到如下错误
Traceback (most recent call last):
File "D:\DevOps\CatVTON\app.py", line 117, in <module>
automasker = AutoMasker(
File "D:\DevOps\CatVTON\model\cloth_masker.py", line 165, in __init__
self.densepose_processor = DensePose(densepose_ckpt, device)
File "D:\DevOps\CatVTON\model\DensePose\__init__.py", line 40, in __init__
self.cfg = self.setup_config()
File "D:\DevOps\CatVTON\model\DensePose\__init__.py", line 48, in setup_config
cfg.merge_from_file(self.config_path)
File "D:\DevOps\CatVTON\detectron2\config\config.py", line 45, in merge_from_file
assert PathManager.isfile(cfg_filename), f"Config file '{cfg_filename}' does not exist!"
AssertionError: Config file 'zhengchong/CatVTON\DensePose\densepose_rcnn_R_50_FPN_s1x.yaml' does not exist!
将下载的DensePose目录也放在zhengchong/CatVTON目录下。
报错信息四
接下来你可能碰到如下错误:
Traceback (most recent call last):
File "D:\DevOps\CatVTON\app.py", line 117, in <module>
automasker = AutoMasker(
File "D:\DevOps\CatVTON\model\cloth_masker.py", line 166, in __init__
self.schp_processor_atr = SCHP(ckpt_path=os.path.join(schp_ckpt, 'exp-schp-201908301523-atr.pth'), device=device)
File "D:\DevOps\CatVTON\model\SCHP\__init__.py", line 73, in __init__
self.load_ckpt(ckpt_path)
File "D:\DevOps\CatVTON\model\SCHP\__init__.py", line 93, in load_ckpt
state_dict = torch.load(ckpt_path, map_location='cpu')['state_dict']
File "D:\DevOps\CatVTON\myvenv\lib\site-packages\torch\serialization.py", line 986, in load
with _open_file_like(f, 'rb') as opened_file:
File "D:\DevOps\CatVTON\myvenv\lib\site-packages\torch\serialization.py", line 435, in _open_file_like
return _open_file(name_or_buffer, mode)
File "D:\DevOps\CatVTON\myvenv\lib\site-packages\torch\serialization.py", line 416, in __init__
super().__init__(open(name, mode))
FileNotFoundError: [Errno 2] No such file or directory: 'zhengchong/CatVTON\\SCHP\\exp-schp-201908301523-atr.pth'
将下载的DensePose目录也放在zhengchong/CatVTON目录下。
控制台出现如下输出信息。
Running on local URL: http://127.0.0.1:7860
报错信息五
当你在浏览器中输入http://127.0.0.1:7860时,页面能显示,这是你以为大功告急。当你选择一个人物和衣服时,控制台有出现错误信息如下:
1. Download this file: https://cdn-media.huggingface.co/frpc-gradio-0.2/frpc_windows_amd64.exe
2. Rename the downloaded file to: frpc_windows_amd64_v0.2
3. Move the file to this location: D:\DevOps\CatVTON\myvenv\lib\site-packages\gradio
ERROR: Exception in ASGI application
Traceback (most recent call last):
...
pydantic.errors.PydanticSchemaGenerationError: Unable to generate pydantic-core schema for <class 'starlette.requests.Request'>. Set `arbitrary_types_allowed=True` in the model_config to ignore this error or implement `__get_pydantic_core_schema__` on your type to fully support it.
If you got this error by calling handler(<some type>) within `__get_pydantic_core_schema__` then you likely need to call `handler.generate_schema(<some type>)` since we do not call `__get_pydantic_core_schema__` on `<some type>` otherwise to avoid infinite recursion.
查阅了一些资料说是gradio要使用最新版本,使用如下命令:
python -m pip install gradio --upgrade
果然解决问题了。到此所有问题解决了。
浙公网安备 33010602011771号