github上で公開されているPytorch-UNetでサンプルデータをそのまま出力しました。
https://github.com/Paul0629/unet-pytorch
出力はうまくできたのですが、predict.pyを実行して、得られたマスク画像の評価を行いたいです。
同じフォルダにeval.pyとdice_loss.pyあったので、評価用のプログラムだと思ったのですが、実行すると、何も保存されず、何も出力されませんでした。
このプログラムは何を実行するプログラムでしょうか?
1,eval.py
python
1import torch 2import torch.nn.functional as F 3from tqdm import tqdm 4 5from dice_loss import dice_coeff 6 7 8def eval_net(net, loader, device): 9 """Evaluation without the densecrf with the dice coefficient""" 10 net.eval() 11 mask_type = torch.float32 if net.n_classes == 1 else torch.long 12 n_val = len(loader) # the number of batch 13 tot = 0 14 15 with tqdm(total=n_val, desc='Validation round', unit='batch', leave=False) as pbar: 16 for batch in loader: 17 imgs, true_masks = batch['image'], batch['mask'] 18 imgs = imgs.to(device=device, dtype=torch.float32) 19 true_masks = true_masks.to(device=device, dtype=mask_type) 20 21 with torch.no_grad(): 22 mask_pred = net(imgs) 23 24 if net.n_classes > 1: 25 tot += F.cross_entropy(mask_pred, true_masks).item() 26 else: 27 pred = torch.sigmoid(mask_pred) 28 pred = (pred > 0.5).float() 29 tot += dice_coeff(pred, true_masks).item() 30 pbar.update() 31 32 net.train() 33 return tot / n_val
2,dice_loss.py
python
1import torch 2from torch.autograd import Function 3 4 5class DiceCoeff(Function): 6 """Dice coeff for individual examples""" 7 8 def forward(self, input, target): 9 self.save_for_backward(input, target) 10 eps = 0.0001 11 self.inter = torch.dot(input.view(-1), target.view(-1)) 12 self.union = torch.sum(input) + torch.sum(target) + eps 13 14 t = (2 * self.inter.float() + eps) / self.union.float() 15 return t 16 17 # This function has only a single output, so it gets only one gradient 18 def backward(self, grad_output): 19 20 input, target = self.saved_variables 21 grad_input = grad_target = None 22 23 if self.needs_input_grad[0]: 24 grad_input = grad_output * 2 * (target * self.union - self.inter) \ 25 / (self.union * self.union) 26 if self.needs_input_grad[1]: 27 grad_target = None 28 29 return grad_input, grad_target 30 31 32def dice_coeff(input, target): 33 """Dice coeff for batches""" 34 if input.is_cuda: 35 s = torch.FloatTensor(1).cuda().zero_() 36 else: 37 s = torch.FloatTensor(1).zero_() 38 39 for i, c in enumerate(zip(input, target)): 40 s = s + DiceCoeff().forward(c[0], c[1]) 41 42 return s / (i + 1) 43
> github上で公開されているPytorch-UNetでサンプルデータをそのまま出力しました。
https://github.com/milesial/Pytorch-UNet
を見ても、
> 同じフォルダにeval.pyとdice_loss.pyがあった
は見当たりませんけど、どこにあるのでしょうか?
> 得られたマスク画像の評価を行いたい
正解のマスク画像が用意されてて、AIが推定して作成したマスク画像を正解画像と比較したい、という意味ですか?
すいません。貼り付けるURLを間違えていました。
修正します。
そうです。
> https://github.com/Paul0629/unet-pytorch
は、
https://github.com/milesial/Pytorch-UNet
がオリジナルで、オリジナルの方がファイルの更新日が新しいですよ
そうなんですね。ありがとうございます!
オリジナルの方でevaluate.pyを試してみたのですが、何も表示されず、新規のファイルを作成されていません。
オリジナルの方はうまく実行されないので、やはり上のURLのプログラムで評価したいです。
> eval.pyとdice_loss.pyの役割
学習に使う
https://github.com/Paul0629/unet-pytorch/blob/master/train.py
の「from eval import eval_net」で「eval.py」を読んでて、それの「def eval_net(...」の
mask_pred = net(imgs)
でマスク画像を推定してるっぽい
「if net.n_classes > 1:」が成立する場合は、
tot += F.cross_entropy(mask_pred, true_masks).item()
で、推定したマスク画像と正解マスク画像を比較評価してるっぽい
「if...」が成立しない場合は、その代わりに
tot += dice_coeff(pred, true_masks).item()
を実行してるので、その「dice_coeff()」がやはり比較評価をしてるっぽくて、「dice_coeff()」は「dice_loss.py」のもので、「dice_coeff()」で「DiceCoeff().forward()」を呼び出して、そこで何やら計算してる
だいたいこんな感じだと思います
(詳しく読んでないので勘違いしてるところあるかも)
上記は「train.py」での検証(val)データの扱いの話です
学習データは「train.py」の
masks_pred = net(imgs)
でマスク画像を推定して、
loss = criterion(masks_probs_flat, true_masks_flat)
で正解マスク画像と比較評価してるっぽくて、「criterion()」の正体は「criterion = nn.BCELoss()」です
参考
http://arduinopid.web.fc2.com/Q5-40.html
> 得られたマスク画像の評価を行いたい
「train.py」の
imgs = torch.from_numpy(imgs)
から
loss = criterion(masks_probs_flat, true_masks_flat)
までを切り出して利用したらできそうな (未確認)

回答1件
あなたの回答
tips
プレビュー