FBA_Matting icon indicating copy to clipboard operation
FBA_Matting copied to clipboard

AttributeError: 'Args' object has no attribute 'seek'. in notebook

Open Ishihara-Masabumi opened this issue 3 years ago • 6 comments

Using FBA Matting.ipynb in Gooble Colab, at the source code below,

class Args:
  encoder = 'resnet50_GN_WS'
  decoder = 'fba_decoder'
  weights = 'FBA.pth'
args=Args()
try:
    model = build_model(args)
except:
    !gdown  https://drive.google.com/uc?id=1T_oiKDE_biWf2kqexMEN7ObWqtXAzbB1
    model = build_model(args)

the following error occurred.

modifying input layer to accept 11 channels
Downloading...
From: https://drive.google.com/uc?id=1T_oiKDE_biWf2kqexMEN7ObWqtXAzbB1
To: /content/drive/MyDrive/FBA Matting/FBA.pth
100% 139M/139M [00:01<00:00, 81.2MB/s]
modifying input layer to accept 11 channels
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
[/usr/local/lib/python3.7/dist-packages/torch/serialization.py](https://localhost:8080/#) in _check_seekable(f)
    307     try:
--> 308         f.seek(f.tell())
    309         return True

AttributeError: 'Args' object has no attribute 'seek'

During handling of the above exception, another exception occurred:

AttributeError                            Traceback (most recent call last)
14 frames
AttributeError: 'Args' object has no attribute 'seek'. You can only torch.load from a file that is seekable. Please pre-load the data into a buffer like io.BytesIO and try to load from it instead.

During handling of the above exception, another exception occurred:

AttributeError                            Traceback (most recent call last)
AttributeError: 'Args' object has no attribute 'seek'

During handling of the above exception, another exception occurred:

AttributeError                            Traceback (most recent call last)
[/usr/local/lib/python3.7/dist-packages/torch/serialization.py](https://localhost:8080/#) in raise_err_msg(patterns, e)
    302                                 + " Please pre-load the data into a buffer like io.BytesIO and"
    303                                 + " try to load from it instead.")
--> 304                 raise type(e)(msg)
    305         raise e
    306 

AttributeError: 'Args' object has no attribute 'seek'. You can only torch.load from a file that is seekable. Please pre-load the data into a buffer like io.BytesIO and try to load from it instead.

Please tell me the cause of the error and how to fix it.

Ishihara-Masabumi avatar Sep 12 '22 07:09 Ishihara-Masabumi

modify build_model( ) from networks.models like this.


def build_model(weights):
    import io
    weights = weights.weights
    net_encoder = fba_encoder()

    net_decoder = fba_decoder()

    model = MattingModule(net_encoder, net_decoder)

    if(weights != 'default'):
        with open(weights, 'rb') as f:
            buffer = io.BytesIO(f.read())
        # sd = torch.load(weights)
        sd = torch.load(buffer)
        model.load_state_dict(sd, strict=True)

    return model
    

Tieck-IT avatar Apr 14 '23 07:04 Tieck-IT

the same, useless

maomao2991 avatar Aug 14 '23 03:08 maomao2991

Got the solution?

rohanblkbox avatar Oct 17 '23 14:10 rohanblkbox

Anyone found the solution??

IndraKumari0703 avatar Oct 18 '23 15:10 IndraKumari0703

It still work at colab. If you want to modify and apply module(e.g. .py) at colab, first modify module and second restart runtime.

image image

modify build_model( ) from networks.models like this.

def build_model(weights):
    import io
    weights = weights.weights
    net_encoder = fba_encoder()

    net_decoder = fba_decoder()

    model = MattingModule(net_encoder, net_decoder)

    if(weights != 'default'):
        with open(weights, 'rb') as f:
            buffer = io.BytesIO(f.read())
        # sd = torch.load(weights)
        sd = torch.load(buffer)
        model.load_state_dict(sd, strict=True)

    return model
    

Tieck-IT avatar Oct 24 '23 01:10 Tieck-IT