1.2 GLAS Dataset
GLAnd Segmentation (GLAS) datatset [19] contains microscopic images of Hematoxylin and Eosin (H&E) stained slides and the corresponding ground truth annotations by expert pathologists. It contains a total of 165 images which are split
into 85 images for training and 80 for testing. Since the images in the dataset
are of different sizes, we resize every image to a resolution of 128 × 128 for all
our experiments
128x128 -> 완료
문제 발생!!!
고유한 픽셀 값들:
[ 0 255]
고유한 픽셀 값들:
[0 1 2 3 4 5 6 7 8]
으로 나옴!!!헐..
gLas 데이터셋의 이미지가 monuSeg랑 다르게 binary가 아니라 opencv로 확인해보니 0, 1, 2, 3, 4, 5.. 다양하게 나왔음...
분류 threshold값 이상..?
for epoch in range(args.epochs): # epoch 파라미터만큼 epoch을 돌림.
print("epoch :" , epoch) # 여기서는 400번 돈다.
epoch_running_loss = 0
for batch_idx, (X_batch, y_batch, *rest) in enumerate(dataloader): # dataloader로부터 배치 데이터를 가져 옴.
X_batch = Variable(X_batch.to(device ='cuda'))
y_batch = Variable(y_batch.to(device='cuda')) # 현재 배치의 ground truth
output = model(X_batch) # 모델의 forward 메서드를 사용하여 input에 대한 출력값을 계산한다.
loss = criterion(output, y_batch) # 손실 함수를 사용하여 출력값과 정답 레이블간의 손실을 계산함.
# ===================backward====================
optimizer.zero_grad() # 옵티마이저의 gradient를 초기화함.
loss.backward() # backward 메서드를 호출하여 손실에 대한 역전파를 수행함.
optimizer.step() # 옵티마이저의 step 메서드를 호출하여 모델 파라미터 업데이트
epoch_running_loss += loss.item() # 해당 배치의 손실을 epoch_running_loss에 더함.
c
criterion = LogNLLLoss()
class LogNLLLoss(_WeightedLoss):
__constants__ = ['weight', 'reduction', 'ignore_index']
def __init__(self, weight=None, size_average=None, reduce=None, reduction=None,
ignore_index=-100):
super(LogNLLLoss, self).__init__(weight, size_average, reduce, reduction)
self.ignore_index = ignore_index
def forward(self, y_input, y_target):
# y_input = torch.log(y_input + EPSILON)
# --찾았다 요놈! 이 파일 저장이 안되있었군 ㅠㅠㅠ
# print(y_input.shape)
# print(y_target.shape)
return cross_entropy(y_input, y_target, weight=self.weight,
ignore_index=self.ignore_index)
def cross_entropy(
input: Tensor,
target: Tensor,
weight: Optional[Tensor] = None,
size_average: Optional[bool] = None,
ignore_index: int = -100,
reduce: Optional[bool] = None,
reduction: str = "mean",
label_smoothing: float = 0.0,
) -> Tensor:
r"""This criterion computes the cross entropy loss between input logits and target.
See :class:`~torch.nn.CrossEntropyLoss` for details.
Args:
input (Tensor) : Predicted unnormalized logits;
see Shape section below for supported shapes.
target (Tensor) : Ground truth class indices or class probabilities;
see Shape section below for supported shapes.
weight (Tensor, optional): a manual rescaling weight given to each
class. If given, has to be a Tensor of size `C`
size_average (bool, optional): Deprecated (see :attr:`reduction`). By default,
the losses are averaged over each loss element in the batch. Note that for
some losses, there multiple elements per sample. If the field :attr:`size_average`
is set to ``False``, the losses are instead summed for each minibatch. Ignored
when reduce is ``False``. Default: ``True``
ignore_index (int, optional): Specifies a target value that is ignored
and does not contribute to the input gradient. When :attr:`size_average` is
``True``, the loss is averaged over non-ignored targets. Note that
:attr:`ignore_index` is only applicable when the target contains class indices.
Default: -100
reduce (bool, optional): Deprecated (see :attr:`reduction`). By default, the
losses are averaged or summed over observations for each minibatch depending
on :attr:`size_average`. When :attr:`reduce` is ``False``, returns a loss per
batch element instead and ignores :attr:`size_average`. Default: ``True``
reduction (str, optional): Specifies the reduction to apply to the output:
``'none'`` | ``'mean'`` | ``'sum'``. ``'none'``: no reduction will be applied,
``'mean'``: the sum of the output will be divided by the number of
elements in the output, ``'sum'``: the output will be summed. Note: :attr:`size_average`
and :attr:`reduce` are in the process of being deprecated, and in the meantime,
specifying either of those two args will override :attr:`reduction`. Default: ``'mean'``
label_smoothing (float, optional): A float in [0.0, 1.0]. Specifies the amount
of smoothing when computing the loss, where 0.0 means no smoothing. The targets
become a mixture of the original ground truth and a uniform distribution as described in
`Rethinking the Inception Architecture for Computer Vision <https://arxiv.org/abs/1512.00567>`__. Default: :math:`0.0`.
Shape:
- Input: Shape :math:`(C)`, :math:`(N, C)` or :math:`(N, C, d_1, d_2, ..., d_K)` with :math:`K \geq 1`
in the case of `K`-dimensional loss.
- Target: If containing class indices, shape :math:`()`, :math:`(N)` or :math:`(N, d_1, d_2, ..., d_K)` with
:math:`K \geq 1` in the case of K-dimensional loss where each value should be between :math:`[0, C)`.
If containing class probabilities, same shape as the input and each value should be between :math:`[0, 1]`.
where:
.. math::
\begin{aligned}
C ={} & \text{number of classes} \\
N ={} & \text{batch size} \\
\end{aligned}
Examples::
>>> # Example of target with class indices
>>> input = torch.randn(3, 5, requires_grad=True)
>>> target = torch.randint(5, (3,), dtype=torch.int64)
>>> loss = F.cross_entropy(input, target)
>>> loss.backward()
>>>
>>> # Example of target with class probabilities
>>> input = torch.randn(3, 5, requires_grad=True)
>>> target = torch.randn(3, 5).softmax(dim=1)
>>> loss = F.cross_entropy(input, target)
>>> loss.backward()
"""
if has_torch_function_variadic(input, target, weight):
return handle_torch_function(
cross_entropy,
(input, target, weight),
input,
target,
weight=weight,
size_average=size_average,
ignore_index=ignore_index,
reduce=reduce,
reduction=reduction,
label_smoothing=label_smoothing,
)
if size_average is not None or reduce is not None:
reduction = _Reduction.legacy_get_string(size_average, reduce)
return torch._C._nn.cross_entropy_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index, label_smoothing)