-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel_py.py
More file actions
79 lines (64 loc) · 9.13 KB
/
Copy pathmodel_py.py
File metadata and controls
79 lines (64 loc) · 9.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# -*- coding: utf-8 -*-
"""model.py
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1toEHsRkzDnHacBwZhgl2bwKK96CUByLB
"""
import torch
import torch.nn as nn
import torchvision.transforms.functional as tf
class doubleconv(nn.Module): #밑의 그림에서 3개의 tensor 가 가공되는과정 화살표 2개 의미
def __init__(self,inchannels,outchannels):
super(doubleconv,self).__init__() #nn.module 불러옴
self.conv = nn.Sequential( #layer의 집합
nn.Conv2d(inchannels,outchannels,3,1,1), #kernal,stride,padding
nn.BatchNorm2d(outchannels), #std,var 정규화
nn.ReLU(inplace=True), # activation
nn.Conv2d(outchannels,outchannels,3,1,1),
nn.Batchnorm2d(outchannels),
nn.ReLU(inplace=True)
) #생성자
def forward(self,x): #forward pass
return self.conv(x)
class unet(nn.Module):
def __init__(
self,inchannels=3,outchannels=1,
features =[64,128,256,512]
):
super(unet,self).__init__()
self.ups = nn.Modulelist() #화살표 위로 가는 과정 upsampling
self.downs = nn.Modulelist() #화살표 밑으로 과정 downsampling
self.pool = nn.MaxPool2d(kernal_size=2,stride=2) #밑으로 내려가는 화살표
for feature in features: #inchannels = 3
self.downs.append(doubleconv(inchannels,feature))
#밑으로 순차적으로 가면서 일련의 모든 conv layer list에 추가
inchannels = feature # inchannel 값 update 시킴으로써 feature 와 상호작용
#위의 과정 거꾸로
for feature in reversed(features):
self.ups.append(
nn.ConvTranspose2d(feature*2,feature,kernel_size=2,stride=2)
# inchannel 안바꿔도 되는이유 outchannel이 정확히 1/2배 됨으로 feature*2,feature
)
self.ups.append(doubleconv(feature*2,feature))
#맨밑의 512->1024 부분
self.bottleneck = doubleconv(features[-1],features[-1]*2)
#맨위의 output segmentation 부분
self.final_conv = nn.Conv2d(features[0],outchannels,kernel_size=1)
# forward pass를 위한 모든 생성자 형성
def forward(self,x):
skip_connections = [] #잔차 연결 resnet에서 한 번 다룸
for down in self.downs:
x= down(x)
skip_connections.append(x) #doublecov 통과할때마다 그값을 upsampling값에 추가
x=self.pool(x)
x = self.bottleneck(x)
skip_connections = skip_connections[::-1] #residual 에서 concat하기 위한 사전조치
for idx in range(0,len(self.ups),2):
x = self.ups[idx](x)
skip_connection = skip_connections[idx//2]
if x.shape != skip_connection.shape:
x = tf.resize(x,size= skip_connection.shape[2:])
concat_skip = torch.cat((skip_connection,x),dim=1)
x= self.ups[idx+1](concat_skip)
return self.final_conv(x)
""""""