In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "how to use pytorch to achieve image classification data set". In daily operation, I believe that many people have doubts about how to use pytorch to achieve image classification data set. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubt of "how to use pytorch to achieve image classification data set". Next, please follow the editor to study!
Catalogue
Read dataset
Read small batch
Integrate all components
At present, one of the widely used image classification data sets is MNIST data set. Today, the MNIST dataset is more like a sound check than a benchmark.
To make it more difficult, we will discuss the similar but relatively complex Fashion-MNIST dataset released in 2017 in the following sections.
Import torchimport torchvisionfrom torch.utils import datafrom torchvision import transformsfrom d2l import torch as d2ld2l.use_svg_display () reads the dataset
We can download and read the Fashion-MNIST dataset into memory through the built-in functions in the framework.
# transform image data from PIL type to 32-bit floating point format through ToTensor instance # and divide by 255so that the values of all pixels are between 0 and 1 trans = transforms.ToTensor () mnist_train = torchvision.datasets.FashionMNIST (root= ".. / data", train=True, transform=trans, download=True) mnist_test = torchvisino.datasets.FashionMNIST (root= ".. / data", train=False, transform=trans, download=True)
Fashion-MNIST consists of 10 categories of images, each consisting of 6000 images in the training set and 1000 images in the test set.
The test data set (test dataset) is not used for training, but only for evaluating model performance. The training set and test set contain 60000 and 10000 images, respectively.
Len (mnist_train), len (mnist_test) (60000, 10000)
The height and width of each input image is 28 pixels. The dataset consists of grayscale images with a channel number of 1.
For the sake of brevity, in this article, we will height h pixel, width w pixel image shape is h × w or (hforce w).
Mnist_train [0] [0] .shapetorch.size ([1,28,28])
There are 10 categories in Fashion-MNIST
T-shirt (T-shirt), trouser (trousers), pullover (pullover), dress (dress), coat (coat),
Sandal (sandals), shirt (shirt), sneaker (sports shoes), bag (bag) and ankle boot (short boots)
The following function is used to convert between a numeric label index and its text name.
Def get_fashion_mnist_labels (labels): "returns the article label of the Fashion-MNIST dataset." Text_labels = ['t Merry shirtship, 'trouser',' pullover', 'dress',' coat', 'sandal',' shirt', 'sneaker',' bag', 'ankle boot'] return [text_ labels [int (I)] for i in labels]
We can now create a function to visualize these samples.
Def show_images (imgs, num_rows, num_cols, titles=None, scale=1.5): "Plot a list of images." Figsize= (num_cols * scale, num_rows * scale) _, axes = d2l.plt.subplots (num_rows, num_cols, figsize=figsize) axes = axes.flatten () for I, (ax, img) in enumerate (zip (axes Imgs): if torch.is_tensor (img): # picture tensor ax.imshow (img.numpy ()) else: # PIL picture ax.imshow (img) ax.axes.get_ Xaxis () .set_visible (False) ax.axes.get_yaxis () .set_visible (False) if titles: ax.set_title (titles [I]) return axes
The following are the images of the first few samples in the training data set and their corresponding labels (in text form).
X, y = next (iter (data.DataLoader (mnist_train, batch_size=18)) show_images (X.reshape (18,28,28), 2,9, titles=get_fashion_mnist_labels (y))
Read small batch
To make it easier for us to read training sets and test sets, we use a built-in data iterator instead of creating one from scratch. Recall that in each iteration, the data loader reads a small batch of data, the size of which is batch_size. We iterated through the training data and randomly disrupted all the samples.
Batch_size = 256def get_dataloader_workers (): "4 processes are used to read data." Return 4train_iter = data.DataLoader (mnist_train, batch_size, shuffle=True, num_workers=get_dataloader_workers ()) integrates all components
Now we define the load_data_fashion_mnist function, which is used to get and read the Fashion-MNIST dataset. It returns the data iterator for the training set and the validation set. In addition, it accepts an optional parameter to resize the image to another shape.
Def load_data_fashion_mnist (batch_size, resize=None): "" download the Fashion-MNIST dataset and load it into memory. " Trans = [transforms.ToTensor ()] if resize: trans.insert (0, transforms.Resize (resize)) trans = transforms.Compose (trans) mnist_train = torchvision.datasets.FashionMNIST (root= ".. / data", train=True, transforms=trans, download=True) mnist_test = torchvision.datasets.FashionMNIST (root= ".. / data", train=False, transforms=trans, download=True) return (data.DataLoader (mnist_train, batch_size, shuffle=True) Num_workers=get_dataloader_workers (), data.DataLoader (mnist_test, batch_size, shuffle=False, num_workers=get_dataloader_workers ())
Next, we test the image resizing function of the load_data_fashion_mnist function by specifying the resize parameter.
Train_iter, test_iter = load_data_fashion_mnist (32, resize=64) for X, y in train_iter: print (X.shape, X.dtype, y.shape, y.dtype) breaktorch.Size ([32, 1, 64, 64]) torch.float32 torch.Size ([32]) torch.int64 so far, on "how to use pytorch to achieve image classification data set" learning is over, hope to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.