o  iB(@sddlmZddlZddlmZddlmZddlZddlm m Z ddlm Z ddl mZddlmZddlmZmZdd lmZmZd%ddZd&ddZejfd'd!d"Zejfd'd#d$ZdS)() annotationsN)Sequence)Any)Tensor)standardize_empty_box) SpatialPad)compute_divisible_spatial_sizeconvert_pad_mode)PytorchPadModeensure_tuple_rep input_imageslist[Tensor] | Tensor spatial_dimsintreturnNonecCst|trt|j|dkrtd|dd|jddSt|trA|D]}t|j|dkr>td|dd|jdq%dStd) 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)r rimgrk/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/apps/detection/utils/detector_utils.pycheck_input_imagess.  rtargetslist[dict[str, Tensor]] | Nonetarget_label_keystrtarget_box_keylist[dict[str, Tensor]]c Cs|durtdt|t|krtdt|dt|dtt|D]}||}||vs7||vrFt|d|d|d||}t|tjsZtdt|dt|jd ksj|jd d |kr| d krt d |jd d |dntdd |d|jdt |std|j dt||d|||<||}t |rt d|j d||||<q%|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) r rrr r"itargetboxeslabelsrrrcheck_training_targets7s@      r4size_divisibleint | Sequence[int]modePytorchPadMode | strkwargsrtuple[Tensor, list[list[int]]]c st|}t|tr[t|j dt|d}fddt|D}dd|Dddd}t|dkrA|g|jdfSt||d}t j ||fd |i|g|jdfSfd d|D} |djd} |dj } |dj } t | } t j| dd \}}t|kst|krtd tt||d}t jt| | gt|| | d }td|d|d|}t|D] \}}||||df<q|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).0r0sp_i) orig_sizerr s&zpad_images..cSs$g|]}|dddD]}|q qS)Nr$r)r>sublistvalrrrrAs$r$r)dstr7r7csg|] }|j dqS)N)r)r>rr%rrrAs)dimzG Require len(max_spatial_size_t) == spatial_dims ==len(size_divisible).)r.deviceend) spatial_sizemethodr7.cSsg|]}t|qSr)r)r>ssrrrrAsr)r rrrrr enumerater=r Fpadr.rFr(tensorrrzerosr)r rr5r7r9new_sizeZ all_pad_width pt_pad_widthmode_ image_sizes in_channelsr.rFZ image_sizes_tZmax_spatial_size_t_max_spatial_sizeimagespadderidxrr)r@rr pad_imagesos0     (    rZcKs*t||t||}t||||fi|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 rZ)r rr5r7r9rrrpreprocess_imagess  r[)r r rrrr) r r rrrrr r!r"r!rr#) r r rrr5r6r7r8r9rrr:) __future__rr+collections.abcrtypingrr(torch.nn.functionalnn functionalrLrmonai.data.box_utilsrmonai.transforms.croppad.arrayrmonai.transforms.utilsrr monai.utilsr r rr4CONSTANTrZr[rrrrs"        < H