Unsuper train with max confidense of conv output
This commit is contained in:
		
							parent
							
								
									81f203ce59
								
							
						
					
					
						commit
						45d5701835
					
				|  | @ -36,22 +36,23 @@ class ConvNet(nn.Module): | ||||||
|         self.fc1 = nn.Linear(1 * 4 * 4, 10) |         self.fc1 = nn.Linear(1 * 4 * 4, 10) | ||||||
| 
 | 
 | ||||||
|     def forward(self, x): |     def forward(self, x): | ||||||
|         x = self.pool(self.conv1(x)) |         x = self.forward_unsuper(x) | ||||||
|  |         x = self.pool(x) | ||||||
|         x = self.pool(self.conv2(x)) |         x = self.pool(self.conv2(x)) | ||||||
|         x = x.view(x.shape[0], -1) |         x = x.view(x.shape[0], -1) | ||||||
|         x = self.fc1(x) |         x = self.fc1(x) | ||||||
|         return x |         return x | ||||||
| 
 | 
 | ||||||
|     def forward_unsuper(self, x): |     def forward_unsuper(self, x): | ||||||
|         x = self.conv1(x) |         weight = self.conv1.weight.reshape(self.conv1.weight.shape[0], -1) | ||||||
|         # x = self.pool(self.conv1(x)) |         weight = weight.permute(1, 0) | ||||||
|         return x |         mean = torch.mean(weight, dim=0) | ||||||
| 
 |         weight = weight - mean | ||||||
|     def forward_finetune(self, x): |         sum = torch.sum(torch.abs(weight), dim=0) | ||||||
|         x = self.pool(self.conv1(x)) |         weight = weight / sum | ||||||
|         x = self.pool(self.conv2(x)) |         weight = weight.permute(1, 0) | ||||||
|         x = x.view(x.shape[0], -1) |         weight = weight.reshape(self.conv1.weight.shape) | ||||||
|         x = self.fc1(x) |         x = torch.conv2d(x, weight) | ||||||
|         return x |         return x | ||||||
| 
 | 
 | ||||||
|     def printFector(self, x, label, dir=""): |     def printFector(self, x, label, dir=""): | ||||||
|  | @ -104,7 +105,7 @@ model = ConvNet().to(device) | ||||||
| model.train() | model.train() | ||||||
| 
 | 
 | ||||||
| # Train the model unsuper | # Train the model unsuper | ||||||
| epochs = 2 | epochs = 10 | ||||||
| model.conv1.weight.requires_grad = True | model.conv1.weight.requires_grad = True | ||||||
| model.conv2.weight.requires_grad = False | model.conv2.weight.requires_grad = False | ||||||
| model.fc1.weight.requires_grad = False | model.fc1.weight.requires_grad = False | ||||||
|  | @ -114,24 +115,40 @@ for epoch in range(epochs): | ||||||
|         images = images.to(device) |         images = images.to(device) | ||||||
|         outputs = model.forward_unsuper(images) |         outputs = model.forward_unsuper(images) | ||||||
| 
 | 
 | ||||||
|         outputs = outputs.permute(1, 0, 2, 3)  # 64 8 24 24 -> 8 64 24 24 |         # outputs = outputs.permute(0, 2, 3, 1)  # 64 8 24 24 -> 64 24 24 8 | ||||||
|         sample = outputs.reshape(outputs.shape[0], -1)  # -> 8 36864 |         # sample = outputs.reshape(-1, outputs.shape[3])  # -> 36864 8 | ||||||
|  |         # abs = torch.abs(sample) | ||||||
|  |         # max, max_index = torch.max(abs, dim=1) | ||||||
|  |         # min, min_index = torch.min(abs, dim=1) | ||||||
|  |         # label = sample * 0.9 | ||||||
|  |         # all = range(0, label.shape[0]) | ||||||
|  |         # label[all, max_index] = label[all, max_index]*1.1 | ||||||
|  |         # loss = F.l1_loss(sample, label) | ||||||
|  |         # model.conv1.weight.grad = None | ||||||
|  |         # loss.backward() | ||||||
| 
 | 
 | ||||||
|         sample_mean = torch.mean(sample, dim=1, keepdim=True) |         outputs = outputs.permute(0, 2, 3, 1)  # 64 8 24 24 -> 64 24 24 8 | ||||||
|         diff_mean = torch.mean(torch.abs(sample - sample_mean), dim=1, keepdim=True) |         sample = outputs.reshape(outputs.shape[0], -1, outputs.shape[3])  # -> 64 24x24 8 | ||||||
|         diff_ratio = (sample - sample_mean) / diff_mean |         abs = torch.abs(sample) | ||||||
|         diff_ratio_mean = torch.mean(diff_ratio * diff_ratio, dim=1) |         sum = torch.sum(abs, dim=1, keepdim=False) | ||||||
|         label = diff_ratio_mean * 0.5 |         max, max_index = torch.max(sum, dim=1) | ||||||
|         loss = F.l1_loss(diff_ratio_mean, label) |         label = sample * 0.9 | ||||||
|  |         all = range(0, label.shape[0]) | ||||||
|  |         all_wh = range(0, 24 * 24) | ||||||
|  |         label[all, :, max_index] = label[all, :, max_index] * 1.1 | ||||||
|  |         loss = F.l1_loss(sample, label) | ||||||
|         model.conv1.weight.grad = None |         model.conv1.weight.grad = None | ||||||
|         loss.backward() |         loss.backward() | ||||||
| 
 | 
 | ||||||
|         grad = model.conv1.weight.data |         # show.DumpTensorToImage(images.view(-1, images.shape[2], images.shape[3]), "input_image.png", Contrast=[0, 1.0]) | ||||||
|         grad = grad.view(8, -1) |         # w = model.conv1.weight.data | ||||||
|         grad_mean = torch.mean(grad, dim=1) |         # show.DumpTensorToImage(w.view(-1, w.shape[2], w.shape[3]), "conv1_weight.png", Contrast=[-1.0, 1.0]) | ||||||
|         max, index = torch.max(grad_mean, dim=0) |         # w = model.conv1.weight.grad | ||||||
|  |         # show.DumpTensorToImage(w.view(-1, w.shape[2], w.shape[3]).cpu(), "conv1_weight_grad.png") | ||||||
|  |         model.conv1.weight.data = model.conv1.weight.data - model.conv1.weight.grad * 1000 | ||||||
|  |         # 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]) | ||||||
| 
 | 
 | ||||||
|         model.conv1.weight.data[index] = model.conv1.weight.data[index] - model.conv1.weight.grad[index] * 0.2 |  | ||||||
|         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}") | ||||||
| 
 | 
 | ||||||
|  | @ -146,7 +163,7 @@ for epoch in range(num_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) | ||||||
|         labels = labels.to(device) |         labels = labels.to(device) | ||||||
|         outputs = model.forward_finetune(images) |         outputs = model(images) | ||||||
|         loss = criterion(outputs, labels) |         loss = criterion(outputs, labels) | ||||||
|         optimizer.zero_grad() |         optimizer.zero_grad() | ||||||
|         loss.backward() |         loss.backward() | ||||||
|  | @ -154,7 +171,7 @@ for epoch in range(num_epochs): | ||||||
|         if (i + 1) % 100 == 0: |         if (i + 1) % 100 == 0: | ||||||
|             print(f"Epoch [{epoch+1}/{num_epochs}], Step [{i+1}/{n_total_steps}], Loss: {loss.item():.4f}") |             print(f"Epoch [{epoch+1}/{num_epochs}], Step [{i+1}/{n_total_steps}], Loss: {loss.item():.4f}") | ||||||
| 
 | 
 | ||||||
| print("Finished Training") | # print("Finished Training") | ||||||
| 
 | 
 | ||||||
| test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=1, shuffle=False) | test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=1, shuffle=False) | ||||||
| test_loader = iter(test_loader) | test_loader = iter(test_loader) | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue