diff --git a/DISTS_pytorch/DISTS_pt.py b/DISTS_pytorch/DISTS_pt.py index 25b828e..41594f7 100644 --- a/DISTS_pytorch/DISTS_pt.py +++ b/DISTS_pytorch/DISTS_pt.py @@ -1,6 +1,7 @@ # This is a pytoch implementation of DISTS metric. # Requirements: python >= 3.6, pytorch >= 1.0 +import importlib.resources import numpy as np import os,sys import torch @@ -27,7 +28,10 @@ def forward(self, input): class DISTS(torch.nn.Module): def __init__(self, load_weights=True): super(DISTS, self).__init__() - vgg_pretrained_features = models.vgg16(pretrained=True).features + vgg_pretrained_features = models.vgg16(pretrained=False).features + vgg_ref = importlib.resources.files('DISTS_pytorch').joinpath('vgg16_features.pt') + with importlib.resources.as_file(vgg_ref) as vgg_weights_path: + vgg_pretrained_features.load_state_dict(torch.load(vgg_weights_path, weights_only=True)) self.stage1 = torch.nn.Sequential() self.stage2 = torch.nn.Sequential() self.stage3 = torch.nn.Sequential() @@ -60,7 +64,9 @@ def __init__(self, load_weights=True): self.alpha.data.normal_(0.1,0.01) self.beta.data.normal_(0.1,0.01) if load_weights: - weights = torch.load(os.path.join(sys.prefix,'weights.pt')) + w_ref = importlib.resources.files('DISTS_pytorch').joinpath('weights.pt') + with importlib.resources.as_file(w_ref) as weights_path: + weights = torch.load(weights_path, weights_only=True) self.alpha.data = weights['alpha'] self.beta.data = weights['beta'] diff --git a/DISTS_pytorch/vgg16_features.pt b/DISTS_pytorch/vgg16_features.pt new file mode 100644 index 0000000..690785d Binary files /dev/null and b/DISTS_pytorch/vgg16_features.pt differ diff --git a/MANIFEST.in b/MANIFEST.in index 2769daa..1aede2d 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -3,6 +3,7 @@ -- __init__.py -- DISTS_pt.py -- weights.pt + -- vgg16_features.pt -- README.md -- requirements.txt -- LICENSE \ No newline at end of file diff --git a/setup.py b/setup.py index 810e894..789a624 100644 --- a/setup.py +++ b/setup.py @@ -9,8 +9,8 @@ long_description=long_description, long_description_content_type="text/markdown", packages=['DISTS_pytorch'], - package_data= {'': ['DISTS_pytorch/weights.pt']}, - data_files= [('', ['DISTS_pytorch/weights.pt'])], + package_data= {'': ['DISTS_pytorch/weights.pt', 'DISTS_pytorch/vgg16_features.pt']}, + data_files= [('', ['DISTS_pytorch/weights.pt', 'DISTS_pytorch/vgg16_features.pt'])], include_package_data=True, author='Keyan Ding', author_email='dingkeyan93@outlook.com',