Diff of /ensemble_model.py [000000] .. [94d9b6]

Switch to unified view

a b/ensemble_model.py
1
import torch
2
import torch.nn as nn
3
from torch.autograd import Variable
4
import torch.optim as optim
5
import torchvision
6
from torchvision import datasets, models
7
from torchvision import transforms as T
8
from torch.utils.data import DataLoader, Dataset
9
import numpy as np
10
import matplotlib.pyplot as plt
11
import os
12
import time
13
import pandas as pd
14
from skimage import io, transform
15
import matplotlib.image as mpimg
16
from PIL import Image
17
from sklearn.metrics import roc_auc_score
18
import torch.nn.functional as F
19
import scipy
20
import random
21
import pickle
22
import scipy.io as sio
23
import itertools
24
from scipy.ndimage.interpolation import shift
25
import copy
26
import warnings
27
warnings.filterwarnings("ignore")
28
plt.ion()
29
30
class PPnet(nn.Module):
31
    def __init__(self, in_channels, out_channels, int_chan = 16, num_layers = 4):
32
        super(PPnet, self).__init__()
33
        self.layers = nn.ModuleList()
34
        in_chan = in_channels
35
        for i in range(num_layers):
36
            if i == num_layers-1:
37
                self.layers.append(nn.Conv2d(int_chan,out_channels,3,padding = 1))
38
            else:
39
                self.layers.append(nn.Conv2d(in_chan, int_chan, 3, padding= 1))
40
                self.layers.append(nn.BatchNorm2d(int_chan))
41
                self.layers.append(nn.ReLU())
42
                in_chan = int_chan
43
        self.layers = nn.Sequential(*self.layers)
44
    
45
    def forward(self,x):
46
        return self.layers(x)
47
48
def predict_pp(scores):
49
    preds = F.softmax(scores)
50
    pred_class = torch.max(preds, dim = 1, keepdim = True)[1]
51
    class_0_pred_seg = (pred_class == 0).type(torch.cuda.FloatTensor)
52
    class_1_pred_seg = (pred_class == 1).type(torch.cuda.FloatTensor)
53
    class_2_pred_seg = (pred_class == 2).type(torch.cuda.FloatTensor)
54
    class_3_pred_seg = (pred_class == 3).type(torch.cuda.FloatTensor)
55
    prediction = torch.cat((class_0_pred_seg,class_1_pred_seg,class_2_pred_seg,class_3_pred_seg),dim = 1)
56
    return prediction
57
58
def smoothing_preds(preds,filter_size = 3):
59
    smooth_pred = torch.round(F.avg_pool1d(preds,filter_size))
60
    return smooth_pred
61
62
from sklearn.metrics import accuracy_score
63
def dice_score_list(true,scores):
64
    N,C,sh1,sh2 = true.size()
65
    prediction = predict_pp(scores)
66
    prediction = prediction.data.cpu().numpy()
67
    true = true.data.cpu().numpy()
68
    dc_sr = {0:[],1:[],2:[]}
69
    acc_sr = {0:[],1:[],2:[]}
70
    for i in range(N):
71
        for j in range(3):
72
            pred = prediction[i,j,:,:]
73
            tr = true[i,j,:,:]
74
            flat_pred = np.ravel(pred)
75
            flat_true = np.ravel(tr)
76
            acc_sr[j].append(accuracy_score(flat_true,flat_pred))
77
            temp_dice_F = (2*(np.sum(pred*tr)) + 1e-4)/(np.sum(pred + tr) + 1e-4)
78
            dc_sr[j].append(temp_dice_F)
79
    return dc_sr, acc_sr
80
81