In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "PyTorch how to achieve salt and pepper noise effect", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "PyTorch how to achieve salt and pepper noise effect" bar!
1. One by one, the transforms method is introduced to realize the transformation of the picture, and the picture is saved through plt.savefig.
Slightly
two。 Customize a transforms method to increase salt and pepper noise to make it work correctly
See the following code
3. Take a picture of the front of your wallet with your mobile phone and put it in the hello pytorch/lesson/lesson-09/test_data/100 folder. By modifying different data enhancement methods, the model can correctly distinguish the RMB100 in your wallet after 10 epoch.
Salt and pepper noise
Peper noise
Import torch import numpy as np from PIL import Imagefrom torchvision import datasets, models, transformsnp.random.seed (1234) image_path = 'a.jpg'class AddPepperNoise (object): def _ init__ (self, snr, p): self.snr = snr self.p = p def _ call__ (self, img): P1 = np.random.uniform (low = High = 1.0) if p1 > self.p: return img else: out_img = np.asarray (img). Copy () h, w C = out_img.shape for i in range (h): for j in range (w): for k in range (c): p2 = np.random.uniform (low = 0.0) High = 1.0) if p2 < self.snr: out_ IMG [I] [j] [k] = out_ IMG [I] [j] [k] elif p2 < self.snr + (1-self.snr) / 2: out_ IMG [I] [j] [k] = 0 else: out_ IMG [I] [j] [k] = 255 # print (out_ IMG [I] [j] [k]) print (out_img) tmp = Image.fromarray (out_img.astype ('uint8')). Convert (' RGB') print ('tmp:') Tmp, np.asarray (tmp) return tmpdef main (): img = Image.open (image_path) now_transforms = transforms.Compose ([transforms.Resize ((224,224)), AddPepperNoise (snr = 0.9, p = 1.0), # transforms.ToTensor (),] img_transformed = now_transforms (img) print (img_transformed) Np.asarray (img_transformed)) img_transformed.save ('axipepperNoise.jpg') if _ _ name__ = =' _ _ main__': main () Thank you for your reading The above is the content of "how to achieve salt and pepper noise effect in PyTorch". After the study of this article, I believe you have a deeper understanding of how to achieve salt and pepper noise effect in PyTorch, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.