Skip to content

High level techniques

PytorchImageModels(model_name, feature_layer=-1, flatten=True, device='cpu', apply_on_output=None, imgs_dirs='imgs_dirs', max_timeout=2, max_retries=5, max_workers=0, batch_size=64, resize_size=(227, 227))

Bases: HighLevelVisual

High level technique which uses the [timm library] (https://timm.fast.ai/) for feature extraction from images using pre-trained models

PARAMETER DESCRIPTION
model_name

a model name supported by the timm library

TYPE: str

feature_layer

the layer index from which the features will be retrieved NOTE: the model is loaded from the timm library with the parameter "features_only" set at True, meaning that only feature layers of the model will be available and accessible through the index

TYPE: int DEFAULT: -1

flatten

whether the features obtained from the model should be flattened or not

TYPE: bool DEFAULT: True

imgs_dirs

directory where the images are stored (or will be stored in the case of fields containing links)

TYPE: str DEFAULT: 'imgs_dirs'

max_timeout

maximum time to wait before considering a request failed (image from link)

TYPE: int DEFAULT: 2

max_retries

maximum number of retries to retrieve an image from a link

TYPE: int DEFAULT: 5

max_workers

maximum number of workers for parallelism

TYPE: int DEFAULT: 0

batch_size

batch size for the images dataloader

TYPE: int DEFAULT: 64

resize_size

since the Tensorflow dataset requires all images to be of the same size, they will all be resized to the specified size. Note that if you were to specify a resize transformer in the preprocessing pipeline, the size specified in the latter will be the final resize size

TYPE: Tuple[int, int] DEFAULT: (227, 227)

Source code in clayrs/content_analyzer/field_content_production_techniques/visual_techniques/high_level_techniques.py
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
def __init__(self, model_name: str, feature_layer: int = -1, flatten: bool = True, device: str = 'cpu',
             apply_on_output: Callable[[torch.Tensor], torch.Tensor] = None,
             imgs_dirs: str = "imgs_dirs", max_timeout: int = 2, max_retries: int = 5,
             max_workers: int = 0, batch_size: int = 64, resize_size: Tuple[int, int] = (227, 227)):

    super().__init__(imgs_dirs, max_timeout, max_retries, max_workers, batch_size, resize_size)
    original_model = timm.create_model(model_name, pretrained=True)

    feature_layer = list(original_model._modules.keys())[feature_layer]

    layers = {}
    for layer_name, layer in original_model._modules.items():
        layers[layer_name] = layer
        if layer_name == feature_layer:
            break

    self.model = torch.nn.Sequential(OrderedDict(layers)).eval()

    def return_self(x: torch.Tensor) -> torch.Tensor:
        return x

    self.apply_on_output = return_self if apply_on_output is None else apply_on_output

    self.model.to(device)
    self.device = device
    self.flatten = flatten
    self.model_name = model_name

    self._repr_string = autorepr(self, inspect.currentframe())