HardNet 개발 (2)
어제부터 계속 학습을 진행하는데 IOU로 평가한 acc는 80%가 나오는데 이미지는 아무것도 안찍혀서 이상하다고 생각했다
...는 학습할 때 이미지하고 테스트할 때 이미지를 다르게 해서 문제였다 (학습할 때는 255로 안나누고, 테스트할 때만 나눔)
그래도 일단은 IOU 코드는 아래와 같이 수정
def iou_pytorch(outputs: torch.Tensor, labels: torch.Tensor, device='cuda'):
# You can comment out this line if you are passing tensors of equal shape
# But if you are passing output from UNet or something it will most probably
# be with the BATCH x 1 x H x W shape
outputs = torch.max(outputs, 1)[1]
batch_size = outputs.size()[0]
intersection = ((outputs.int() != 0) & (labels.int() != 0) & (outputs.int() == labels.int())).float()
intersection = intersection.view(batch_size, -1).sum(1)
union = ((outputs.int() != 0) | (labels.int() != 0)).float()
union = union.view(batch_size, -1).sum(1)
iou = (intersection + SMOOTH) / (union + SMOOTH)
return iou.mean()
loss는 그대로 진행, optimizer의 learning rate는 0.001로 수정
그래서 테스트 삼아 batch size를 32로 이미지 32개 씩 학습하고
valid 이미지도 train에 쓰인 것과 똑같이 학습시켜봤다
30 epoch를 수행한 결과
보기는 힘들지만
똑같은 이미지로 train과 valid를 계속 진행하는데 valid loss하고 acc가 들쭉날쭉했다
다음 테스트로는 threshold를 왜 거나 싶어서 걍 cross entropy를 해당 차원에 맞춰서 loss function을 새로 짰다
class MyCrossEntropyLoss(torch.nn.modules.loss._WeightedLoss):
__constants__ = ['weight', 'ignore_index', 'reduction']
def __init__(self, weight=None, size_average=None, ignore_index=-100,reduce=None, reduction='mean'):
super(MyCrossEntropyLoss, self).__init__(weight, size_average, reduce, reduction)
self.ignore_index = ignore_index
def forward(self, input, target):
return F.cross_entropy(input, target, weight=self.weight, ignore_index=self.ignore_index, reduction=self.reduction).mean()
짜놓고 보니까 CrossEntropy를 바로 상속받아서 forward만 수정해도 됐을거 같은데
일단은 짜놨으니 나중에 수정하던지 하고 패스
나머지는 이전과 똑같이 학습을 진행한 결과
얘는 valid loss하고 acc가 이전 loss function보다 원만하게 진행이 되었다
valid acc가 올라가는 속도는 이전이 더 빠르지만 안정적인게 더 좋은거 같으니 일단 이걸로 ㄱㄱ
학습 중간중간에 테스트한 결과 제대로 학습되는 것으로 확인하였다
bootstrapped_cross_entropy2d 으로 학습해도 무방할 것으로 보이지만 일단은 현재 학습하는 loss에 대한 model이 제대로 나오고 다음에 테스트하던지 아니면 잘나오면 안하던지
다음은 unet을 진행해볼 예정