Skip to content

convert_weights 🗿

Convert VGG-Face weights from LuaTorch to PyTorch.

Author: Simon M. Hofmann Years: 2023

Functions:

Name Description
convert_weights

Convert LuaTorch weights and load them into a PyTorch model.

download_torch_weights

Download torch weights for VGG-Face if necessary.

convert_weights 🗿

convert_weights(
    path_to_weights: str | Path, model: VGGFace
) -> VGGFace

Convert LuaTorch weights and load them into a PyTorch model.

Parameters:

Name Type Description Default
path_to_weights str | Path

filename of the pre-trained LuaTorch weights file [str]

required
model VGGFace

VGGFace model

required

Returns:

Type Description
VGGFace

VGGFace model with loaded weights

Source code in code/facesim3d/modeling/VGG/convert_weights.py
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
def convert_weights(path_to_weights: str | Path, model: VGGFace) -> VGGFace:
    """
    Convert `LuaTorch` weights and load them into a `PyTorch` model.

    :param path_to_weights: filename of the pre-trained `LuaTorch` weights file [str]
    :param model: `VGGFace` model
    :return: `VGGFace` model with loaded weights
    """
    torch_model = torchfile.load(path_to_weights)
    counter = 1
    block = 1
    block_size = [2, 2, 3, 3, 3]
    block_cut: int = 5
    for _i, layer in enumerate(torch_model.modules):
        if layer.weight is not None:
            if block <= block_cut:
                self_layer = model.features[f"conv_{block}_{counter}"]
                counter += 1
                if counter > block_size[block - 1]:
                    counter = 1
                    block += 1
                self_layer.weight.data[...] = torch.tensor(layer.weight).view_as(self_layer.weight)[...]
                self_layer.bias.data[...] = torch.tensor(layer.bias).view_as(self_layer.bias)[...]
            else:
                self_layer = model.fc[f"fc{block}"]
                block += 1
                self_layer.weight.data[...] = torch.tensor(layer.weight).view_as(self_layer.weight)[...]
                self_layer.bias.data[...] = torch.tensor(layer.bias).view_as(self_layer.bias)[...]
    return model

download_torch_weights 🗿

download_torch_weights() -> Path

Download torch weights for VGG-Face if necessary.

Source code in code/facesim3d/modeling/VGG/convert_weights.py
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
def download_torch_weights() -> Path:
    """Download `torch` weights for `VGG-Face` if necessary."""
    # Create output directory (if necessary)
    p2_model = Path(paths.data.models.vggface).parent
    p2_model.mkdir(parents=True, exist_ok=True)

    p2_torch_weights = Path(paths.data.models.vggface, WEIGHT_FILE)

    if not p2_torch_weights.is_file():
        torch_tar_file = Path(paths.data.models.vggface).parent / "vgg_face_torch.tar.gz"
        # Download tar.gz file
        cprint(string="Downloading VGG-Face torch weights 'vgg_face_torch.tar.gz'...", col="b")
        urllib.request.urlretrieve(
            url="http://www.robots.ox.ac.uk/~vgg/software/vgg_face/src/vgg_face_torch.tar.gz", filename=torch_tar_file
        )

        # Extract weight file from tar.gz
        if not p2_torch_weights.is_file():
            cprint(string="Extracting weights from 'vgg_face_torch.tar.gz'...", col="b")
            with tarfile.open(torch_tar_file) as tar_file:
                tar_file.extract(member=f"vgg_face_torch/{WEIGHT_FILE}", path=p2_model)
            torch_tar_file.unlink()

    return p2_torch_weights