here is my code:
from scipy import stats
from scipy import signal
import torch
from skimage import io
import numpy as np
import matplotlib.pyplot as plt
import torch.nn.functional as F
def gkern(kernellen=21,nsig=3):
x = np.linspace(-nsig,nsig,kernellen+1)
kern1d = np.diff(stats.norm.cdf(x))
kern2d = np.outer(kern1d,kern1d)
return kern2d/kern2d.sum()
path ='C:/Users/zcw/Desktop/lena.jpg'
img = io.imread(path)
kernel = gkern()
corrupt_img = signal.convolve2d(img,kernel,boundary='symm',mode='same')
noise = np.random.randn(img.shape[0],img.shape[1])*img.max()*0.02
corrupt_img = np.clip(corrupt_img + noise,0,255)
plt.imshow(corrupt_img)
## 如下开始使用pytorch来进行梯度更新
dtype = torch.float
device = torch.device("cpu")
corrupt_img = torch.tensor(corrupt_img,dtype=dtype,device=device)
corrected_im = torch.randn(1,1,corrupt_img.shape[0],corrupt_img.shape[1],device=device,dtype=dtype,requires_grad=True)
gt = torch.tensor(img)
#corrected_im = corrected_im[None,None,:,:]
corrupt_img = corrupt_img[None,None,:,:]
kernel = torch.tensor(kernel,dtype=dtype,device=device)
kernel = kernel[None,None,:,:]
lr = 1e-5
epoch = 1000
for i in range(epoch):
conv_result = F.conv2d(corrected_im,kernel,padding=10)
loss = (conv_result - corrupt_img).pow(2).sum() + torch.abs(corrected_im).sum()
if i%10==0:
print(i,loss.item())
loss.backward()
with torch.no_grad():
corrected_im -= lr*corrected_im.grad
corrected_im.grad.zero_()
I am trying to solve a l1-regulirized deconvolution problem using pytorch, but except for implementing my own adam(adagrad,momentum) optimization, how should I change my code so that I can utilize nn.optimizer in my code?