U PhB(@sddlmZddlZddlmZddlmZddlZddlm m Z ddlm Z ddl mZddlmZddlmZmZdd lmZmZd d d d ddZd dd ddddddZejfd d dddddddZejfd d dddddddZdS)) annotationsN)Sequence)Any)Tensor)standardize_empty_box) SpatialPad)compute_divisible_spatial_sizeconvert_pad_mode)PytorchPadModeensure_tuple_repzlist[Tensor] | TensorintNone) input_images spatial_dimsreturncCst|tr:t|j|dkrtd|dd|jdnLt|tr~|D]2}t|j|dkrHtd|dd|jdqHntddS) am Validate the input dimensionality (raise a `ValueError` if invalid). Args: input_images: It can be 1) a tensor sized (B, C, H, W) or (B, C, H, W, D), or 2) a list of image tensors, each image i may have different size (C, H_i, W_i) or (C, H_i, W_i, D_i). spatial_dims: number of spatial dimensions of the images, 2 or 3. z`When input_images is a Tensor, its need to be (spatial_dims + 2)-D.In this case, it should be a z-D Tensor, got Tensor shape .zsWhen input_images is a List[Tensor], each element should have be (spatial_dims + 1)-D.In this case, it should be a z2input_images needs to be a List[Tensor] or Tensor.N) isinstancerlenshape ValueErrorlist)rrimgr^/home/dell461/cl/sdc2/HISourceFinder-master-l/src/monai/apps/detection/utils/detector_utils.pycheck_input_imagess  rzlist[dict[str, Tensor]] | Nonestrzlist[dict[str, Tensor]])rtargetsrtarget_label_keytarget_box_keyrc Cs|dkrtdt|t|kr>tdt|dt|dtt|D]>}||}||ksp||krt|d|d|d||}t|tjstdt|dt|jd ks|jd d |kr"| d krt d |jd d |dntdd |d|jdt |s@td|j dt||d|||<||}t |rJt d|j d||||<qJ|S)a Validate the input images/targets during training (raise a `ValueError` if invalid). Args: input_images: It can be 1) a tensor sized (B, C, H, W) or (B, C, H, W, D), or 2) a list of image tensors, each image i may have different size (C, H_i, W_i) or (C, H_i, W_i, D_i). targets: a list of dict. Each dict with two keys: target_box_key and target_label_key, ground-truth boxes present in the image. spatial_dims: number of spatial dimensions of the images, 2 or 3. target_label_key: the expected key of target labels. target_box_key: the expected key of target boxes. Nz4Please provide ground truth targets during training.z4len(input_images) should equal to len(targets), got z, rz and z# are expected keys in targets. Got z0Expected target boxes to be of type Tensor, got rrz)Warning: Given target boxes has shape of zA. The detector reshaped it with boxes = torch.reshape(boxes, [0, z]).z2Expected target boxes to be a tensor of shape [N, z], got z.).z0Expected target boxes to be a float tensor, got rz Warning: Given target labels is z*. The detector converted it to torch.long.)rrrangekeysrtorchrtypernumelwarningswarnis_floating_pointdtyperlong) rrrrr itargetboxeslabelsrrrcheck_training_targets7s: "  r1zint | Sequence[int]zPytorchPadMode | strrztuple[Tensor, list[list[int]]])rrsize_divisiblemodekwargsrc st|}t|trt|j dt|d}fddt|D}dd|Dddd}t|dkr|g|jdfSt||d}t j ||fd |i|g|jdfSfd d|D} |djd} |dj } |dj } t | } t j| dd \}}t|ks"t|kr*td tt||d}t jt| | gt|| | d }tf|d|d|}t|D]\}}||||df<qx|dd| DfS)a Pad the input images, so that the output spatial sizes are divisible by `size_divisible`. It pads them at the end to create a (B, C, H, W) or (B, C, H, W, D) Tensor. Padded size (H, W) or (H, W, D) is divisible by size_divisible. Default padding uses constant padding with value 0.0 Args: input_images: It can be 1) a tensor sized (B, C, H, W) or (B, C, H, W, D), or 2) a list of image tensors, each image i may have different size (C, H_i, W_i) or (C, H_i, W_i, D_i). spatial_dims: number of spatial dimensions of the images, 2D or 3D. size_divisible: int or Sequence[int], is the expected pattern on the input image shape. If an int, the same `size_divisible` will be applied to all the input spatial dimensions. mode: available modes for PyTorch Tensor: {``"constant"``, ``"reflect"``, ``"replicate"``, ``"circular"``}. One of the listed string values or a user supplied function. Defaults to ``"constant"``. See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.pad.html kwargs: other arguments for `torch.pad` function. Return: - images, a (B, C, H, W) or (B, C, H, W, D) Tensor - image_sizes, the original spatial size of each image N) spatial_shapekcs&g|]\}}dt||dfqS)r)max).0r-sp_i) orig_sizerr szpad_images..cSs$g|]}|dddD]}|qqS)Nr!r)r8sublistvalrrrr;sr!r)dstr3r3csg|]}|j dqS)N)r)r8rr"rrr;s)dimzG Require len(max_spatial_size_t) == spatial_dims ==len(size_divisible).)r+deviceend) spatial_sizemethodr3.cSsg|] }t|qSr)r)r8ssrrrr;s)r rrrrr enumerater7r Fpadr+r@r%tensorrrzerosr)rrr2r3r4new_sizeZ all_pad_width pt_pad_widthmode_ image_sizes in_channelsr+r@Z image_sizes_tZmax_spatial_size_t_max_spatial_sizeimagespadderidxrr)r:rr pad_imagesos0     (    rTcKs&t||t||}t||||f|S)aV Preprocess the input images, including - validate of the inputs - pad the inputs so that the output spatial sizes are divisible by `size_divisible`. It pads them at the end to create a (B, C, H, W) or (B, C, H, W, D) Tensor. Padded size (H, W) or (H, W, D) is divisible by size_divisible. Default padding uses constant padding with value 0.0 Args: input_images: It can be 1) a tensor sized (B, C, H, W) or (B, C, H, W, D), or 2) a list of image tensors, each image i may have different size (C, H_i, W_i) or (C, H_i, W_i, D_i). spatial_dims: number of spatial dimensions of the images, 2 or 3. size_divisible: int or Sequence[int], is the expected pattern on the input image shape. If an int, the same `size_divisible` will be applied to all the input spatial dimensions. mode: available modes for PyTorch Tensor: {``"constant"``, ``"reflect"``, ``"replicate"``, ``"circular"``}. One of the listed string values or a user supplied function. Defaults to ``"constant"``. See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.pad.html kwargs: other arguments for `torch.pad` function. Return: - images, a (B, C, H, W) or (B, C, H, W, D) Tensor - image_sizes, the original spatial size of each image )rr rT)rrr2r3r4rrrpreprocess_imagess  rU) __future__rr(collections.abcrtypingrr%torch.nn.functionalnn functionalrFrmonai.data.box_utilsrZmonai.transforms.croppad.arrayrmonai.transforms.utilsrr monai.utilsr r rr1CONSTANTrTrUrrrr s      <H