So my problem is that I try a CNN to learn to classify images of skin cancer as benign or malignant. I feed the images, and whatever the image, I get the same outputs always. I tracked it down and it seems that after the first conv layer, the outputs are the same with every image. And it is not getting any better with more epochs. Is it a problem of the input to the neural network? Because every image i feed it is different, i checked. Or is it something else? Any ideas?
class ConvNet(nn.Module):
def __init__(self):
super(ConvNet, self).__init__()
self.conv1 = nn.Conv2d(3, 32, kernel_size=5)
self.conv2 = nn.Conv2d(32,64, kernel_size=5)
self.conv3 = nn.Conv2d(64,128, kernel_size=5)
self.pool = nn.MaxPool2d(3,3)
self.fc1 = nn.Linear(128*5*5, 512)
self.fc2 = nn.Linear(512, 256)
self.fc3 = nn.Linear(256,1)
self.check = []
def forward(self, x):
x = self.pool(F.relu(self.conv1(x)))
x = self.pool(F.relu(self.conv2(x)))
self.check.append(x)
if len(self.check) > 1:
print((self.check[-1] == self.check[-2]).float().sum())
x = self.pool(F.relu(self.conv3(x)))
x = x.view(-1, 128*5*5)
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = torch.sigmoid(self.fc3(x))
return x
model = ConvNet().to(device)
PATH = “CNN_model.pt”
try:
model.load_state_dict(torch.load(PATH))
except:
print(‘Model not found!’)
criterion = nn.BCELoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)
epochs = 50
accuracy = []
batch_size = 1
for epoch in range(epochs):
model.train()
permutation = torch.randperm(len(train_data))
for i in range(0, len(train_imgs[0]), batch_size):
optimizer.zero_grad()
indices = permutation[i:i+batch_size]
batch_x, batch_y = train_imgs[0, indices], y_train[indices].unsqueeze(0).transpose(0,1).float()
outputs = model.forward(batch_x)
loss = criterion(outputs,batch_y)
loss.backward()
optimizer.step()
torch.save(model.state_dict(), PATH)
model.eval()
output = model(val_imgs[0])
print(output[0:5])
val_loss = criterion(output, y_val.unsqueeze(0).transpose(0,1).float())
y_pred = (output>0.5).float()
correct = (torch.squeeze(y_pred) == y_val.float()).float().sum()
accuracy += [100 * correct / len(val_data)]
print('Epoch:', epoch, 'Accuracy', accuracy[-1], 'loss', val_loss, '\n')