o
    ߥi                     @   sb   d dl Z d dlmZ d dlmZ d dlmZ d dlmZm	Z	 ej
e	jejdG dd deZdS )	    N)Models)
TorchModel)MODELS)	ModelFileTasks)module_namec                       s2   e Zd Zdef fddZdd Zdd Z  ZS )ClassificationModel	model_dirc           	         s   dd l }ddlm} dd l}ddlm} t | d| _t	j
|d}t	j
|r>|j|}d |j_||j| _d| _n||}d |jj_||jj| _d| _|| _|| _|   d S )Nr   )build_classifier)read_config	ms_configz	config.pymmcv_config)mmcvmmcls.modelsr
   3modelscope.models.cv.image_classification.backbonesmodelscope.utils.hubr   super__init__config_typeospathjoinexistsConfigfromfilemodel
pretrained	cls_modelmm_modelcfgms_model_dirload_pretrained_checkpoint)	selfr	   kwargsr   r
   
modelscoper   	mm_configr   	__class__ i/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/models/cv/image_classification/mmcls_model.pyr      s&   
zClassificationModel.__init__c                 C   s   | j di |S )Nr(   )r   )r"   inputsr(   r(   r)   forward(   s   zClassificationModel.forwardc                 C   s   dd l }tjtj| jtjrtj| jtj}ntj| jd}tj|rJ|jj	| j
|dd}d|di v rL|d d | j
_| j
j| _d S d S d S )Nr   zcheckpoints.pthcpu)map_locationCLASSESmeta)r   r   r   r   r   r    r   TORCH_MODEL_FILErunnerload_checkpointr   getr.   )r"   r   checkpoint_path
checkpointr(   r(   r)   r!   ,   s&   

z.ClassificationModel.load_pretrained_checkpoint)__name__
__module____qualname__strr   r+   r!   __classcell__r(   r(   r&   r)   r   
   s    r   )r   modelscope.metainfor   'modelscope.models.base.base_torch_modelr   modelscope.models.builderr   modelscope.utils.constantr   r   register_moduleimage_classificationclassification_modelr   r(   r(   r(   r)   <module>   s   