diff --git a/unsuper/Readme.md b/unsuper/Readme.md new file mode 100644 index 0000000..3b5ef4e --- /dev/null +++ b/unsuper/Readme.md @@ -0,0 +1,5 @@ +1. 3x3的时候会有重复 + 1. 重复的权重,虽然权重看起来都一样,但是有稍微的不同,不是完全一样 + 2. 3x3太小了导致了样本的信噪比太低,大部分的样本切出来都是0 +2. 5x5的时候会有网格状重复 +3. 7x7的时候边框区域问题 \ No newline at end of file diff --git a/unsuper/minist.py b/unsuper/minist.py index 73818a3..77b750c 100644 --- a/unsuper/minist.py +++ b/unsuper/minist.py @@ -30,7 +30,7 @@ test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=batch_size, s class ConvNet(nn.Module): def __init__(self): 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.conv2 = nn.Conv2d(8, 1, 5, 1, 0) self.fc1 = nn.Linear(1 * 4 * 4, 10) @@ -43,7 +43,7 @@ class ConvNet(nn.Module): x = self.fc1(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 = weight.permute(1, 0) mean = torch.mean(weight, dim=0) @@ -52,31 +52,31 @@ class ConvNet(nn.Module): weight = weight / sum weight = weight.permute(1, 0) 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 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.DumpTensorToLog(x, "input_image.log") - x = self.conv1(x) - w = self.conv1.weight - show.DumpTensorToImage(w.view(-1, w.shape[2], w.shape[3]), dir + "/conv1_weight.png", Contrast=[-1.0, 1.0]) + + w = self.normal_conv1_weight() + 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.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") x = self.pool(x) x = self.conv2(x) w = self.conv2.weight - show.DumpTensorToImage( - 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", Contrast=[-1.0, 1.0] - ) + show.DumpTensorToImage(w.view(-1, w.shape[2], w.shape[3]).cpu(), dir + "/conv2_weight.png") + show.DumpTensorToImage(x.view(-1, x.shape[2], x.shape[3]).cpu(), dir + "/conv2_output.png") 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 x = x.view(x.shape[0], -1) x = self.fc1(x) @@ -105,14 +105,27 @@ model = ConvNet().to(device) model.train() # Train the model unsuper -epochs = 20 -model.conv1.weight.requires_grad = True -model.conv2.weight.requires_grad = False -model.fc1.weight.requires_grad = False +epochs = 1 n_total_steps = len(train_loader) for epoch in range(epochs): for i, (images, labels) in enumerate(train_loader): 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 = 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 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: print(f"Epoch [{epoch+1}/{epochs}], Step [{i+1}/{n_total_steps}], Loss: {loss.item():.8f}") -w = model.conv1.weight.grad -show.DumpTensorToImage(w.view(-1, w.shape[2], w.shape[3]).cpu(), "conv1_weight_grad.png") +show.DumpTensorToImage(images.view(-1, images.shape[2], images.shape[3]), "input_image.png", Contrast=[0, 1.0]) +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 -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) # images, labels = next(iter(loader))