我正试图在cifar10上运行deep Smote,在tensorflow中编码时,我对pytorch没有太多经验。当我在MNIST和FMNIST上运行它时,它工作得很好,保持channles=1然而,当我在cifar10上试用的那一刻,我的表现并不好。文中给出的代码表明它也适用于Cifar10,感谢所有的帮助这是论文源代码的链接https://github.com/dd1github/DeepSMOTE源代码在Tensorflow中,有人能在这里帮我吗
RuntimeError Traceback (most recent call last)
C:UsersRESEAR~1AppDataLocalTemp/ipykernel_24844/1514724550.py in <module>
93
94 # run images
---> 95 z_hat = encoder(images)
96
97 x_hat = decoder(z_hat) #decoder outputs tanh
~.condaenvspytorchlibsite-packagestorchnnmodulesmodule.py in _call_impl(self, *input, **kwargs)
1100 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
1101 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1102 return forward_call(*input, **kwargs)
1103 # Do not call functions when jit is used
1104 full_backward_hooks, non_full_backward_hooks = [], []
~.condaenvspytorchlibsite-packagestorchnnfunctional.py in linear(input, weight, bias)
1846 if has_torch_function_variadic(input, weight, bias):
1847 return handle_torch_function(linear, (input, weight, bias), input, weight, bias=bias)
-> 1848 return torch._C._nn.linear(input, weight, bias)
1849
1850
RuntimeError: mat1 and mat2 shapes cannot be multiplied (51200x1 and 512x300)
这是代码:
## create encoder model and decoder model
class Encoder(nn.Module):
def __init__(self, args):
super(Encoder, self).__init__()
self.n_channel = args['n_channel']
self.dim_h = args['dim_h']
self.n_z = args['n_z']
# convolutional filters, work excellent with image data
self.conv = nn.Sequential(
nn.Conv2d(self.n_channel, self.dim_h, 4, 2, 1, bias=False),
#nn.ReLU(True),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(self.dim_h, self.dim_h * 2, 4, 2, 1, bias=False),
nn.BatchNorm2d(self.dim_h * 2),
#nn.ReLU(True),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(self.dim_h * 2, self.dim_h * 4, 4, 2, 1, bias=False),
nn.BatchNorm2d(self.dim_h * 4),
#nn.ReLU(True),
nn.LeakyReLU(0.2, inplace=True),
# nn.Conv2d(self.dim_h * 4, self.dim_h * 8, 4, 2, 1, bias=False),
#3d and 32 by 32
nn.Conv2d(self.dim_h * 4, self.dim_h * 8, 4, 1, 0, bias=False),
nn.BatchNorm2d(self.dim_h * 8), # 40 X 8 = 320
#nn.ReLU(True),
nn.LeakyReLU(0.2, inplace=True) )#,
#nn.Conv2d(self.dim_h * 8, 1, 2, 1, 0, bias=False))
#nn.Conv2d(self.dim_h * 8, 1, 4, 1, 0, bias=False))
# final layer is fully connected
print("linearer >>>>>>>> ",self.dim_h * (2 ** 3), self.n_z)
self.fc = nn.Linear(self.dim_h * (2 ** 3), self.n_z)
print("leeeeeeeeeee ")
def forward(self, x):
#print('enc')
#print('input ',x.size()) #torch.Size([100, 3,32,32])
x = self.conv(x)
# x = x.squeeze()
# print('aft squeeze ',x.size()) #torch.Size([128, 320])
#aft squeeze torch.Size([100, 320])
x = self.fc(x)
#print('out ',x.size()) #torch.Size([128, 20])
#out torch.Size([100, 300])
return x
class Decoder(nn.Module):
def __init__(self, args):
super(Decoder, self).__init__()
self.n_channel = args['n_channel']
self.dim_h = args['dim_h']
self.n_z = args['n_z']
# first layer is fully connected
self.fc = nn.Sequential(
nn.Linear(self.n_z, self.dim_h * 8 * 7 * 7),
nn.ReLU())
# deconvolutional filters, essentially inverse of convolutional filters
self.deconv = nn.Sequential(
nn.ConvTranspose2d(self.dim_h * 8, self.dim_h * 4, 4),
nn.BatchNorm2d(self.dim_h * 4),
nn.ReLU(True),
nn.ConvTranspose2d(self.dim_h * 4, self.dim_h * 2, 4),
nn.BatchNorm2d(self.dim_h * 2),
nn.ReLU(True),
nn.ConvTranspose2d(self.dim_h * 2, 1, 4, stride=2),
#nn.Sigmoid())
nn.Tanh())
def forward(self, x):
#print('dec')
#print('input ',x.size())
x = self.fc(x)
x = x.view(-1, self.dim_h * 8, 7, 7)
x = self.deconv(x)
return x
#NOTE: Download the training ('.../0_trn_img.txt') and label files
# ('.../0_trn_lab.txt'). Place the files in directories (e.g., ../MNIST/trn_img/
# and /MNIST/trn_lab/). Originally, when the code was written, it was for 5 fold
#cross validation and hence there were 5 files in each of the
#directories. Here, for illustration, we use only 1 training and 1 label
#file (e.g., '.../0_trn_img.txt' and '.../0_trn_lab.txt').
path = "C:/Users/antpc/Documents/saqib_smote/fmnist/"
path = "C:/Users/Research6/Desktop/smote experimentation/mnist/"
dtrnimg = (path+'/CBL_images')
dtrnlab = (path+'/CBL_labels')
ids = os.listdir(dtrnimg)
idtri_f = [os.path.join(dtrnimg, image_id) for image_id in ids]
print(idtri_f)
ids = os.listdir(dtrnlab)
idtrl_f = [os.path.join(dtrnlab, image_id) for image_id in ids]
print(idtrl_f)
#for i in range(5):
for i in range(len(ids)):
print()
print(i)
encoder = Encoder(args)
decoder = Decoder(args)
device = 'cuda' if torch.cuda.is_available() else 'cpu'
print(device)
decoder = decoder.to(device)
encoder = encoder.to(device)
train_on_gpu = torch.cuda.is_available()
#decoder loss function
criterion = nn.MSELoss()
criterion = criterion.to(device)
trnimgfile = idtri_f[i]
trnlabfile = idtrl_f[i]
print(trnimgfile)
print(trnlabfile)
dec_x = np.loadtxt(trnimgfile)
dec_y = np.loadtxt(trnlabfile)
print('train imgs before reshape ',dec_x.shape)
print('train labels ',dec_y.shape)
print(collections.Counter(dec_y))
# dec_x = dec_x.reshape(shape)
# dec_x = dec_x.permute(0, 4 1, 2, 3)
# dec_x = dec_x.reshape(shape[0],shape[3],shape[1],shape[2])
print("shape >>>>>>>>>>>>>> ",)
dec_x = dec_x.reshape(shape[0],3,32,32)
print('train imgs after reshape ',dec_x.shape)
batch_size = 100
num_workers = 0
#torch.Tensor returns float so if want long then use torch.tensor
tensor_x = torch.Tensor(dec_x)
tensor_y = torch.tensor(dec_y,dtype=torch.long)
mnist_bal = TensorDataset(tensor_x,tensor_y)
train_loader = torch.utils.data.DataLoader(mnist_bal,
batch_size=batch_size,shuffle=True,num_workers=num_workers)
best_loss = np.inf
t0 = time.time()
if args['train']:
enc_optim = torch.optim.Adam(encoder.parameters(), lr = args['lr'])
dec_optim = torch.optim.Adam(decoder.parameters(), lr = args['lr'])
for epoch in range(args['epochs']):
train_loss = 0.0
tmse_loss = 0.0
tdiscr_loss = 0.0
# train for one epoch -- set nets to train mode
encoder.train()
decoder.train()
for images,labs in train_loader:
# zero gradients for each batch
encoder.zero_grad()
decoder.zero_grad()
#print(images)
images, labs = images.to(device), labs.to(device)
#print('images ',images.size())
labsn = labs.detach().cpu().numpy()
#print('labsn ',labsn.shape, labsn)
# run images
z_hat = encoder(images)
x_hat = decoder(z_hat) #decoder outputs tanh
#print('xhat ', x_hat.size())
#print(x_hat)
mse = criterion(x_hat,images)
#print('mse ',mse)
resx = []
resy = []
tc = np.random.choice(10,1)
#tc = 9
xbeg = dec_x[dec_y == tc]
ybeg = dec_y[dec_y == tc]
xlen = len(xbeg)
nsamp = min(xlen, 100)
ind = np.random.choice(list(range(len(xbeg))),nsamp,replace=False)
xclass = xbeg[ind]
yclass = ybeg[ind]
xclen = len(xclass)
#print('xclen ',xclen)
xcminus = np.arange(1,xclen)
#print('minus ',xcminus.shape,xcminus)
xcplus = np.append(xcminus,0)
#print('xcplus ',xcplus)
xcnew = (xclass[[xcplus],:])
#xcnew = np.squeeze(xcnew)
xcnew = xcnew.reshape(xcnew.shape[1],xcnew.shape[2],xcnew.shape[3],xcnew.shape[4])
#print('xcnew ',xcnew.shape)
xcnew = torch.Tensor(xcnew)
xcnew = xcnew.to(device)
#encode xclass to feature space
xclass = torch.Tensor(xclass)
xclass = xclass.to(device)
xclass = encoder(xclass)
#print('xclass ',xclass.shape)
xclass = xclass.detach().cpu().numpy()
xc_enc = (xclass[[xcplus],:])
xc_enc = np.squeeze(xc_enc)
#print('xc enc ',xc_enc.shape)
xc_enc = torch.Tensor(xc_enc)
xc_enc = xc_enc.to(device)
ximg = decoder(xc_enc)
mse2 = criterion(ximg,xcnew)
comb_loss = mse2 + mse
comb_loss.backward()
enc_optim.step()
dec_optim.step()
train_loss += comb_loss.item()*images.size(0)
tmse_loss += mse.item()*images.size(0)
tdiscr_loss += mse2.item()*images.size(0)
# print avg training statistics
train_loss = train_loss/len(train_loader)
tmse_loss = tmse_loss/len(train_loader)
tdiscr_loss = tdiscr_loss/len(train_loader)
print('Epoch: {} tTrain Loss: {:.6f} tmse loss: {:.6f} tmse2 loss: {:.6f}'.format(epoch,
train_loss,tmse_loss,tdiscr_loss))
#store the best encoder and decoder models
#here, /crs5 is a reference to 5 way cross validation, but is not
#necessary for illustration purposes
if train_loss < best_loss:
print('Saving..')
# path_enc = "C:\Users\Research6\Desktop\smote" + '\bst_enc.pth'
# path_dec = "C:\Users\Research6\Desktop\smote" + '\bst_dec.pth'
path_enc = path + '\bst_enc.pth'
path_dec = path + '\bst_dec.pth'
# path_enc = '/content/gdrive/My Drive/smote/'
# + str(i) + '/bst_enc.pth'
# path_dec = '/content/gdrive/My Drive/smote/'
# + str(i) + '/bst_dec.pth'
torch.save(encoder.state_dict(), path_enc)
torch.save(decoder.state_dict(), path_dec)
best_loss = train_loss
#in addition, store the final model (may not be the best) for
#informational purposes
path_enc = path + '\f_enc.pth'
path_dec = path + '\f_dec.pth'
print(path_enc)
print(path_dec)
torch.save(encoder.state_dict(), path_enc)
torch.save(decoder.state_dict(), path_dec)
print()
t1 = time.time()
print('total time(min): {:.2f}'.format((t1 - t0)/60))
t4 = time.time()
print('final time(min): {:.2f}'.format((t4 - t3)/60))
我在pytorch上也有同样的问题,你能试着在解码器的nn.sequential()
内取消注释下面的行吗。#3d和32乘32。
nn.Conv2d(self.dim_h * 4, self.dim_h * 8, 4, 1, 0, bias=False)