日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

python 降噪_使用降噪自动编码器重建损坏的数据(Python代码)

發布時間:2023/12/15 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 降噪_使用降噪自动编码器重建损坏的数据(Python代码) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

python 降噪

Autoencoders aren’t too useful in practice, but they can be used to denoise images quite successfully just by training the network on noisy images. We can generate noisy images by adding Gaussian noise to the training images, then clipping the values to be between 0 and 1.

自動編碼器在實踐中并不太有用,但是僅通過在嘈雜的圖像上訓練網絡,它們就可以非常成功地對圖像進行去噪。 我們可以通過將高斯噪聲添加到訓練圖像中,然后將值裁剪為0到1之間的方式來生成嘈雜的圖像。

“Denoising auto-encoder forces the hidden layer to extract more robust features and restrict it from merely learning the identity. Autoencoder reconstructs the input from a corrupted version of it.”

去噪自動編碼器會強制隱藏層提取更強大的功能,并限制其僅學習身份。 自動編碼器會從損壞的版本中重建輸入?!?

A denoising auto-encoder does two things:

去噪自動編碼器有兩件事:

  • Encode the input (preserve the information about the data)

    編碼輸入(保留有關數據的信息)
  • Undo the effect of a corruption process stochastically applied to the input of the auto-encoder.

    撤消隨機應用于自動編碼器輸入的損壞過程的影響。

For the depiction of the denoising capabilities of Autoencoders, we’ll use noisy images as input and the original, clean images as targets.

為了描述自動編碼器的降噪功能,我們將使用嘈雜的圖像作為輸入,并使用原始的干凈圖像作為目標。

Example: Top image is input, and the bottom image is the target.

示例:輸入了頂部圖像,而底部圖像是目標。

問題陳述: (Problem Statement:)

Build the model for the denoising autoencoder. Add deeper and additional layers to the network. Using MNIST dataset, add noise to the data and try to define and train an autoencoder to denoise the images.

為降噪自動編碼器構建模型。 向網絡添加更深的層。 使用MNIST數據集,向數據添加噪聲,并嘗試定義和訓練自動編碼器以對圖像進行降噪。

解: (Solution:)

Import Libraries and Load Dataset: Given below is the standard procedure to import the libraries and load the MNIST dataset.

導入庫和加載數據集:以下是導入庫和加載MNIST數據集的標準過程。

import torch
import numpy as np
from torchvision import datasets
import torchvision.transforms as transforms# convert data to torch.FloatTensor
transform = transforms.ToTensor()
# load the training and test datasets
train_data = datasets.MNIST(root='data', train=True, download=True, transform=transform)
test_data = datasets.MNIST(root='data', train=False, download=True, transform=transform)
# Create training and test dataloaders
num_workers = 0
# how many samples per batch to load
batch_size = 20
# prepare data loaders
train_loader = torch.utils.data.DataLoader(train_data, batch_size=batch_size, num_workers=num_workers)
test_loader = torch.utils.data.DataLoader(test_data, batch_size=batch_size, num_workers=num_workers)

Visualize the Data: You can use standard matplotlib library to view whether you’ve loaded your dataset correctly or not.

可視化數據:可以使用標準的matplotlib庫查看是否正確加載了數據集。

import matplotlib.pyplot as plt
%matplotlib inline

# obtain one batch of training images
dataiter = iter(train_loader)
images, labels = dataiter.next()
images = images.numpy()# get one image from the batch
img = np.squeeze(images[0])fig = plt.figure(figsize = (5,5))
ax = fig.add_subplot(111)
ax.imshow(img, cmap='gray')

The output should be something like this:

輸出應該是這樣的:

Network Architecture: The most crucial part is the network generation. It is because denoising is a hard problem for the network; hence we’ll need to use deeper convolutional layers here. It is recommended to start with a depth of 32 for the convolutional layers in the encoder, and the same depth going backwards through the decoder.

網絡體系結構:最關鍵的部分是網絡生成。 這是因為去噪是網絡的難題。 因此我們需要在這里使用更深的卷積層。 對于編碼器中的卷積層,建議從32的深度開始,并向后穿過解碼器。

import torch.nn as nn
import torch.nn.functional as F# define the NN architecture
class ConvDenoiser(nn.Module):
def __init__(self):
super(ConvDenoiser, self).__init__()
## encoder layers ##
# conv layer (depth from 1 --> 32), 3x3 kernels
self.conv1 = nn.Conv2d(1, 32, 3, padding=1)
# conv layer (depth from 32 --> 16), 3x3 kernels
self.conv2 = nn.Conv2d(32, 16, 3, padding=1)
# conv layer (depth from 16 --> 8), 3x3 kernels
self.conv3 = nn.Conv2d(16, 8, 3, padding=1)
# pooling layer to reduce x-y dims by two; kernel and stride of 2
self.pool = nn.MaxPool2d(2, 2)

## decoder layers ##
# transpose layer, a kernel of 2 and a stride of 2 will increase the spatial dims by 2
self.t_conv1 = nn.ConvTranspose2d(8, 8, 3, stride=2) # kernel_size=3 to get to a 7x7 image output
# two more transpose layers with a kernel of 2
self.t_conv2 = nn.ConvTranspose2d(8, 16, 2, stride=2)
self.t_conv3 = nn.ConvTranspose2d(16, 32, 2, stride=2)
# one, final, normal conv layer to decrease the depth
self.conv_out = nn.Conv2d(32, 1, 3, padding=1)def forward(self, x):
## encode ##
# add hidden layers with relu activation function
# and maxpooling after
x = F.relu(self.conv1(x))
x = self.pool(x)
# add second hidden layer
x = F.relu(self.conv2(x))
x = self.pool(x)
# add third hidden layer
x = F.relu(self.conv3(x))
x = self.pool(x) # compressed representation

## decode ##
# add transpose conv layers, with relu activation function
x = F.relu(self.t_conv1(x))
x = F.relu(self.t_conv2(x))
x = F.relu(self.t_conv3(x))
# transpose again, output should have a sigmoid applied
x = F.sigmoid(self.conv_out(x))

return x# initialize the NN
model = ConvDenoiser()
print(model)

Training: The training of the network takes significantly less time with GPU; hence I would recommend using one. Though here we are only concerned with the training images, which we can get from the train_loader.

培訓:使用GPU進行網絡培訓所需的時間大大減少; 因此,我建議使用一個。 雖然在這里我們只關心訓練圖像,但可以從train_loader獲得。

# specify loss function
criterion = nn.MSELoss()# specify loss function
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
# number of epochs to train the model
n_epochs = 20# for adding noise to images
noise_factor=0.5for epoch in range(1, n_epochs+1):
# monitor training loss
train_loss = 0.0

###################
# train the model #
###################
for data in train_loader:
# _ stands in for labels, here
# no need to flatten images
images, _ = data

## add random noise to the input images
noisy_imgs = images + noise_factor * torch.randn(*images.shape)
# Clip the images to be between 0 and 1
noisy_imgs = np.clip(noisy_imgs, 0., 1.)

# clear the gradients of all optimized variables
optimizer.zero_grad()
## forward pass: compute predicted outputs by passing *noisy* images to the model
outputs = model(noisy_imgs)
# calculate the loss
# the "target" is still the original, not-noisy images
loss = criterion(outputs, images)
# backward pass: compute gradient of the loss with respect to model parameters
loss.backward()
# perform a single optimization step (parameter update)
optimizer.step()
# update running training loss
train_loss += loss.item()*images.size(0)

# print avg training statistics
train_loss = train_loss/len(train_loader)
print('Epoch: {} \tTraining Loss: {:.6f}'.format(
epoch,
train_loss
))

In this case, we are actually adding some noise to these images and we’ll feed these noisy_imgs to our model. The model will produce reconstructed images based on the noisy input. But, we want it to produce normal un-noisy images, and so, when we calculate the loss, we will still compare the reconstructed outputs to the original images!

在這種情況下,我們實際上 會在這些圖像上 添加一些噪點 ,并將這些 noisy_imgs 入模型。 該模型將根據嘈雜的輸入產生重建的圖像。 但是,我們希望它產生正常的無噪圖像,因此,當我們計算損耗時,我們仍然會將重構的輸出與原始圖像進行比較!

Because we’re comparing pixel values in input and output images, it will be best to use a loss that is meant for a regression task. Regression is all about comparing quantities rather than probabilistic values. So, in this case, I’ll use MSELoss.

因為我們正在比較輸入和輸出圖像中的像素值,所以最好使用用于回歸任務的損耗。 回歸全是比較數量而不是概率值。 因此,在這種情況下,我將使用MSELoss 。

Results: Here let’s add noise to the test images and pass them through the autoencoder.

結果:在這里,我們將噪聲添加到測試圖像中,然后將其通過自動編碼器。

# obtain one batch of test images
dataiter = iter(test_loader)
images, labels = dataiter.next()# add noise to the test images
noisy_imgs = images + noise_factor * torch.randn(*images.shape)
noisy_imgs = np.clip(noisy_imgs, 0., 1.)# get sample outputs
output = model(noisy_imgs)
# prep images for display
noisy_imgs = noisy_imgs.numpy()# output is resized into a batch of iages
output = output.view(batch_size, 1, 28, 28)
# use detach when it's an output that requires_grad
output = output.detach().numpy()# plot the first ten input images and then reconstructed images
fig, axes = plt.subplots(nrows=2, ncols=10, sharex=True, sharey=True, figsize=(25,4))# input images on top row, reconstructions on bottom
for noisy_imgs, row in zip([noisy_imgs, output], axes):
for img, ax in zip(noisy_imgs, row):
ax.imshow(np.squeeze(img), cmap='gray')
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)

It does a surprisingly great job of removing the noise, even though it’s sometimes difficult to tell what the original number is.

盡管有時很難說出原始數字是多少,但是它在消除噪聲方面卻做得非常出色。

Code: You can find this code on my Github: Denoising Autoencoder

代碼:您可以在我的Github上找到此代碼: Denoising Autoencoder

Conclusion: In this article, we learnt how to code denoising autoencoder in python properly. We also learnt that denoising is a hard problem for the network, hence using deeper convolutional layers provide exceptionally accurate results.

結論:在本文中,我們學習了如何在python中正確編碼去噪自動編碼器。 我們還了解到,去噪對于網絡來說是一個難題,因此使用更深的卷積層可提供異常準確的結果。

Reference: I learnt this topic from “Udacity’s Secure and Private AI Scholarship Challenge Nanodegree Program.”

參考資料:我從“ Udacity的安全和私人AI獎學金挑戰納米學位計劃”中學到了這個主題。

翻譯自: https://towardsdatascience.com/reconstruct-corrupted-data-using-denoising-autoencoder-python-code-aeaff4b0958e

python 降噪

總結

以上是生活随笔為你收集整理的python 降噪_使用降噪自动编码器重建损坏的数据(Python代码)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。