Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of pytorch Image recognition in python

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail the example analysis of pytorch image recognition in python. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

1. Data set crawling

Nowadays, there is a growing demand for the number of datasets in deep learning, and there are many ready-made datasets for you to find and download, but if you just want to do an example of deep learning to be proficient or can not find a good dataset, then you can also try to make your own dataset-crawling pictures from the Internet, the following is an example of crawling data through Baidu pictures.

Import osimport timeimport requestsimport redef imgdata_set (save_path,word,epoch): Qroom0 # stop crawling picture condition aqum0 # picture name while (True): time.sleep (1) url= "https://image.baidu.com/search/flip?tn=baiduimage&ie=utf-8&word={}&pn={}&ct=&ic=0&lm=-1&width=0&height=0".format(word, Q) # the name word= needs to search for: headers= {'User-Agent':' Mozilla/5.0 (Windows NT 10.0) Win64 X64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36 Edg/88.0.705.56'} response=requests.get (url,headers=headers) # print (response.request.headers) html=response.text # print (html) urls=re.findall ('"objURL": "(. *?)" Html) # print (urls) for url in urls: print (a) # the name of the picture response = requests.get (url, headers=headers) image=response.content with open (os.path.join (save_path, "{} .jpg" .format (a)) 'wb') as f: f.write (image) a=a+1 q=q+20 if (Q _ main__ 20) > = int (epoch): breakif _ _ name__== "_ _ main__": save_path = input (' the path you want to save:') word = input ('what picture do you want to download? Please enter:') epoch = input ('how many rounds of pictures do you want to download? Please enter (about 60 pictures in a round):') # need to iterate several times for imgdata_set (save_path, word, epoch)

Through the above code, you can choose the path, type and number of pictures you want to save. As I do below to crawl the pictures of several common potted plants, you only need to execute the code six times and change the name of the corresponding potted plant. The following is an example of the input of climbing potted aloe. After the input is completed, press Enter to execute, and the picture will be automatically crawled and saved to the specified folder.

The picture shown in the picture is the crawled picture.

You can see that there are some pictures that cannot be opened in the picture, and because the pictures on the web are crawled directly, some of the same pictures may appear, all of which need to be deleted, which requires us to proceed to the second step.

II. Data processing

As the image crawled directly above has some defects, it needs to be further processed and reprocessed.

By repeating the pictures to reprocess, save the datasets you need in their respective folders according to the category. Similarly, because there may be pictures in the dataset that cannot be opened, this requires further processing of the dataset.

First of all, save the reprocessed folder above in the same folder, as shown in the following figure.

Remember this folder path. Here is'C:\ Users\ Lenovo\ Desktop\ data',. Enter this path into the following code.

Import osfrom PIL import Imageroot_path=r "C:\ Users\ Lenovo\ Desktop\ data" # absolute path of pending folder (can be copied by 'Ctrl+Shift+c') root_names=os.listdir (root_path) for root_name in root_names: path=os.path.join (root_path,root_name) print ("deleting folder:" Path) names=os.listdir (path) names_path= [] for name in names: # print (name) img=Image.open (os.path.join (path,name)) name_path=os.path.join (path Name) if img==None: # filter images that cannot be opened names_path.append (name_path) print ('saved error image path: {}' .format (name) successfully) else: wrecoverimg.size if w

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report