Error on torch.load() (PytorchStreamReader failed)

Pritesh_Gohil: <p>Hi,<br>
I was trying to load the pytorch model but facing an unexpected error. I do not disturb the folder structure and still getting this error.</p>
<pre><code class="lang-auto">&gt;&gt;&gt; torch.load("outputs/test_validation_loss_logging/model_001650.pth", map_location="cpu")
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  File "/home/pritesh/anaconda3/envs/pytorch/lib/python3.7/site-packages/torch/serialization.py", line 586, in load
    with _open_zipfile_reader(f) as opened_zipfile:
  File "/home/pritesh/anaconda3/envs/pytorch/lib/python3.7/site-packages/torch/serialization.py", line 246, in __init__
    super(_open_zipfile_reader, self).__init__(torch._C.PyTorchFileReader(name_or_buffer))
RuntimeError: [enforce fail at inline_container.cc:143] . PytorchStreamReader failed reading zip archive: failed finding central directory
</code></pre>
<p>Linux OS<br>
Python3.7<br>
torch version 1.5.0</p>
<p>Check how the model is saved <a href="https://github.com/lufficc/SSD/blob/50373c79b861d5d239be4206fafc6661cea040b4/ssd/utils/checkpoint.py#L29" rel="nofollow noopener">here</a></p>
<p>.</p>

ptrblck: <p>Were you saving the complete model via <code>torch.save(model, path)</code> instead of the <code>state_dict</code>?<br>
If that’s the case, did you change any files or folder structure?</p>

Pritesh_Gohil: <p>I save the model using <code> torch.save(model, path)</code> and folder or file structure is not changed.</p>

Pritesh_Gohil: <p>Ok, I’m able to load the model. The problem was with the saved weight file. It wasn’t saved properly and the weight file size was smaller (only 90 MB instead of 200 MB).</p>

Tupakula_Mallikarjun: <p>Hi <a class="mention" href="/u/ptrblck">@ptrblck</a> <a class="mention" href="/u/pritesh_gohil">@Pritesh_Gohil</a>. I am also facing the same problem. I have a downloaded model. How can I load it?</p>

ptrblck: <p>Could you explain your use case and the issue you are seeing a bit more?<br>
Usually you could create the model instance and load the downloaded <code>state_dict</code> via <code>model.load_state_dict</code>.</p>

Tupakula_Mallikarjun: <p>Hey <a class="mention" href="/u/ptrblck">@ptrblck</a>. I solved the error. I am getting this error All bounding boxes should have positive height and width. Found invaid box [380.0525207519531, 247.53013610839844, 380.0525207519531, 249.6278533935547] for the target at index 3.Don’t you’ve any idea about this?</p>

ptrblck: <p>I’m not sure how the bounding box coordinates are encoded, but if they are stored as <code>[x0, y0, x1, y1]</code>, the width would be zero, since <code>x1 - x0 = 0</code>.</p>

HaukurPall: <p>I am also having this issue.</p>
<ul>
<li>Have not changed the directory structure / code.</li>
<li>I am able to load the model in certain cases, see below.</li>
</ul>
<p>I use torch.save to distribute my model more easily to users. I attach some vocabulary mappings and tokenizers to the Modules so I don’t have to load them separately.<br>
When training models which have smaller vocabulary mappings I am able to load the model. When using larger vocabularies (for a release) I am unable to load the model and I get this error. I am using PyTorch version 1.7.1 and Python 3.8.6.</p>

HaukurPall: <p>I was able to resolve my issue.</p>
<p>I noticed that the files for the larger models were smaller than to be expected. Then I checked the logs of the training and the run crashed when saving the model due to OOM.</p>

eslam_fouda: <p>shouldn’t one use model.load_weights(opt.weights_path)?</p>

Udith_Haputhanthri: <p>Hi, I have solved this error by creating a new .pth file using the content of the existing .pth file as explained in <a href="https://pytorch.org/tutorials/beginner/saving_loading_models.html#saving-loading-a-general-checkpoint-for-inference-and-or-resuming-training" rel="noopener nofollow ugc">here</a>. Even though this newly created .pth file has the exactly the same content as in the previous one, it could be loaded without errors.</p>

NickYi1990: <p>lol, same for me, thanks!</p>

Jianquan_Zhao: <p>I am agree with you .<br>
During the programming, there are two ways to save and load model.<br>
The first one is save the whole model and we must load in the same folder where the second one is to save it’s weights(state_dict) and we must claim a model and load state_dict.</p>

aktaseren: <p>Hi <a class="mention" href="/u/pritesh_gohil">@Pritesh_Gohil</a> , <a class="mention" href="/u/haukurpall">@HaukurPall</a>, sorry for tagging you both here. It seems that you solved the issue I am also having. Some of you said that the saved weight wasn’t saved properly. I am sure that my weight file saved properly, whose size is around 300MB.</p>
<p>I tried both saving options as follows with both filing ways:</p>
<pre><code class="lang-auto">        # torch.save(model.state_dict(), '/home/aktaseren/people-torch/pidxx.pt')
        # torch.save(model, '/home/aktaseren/people-torch/pidxx.pth')
</code></pre>
<p>However, I am not able to load it via any of these. I am having the same error you got. My main purpose is to convert one Pytorch model to ONXX version in order to use it for Opencv framework.</p>
<p>I would be appreciated if any of you can explain in detail how you solved this.</p>

G123: <p>Hi! New member here! I am following this code: <a href="https://blog.paperspace.com/train-yolov5-custom-data/" class="inline-onebox" rel="noopener nofollow ugc">How to Train YOLO v5 on a Custom Dataset | Paperspace Blog</a><br>
but when I tried this command: <code>python train.py --img 640 --cfg yolov5s.yaml --hyp hyp.scratch.yaml --batch 32 --epochs 100 --data road_sign_data.yaml --weights yolov5s.pt --workers 24 --name yolo_road_det</code></p>
<p>I got this error:</p>
<p><code>  File "/home/UbuntuUser/.local/lib/python3.8/site-packages/torch/serialization.py", line 242, in __init__     super(_open_zipfile_reader, self).__init__(torch._C.PyTorchFileReader(name_or_buffer)) RuntimeError: [enforce fail at inline_container.cc:145] . PytorchStreamReader failed reading zip archive: failed finding central directory</code></p>
<p>Any idea, what is wrong and how to fix it?</p>

G123: <p>How do you save the weight? Where?</p>

enterthevoidf22: <p>i am getting the same error when trying to load a simple torch tensor (.pt) it didn’t happen in the past and only started happening after i did some preprocessing protocol on all the tensors in my in-disk dataset.</p>
<p>my code is simple and goes like this:</p>
<pre><code class="lang-auto">mask = np.load(path_to_mask) #the preprocess is actually a simple mask application
for path in os.listdir(path_to_all_pt_files):
    scan = torch.load(path)
    scan = preprocess(scan,mask)
    torch.save(scan,path)

def preprocess(scan,mask):
     masked = scan * torch.from_numpy(mask)
     masked = masked.to(dtype=scan.dtype)
     return masked
</code></pre>
<p>what could be the cause?</p>

lzq1477960451: <p>Thank you. I have the same problem as you. The problem has been solved. It should be the last time the parameter is saved, it is forcibly interrupted, resulting in saving The .pth file is incomplete.</p>

desmond-rn: <p>It could be a problem with the PyTorch version and saving mechanism.<br>
I had the same problem and solved it by passing the kwarg <code>_use_new_zipfile_serialization=False</code> when saving the model.<br>
More details <a href="https://www.10k.cn/article/11784.html" rel="noopener nofollow ugc">here</a>.</p>