towhee
/
            
			You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
			Readme
Files and versions
		
			
				
					54 lines
				
				1.9 KiB
			
		
		
			
		
	
	
					54 lines
				
				1.9 KiB
			| 
								 
											4 years ago
										 
									 | 
							
								# Copyright 2021 Zilliz. All rights reserved.
							 | 
						||
| 
								 | 
							
								#
							 | 
						||
| 
								 | 
							
								# Licensed under the Apache License, Version 2.0 (the "License");
							 | 
						||
| 
								 | 
							
								# you may not use this file except in compliance with the License.
							 | 
						||
| 
								 | 
							
								# You may obtain a copy of the License at
							 | 
						||
| 
								 | 
							
								#
							 | 
						||
| 
								 | 
							
								#     http://www.apache.org/licenses/LICENSE-2.0
							 | 
						||
| 
								 | 
							
								#
							 | 
						||
| 
								 | 
							
								# Unless required by applicable law or agreed to in writing, software
							 | 
						||
| 
								 | 
							
								# distributed under the License is distributed on an "AS IS" BASIS,
							 | 
						||
| 
								 | 
							
								# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
							 | 
						||
| 
								 | 
							
								# See the License for the specific language governing permissions and
							 | 
						||
| 
								 | 
							
								# limitations under the License.
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								from typing import NamedTuple
							 | 
						||
| 
								 | 
							
								import numpy
							 | 
						||
| 
								 | 
							
								from towhee.operator import Operator
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								class ImageEmbeddingOperatorTemplate(Operator):
							 | 
						||
| 
								 | 
							
								    """
							 | 
						||
| 
								 | 
							
								    An operator class that implements image embedding.
							 | 
						||
| 
								 | 
							
								    
							 | 
						||
| 
								 | 
							
								    Args:
							 | 
						||
| 
								 | 
							
								        model_name (`str`):
							 | 
						||
| 
								 | 
							
								            Embedding the image withe the specific model.
							 | 
						||
| 
								 | 
							
								    """
							 | 
						||
| 
								 | 
							
								    def __init__(self, model_name: str, framework: str = 'pytorch') -> None:
							 | 
						||
| 
								 | 
							
								        """
							 | 
						||
| 
								 | 
							
								        Initialize some parameters and load the model.
							 | 
						||
| 
								 | 
							
								        """
							 | 
						||
| 
								 | 
							
								        super().__init__()
							 | 
						||
| 
								 | 
							
								        # please define Model class and declare model object, and initialize with the model_name
							 | 
						||
| 
								 | 
							
								        # if framework == 'pytorch':
							 | 
						||
| 
								 | 
							
								        #     from pytorch import Model
							 | 
						||
| 
								 | 
							
								        self.model = Model(model_name)
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    def __call__(self, img_tensor: torch.Tensor) -> NamedTuple('Outputs', [('feature_vector', numpy.ndarray)]):
							 | 
						||
| 
								 | 
							
								        """
							 | 
						||
| 
								 | 
							
								        Call it when use this class.
							 | 
						||
| 
								 | 
							
								        
							 | 
						||
| 
								 | 
							
								        Args:
							 | 
						||
| 
								 | 
							
								            img_tensor (`torch.Tensor`):
							 | 
						||
| 
								 | 
							
								                The image tensor, you can generate it with TransformImageOperatorClass.
							 | 
						||
| 
								 | 
							
								        Returns:
							 | 
						||
| 
								 | 
							
								            (`numpy.ndarray`)
							 | 
						||
| 
								 | 
							
								                The image embedding.
							 | 
						||
| 
								 | 
							
								        """
							 | 
						||
| 
								 | 
							
								        # call model with the img_tensor parameter
							 | 
						||
| 
								 | 
							
								        result = self.model(img_tensor)
							 | 
						||
| 
								 | 
							
								        Outputs = NamedTuple('Outputs', [('feature_vector', numpy.ndarray)])
							 | 
						||
| 
								 | 
							
								        return Outputs(result)
							 |