o
    ϯi                     @   sv   d dl mZmZ d dlZd dlmZ d dlmZ d dlm	Z	 G dd dej
ZG dd dej
ZG d	d
 d
ej
ZdS )    )ListOptionalN)set_attributes)init_net_weightsc                       sB   e Zd ZdZdejddf fddZdejdejfdd	Z	  Z
S )
Netug  
    Build a general Net models with a list of blocks for video recognition.

    ::

                                         Input
                                           ↓
                                         Block 1
                                           ↓
                                           .
                                           .
                                           .
                                           ↓
                                         Block N
                                           ↓

    The ResNet builder can be found in `create_resnet`.
    blocksreturnNc                   s(   t    |dusJ || _t|  dS )z]
        Args:
            blocks (torch.nn.module_list): the list of block modules.
        N)super__init__r   r   )selfr   	__class__ K/home/ubuntu/.local/lib/python3.10/site-packages/pytorchvideo/models/net.pyr
      s   
zNet.__init__xc                 C   s&   t t| jD ]	}| j| |}q|S )N)rangelenr   )r   r   idxr   r   r   forward)   s   zNet.forward)__name__
__module____qualname____doc__nn
ModuleListr
   torchTensorr   __classcell__r   r   r   r   r      s    
r   c                       sD   e Zd ZdZdejdejf fddZdejdejfdd	Z	  Z
S )
DetectionBBoxNetworkzO
    A general purpose model that handles bounding boxes as part of input.
    modeldetection_headc                    s   t    || _|| _dS )z
        Args:
            model (nn.Module): a model that preceeds the head. Ex: stem + stages.
            detection_head (nn.Module): a network head. that can take in input bounding boxes
                and the outputs from the model.
        N)r	   r
   r   r    )r   r   r    r   r   r   r
   4   s   

zDetectionBBoxNetwork.__init__r   bboxesc                 C   s(   |  |}| ||}||jd dS )aC  
        Args:
            x (torch.tensor): input tensor
            bboxes (torch.tensor): accociated bounding boxes.
                The format is N*5 (Index, X_1,Y_1,X_2,Y_2) if using RoIAlign
                and N*6 (Index, x_ctr, y_ctr, width, height, angle_degrees) if
                using RoIAlignRotated.
        r   )r   r    viewshape)r   r   r!   featuresoutr   r   r   r   ?   s   
	zDetectionBBoxNetwork.forward)r   r   r   r   r   Moduler
   r   r   r   r   r   r   r   r   r   /   s    r   c                	       s^   e Zd ZdZdddejdeej dee ddf fd	d
Z	de
ej dejfddZ  ZS )MultiPathWayWithFuseu  
    Build multi-pathway block with fusion for video recognition, each of the pathway
    contains its own Blocks and Fusion layers across different pathways.

    ::

                            Pathway 1  ... Pathway N
                                ↓              ↓
                             Block 1        Block N
                                ↓⭠ --Fusion----↓
    T)inplacemultipathway_blocksmultipathway_fusionr)   r   Nc                   s   t    t| t  dS )a  
        Args:
            multipathway_blocks (nn.module_list): list of models from all pathways.
            multipathway_fusion (nn.module): fusion model.
            inplace (bool): If inplace, directly update the input list without making
                a copy.
        N)r	   r
   r   locals)r   r*   r+   r)   r   r   r   r
   Z   s   
zMultiPathWayWithFuse.__init__r   c                 C   s|   t |ts	J d| jr|}nd gt| }tt| jD ]}| j| d ur1| j| || ||< q| jd ur<| |}|S )Nz<input for MultiPathWayWithFuse needs to be a list of tensors)
isinstancelistr)   r   r   r*   r+   )r   r   x_outpathway_idxr   r   r   r   k   s"   

zMultiPathWayWithFuse.forward)r   r   r   r   r   r   r   r'   boolr
   r   r   r   r   r   r   r   r   r   r(   M   s    "r(   )typingr   r   r   torch.nnr   pytorchvideo.layers.utilsr   pytorchvideo.models.weight_initr   r'   r   r   r(   r   r   r   r   <module>   s   $