Torch.save FileNotFoundError: [Errno 2] No such file or directory

when i try to call torch.save to save my model in a name, it rises a FileNotFoundError. the trace back is as follow:

Traceback (most recent call last):

  File "D:\python\RDANET\models\RDAnet.py", line 236, in <module>
    model.save()

  File "D:\python\RDANET\models\RDAnet.py", line 173, in save
    t.save(self.state_dict(), name)

  File "d:\anaconda3\lib\site-packages\torch\serialization.py", line 369, in save
    with _open_file_like(f, 'wb') as opened_file:

  File "d:\anaconda3\lib\site-packages\torch\serialization.py", line 234, in _open_file_like
    return _open_file(name_or_buffer, mode)

  File "d:\anaconda3\lib\site-packages\torch\serialization.py", line 215, in __init__
    super(_open_file, self).__init__(open(name, mode))

FileNotFoundError: [Errno 2] No such file or directory: 'D:/python/RDANET/checkpoints/RDAnet_0718_16_30_44.pkl'

I’m running code on a Windows remote desktop,the code:

    def save(self, name=None):

        if name is None:
            prefix = 'D:/python/RDANET/checkpoints/' + self.model_name + '_'
            name = time.strftime(prefix + '%m%d_%H_%M_%S.pkl')
        t.save(self.state_dict(), name)
        return name
        model.save()
1 Like

I solved the problem,This works when I use prefix = self.model_name + ‘_’ .So I created the folder checkpoints. And then it didn’t go wrong. This is so strange

2 Likes

One or more of your intermediate directories do not exist in your path (D:/python/RDANET/checkpoints/) where you are trying to save the model. Torch expects the directories to exist in the path of saving the model.