Diff of /Unet/model.py [000000] .. [6d4adb]

Switch to unified view

a b/Unet/model.py
1
import torch
2
import torch.nn as nn
3
import torchvision.transforms.functional as TF
4
5
6
class DoubleConv(nn.Module):
7
    def __init__(self, in_channels, out_channels):
8
        super(DoubleConv, self).__init__()
9
        self.conv = nn.Sequential(
10
            # kernel_size=3
11
            # stride=1(evry rown a nd column is affected)
12
            # padding=1(same conv)input height i width isti nakon konv
13
            # bias=false
14
            # batchnomr-normalizacija
15
            # relu aktivacijska funk
16
            nn.Conv2d(in_channels, out_channels, 3, 1, 1, bias=False),
17
            nn.BatchNorm2d(out_channels),
18
            nn.ReLU(inplace=True),
19
20
            nn.Conv2d(out_channels, out_channels, 3, 1, 1, bias=False),
21
            nn.BatchNorm2d(out_channels),
22
            ##inplace means that it will modify the input directly, without allocating any additional output
23
            nn.ReLU(inplace=True),
24
25
        )
26
27
    def forward(self, x):
28
        return self.conv(x)
29
30
31
class UNET(nn.Module):
32
    ##moguce da broj out kanala promjenis,ovjde 1 jer je binary image segemntation
33
    def __init__(self, in_channels=3, out_channels=2, features=[64, 128, 256, 512]):
34
        super(UNET, self).__init__()
35
        # jer hocemo evaluirat i sve to imamo tu listu zbog layera,spremamo sve te konvolucije
36
        self.downs = nn.ModuleList()
37
        self.ups = nn.ModuleList()
38
        self.pool = nn.MaxPool2d(kernel_size=2, stride=2)
39
40
        # Down part
41
        for feature in features:
42
            self.downs.append(DoubleConv(in_channels, feature))
43
            in_channels = feature
44
45
        # Up part
46
        for feature in reversed(features):
47
            self.ups.append(nn.ConvTranspose2d(feature * 2, feature, kernel_size=2,
48
                                               stride=2))  ##kernel size is tride ce poduplat ovdje height i width slike
49
            self.ups.append(DoubleConv(feature * 2, feature))
50
51
        self.bottleneck = DoubleConv(features[-1], features[-1] * 2)  # onaj zasebni dio
52
        self.final_conv = nn.Conv2d(features[0], out_channels, kernel_size=1)
53
54
    def forward(self, x):
55
        skip_connections = []
56
        for down in self.downs:
57
            x = down(x)
58
            skip_connections.append(x)
59
            x = self.pool(x)
60
61
        x = self.bottleneck(x)
62
        skip_connections = skip_connections[::-1]  # obrnuti redoslijed
63
64
        for idx in range(0, len(self.ups), 2):
65
            x = self.ups[idx](x)
66
            skip_connection = skip_connections[idx // 2]
67
            if x.shape != skip_connection.shape:
68
                x = TF.resize(x, size=skip_connection.shape[2:])  ##ako input nije djeljiv s 2, resize se
69
70
            concat_skip = torch.cat((skip_connection, x), dim=1)
71
            x = self.ups[idx + 1](concat_skip)
72
73
        return self.final_conv(x)