System Enviroment:
Windows 10
Python 3.7.6
Pytorch 1.8.1
First Run:
RuntimeError: view size is not compatible with input tensor‘s size and stride
(1)line 91
r = r.view((1,self.filter_dict[self.feature_layer],self.h_proj,self.w_proj))
change to:
r = r.contiguous().view((1,self.filter_dict[self.feature_layer],self.h_proj,self.w_proj))
(2)line 121:
h_0_flat = h_0.view((self.h_projself.w_projself.filter_dict[self.feature_layer],)).unsqueeze(0)
change to:
h_0_flat = h_0.contiguous().view((self.h_projself.w_projself.filter_dict[self.feature_layer],)).unsqueeze(0)
import torch
import torch.nn as nn
import torch, torch.nn as nn, torch.nn.functional as F
import numpy
from torchvision import datasets, models, transforms
from utils import pool, newlayer, Flatten, Identity
import numpy as np
import os
def vgg_gamma(i):
'''Setting gamma according to vgg layer index i'''
if i <=10: gamma=0.5
if 11 <= i <= 17: gamma=0.25
if 18 <= i <= 24: gamma=0.1
if i > 24: gamma=0.0
return gamma
System Enviroment: Windows 10 Python 3.7.6 Pytorch 1.8.1
First Run: RuntimeError: view size is not compatible with input tensor‘s size and stride
(1)line 91 r = r.view((1,self.filter_dict[self.feature_layer],self.h_proj,self.w_proj)) change to: r = r.contiguous().view((1,self.filter_dict[self.feature_layer],self.h_proj,self.w_proj))
(2)line 121: h_0_flat = h_0.view((self.h_projself.w_projself.filter_dict[self.feature_layer],)).unsqueeze(0) change to: h_0_flat = h_0.contiguous().view((self.h_projself.w_projself.filter_dict[self.feature_layer],)).unsqueeze(0)
reference: https://blog.csdn.net/weixin_45377629/article/details/125583650
final bilrp.py version:
import torch import torch.nn as nn import torch, torch.nn as nn, torch.nn.functional as F import numpy from torchvision import datasets, models, transforms from utils import pool, newlayer, Flatten, Identity import numpy as np import os
def vgg_gamma(i): '''Setting gamma according to vgg layer index i''' if i <=10: gamma=0.5 if 11 <= i <= 17: gamma=0.25 if 18 <= i <= 24: gamma=0.1 if i > 24: gamma=0.0 return gamma
class VggLayers(nn.Module): def init(self, feature_layer, h,w, embedding_size = 100, proj_case='random', seed=1): super(VggLayers, self).init() self.feature_layer = feature_layer self.h, self.w = h,w self.proj_case = proj_case self.embedding_size = embedding_size self.ls = [5,10,17,24,31] self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu") self.filter_dict = {'5':64, '10':128, '17':256, '24':512, '31':512} self.size_dict = {'5':2,'10':4, '17':8, '24':16, '31':32}