Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to realize the salt and pepper noise effect of ​ by PyTorch

Shulou Source: shulou.com Published: 2022-06-01 12:47:12 09月26日 Update

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!

Tags: Noise salt and pepper effects methods learning content pictures wallets different people RMB code that is ideas situations mobile phones data files folders articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Huawei MariaDB vpn Shulou Information