Update unsuper.

This commit is contained in:
Colin 2024-10-22 13:54:26 +08:00
parent f3690fd47f
commit 385c438c1c
2 changed files with 41 additions and 22 deletions

5
unsuper/Readme.md Normal file
View File

@ -0,0 +1,5 @@
1. 3x3的时候会有重复
1. 重复的权重,虽然权重看起来都一样,但是有稍微的不同,不是完全一样
2. 3x3太小了导致了样本的信噪比太低大部分的样本切出来都是0
2. 5x5的时候会有网格状重复
3. 7x7的时候边框区域问题

View File

@ -30,7 +30,7 @@ test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=batch_size, s
class ConvNet(nn.Module): class ConvNet(nn.Module):
def __init__(self): def __init__(self):
super(ConvNet, self).__init__() super(ConvNet, self).__init__()
self.conv1 = nn.Conv2d(1, 8, 5, 1, 0) self.conv1 = nn.Conv2d(1, 8, 3, 1, 0)
self.pool = nn.MaxPool2d(2, 2) self.pool = nn.MaxPool2d(2, 2)
self.conv2 = nn.Conv2d(8, 1, 5, 1, 0) self.conv2 = nn.Conv2d(8, 1, 5, 1, 0)
self.fc1 = nn.Linear(1 * 4 * 4, 10) self.fc1 = nn.Linear(1 * 4 * 4, 10)
@ -43,7 +43,7 @@ class ConvNet(nn.Module):
x = self.fc1(x) x = self.fc1(x)
return x return x
def forward_unsuper(self, x): def normal_conv1_weight(self):
weight = self.conv1.weight.reshape(self.conv1.weight.shape[0], -1) weight = self.conv1.weight.reshape(self.conv1.weight.shape[0], -1)
weight = weight.permute(1, 0) weight = weight.permute(1, 0)
mean = torch.mean(weight, dim=0) mean = torch.mean(weight, dim=0)
@ -52,31 +52,31 @@ class ConvNet(nn.Module):
weight = weight / sum weight = weight / sum
weight = weight.permute(1, 0) weight = weight.permute(1, 0)
weight = weight.reshape(self.conv1.weight.shape) weight = weight.reshape(self.conv1.weight.shape)
x = torch.conv2d(x, weight) return weight
def forward_unsuper(self, x):
x = torch.conv2d(x, self.normal_conv1_weight(), stride=1)
return x return x
def printFector(self, x, label, dir=""): def printFector(self, x, label, dir=""):
show.DumpTensorToImage(x.view(-1, x.shape[2], x.shape[3]), dir + "/input_image.png", Contrast=[0, 1.0]) show.DumpTensorToImage(x.view(-1, x.shape[2], x.shape[3]), dir + "/input_image.png", Contrast=[0, 1.0])
# show.DumpTensorToLog(x, "input_image.log") # show.DumpTensorToLog(x, "input_image.log")
x = self.conv1(x)
w = self.conv1.weight w = self.normal_conv1_weight()
show.DumpTensorToImage(w.view(-1, w.shape[2], w.shape[3]), dir + "/conv1_weight.png", Contrast=[-1.0, 1.0]) x = torch.conv2d(x, w)
show.DumpTensorToImage(w.view(-1, w.shape[2], w.shape[3]), dir + "/conv1_weight.png")
# show.DumpTensorToLog(w, "conv1_weight.log") # show.DumpTensorToLog(w, "conv1_weight.log")
show.DumpTensorToImage(x.view(-1, x.shape[2], x.shape[3]), dir + "/conv1_output.png", Contrast=[-1.0, 1.0]) show.DumpTensorToImage(x.view(-1, x.shape[2], x.shape[3]), dir + "/conv1_output.png")
# show.DumpTensorToLog(x, "conv1_output.png") # show.DumpTensorToLog(x, "conv1_output.png")
x = self.pool(x) x = self.pool(x)
x = self.conv2(x) x = self.conv2(x)
w = self.conv2.weight w = self.conv2.weight
show.DumpTensorToImage( show.DumpTensorToImage(w.view(-1, w.shape[2], w.shape[3]).cpu(), dir + "/conv2_weight.png")
w.view(-1, w.shape[2], w.shape[3]).cpu(), dir + "/conv2_weight.png", Contrast=[-1.0, 1.0] show.DumpTensorToImage(x.view(-1, x.shape[2], x.shape[3]).cpu(), dir + "/conv2_output.png")
)
show.DumpTensorToImage(
x.view(-1, x.shape[2], x.shape[3]).cpu(), dir + "/conv2_output.png", Contrast=[-1.0, 1.0]
)
x = self.pool(x) x = self.pool(x)
show.DumpTensorToImage(x.view(-1, x.shape[2], x.shape[3]).cpu(), dir + "/pool_output.png", Contrast=[-1.0, 1.0]) show.DumpTensorToImage(x.view(-1, x.shape[2], x.shape[3]).cpu(), dir + "/pool_output.png")
pool_shape = x.shape pool_shape = x.shape
x = x.view(x.shape[0], -1) x = x.view(x.shape[0], -1)
x = self.fc1(x) x = self.fc1(x)
@ -105,14 +105,27 @@ model = ConvNet().to(device)
model.train() model.train()
# Train the model unsuper # Train the model unsuper
epochs = 20 epochs = 1
model.conv1.weight.requires_grad = True
model.conv2.weight.requires_grad = False
model.fc1.weight.requires_grad = False
n_total_steps = len(train_loader) n_total_steps = len(train_loader)
for epoch in range(epochs): for epoch in range(epochs):
for i, (images, labels) in enumerate(train_loader): for i, (images, labels) in enumerate(train_loader):
images = images.to(device) images = images.to(device)
# # images = images[:,:,12:15,12:15]
# kernel_size = 3
# mean_filter = torch.ones((1, 1, kernel_size, kernel_size), device=device) / (kernel_size * kernel_size)
# images = F.conv2d(images, mean_filter, padding=1)
# images = F.conv2d(images, mean_filter, padding=1)
# images = F.conv2d(images, mean_filter, padding=1)
# # images = F.conv2d(images, mean_filter, padding=1)
# # images = F.conv2d(images, mean_filter, padding=1)
# images = torch.rand(3, 3).to(device=device)
# # images[1, 1] = images[1, 1] * 2
# # images[0, 0] = images[1, 1] * 2
# # images[2, 2] = images[1, 1] * 2
# images = images.view(1, 1, 3, 3)
outputs = model.forward_unsuper(images) outputs = model.forward_unsuper(images)
outputs = outputs.permute(0, 2, 3, 1) # 64 8 24 24 -> 64 24 24 8 outputs = outputs.permute(0, 2, 3, 1) # 64 8 24 24 -> 64 24 24 8
@ -134,15 +147,16 @@ for epoch in range(epochs):
model.conv1.weight.grad = None model.conv1.weight.grad = None
loss.backward() loss.backward()
model.conv1.weight.data = model.conv1.weight.data - model.conv1.weight.grad * 10 model.conv1.weight.data = model.conv1.weight.data - model.conv1.weight.grad * 100
if (i + 1) % 100 == 0: if (i + 1) % 100 == 0:
print(f"Epoch [{epoch+1}/{epochs}], Step [{i+1}/{n_total_steps}], Loss: {loss.item():.8f}") print(f"Epoch [{epoch+1}/{epochs}], Step [{i+1}/{n_total_steps}], Loss: {loss.item():.8f}")
w = model.conv1.weight.grad show.DumpTensorToImage(images.view(-1, images.shape[2], images.shape[3]), "input_image.png", Contrast=[0, 1.0])
show.DumpTensorToImage(w.view(-1, w.shape[2], w.shape[3]).cpu(), "conv1_weight_grad.png") g = model.conv1.weight.grad
show.DumpTensorToImage(g.view(-1, g.shape[2], g.shape[3]).cpu(), "conv1_weight_grad.png")
w = model.conv1.weight.data w = model.conv1.weight.data
show.DumpTensorToImage(w.view(-1, w.shape[2], w.shape[3]), "conv1_weight_update.png", Contrast=[-1.0, 1.0]) show.DumpTensorToImage(w.view(-1, w.shape[2], w.shape[3]), "conv1_weight_update.png")
# loader = torch.utils.data.DataLoader(test_dataset, batch_size=1, shuffle=False) # loader = torch.utils.data.DataLoader(test_dataset, batch_size=1, shuffle=False)
# images, labels = next(iter(loader)) # images, labels = next(iter(loader))