GaParmar / clean-fid

PyTorch - FID calculation with proper image resizing and quantization steps [CVPR 2022]
https://www.cs.cmu.edu/~clean-fid/
MIT License
894 stars 68 forks source link

FID error #24

Open Yves907 opened 2 years ago

Yves907 commented 2 years ago

RuntimeError: PytorchStreamReader failed reading zip archive: failed finding central directory

How to solve it?

rickkk856 commented 2 years ago

same issue here.

I'm trying to modify the code to windows like suggested here https://github.com/GaParmar/clean-fid/pull/14 and https://github.com/GaParmar/clean-fid/pull/13/commits/13dddbe63bf0cfd57ff8f02e23ab20a263dbeb28 ... It seems that torch is having trouble in finding the location of InceptionV3W file on Windows.

fid.compute_fid(fdir1, fdir2, mode="clean", num_workers=0,batch_size=16) Traceback error on CMD:

Traceback (most recent call last):
  File "fid.py", line 22, in <module>
    score = fid.compute_fid(fdir1, fdir2, mode="clean", num_workers=2,batch_size=16)
  File "C:\Users\Pichau\anaconda3\lib\site-packages\cleanfid\fid.py", line 384, in compute_fid
    feat_model = build_feature_extractor(mode, device)
  File "C:\Users\Pichau\anaconda3\lib\site-packages\cleanfid\features.py", line 43, in build_feature_extractor
    feat_model = feature_extractor(name="torchscript_inception", resize_inside=False, device=device)
  File "C:\Users\Pichau\anaconda3\lib\site-packages\cleanfid\features.py", line 22, in feature_extractor
    model = InceptionV3W(tmp_dir, download=True, resize_inside=resize_inside).to(device)
  File "C:\Users\Pichau\anaconda3\lib\site-packages\cleanfid\inception_torchscript.py", line 35, in __init__
    self.base = torch.jit.load(path).eval()
  File "C:\Users\Pichau\anaconda3\lib\site-packages\torch\jit\_serialization.py", line 161, in load
    cpp_module = torch._C.import_ir_module(cu, str(f), map_location, _extra_files)
RuntimeError: PytorchStreamReader failed reading zip archive: failed finding central directory

Strangely the code runs on Jupyter Notebook, however when it finishes the fid calculation, there's an error [The kernel appears to have died. It will restart automatically.](https://github.com/apple/tensorflow_macos/issues/51)

Traceback on Jupyter Notebook:

compute FID between two folders
Found 8000 images in the folder ./results/FINAL_TEST/08K_RPlan_Black/test_110/images/_real_B
FID _real_B :   0%|                                                                            | 0/500 [00:00<?, ?it/s]C:\Users\Pichau\anaconda3\lib\site-packages\cleanfid\inception_torchscript.py:54: UserWarning: Named tensors and all their associated APIs are an experimental feature and subject to change. Please do not use them for anything important until they are released as stable. (Triggered internally at  ..\c10/core/TensorImpl.h:1156.)
  features = self.layers.forward(x2, ).view((bs, 2048))
FID _real_B : 100%|██████████████████████████████████████████████████████████████████| 500/500 [03:28<00:00,  2.40it/s]
Found 8000 images in the folder ./results/FINAL_TEST/08K_RPlan_Black/test_110/images/_fake_B
FID _fake_B : 100%|██████████████████████████████████████████████████████████████████| 500/500 [03:33<00:00,  2.35it/s]
rickkk856 commented 2 years ago

@Yves907 Similarly on Issue https://github.com/GaParmar/clean-fid/issues/3 to make it work I needed to put the inception-2015-12-05.pt file of --> https://nvlabs-fi-cdn.nvidia.com/stylegan2-ada-pytorch/pretrained/metrics/inception-2015-12-05.pt inside the working directory... So it was something like this:

%cd C:\Users\Pichau\pytorch-CycleGAN-and-pix2pix
fdir1 = "./results/FINAL_TEST/images/_real_B"
fdir2 = "./results/FINAL_TEST/images/_fake_B"

score = fid.compute_fid(fdir1, fdir2, mode="clean", num_workers=0,batch_size=16)
print(f"clean-fid score: {score:.3f}")
GaParmar commented 2 years ago

Hi,

Thank you for taking a look at the issue and the corresponding pull request. I have merged the PR and the new clean-fid release should have this issue addressed.

Regards, Gaurav

Yves907 commented 2 years ago

@GaParmar I re-installed clean-FID, but this error still occurs on linux. In fact, I ran this code to calculate FID without any errors three months ago, and recently started to get the error.

File "/home/sll/miniconda3/envs/yanjie_torch/lib/python3.6/site-packages/cleanfid/features.py", line 42, in build_feature_extractor feat_model = feature_extractor(name="torchscript_inception", resize_inside=False, device=device) File "/home/sll/miniconda3/envs/yanjie_torch/lib/python3.6/site-packages/cleanfid/features.py", line 21, in feature_extractor model = InceptionV3W(path, download=True, resize_inside=resize_inside).to(device) File "/home/sll/miniconda3/envs/yanjie_torch/lib/python3.6/site-packages/cleanfid/inception_torchscript.py", line 35, in init self.base = torch.jit.load(path).eval() File "/home/sll/miniconda3/envs/yanjie_torch/lib/python3.6/site-packages/torch/jit/_serialization.py", line 161, in load cpp_module = torch._C.import_ir_module(cu, str(f), map_location, _extra_files) RuntimeError: PytorchStreamReader failed reading zip archive: failed finding central directory

GaParmar commented 2 years ago

Hi @Yves907,

Thank you for pointing out the issue! Can you show the contents of this file?

home/sll/miniconda3/envs/yanjie_torch/lib/python3.6/site-packages/cleanfid/features.py

This will better help me debug why this runtime error is occurring.

Regards, Gaurav

Yves907 commented 2 years ago

Hi @GaParmar

These are contents of "feature.py" file

import os import numpy as np import torch import cleanfid from cleanfid.downloads_helper import check_download_url from cleanfid.inception_pytorch import InceptionV3 from cleanfid.inception_torchscript import InceptionV3W

""" returns a functions that takes an image in range [0,255] and outputs a feature embedding vector """ def feature_extractor(name="torchscript_inception", device=torch.device("cuda"), resize_inside=False): if name == "torchscript_inception": model = InceptionV3W("/tmp", download=True, resize_inside=resize_inside).to(device) model.eval() def model_fn(x): return model(x) elif name == "pytorch_inception": model = InceptionV3(output_blocks=[3], resize_input=False).to(device) model.eval() def model_fn(x): return model(x/255)[0].squeeze(-1).squeeze(-1) else: raise ValueError(f"{name} feature extractor not implemented") return model_fn

""" Build a feature extractor for each of the modes """ def build_feature_extractor(mode, device=torch.device("cuda")): if mode == "legacy_pytorch": feat_model = feature_extractor(name="pytorch_inception", resize_inside=False, device=device) elif mode == "legacy_tensorflow": feat_model = feature_extractor(name="torchscript_inception", resize_inside=True, device=device) elif mode == "clean": feat_model = feature_extractor(name="torchscript_inception", resize_inside=False, device=device) return feat_model

""" Load precomputed reference statistics for commonly used datasets """ def get_reference_statistics(name, res, mode="clean", seed=0, split="test", metric="FID"): base_url = "https://www.cs.cmu.edu/~clean-fid/stats/" if split == "custom": res = "na" if metric == "FID": relpath = (f"{name}{mode}{split}{res}.npz").lower() url = f"{base_url}/{rel_path}" mod_path = os.path.dirname(cleanfid.file) stats_folder = os.path.join(mod_path, "stats") fpath = check_download_url(local_folder=stats_folder, url=url) stats = np.load(fpath) mu, sigma = stats["mu"], stats["sigma"] return mu, sigma elif metric == "KID": relpath = (f"{name}{mode}{split}{res}_kid.npz").lower() url = f"{base_url}/{rel_path}" mod_path = os.path.dirname(cleanfid.file) stats_folder = os.path.join(mod_path, "stats") fpath = check_download_url(local_folder=stats_folder, url=url) stats = np.load(fpath) return stats["feats"]

Thank you for your consideration.

GaParmar commented 2 years ago

It looks like the Inception model was not downloaded correctly. Can you check if the file /tmp/inception-2015-12-05.pt exists?

Yves907 commented 2 years ago

Hi @GaParmar The code runs successfully on my win10, but cannot work well on the linux. I have checked that the "inception-2015-12-05.pt" exists. Next, I will completely clear the file and reinstall it again, I think I can solve the problem this time.

Thank you!

antaegun20 commented 2 years ago

@Yves907 Hi, I'm using ubuntu 20.04 LTS and CUDA 11.6. I suffered from same error, [RuntimeError: PytorchStreamReader failed reading zip archive: failed finding central directory] although I ahve inception-2015-12-05.pt file in /tmp/

I fixed it by removing the file and run fid.compute_fid(dir1, dir2). I guess that I'm using other fid calculation packags so those packages already downloaded inception model in /tmp/, so that clean-fid package may suffer conflicts in downloading or may not recognize existing inception-2015-12-05.pt file because it's downloaded by other package (may be accessibility issue?).

I feel this package very useful, so hope that this error never happen again... @GaParmar Thank you for great project!

GaParmar commented 2 years ago

Thank you for pointing these details out! I have noticed that sometime the download of the inception file gets corrupted if the internet connection is interrupted midway. I will add a check for this that will re-download the file in the next version of the library.

-Gaurav

ryanxingql commented 1 year ago

Removing the inception-2015-12-05.pt in the /tmp/ folder works for me.

genggeng12345 commented 1 month ago

In the Linux system, the same error is reported:

Traceback (most recent call last): File "/home/genggeng/SDD_svd_contra-main/myeval_fid.py", line 64, in f = fid.compute_fid(temp_dir1, temp_dir2) File "/home/genggeng/.conda/envs/SDD/lib/python3.9/site-packages/cleanfid/fid.py", line 465, in compute_fid feat_model = build_feature_extractor(mode, device, use_dataparallel=use_dataparallel) File "/home/genggeng/.conda/envs/SDD/lib/python3.9/site-packages/cleanfid/features.py", line 46, in build_feature_extractor feat_model = feature_extractor(name="torchscript_inception", resize_inside=False, device=device, use_dataparallel=use_dataparallel) File "/home/genggeng/.conda/envs/SDD/lib/python3.9/site-packages/cleanfid/features.py", line 21, in feature_extractor model = InceptionV3W(path, download=True, resize_inside=resize_inside).to(device) File "/home/genggeng/.conda/envs/SDD/lib/python3.9/site-packages/cleanfid/inception_torchscript.py", line 35, in init self.base = torch.jit.load(path).eval() File "/home/genggeng/.conda/envs/SDD/lib/python3.9/site-packages/torch/jit/_serialization.py", line 162, in load cpp_module = torch._C.import_ir_module(cu, str(f), map_location, _extra_files, _restore_shapes) # type: ignore[call-arg] RuntimeError: PytorchStreamReader failed reading zip archive: failed finding central directory

and the file "inception-2015-12-05.pt" does not exist. How to deal with it?

catsled commented 1 month ago

在 Linux 系统中,报告了相同的错误:

回溯(最近一次调用):文件“/home/genggeng/SDD_svd_contra-main/myeval_fid.py”,第 64 行,f = fid.compute_fid(temp_dir1,temp_dir2) 文件“/home/genggeng/.conda/envs/SDD/lib/python3.9/site-packages/cleanfid/fid.py”,第 465 行,在 compute_fid 中 feat_model = build_feature_extractor(mode, device, use_dataparallel=use_dataparallel)文件“/home/genggeng/.conda/envs/SDD/lib/python3.9/site-packages/cleanfid/features.py”,第 46 行,在build_feature_extractor feat_model = feature_extractor(name=“torchscript_inception”, resize_inside=False, device=device, use_dataparallel=use_dataparallel) 文件“/home/genggeng/.conda/envs/SDD/lib/python3.9/site-packages/cleanfid/features.py”,第 21 行,在 feature_extractor 模型中 = InceptionV3W(path, download=True, resize_inside=resize_inside).to(device) 文件 “/home/genggeng/.conda/envs/SDD/lib/python3.9/site-packages/cleanfid/inception_torchscript.py”,第 35 行,init self.base = torch.jit.load(path).eval() 文件 “/home/genggeng/.conda/envs/SDD/lib/python3.9/site-packages/torch/jit/_serialization.py”,第 162 行,加载 cpp_module = torch._C.import_ir_module(cu, str(f), map_location, _extra_files, _restore_shapes) # type: ignore[call-arg]运行时错误:PytorchStreamReader 读取 zip 存档失败:查找中央目录失败

并且文件“inception-2015-12-05.pt”不存在。如何处理?

maybe the xxx.pth is bad, you could delete it from the /tmp and then download it from scratch...