clean-fid icon indicating copy to clipboard operation
clean-fid copied to clipboard

FID error

Open Yves907 opened this issue 2 years ago • 13 comments

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

How to solve it?

Yves907 avatar Mar 03 '22 11:03 Yves907

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 avatar Mar 04 '22 14:03 rickkk856

@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}")

rickkk856 avatar Mar 04 '22 16:03 rickkk856

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

GaParmar avatar Mar 08 '22 21:03 GaParmar

@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

Yves907 avatar Mar 13 '22 08:03 Yves907

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

GaParmar avatar Mar 13 '22 15:03 GaParmar

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": rel_path = (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": rel_path = (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.

Yves907 avatar Mar 24 '22 03:03 Yves907

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

GaParmar avatar Mar 29 '22 15:03 GaParmar

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!

Yves907 avatar Mar 30 '22 07:03 Yves907

@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!

antaegun20 avatar Mar 31 '22 14:03 antaegun20

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

GaParmar avatar Mar 31 '22 21:03 GaParmar

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

ryanxingql avatar Nov 18 '22 07:11 ryanxingql