Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
562 views
in Technique[技术] by (71.8m points)

classification - Why do I always get the same value as the result in a CNN in pytorch?

Here is my code

dataset = pd.read_csv('augmented_data.csv')
dataset = dataset.sample(frac=1)

class ConvNet(nn.Module):
def __init__(self):
    super(ConvNet, self).__init__()
    self.conv1 = nn.Conv2d(3,6,5)
    self.pool = nn.MaxPool2d(2,2)
    self.conv2 = nn.Conv2d(6,16,5)
    self.fc1 = nn.Linear(1024144, 120)
    self.fc2 = nn.Linear(120, 84)
    self.fc3 = nn.Linear(84,1)

def forward(self, x):
    x = self.pool(F.relu(self.conv1(x)))
    x = self.pool(F.relu(self.conv2(x)))
    x = x.view(-1, 1024144)
    x = F.relu(self.fc1(x))
    x = F.relu(self.fc2(x))
    print(x)
    x = self.fc3(x)
    return x   

files_read = 0
preprocess = transforms.Compose([
transforms.Resize(1024),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5),(0.5, 0.5, 0.5))])

# device = torch.device('cuda' if torch.cuda.is_available else 'cpu')
device = torch.device('cpu')

# model = ConvNet().to(device)

criterion = torch.nn.MSELoss(reduction='sum')
optimizer = optim.Adam(model.parameters(), lr=0.001)

results = []    
for index, row in dataset.iterrows():
    try:
      image = load_img('padded_images/' + row['image_name'] +'.jpg')
    except:
      image = load_img('augmented_images/' + row['image_name'] +'.jpeg')

    files_read += 1

    input_tensor = preprocess(image)
    input_batch = input_tensor.unsqueeze(0).to(device)

    if files_read <= 80 * len(dataset) // 100:
        output = model(input_batch)
        optimizer.zero_grad()
    
        y = torch.tensor([[float(row['target'])]]).to(device)
        loss = criterion(output, y)
    
        loss.backward()
        optimizer.step()
    else:
        model.eval()
        output = model(input_batch)
        results.append([1.0 if output[0][0].double() > 0.5 else 0, float(row['target'])])

     

So i am using pytorch CNN to classify 60k images in 2 classes. When i print the output after the model has trained, whatever the image as input, the ouput is always "tensor([[0.6384]], grad_fn=)". Always the same value. So it predicts only 1 (because it's greater than 0.5). The thing is, when i print the ouput while training, the results vary (16, 1 , 0, 4 ,0.6 etc) but when i print the output (with the same model but not trained) the results don't vary that much (0.5, 0.51, 0.49 0.52, 0.55). So I think it's safe to say that it is converging to a single value. I just don't know why. what could i do differently?

question from:https://stackoverflow.com/questions/65926353/why-do-i-always-get-the-same-value-as-the-result-in-a-cnn-in-pytorch

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...