To use the wavelet coefficients as input for a ResNet-18 model in Python for denoising, you can follow these steps:
import torch
import torch.nn as nn
import torch.optim as optim
class ResNet18(nn.Module):
def __init__(self):
super().__init__()
self.conv1 = nn.Conv1d(in_channels=1, out_channels=64, kernel_size=3, padding=1, bias=False)
self.bn1 = nn.BatchNorm1d(64)
self.relu = nn.ReLU(inplace=True)
self.layer1 = nn.Sequential(
nn.Conv1d(64, 64, kernel_size=3, stride=1, padding=1, bias=False),
nn.BatchNorm1d(64),
nn.ReLU(inplace=True),
nn.Conv1d(64, 64, kernel_size=3, stride=1, padding=1, bias=False),
nn.BatchNorm1d(64)
)
self.layer2 = nn.Sequential(
nn.Conv1d(64, 128, kernel_size=3, stride=2, padding=1, bias=False),
nn.BatchNorm1d(128),
nn.ReLU(inplace=True),
nn.Conv1d(128, 128, kernel_size=3, stride=1, padding=1, bias=False),
nn.BatchNorm1d(128),
)
self.layer3 = nn.Sequential(
nn.Conv1d(128, 256, kernel_size=3, stride=2, padding=1, bias=False),
nn.BatchNorm1d(256),
nn.ReLU(inplace=True),
nn.Conv1d(256, 256, kernel_size=3, stride=1, padding=1, bias=False),
nn.BatchNorm1d(256),
)
self.layer4 = nn.Sequential(
nn.Conv1d(256, 512, kernel_size=3, stride=2, padding=1, bias=False),
nn.BatchNorm1d(512),
nn.ReLU(inplace=True),
nn.Conv1d(512, 512, kernel_size=3, stride=1, padding=1, bias=False),
nn.BatchNorm1d(512),
)
self.avgpool = nn.AdaptiveAvgPool1d(1)
self.fc = nn.Linear(512, 1)
def forward(self, x):
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
x = self.layer1(x) + x
x = self.layer2(x) + x
x = self.layer3(x) + x
x = self.layer4(x) + x
x = self.avgpool(x)
x = x.view(x.size(0), -1)
x = self.fc(x)
return x