https://discuss.pytorch.org/t/torchvision-transfors-how-to-perform-identical-transform-on-both-image-and-target/10606 1

Read the topic above, and then read some Github issues. Now, I think the best solution is to write own transform(take both image and target as arguments).
For example, my initial question can be solved as below.

屏幕截图 2021-09-22 1538121170×349 68 KB


屏幕截图 2021-09-22 1538301302×106 22.4 KB


屏幕截图 2021-09-22 153844822×556 42.5 KB

Now, the transform result of image and target are same. Here, mainly use torchvision.Transform.get_params() and torchvision.transforms.functional.xxx.
The Github Issue link mentioned above: https://github.com/pytorch/vision/issues/230 1.
If you want convenience, you can git clone https://github.com/pytorch/vision/blob/main/references/segmentation/transforms.py 1 to your workspace.(My solution is also mimic him)