In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
What this article shares with you is about how to achieve image classification of diseases and insect pests in Pytorch. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
I. pytorch Framework 1.1.Concepts
PyTorch is an open source Python machine learning library based on Torch for applications such as natural language processing.
In January 2017, the Facebook Institute of artificial Intelligence (FAIR) launched PyTorch based on Torch. It is a Python-based renewable computing package that provides two advanced features:
1. Tensor computation with powerful GPU acceleration (such as NumPy).
2. The depth neural network including the automatic derivation system.
1.2. The difference between machine learning and deep learning
There are many differences between the two, which are only briefly described in this blog. Show it in the form of a picture.
The former is the process of machine learning.
The latter is a process of deep learning.
1.3.Screenshot of importing pytorch into python successfully
2. Data set
This experiment uses the plant diseases and insect pests data set from the coco data set. It is divided into training file Traindata and test file TestData.
TrainData has nine categories, each with 100 pictures.
TestData has 9 categories, with 10 pictures in each category.
Open source the dataset in my next blog post.
Here is a screenshot of my dataset:
Code repetition 3.1.Import third party library import torchfrom torch.utils.data import Dataset, DataLoaderimport numpy as npimport matplotlibimport osimport cv2from PIL import Imageimport torchvision.transforms as transformsimport torch.optim as optimfrom torch.autograd import Variableimport torch.nn as nnimport torch.nn.functional as Ffrom Test.CNN import Netimport jsonfrom Test.train_data import Mydataset,pad_image3.2, CNN code # to construct neural network class Net (nn.Module): # define network module def _ _ init__ (self): super (Net) Self). _ _ init__ () # convolution The picture has 3 layers, 6 features and 5-by-5 pixels in length and width. Self.conv1 = nn.Conv2d (3,6,5) # / / (conv1): Conv2d (3,6, kernel_size= (5,5), stride= (1,1)) self.pool = nn.MaxPool2d (2,2) # maximum pooling # / / (pool): MaxPool2d (kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False) self.conv2 = nn.Conv2d (6,16) 5) # convolution # / / (conv2): Conv2d (6,16, kernel_size= (5,5), stride= (1,1)) self.fc1 = nn.Linear (167,777,120) # full connection layer The dimension of the picture is 16, # (fc1): Linear (in_features=94864, out_features=120, bias=True) self.fc2 = nn.Linear (120,84) # full connection layer, input 120features output 84 features self.fc3 = nn.Linear (84,7) # full connection layer Input 84 features and output 7 features def forward (self, x): print ("x.shape1:", x.shape) x = self.pool (F.relu (self.conv1 (x) print ("x.shape2:", x.shape) x = self.pool (F.relu (self.conv2 (x) print ("x.shape3:" X.shape) x = x.view (- 1,16,777,77) print ("x.shape4:", x.shape) x = F.relu (self.fc1 (x)) print ("x.shape5:", x.shape) x = F.relu (self.fc2 (x)) print ("x.shape6:" X.shape) x = self.fc3 (x) print ("x.shape7:", x.shape) return x3.3, test code img_path = "TestData/test_data/1/Apple2 (1) .jpg" # use the relative path image = Image.open (img_path) .convert ('RGB') image_pad = pad_image (image, Input = transform (image_pad) .to (device) .unsqueeze (0) output = F.softmax (net (input), 1) _, predicted = torch.max (output, 1) score = float (output [0] [predicted] * 100) print (class_map [predicted], ", str (score) +"% ") plt.imshow (image_pad) # shows picture 4, training result 4.1, LOSS loss function
4.2 、 ACC
4.3. Accuracy of single picture recognition
The above is how to achieve image classification of diseases and insect pests in Pytorch. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
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.