Home > Technology peripherals > AI > body text

A super powerful Pytorch operation! !

PHPz
Release: 2024-01-06 21:02:14
forward
922 people have browsed it

Hi, Xiaozhuang! Nice to meet you! Is there anything I can do to help you?

I have shared some content about deep learning over the past few days.

In addition, there are some common data processing functions similar to numpy and pandas in Pytorch, which are equally important and interesting!

Similarly, PyTorch also provides many functions for data processing and conversion.

Now let’s take a look at the most important necessary functions.

一个超强 Pytorch 操作!!

torch.Tensor

In PyTorch, torch.Tensor is a basic data structure used to represent tensors. A tensor is a multi-dimensional array that can contain different types of data such as numbers and Boolean values. You can use the constructor of torch.Tensor to create a tensor, or you can use other functions to create it.

import torch# 创建一个空的张量empty_tensor = torch.Tensor()# 从列表创建张量data = [1, 2, 3, 4]tensor_from_list = torch.Tensor(data)
Copy after login

torch.from_numpy

is used to convert NumPy arrays to PyTorch tensors.

import numpy as npnumpy_array = np.array([1, 2, 3, 4])torch_tensor = torch.from_numpy(numpy_array)
Copy after login

torch.Tensor.item

Used to extract Python values ​​from a tensor containing only one element. Applies to scalar tensors.

scalar_tensor = torch.tensor(5)scalar_value = scalar_tensor.item()
Copy after login

torch.Tensor.view

is used to change the shape of the tensor.

original_tensor = torch.randn(2, 3)# 2x3的随机张量reshaped_tensor = original_tensor.view(3, 2)# 将形状改变为3x2
Copy after login

torch.Tensor.to

is used to convert tensors to a specified device (such as CPU or GPU).

cpu_tensor = torch.randn(3)gpu_tensor = cpu_tensor.to("cuda")# 将张量移动到GPU
Copy after login

torch.Tensor.numpy

Convert a tensor to a NumPy array.

pytorch_tensor = torch.tensor([1, 2, 3])numpy_array = pytorch_tensor.numpy()
Copy after login

torch.nn.functional.one_hot

Used for one-hot encoding of integer tensors.

import torch.nn.functional as Finteger_tensor = torch.tensor([0, 2, 1])one_hot_encoded = F.one_hot(integer_tensor)
Copy after login

torch.utils.data.Dataset and torch.utils.data.DataLoader

are used to load and process data sets. These two classes are typically used together with custom dataset classes.

from torch.utils.data import Dataset, DataLoaderclass CustomDataset(Dataset):def __init__(self, data):self.data = datadef __len__(self):return len(self.data)def __getitem__(self, index):return self.data[index]dataset = CustomDataset([1, 2, 3, 4, 5])dataloader = DataLoader(dataset, batch_size=2, shuffle=True)
Copy after login

The above are some important data conversion functions in PyTorch, which are simply used.

They are very, very helpful for processing and preparing data in deep learning tasks.

A case

Next, we make an image segmentation case.

In this case, we will use PyTorch and torchvision library for image segmentation, using the pre-trained DeepLabV3 model and PASCAL VOC dataset.

In the entire code, it involves the content learned above, resizing, cropping, standardization, etc.

import torchimport torchvision.transforms as transformsfrom torchvision import modelsfrom PIL import Imageimport matplotlib.pyplot as plt# 下载示例图像!wget -O example_image.jpg https://pytorch.org/assets/deeplab/deeplab1.jpg# 定义图像转换transform = transforms.Compose([transforms.Resize((256, 256)),# 调整大小transforms.ToTensor(), # 转换为张量transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])# 标准化])# 加载并转换图像image_path = 'example_image.jpg'image = Image.open(image_path).convert("RGB")input_tensor = transform(image).unsqueeze(0)# 添加批次维度# 加载预训练的DeepLabV3模型model = models.segmentation.deeplabv3_resnet101(pretrained=True)model.eval()# 进行图像分割with torch.no_grad():output = model(input_tensor)['out'][0]output_predictions = output.argmax(0)# 将预测结果转换为彩色图像def decode_segmap(image, nc=21):label_colors = np.array([(0, 0, 0),# 0: 背景 (128, 0, 0), (0, 128, 0), (128, 128, 0), (0, 0, 128), (128, 0, 128),# 1-5: 物体 (0, 128, 128), (128, 128, 128), (64, 0, 0), (192, 0, 0),# 6-9: 道路 (64, 128, 0), (192, 128, 0), (64, 0, 128), (192, 0, 128),# 10-13: 面部 (64, 128, 128), (192, 128, 128), (0, 64, 0), (128, 64, 0),# 14-17: 植物 (0, 192, 0), (128, 192, 0), (0, 64, 128)])# 18-20: 建筑r = np.zeros_like(image).astype(np.uint8)g = np.zeros_like(image).astype(np.uint8)b = np.zeros_like(image).astype(np.uint8)for l in range(0, nc):idx = image == lr[idx] = label_colors[l, 0]g[idx] = label_colors[l, 1]b[idx] = label_colors[l, 2]rgb = np.stack([r, g, b], axis=2)return rgb# 将预测结果转换为彩色图像output_rgb = decode_segmap(output_predictions.numpy())# 可视化原始图像和分割结果plt.figure(figsize=(12, 6))plt.subplot(1, 2, 1)plt.imshow(image)plt.title('Original Image')plt.subplot(1, 2, 2)plt.imshow(output_rgb)plt.title('Segmentation Result')plt.show()
Copy after login

In this case, we first define a series of image transformation functions, including resize, convert to tensor and normalize. These transformations ensure that the input image meets the model's needs.

Then, a sample image was loaded and these transformations were applied.

Next, we used the pre-trained DeepLabV3 model in torchvision for image segmentation. For the output, we extracted the maximum index of the prediction results to obtain the predicted class for each pixel.

Finally, we convert the prediction results into color images and visualize the original images and segmentation results.

一个超强 Pytorch 操作!!

This case highlights the important role of the image transformation function in the image segmentation task, ensuring that the input image meets the input requirements of the model and that the output result is easy to visualize.

The above is the detailed content of A super powerful Pytorch operation! !. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:51cto.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!