Description
Some of the oldest models we have (including resnets) have been serialized with a legacy format which, while is mostly compatible with the current code, has some corner cases that do not work as expected anymore, see pytorch/pytorch#31615 (basically, if the data is in a BytesIO
object it doesn't work).
Here is a snippet to reproduce the problem:
import urllib.request
import io
url = 'https://download.pytorch.org/models/resnet50-19c8e357.pth'
path = './tmp.pth'
urllib.request.urlretrieve(url, path)
with open(path, "rb") as fd:
buf = io.BytesIO(fd.read())
state_dict = torch.load(buf, "cpu")
This fails. We should re-save those weights and re-upload them (while keeping the old files around for BC) and update the paths in the model files to use the new files. This code re-downloads the old files and re-serializes it with the new format
import urllib.request
import io
url = 'https://download.pytorch.org/models/resnet18-5c106cde.pth'
path = './tmp.pth'
urllib.request.urlretrieve(url, path)
torch.save(torch.load(path), path)
with open(path, "rb") as fd:
buf = io.BytesIO(fd.read())
state_dict = torch.load(buf, "cpu")
One thing to keep in mind is if changing the default weights files (while being equivalent to the old models) won't bring unexpected changes / issues to some users, as it will trigger a re-download of the file, which might fail because of many reasons (the server doesn't have access to internet, files have been manually downloaded, etc).