Yes, I have looked and tried solution from other related questions. Let me explain.
Description: So, I am doing adversarial training and I’ve used code from this Github repo. It uses Pytorch to train model on MNIST dataset.
What want is that instead of MNIST I want to use CelebeA dataset. And when I run this exact code on that it is giving me the above error.
What I’ve tried: I googled the error and got 2,3 related issues in which the issue got resolved when people changed their input-size in first conv layer. When I changed self.conv1 = torch.nn.Conv2d(1, 6, 5, padding=2)
to self.conv1 = torch.nn.Conv2d(3, 6, 5, padding=2)
it results into another error on line (x=x.view(-1, 16*5*5))
saying: “RuntimeError: shape '[-1, 400]' is invalid for input of size 4472832
” .
I even tried changing this -1 value to 3, but its resulted into another different error and to be honest I don’t know what I’m doing so I stopped messing around.
Part of the code that I think is related to this error. You can see the whole code here:
# Creating a simple network
class LeNet5(torch.nn.Module):
def __init__(self):
super(LeNet5, self).__init__()
self.conv1 = torch.nn.Conv2d(1, 6, 5, padding=2)
self.conv2 = torch.nn.Conv2d(6, 16, 5)
self.fc1 = nn.Linear(16*5*5, 120)
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 10)
def forward(self, x):
x = F.relu(self.conv1(x))
x = F.max_pool2d(x, 2)
x = F.relu(self.conv2(x))
x = F.max_pool2d(x, 2)
x = x.view(-1, 16*5*5)
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return F.log_softmax(x,dim=-1)
FLAGS = flags.FLAGS
NB_EPOCHS = 2
BATCH_SIZE = 128
LEARNING_RATE = .001
#Training the Network
def trainTorch(torch_model, train_loader, test_loader,
nb_epochs=NB_EPOCHS, batch_size=BATCH_SIZE, train_end=-1, test_end=-1, learning_rate=LEARNING_RATE, optimizer=None):
train_loss = []
total = 0
correct = 0
step = 0
for _epoch in range(nb_epochs):
for xs, ys in train_loader:
xs, ys = Variable(xs), Variable(ys)
if torch.cuda.is_available():
xs, ys = xs.cuda(), ys.cuda()
print('Cuda is available')
optimizer.zero_grad()
preds = torch_model(xs)
# print("HI")
loss = F.nll_loss(preds, ys)
# print("HADSFSDF")
loss.backward() # calc gradients
train_loss.append(loss.data.item())
optimizer.step() # update gradients
preds_np = preds.cpu().detach().numpy()
correct += (np.argmax(preds_np, axis=1) == ys.cpu().detach().numpy()).sum()
total += train_loader.batch_size
step += 1
if total % 1000 == 0:
acc = float(correct) / total
print('[%s] Training accuracy: %.2f%%' % (step, acc * 100))
total = 0
correct = 0
model1 = LeNet5()
if torch.cuda.is_available():
model1 = model1.cuda()
nb_epochs = 4
batch_size = 128
learning_rate = 0.001
train_end = -1
test_end = -1
report = AccuracyReport()
train_loader = torch.utils.data.DataLoader(
datasets.CelebA('data', split='train', transform=transforms.ToTensor(), download="True"),
batch_size=batch_size, shuffle=True)
test_loader = torch.utils.data.DataLoader(
datasets.CelebA('data', split='test', transform=transforms.ToTensor()),
batch_size=batch_size)
# In[ ]:
#Training the model
print("Training Model")
optimizer = optim.Adam(model1.parameters(), lr=learning_rate)
trainTorch(model1, train_loader, test_loader, nb_epochs, batch_size, train_end, test_end, learning_rate, optimizer = optimizer)
What I want to achieve:
1- To understand this and resolve this error and get my CelebA dataset working on this code.
2- Would I require any other change (for adversarial training on CelebA instead of MNIST) even if this is resolved?
3- My ultimate goal is to do adversarial training on any facial recognition dataset (ideally with pytorch and FGSM attack). If my current approach is not right, can you guys suggest me any other sources where it is done or can provide information on how to do this?
Thanks!