lundi 4 avril 2022

Python : using import * at local scope

I'm tring to use factory pattern (or factory method pattern) for image analysis. But memory and harddisk restriction keeps me to reduce any of dependencies as much as possible or make them as soft-dependencies. And I encountered SyntaxError by importing * at local scope. Sample code is below:

Sample Project File Tree

.
└── root/
    ├── models/
    │   ├── pytorch.py
    │   └── opencv.py
    └── factory/
        └── __init__.py

models

For this examples, pytorch.py and opencv.py have same functional unit object but the dependency is differ to each other.

pytorch.py

import torch
from torchvision.models import resnet18

__all__ = ["Resnet18Logit"]

class Resnet18Logit:
    def forward(self, img, *args, **kwargs):
        model = resnet18(pretrained=True)
        return model.forward(img)

opencv.py

import cv2 

__all__ = ["HOGfeatureextract"]

class HOGfeatureextract:
    def forward(self, img, *args, **kwargs):
         hog = cv2.HOGDescriptor()
         return hog.compute(im)

class SIFTfeatureextract:
    def forward(self, img,, *args, **kwargs):
        sift = cv2.SIFTDescriptor()
        return sift.detect_and_compute(img)

factory

init.py

class FeatureExtractor:
    def __init__(self, lib_name, name):
        if lib_name == "pytorch":
            from models.pytorch import * # This is impossible 
        elif lib_name == "opencv":
            from models.opencv import * # This is impossible
        self.fe = eval(name)

from models.pytorch import * is the part which is the problem. The import syntax from ~ import * is needed to be inside of function to avoid loading all dependencies at all, but I'll produce SyntaxError: import * only allowed at module level. I already checked 'import' or 'importlib', but in this case both of them only imports on module level and cannot load class, which is prerequisite to make eval() accepts any arbitrary Class name in __all__.

There is any workaround or solution while keeping factory pattern and soft-dependency?

Aucun commentaire:

Enregistrer un commentaire