From 63882fc3f38ff4cdf06b4141a4da1e4fa44b6eed Mon Sep 17 00:00:00 2001 From: Matthijs van der Burgh Date: Thu, 13 Jun 2024 09:45:45 +0200 Subject: [PATCH] Move library into own folder --- README.md | 4 ++-- README_cn.md | 4 ++-- __init__.py | 12 ------------ examples/infer.ipynb | 3 +-- facenet_pytorch/__init__.py | 14 ++++++++++++++ .../models}/inception_resnet_v1.py | 0 {models => facenet_pytorch/models}/mtcnn.py | 10 ++++++---- .../models}/utils/detect_face.py | 0 .../models}/utils/download.py | 0 .../models}/utils/tensorflow2pytorch.py | 4 +--- .../models}/utils/training.py | 0 setup.py | 2 +- tests/actions_test.py | 5 ++--- 13 files changed, 29 insertions(+), 29 deletions(-) delete mode 100644 __init__.py create mode 100644 facenet_pytorch/__init__.py rename {models => facenet_pytorch/models}/inception_resnet_v1.py (100%) rename {models => facenet_pytorch/models}/mtcnn.py (98%) rename {models => facenet_pytorch/models}/utils/detect_face.py (100%) rename {models => facenet_pytorch/models}/utils/download.py (100%) rename {models => facenet_pytorch/models}/utils/tensorflow2pytorch.py (99%) rename {models => facenet_pytorch/models}/utils/training.py (100%) diff --git a/README.md b/README.md index 44090393..9ede13b9 100644 --- a/README.md +++ b/README.md @@ -78,7 +78,7 @@ See `help(MTCNN)` and `help(InceptionResnetV1)` for usage and implementation det ## Pretrained models -See: [models/inception_resnet_v1.py](models/inception_resnet_v1.py) +See: [models/inception_resnet_v1.py](facenet_pytorch/models/inception_resnet_v1.py) The following models have been ported to pytorch (with links to download pytorch state_dict's): @@ -193,7 +193,7 @@ Alternatively, the code can be installed as a package using pip: ## Conversion of parameters from Tensorflow to Pytorch -See: [models/utils/tensorflow2pytorch.py](models/tensorflow2pytorch.py) +See: [models/utils/tensorflow2pytorch.py](facenet_pytorch/models/tensorflow2pytorch.py) Note that this functionality is not needed to use the models in this repo, which depend only on the saved pytorch `state_dict`'s. diff --git a/README_cn.md b/README_cn.md index b11c9eca..3ed4efe8 100644 --- a/README_cn.md +++ b/README_cn.md @@ -118,7 +118,7 @@ img_probs = resnet(img_cropped.unsqueeze(0)) ## 预训练模型 -请参阅:[models/inception_resnet_v1.py](models/inception_resnet_v1.py) +请参阅:[models/inception_resnet_v1.py](facenet_pytorch/models/inception_resnet_v1.py) 以下模型已移植到 pytorch(包含下载 pytorch state_dict 的链接): @@ -233,7 +233,7 @@ docker run --rm -p 8888:8888 ## Tensorflow 到 Pytorch 的参数转换 -请参阅:[models/utils/tensorflow2pytorch.py](models/tensorflow2pytorch.py) +请参阅:[models/utils/tensorflow2pytorch.py](facenet_pytorch/models/tensorflow2pytorch.py) 请注意,使用此存储库中的模型不需要此功能,该功能仅依赖于pytorch保存的 `state_dict`。 diff --git a/__init__.py b/__init__.py deleted file mode 100644 index 7f1018f1..00000000 --- a/__init__.py +++ /dev/null @@ -1,12 +0,0 @@ -import warnings - -from .models.inception_resnet_v1 import InceptionResnetV1 -from .models.mtcnn import MTCNN, ONet, PNet, RNet, fixed_image_standardization, prewhiten -from .models.utils import training -from .models.utils.detect_face import extract_face - -warnings.filterwarnings( - action="ignore", - message="This overload of nonzero is deprecated:\n\tnonzero()", - category=UserWarning, -) diff --git a/examples/infer.ipynb b/examples/infer.ipynb index d36a056f..d61a5e88 100644 --- a/examples/infer.ipynb +++ b/examples/infer.ipynb @@ -24,7 +24,6 @@ "import torch\n", "from torch.utils.data import DataLoader\n", "from torchvision import datasets\n", - "import numpy as np\n", "import pandas as pd\n", "import os\n", "\n", @@ -242,4 +241,4 @@ }, "nbformat": 4, "nbformat_minor": 2 -} \ No newline at end of file +} diff --git a/facenet_pytorch/__init__.py b/facenet_pytorch/__init__.py new file mode 100644 index 00000000..f11d3287 --- /dev/null +++ b/facenet_pytorch/__init__.py @@ -0,0 +1,14 @@ +import warnings + +from facenet_pytorch.models.inception_resnet_v1 import InceptionResnetV1 +from facenet_pytorch.models.mtcnn import MTCNN, ONet, PNet, RNet, fixed_image_standardization, prewhiten +from facenet_pytorch.models.utils import training +from facenet_pytorch.models.utils.detect_face import extract_face + +__all__ = ["InceptionResnetV1", "MTCNN", "ONet", "PNet", "RNet", "fixed_image_standardization", "prewhiten", "training", "extract_face"] + +warnings.filterwarnings( + action="ignore", + message="This overload of nonzero is deprecated:\n\tnonzero()", + category=UserWarning, +) diff --git a/models/inception_resnet_v1.py b/facenet_pytorch/models/inception_resnet_v1.py similarity index 100% rename from models/inception_resnet_v1.py rename to facenet_pytorch/models/inception_resnet_v1.py diff --git a/models/mtcnn.py b/facenet_pytorch/models/mtcnn.py similarity index 98% rename from models/mtcnn.py rename to facenet_pytorch/models/mtcnn.py index 7eb64550..202943d0 100644 --- a/models/mtcnn.py +++ b/facenet_pytorch/models/mtcnn.py @@ -1,10 +1,12 @@ import os +import importlib.resources import numpy as np import torch from torch import nn -from .utils.detect_face import detect_face, extract_face +from facenet_pytorch.models.utils.detect_face import detect_face, extract_face +import facenet_pytorch.data class PNet(nn.Module): @@ -31,7 +33,7 @@ def __init__(self, pretrained=True): self.training = False if pretrained: - state_dict_path = os.path.join(os.path.dirname(__file__), "../data/pnet.pt") + state_dict_path = importlib.resources.path(facenet_pytorch.data, "pnet.pt") state_dict = torch.load(state_dict_path) self.load_state_dict(state_dict) @@ -76,7 +78,7 @@ def __init__(self, pretrained=True): self.training = False if pretrained: - state_dict_path = os.path.join(os.path.dirname(__file__), "../data/rnet.pt") + state_dict_path = importlib.resources.path(facenet_pytorch.data, "rnet.pt") state_dict = torch.load(state_dict_path) self.load_state_dict(state_dict) @@ -129,7 +131,7 @@ def __init__(self, pretrained=True): self.training = False if pretrained: - state_dict_path = os.path.join(os.path.dirname(__file__), "../data/onet.pt") + state_dict_path = importlib.resources.path(facenet_pytorch.data, "onet.pt") state_dict = torch.load(state_dict_path) self.load_state_dict(state_dict) diff --git a/models/utils/detect_face.py b/facenet_pytorch/models/utils/detect_face.py similarity index 100% rename from models/utils/detect_face.py rename to facenet_pytorch/models/utils/detect_face.py diff --git a/models/utils/download.py b/facenet_pytorch/models/utils/download.py similarity index 100% rename from models/utils/download.py rename to facenet_pytorch/models/utils/download.py diff --git a/models/utils/tensorflow2pytorch.py b/facenet_pytorch/models/utils/tensorflow2pytorch.py similarity index 99% rename from models/utils/tensorflow2pytorch.py rename to facenet_pytorch/models/utils/tensorflow2pytorch.py index 7972b74b..a55e2634 100644 --- a/models/utils/tensorflow2pytorch.py +++ b/facenet_pytorch/models/utils/tensorflow2pytorch.py @@ -1,14 +1,12 @@ import json import os -import sys import tensorflow as tf import torch from dependencies.facenet.src import facenet from dependencies.facenet.src.align import detect_face -from dependencies.facenet.src.models import inception_resnet_v1 as tf_mdl -from models.inception_resnet_v1 import InceptionResnetV1 +from facenet_pytorch.models.inception_resnet_v1 import InceptionResnetV1 from models.mtcnn import ONet, PNet, RNet diff --git a/models/utils/training.py b/facenet_pytorch/models/utils/training.py similarity index 100% rename from models/utils/training.py rename to facenet_pytorch/models/utils/training.py diff --git a/setup.py b/setup.py index 3c096e97..e8107bea 100644 --- a/setup.py +++ b/setup.py @@ -30,7 +30,7 @@ "facenet_pytorch.models.utils", "facenet_pytorch.data", ], - package_dir={"facenet_pytorch": "."}, + package_dir={"facenet_pytorch.data": "data"}, package_data={"": ["*net.pt"]}, classifiers=[ "Programming Language :: Python :: 3", diff --git a/tests/actions_test.py b/tests/actions_test.py index 307673f9..6a9dc6b9 100644 --- a/tests/actions_test.py +++ b/tests/actions_test.py @@ -12,11 +12,10 @@ import pandas as pd import torch from PIL import Image, ImageDraw -from torch.utils.data import DataLoader from torchvision import datasets, transforms -from models.inception_resnet_v1 import InceptionResnetV1, get_torch_home -from models.mtcnn import MTCNN, fixed_image_standardization +from facenet_pytorch.models.inception_resnet_v1 import InceptionResnetV1, get_torch_home +from facenet_pytorch.models.mtcnn import MTCNN, fixed_image_standardization #### CLEAR ALL OUTPUT FILES ####