faustomorales / vit-keras

Keras implementation of ViT (Vision Transformer)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

weights file failed to load ("File is not a zip file")

chrisrapson opened this issue · comments

This looks like a really promising resource. Thanks for putting it together.

When I try to use a model (specifically vit_l16) it downloads an NPZ file to my .keras/weights folder. That file is 1,190,422KB. However, that file fails to load:

np.load('ViT-L_16_imagenet21k+imagenet2012.npz', allow_pickle=False)

gives one of two errors:

Traceback (most recent call last):
  File "c:\xxx\.vscode\extensions\ms-python.python-2021.7.1053846006\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_comm.py", line 1207, in internal_evaluate_expression_json
    pydevd_vars.evaluate_expression(py_db, frame, expression, is_exec=True)
  File "c:\xxx\.vscode\extensions\ms-python.python-2021.7.1053846006\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_vars.py", line 371, in new_func
    return _run_with_unblock_threads(original_func, py_db, curr_thread, frame, expression, is_exec)
  File "c:\xxx\.vscode\extensions\ms-python.python-2021.7.1053846006\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_vars.py", line 339, in _run_with_unblock_threads
    return _run_with_interrupt_thread(original_func, py_db, curr_thread, frame, expression, is_exec)
  File "c:\xxx\.vscode\extensions\ms-python.python-2021.7.1053846006\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_vars.py", line 310, in _run_with_interrupt_thread
    return original_func(py_db, frame, expression, is_exec)
  File "c:\xxx\.vscode\extensions\ms-python.python-2021.7.1053846006\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_vars.py", line 421, in evaluate_expression
    Exec(_expression_to_evaluate(expression), updated_globals, frame.f_locals)
  File "c:\xxx\.vscode\extensions\ms-python.python-2021.7.1053846006\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_exec2.py", line 3, in Exec
    exec(exp, global_vars, local_vars)
  File "<string>", line 1
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \U', '', ' escape

or

Traceback (most recent call last):
  File "C:\xxxxx\site-packages\numpy\lib\npyio.py", line 230, in __del__
    self.close()
  File "C:\xxxxx\site-packages\numpy\lib\npyio.py", line 221, in close
    if self.zip is not None:
AttributeError: 'NpzFile' object has no attribute 'zip'

Have you seen this before? Is there something obvious I'm doing wrong? The code I am calling is:

model = vit.vit_l16(
        image_size = (input_h, input_w),
        activation = 'sigmoid',
        pretrained = True,
        include_top = False,
        pretrained_top = False,
        classes = n_classes
    )

Sounds like it's probably a corrupt download because the following code works just fine on Google Colab.

import os
import numpy as np
from vit_keras import vit, utils

# Triggers weight download.
model = vit.vit_l16(
    image_size=384,
    activation='sigmoid',
    pretrained=True,
    include_top=True,
    pretrained_top=True
)

# Does the file load?
np.load(os.path.expanduser(os.path.join("~", ".keras", "weights", "ViT-L_16_imagenet21k+imagenet2012.npz")), allow_pickle=False)

I suggest downloading the file manually from here and trying again. Realistically, the right way to ensure there's nothing going wrong with the download is to record the SHA hashes and validate them. I just haven't had the chance to get back into this library recently.

I downloaded from the browser and did a diff, which showed the files were identical. Then I tried the line you suggested by itself (np.load(os.path.expanduser...) and that was fine. Finally I tried running my full script again, and it just worked. So I can't help with troubleshooting the problem, but anyway I'm glad it's working for me now. Thanks :-)