【发布时间】:2020-10-12 17:23:30
【问题描述】:
所以我一直在针对二值图像分类问题训练 Inception-Resnet V2,在训练时我观察到 logits 不会收敛到 0 或 1。它们只会在 0.5 左右波动。似乎是什么错误?我在一个高度不平衡的数据集上训练了 4 个 epoch 的预训练模型,这就是为什么我也使用加权随机采样器。批量大小为 128,优化器为 adam,学习率为 0.001
device = "cuda"
epochs=4
print("======== Training for ", epochs, "epochs=============")
for epoch in range(epochs):
total_loss = 0
model.train()
print("Training.......")
print("======== EPOCH #",epoch,"=================")
tmp_acc = 0
for i,batch in enumerate(train_loader):
img,label = batch["images"],batch["labels"]
label = label.type(torch.FloatTensor)
img,label = img.to(device),label.to(device)
model.zero_grad()
op,aux = model(img)
label_cpu = label.cpu().numpy()
op = F.sigmoid(op)
output = op.detach().cpu().numpy()
tmp_acc += accuracy_score(output,label_cpu)
loss = criterion(op,label)
total_loss = loss.item()
loss.backward()
adam.step()
if(i%10==0 and i>0):
print("STEP: ",i, "of steps ",len(train_loader))
print("Current loss: ",total_loss/i)
print("Training Accuracy ",tmp_acc/i)
print("OP",op)
print("Label",label_cpu)
avg_loss = total_loss/len(train_loader)
print("The loss after ",epoch," epochs is ",avg_loss)
model.eval()
print("Validating.....")
tmp_accuracy = 0
z_count,o_count=0,0
z_count_truth,o_count_truth = 0,0
for i,batch in enumerate(val_loader):
img,label = batch["images"],batch["labels"]
img = img.to(device)
with torch.no_grad():
op = F.sigmoid(model(img))
op_cpu = op.detach().cpu().numpy()
label = label.numpy()
tmp_accuracy += accuracy_score(op_cpu,label)
z_count += np.sum(op_cpu==0)
o_count += np.sum(op_cpu==1)
z_count_truth += np.sum(label==0)
o_count_truth += np.sum(label==1)
percent_correct_z = z_count/z_count_truth
percent_correct_o = o_count/o_count_truth
accuracy = tmp_accuracy/len(val_loader)
print("Accuracy: ", "is ",accuracy)
#print("Percent of correct zero labels ",percent_correct_z)
#print("Percent of correct one labels ",percent_correct_o)```
输出看起来像
STEP: 90 of steps 99
Current loss: 0.007694996065563626
Training Accuracy 0.5019965277777778
OP tensor([[0.4962],
[0.4956],
[0.4950],
[0.4957],
[0.4945],
[0.4957],
[0.4952],
[0.4965],
[0.4950],
[0.4962],
[0.4956],
[0.4956],
[0.4951],
[0.4953],
[0.4956],
[0.4958],
[0.4949],
[0.4945],
[0.4955],
[0.4924],
[0.4952],
[0.4952],
[0.4958],
[0.4953],
[0.4959],
[0.4952],
[0.4965],
[0.4956],
[0.4956],
[0.4381],
[0.4951],
[0.4946],
[0.4957],
[0.4951],
[0.4955],
[0.4952],
[0.4955],
[0.4948],
[0.4951],
[0.4960],
[0.4956],
[0.4955],
[0.4958],
[0.4957],
[0.4953],
[0.4954],
[0.4955],
[0.4959],
[0.4949],
[0.4960],
[0.4953],
[0.4949],
[0.4951],
[0.4952],
[0.4949],
[0.4954],
[0.4956],
[0.4951],
[0.4947],
[0.4958],
[0.4953],
[0.4960],
[0.4959],
[0.4958],
[0.4948],
[0.4947],
[0.4957],
[0.4961],
[0.4955],
[0.4959],
[0.4955],
[0.4954],
[0.4959],
[0.4952],
[0.4955],
[0.4951],
[0.4962],
[0.4961],
[0.4961],
[0.4960],
[0.4956],
[0.4959],
[0.4953],
[0.4960],
[0.4955],
[0.4949],
[0.4958],
[0.4953],
[0.4955],
[0.4959],
[0.4951],
[0.4961],
[0.4939],
[0.4954],
[0.4953],
[0.4958],
[0.4953],
[0.4949],
[0.4959],
[0.4958],
[0.4960],
[0.4949],
[0.4957],
[0.4964],
[0.4949],
[0.4956],
[0.4952],
[0.4959],
[0.4954],
[0.4958],
[0.4954],
[0.4951],
[0.4953],
[0.4953],
[0.4958],
[0.4954],
[0.4955],
[0.4954],
[0.4960],
[0.4946],
[0.4950],
[0.4953],
[0.4957],
[0.4956],
[0.4954],
[0.4940],
[0.4951],
[0.4955]], device='cuda:0', grad_fn=<SigmoidBackward>)
Label [0. 1. 0. 1. 1. 0. 1. 0. 1. 1. 1. 0. 0. 0. 0. 1. 0. 1. 1. 0. 1. 0. 1. 0.
1. 0. 0. 1. 1. 0. 0. 1. 0. 0. 0. 0. 1. 0. 0. 1. 0. 0. 1. 1. 1. 0. 1. 1.
0. 0. 1. 1. 1. 0. 1. 0. 0. 0. 1. 1. 1. 1. 0. 1. 0. 1. 1. 0. 0. 0. 1. 1.
0. 1. 1. 0. 0. 0. 1. 1. 0. 0. 1. 0. 1. 0. 0. 0. 1. 0. 1. 0. 0. 1. 1. 0.
0. 1. 1. 0. 0. 1. 1. 0. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 0. 0. 0. 0. 0. 0.
1. 0. 1. 1. 0. 1. 1. 0.]
OP Tensor 对应输出 logits 标签对应原来的标签
【问题讨论】:
-
4 个 epoch 太少了。尝试训练更长的时间,也许 100 个 epoch...
-
你能展示一些标签吗?你确定它们是 0 还是 1?
-
感谢所有输入,标签已经过验证,它们分布正确,只有 0 和 1。但我认为 Ash 对此是正确的,因为使用 transforms.CenterCrop 导致图像像素转到范围为 0.5-1。现在尝试不同的变换
-
我也可以训练更多的时期,但只是看到损失,已经很低了
-
请注意不要将部分文本格式化为代码(已编辑)。
标签: python deep-learning computer-vision pytorch artificial-intelligence