Update minist unsuper.
This commit is contained in:
		
							parent
							
								
									385c438c1c
								
							
						
					
					
						commit
						a0da2565fe
					
				| 
						 | 
					@ -2,4 +2,8 @@
 | 
				
			||||||
  1. 重复的权重,虽然权重看起来都一样,但是有稍微的不同,不是完全一样
 | 
					  1. 重复的权重,虽然权重看起来都一样,但是有稍微的不同,不是完全一样
 | 
				
			||||||
  2. 3x3太小了导致了样本的信噪比太低,大部分的样本切出来都是0
 | 
					  2. 3x3太小了导致了样本的信噪比太低,大部分的样本切出来都是0
 | 
				
			||||||
2. 5x5的时候会有网格状重复
 | 
					2. 5x5的时候会有网格状重复
 | 
				
			||||||
3. 7x7的时候边框区域问题
 | 
					3. 7x7的时候边框区域问题
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					1. 输入的信噪比
 | 
				
			||||||
 | 
					2. loss函数的设计
 | 
				
			||||||
 | 
					3. grad信息的应用
 | 
				
			||||||
| 
						 | 
					@ -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, 3, 1, 0)
 | 
					        self.conv1 = nn.Conv2d(1, 8, 5, 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)
 | 
				
			||||||
| 
						 | 
					@ -105,27 +105,11 @@ model = ConvNet().to(device)
 | 
				
			||||||
model.train()
 | 
					model.train()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Train the model unsuper
 | 
					# Train the model unsuper
 | 
				
			||||||
epochs = 1
 | 
					epochs = 2
 | 
				
			||||||
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
 | 
				
			||||||
| 
						 | 
					@ -137,9 +121,10 @@ for epoch in range(epochs):
 | 
				
			||||||
        max = torch.expand_copy(max.reshape(-1, 1), sample.shape)
 | 
					        max = torch.expand_copy(max.reshape(-1, 1), sample.shape)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        all = range(0, sample.shape[0])
 | 
					        all = range(0, sample.shape[0])
 | 
				
			||||||
        ratio_max = abs / mean
 | 
					        # ratio_max = abs / mean
 | 
				
			||||||
        ratio_nor = (max - abs) / max
 | 
					        # ratio_nor = (max - abs) / max
 | 
				
			||||||
        ratio_nor[all, max_index] = ratio_max[all, max_index].clone()
 | 
					        ratio_nor = torch.pow(abs / mean, 4)
 | 
				
			||||||
 | 
					        # ratio_nor[all, max_index] = ratio_max[all, max_index].clone()
 | 
				
			||||||
        ratio_nor = torch.where(torch.isnan(ratio_nor), 1.0, ratio_nor)
 | 
					        ratio_nor = torch.where(torch.isnan(ratio_nor), 1.0, ratio_nor)
 | 
				
			||||||
        label = sample * ratio_nor
 | 
					        label = sample * ratio_nor
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -147,7 +132,7 @@ 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 * 100
 | 
					        model.conv1.weight.data = model.conv1.weight.data - model.conv1.weight.grad * 10000
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        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}")
 | 
				
			||||||
| 
						 | 
					@ -158,6 +143,8 @@ show.DumpTensorToImage(g.view(-1, g.shape[2], g.shape[3]).cpu(), "conv1_weight_g
 | 
				
			||||||
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")
 | 
					show.DumpTensorToImage(w.view(-1, w.shape[2], w.shape[3]), "conv1_weight_update.png")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# model.conv1.weight.data = torch.rand(model.conv1.weight.data.shape, device=device)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# 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))
 | 
				
			||||||
# images = images.to(device)
 | 
					# images = images.to(device)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue