ChatGPT
1import cv22import numpy as np3import torch4
5# Sample image tensor dimensions: (channels=3, height=100, width=100)6image_tensor = torch.rand(3, 100, 100) # Replace with your actual tensor7
8# Convert PyTorch tensor to NumPy array9image_np = image_tensor.permute(1, 2, 0).cpu().numpy() # Channels last for OpenCV10
11# Define the target size12target_height = 6413target_width = 6414
15# Resize the image using OpenCV8 collapsed lines
16resized_image_np = cv2.resize(image_np, (target_width, target_height))17
18# Convert NumPy array back to PyTorch tensor19resized_image_tensor = torch.from_numpy(resized_image_np).permute(2, 0, 1).float()20
21# Print the shapes to verify22print("Original tensor shape:", image_tensor.shape)23print("Resized tensor shape:", resized_image_tensor.shape)
1import torch2import torch.nn.functional as F3
4# 假设你有一个480x480的RGB图片,可以表示为一个3维的torch.Tensor5# 假设img是你的原始图像,大小为(1, 3, 480, 480)6# (1, 3, 480, 480)表示(batch_size, channels, height, width)7
8# 生成一个480x480的假图片,这里用随机数代替9img = torch.rand(1, 3, 480, 480)10
11# 缩放成128x128的图像12scaled_img = F.interpolate(img, size=(128, 128), mode='bilinear', align_corners=False)13
14# 输出缩放后图像的大小15print("缩放后图像大小:", scaled_img.size()) # 输出: torch.Size([1, 3, 128, 128])