| 
						
						
							
								
							
						
						
					 | 
				
				 | 
				
					@ -20,12 +20,12 @@ from PIL import Image | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					import torch | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					from timm.data import resolve_data_config | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					from timm.data.transforms_factory import create_transform | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					import numpy | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					from towhee.operator import Operator | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					class VisionTransformerEmbeddingOperator(Operator): | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					class VitImageEmbedding(Operator): | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    """ | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    Embedding extractor using ViT. | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    Args: | 
				
			
			
		
	
	
		
			
				
					| 
						
						
						
							
								
							
						
					 | 
				
				 | 
				
					@ -40,13 +40,13 @@ class VisionTransformerEmbeddingOperator(Operator): | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        super().__init__() | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        sys.path.append(str(Path(__file__).parent)) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        if framework == 'pytorch': | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					            from vit_embedding.pytorch.model import Model | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					            from pytorch.model import Model | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        self.model = Model(model_name, weights_path) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        config = resolve_data_config({}, model=self.model._model) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        self.tfms = create_transform(**config) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    def __call__(self, img_path: str) -> NamedTuple('Outputs', [('embedding', torch.Tensor)]): | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        Outputs = NamedTuple('Outputs', [('embedding', torch.Tensor)]) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    def __call__(self, img_path: str) -> NamedTuple('Outputs', [('feature_vector', numpy.ndarray)]): | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        Outputs = NamedTuple('Outputs', [('feature_vector', numpy.ndarray)]) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        img = self.tfms(Image.open(img_path)).unsqueeze(0) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        features = self.model(img) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        return Outputs(features.flatten().detach().numpy()) | 
				
			
			
		
	
	
		
			
				
					| 
						
						
						
					 | 
				
				 | 
				
					
  |