辦法1:debug原來的unet代碼蘑志,看看哪里不對(duì)勁。
1.sigmoid
sigmoid是處理二分類問題的贬派。
在loss function里急但,用了sigmoid函數(shù)進(jìn)行prediction結(jié)果的二分類。
所以應(yīng)該是一個(gè)channel用來表示一個(gè)label比較合適搞乏。
所以針對(duì)我們的問題:
就是2個(gè)output channel 就可以了波桩。
辦法2:跑新的代碼
https://github.com/zsdonghao/u-net-brain-tumor
以后再說。
具體操作:
1.label the gourndtruth
2. 思路是:
在同一張圖中l(wèi)abel请敦,然后再手動(dòng)轉(zhuǎn)化成two channels.然后丟進(jìn)網(wǎng)絡(luò)里去跑镐躲。
3.直接改loss function部分就好啦
直接在lossfunction里 把groudnturth的一張圖變成兩個(gè)channel
詳細(xì)的改loss function的步驟:
建議是都用 tensor寫储玫,不要轉(zhuǎn)化成numpy
view(-1) 是把a(bǔ)rray變成一維的
torch的一些tip:
1.查看數(shù)組的type
var.size()
2.
torch.zeros([2,4],dtype=torch.int32)
3.
torch轉(zhuǎn)numpy的轉(zhuǎn)換方法
Numpy2Tensor:1. torch.from_numpy(Numpy_data)?2. torch.tensor(Numpy_data)?
Tensor2Variable: 1.?Variable(Tensor_data)
Variable2Tensor: 1. Variable_data.data()
Tensor2Numpy : 1.Tensor_data.numpy()?
4.
畫圖
import matplotlib.pyplot?as?plt?
plt.imshow(img).?imshow()
debug:
1. Totensor這個(gè)是怎么實(shí)現(xiàn)的。
可以從to_tensor()函數(shù)看到匀油,函數(shù)接受PIL Image或numpy.ndarray缘缚,將其先由HWC轉(zhuǎn)置為CHW格式,再轉(zhuǎn)為float后每個(gè)像素除以255.
記錄:label1是171敌蚜,label2是85
2.問題出在label不是單純的171和85
找到原因了桥滨。放大nrrd圖,其實(shí)同一個(gè)cell的label的顏色還是不一樣的弛车!
現(xiàn)在需要做的事情是nrrd圖轉(zhuǎn)tif圖的過程的檢查
運(yùn)行命令行:
python main.py -n_classes 2
報(bào)錯(cuò):
RuntimeError: expected backend CPU and dtype Float but got backend CUDA and dtype Float
期望得到CPU類型張量齐媒,得到的卻是CUDA張量類型。
很典型的錯(cuò)誤纷跛,例如計(jì)算圖中有的參數(shù)為cuda型有的參數(shù)卻是cpu型就會(huì)遇到這樣的錯(cuò)誤喻括。
把數(shù)據(jù)從cpu遷移到cuda的網(wǎng)址:
https://blog.csdn.net/qq_28444159/article/details/78781201
https://blog.csdn.net/hustchenze/article/details/79154139
結(jié)果:
用了這里的代碼
https://github.com/HZCTony/U-net-with-multiple-classification
代碼理解和分析:
為什么之前自己跑不出來!
shape問題贫奠!
gt? 和 prediction flat之后相減唬血,對(duì)應(yīng)元素對(duì)不上!