当前位置:网站首页>UNET code implementation
UNET code implementation
2022-06-23 07:11:00 【Houston pineapple】
principle :
Encoder + decoder + The Internet
Realization :
# Code reference official
import numpy as np
import paddle
import paddle.fluid as fluid
from paddle.fluid.dygraph import to_variable
from paddle.fluid.dygraph import Layer
from paddle.fluid.dygraph import Conv2D
from paddle.fluid.dygraph import BatchNorm
from paddle.fluid.dygraph import Pool2D
from paddle.fluid.dygraph import Conv2DTranspose
class Encoder(Layer):
def __init__(self,num_channels,num_filters):
super(Encoder,self).__init__()
self.conv1 = Conv2D(num_channels,
num_filters,
filter_size=3,
stride=1,
padding=1)
self.bn1 = BatchNorm(num_filters,act = 'relu')
self.conv2 = Conv2D(num_filters,
num_filters,
filter_size=3,
stride=1,
padding=1)
self.bn2 = BatchNorm(num_filters,act = 'relu')
self.pool = Pool2D(pool_size=2,pool_stride=2,pool_type='max',ceil_mode=True)
def forward(self,inputs):
x = self.conv1(inputs)
x = self.bn1(x)
x = self.conv2(x)
x = self.bn2(x)
x_pooled = self.pool(x)
return x,x_pooled
class Decoder(Layer):
def __init__(self,num_channels,num_filters):
super(Decoder,self).__init__()
self.up = Conv2DTranspose(num_channels=num_channels,
num_filters=num_filters,
filter_size=2,
stride = 2)
self.conv1 = Conv2D(num_channels,
num_filters,
filter_size=3,
stride=1,
padding=1)
self.bn1 = BatchNorm(num_filters,act = 'relu')
self.conv2 = Conv2D(num_filters,
num_filters,
filter_size=3,
stride=1,
padding=1
)
self.bn2 = BatchNorm(num_filters,act='relu')
def forward(self,inputs_prev,inputs):
x = self.up(inputs)
h_diff = (inputs_prev.shape[2]-x.shape[2])
w_diff = (inputs_prev.shape[3]-x.shape[3])
x = fluid.layers.pad2d(x,paddings=[h_diff//2,h_diff-h_diff//2,w_diff//2,w_diff-w_diff//2])
x = fluid.layers.concat([inputs_prev,x],axis=1)
x = self.conv1(x)
x = self.bn1(x)
x = self.conv2(x)
x = self.bn2(x)
return x
class UNet(Layer):
def __init__(self,num_classes=59):
super(UNet,self).__init__()
self.down1 = Encoder(num_channels=3,num_filters=64)
self.down2 = Encoder(num_channels=64,num_filters=128)
self.down3 = Encoder(num_channels=128,num_filters=256)
self.down4 = Encoder(num_channels=256,num_filters=512)
self.mid_conv1 = Conv2D(512,1024,filter_size=1,padding=0,stride=1)
self.mid_bn1 = BatchNorm(1024,act = 'relu')
self.mid_conv2 = Conv2D(1024,1024,filter_size=1,padding=0,stride=1)
self.mid_bn2 = BatchNorm(1024,act='relu')
self.up4 = Decoder(1024,512)
self.up3 = Decoder(512,256)
self.up2 = Decoder(256,128)
self.up1 = Decoder(128,64)
self.last_conv = Conv2D(num_channels=64,num_filters=num_classes,filter_size=1)
def forward(self,inputs):
x1,x = self.down1(inputs)
print(x1.shape,x.shape)
x2,x = self.down2(x)
print(x2.shape,x.shape)
x3,x = self.down3(x)
print(x3.shape,x.shape)
x4,x = self.down4(x)
print(x4.shape,x.shape)
#middle layers
x = self.mid_conv1(x)
x = self.mid_bn1(x)
x = self.mid_conv2(x)
x = self.mid_bn2(x)
print(x4.shape,x.shape)
x = self.up4(x4,x)
print(x3.shape,x.shape)
x = self.up3(x3,x)
print(x2.shape,x.shape)
x = self.up2(x2,x)
print(x1.shape,x.shape)
x = self.up1(x1,x)
x = self.last_conv(x)
return x
def main():
with fluid.dygraph.guard(fluid.CPUPlace()):
model = UNet(num_classes=59)
x_data = np.random.rand(1,3,123,123).astype(np.float32)
inputs = to_variable(x_data)
pred = model(inputs)
print(pred.shape)
if __name__ == "__main__":
main()
effect

边栏推荐
- [system] right click the desktop icon. After turning around, the Explorer will crash and the desktop will be refreshed
- Eureka
- MySQL basic query
- Analyzing the creation principle in maker Education
- junit单元测试报错org.junit.runners.model.InvalidTestClassError: Invalid test class ‘xxx‘ .No runnable meth
- PSP代码实现
- Detailed explanation of callback function
- Run typescript code directly using TS node
- C language operator priority formula
- asp. Net file download demo and related problems
猜你喜欢
![[daily training] 513 Find the value in the lower left corner of the tree](/img/97/ab2179d6dbd0536e8cc139659aecc2.png)
[daily training] 513 Find the value in the lower left corner of the tree

excel高级绘图技巧100讲(八)-Excel绘制WIFI图

Xxl-sso enables SSO single sign on

WPF command directive and inotifypropertychanged

深度学习系列47:styleGAN总结

MySQL重做日志 redo log

MySQL mvcc multi version concurrency control

Why does TCP protocol shake hands three times instead of two?

产品-Axure9(英文版),原型设计后台动态二级菜单显示内容

406 double pointer (27. remove elements, 977. square of ordered array, 15. sum of three numbers, 18. sum of four numbers)
随机推荐
asp. Net file download demo and related problems
GIS实战应用案例100篇(七十九)-多规整合底图的制作要点
406 double pointer (27. remove elements, 977. square of ordered array, 15. sum of three numbers, 18. sum of four numbers)
[daily training] 513 Find the value in the lower left corner of the tree
897. 递增顺序搜索树
899. ordered queue
deeplab v3 代码结构图
406-双指针(27. 移除元素、977.有序数组的平方、15. 三数之和、18. 四数之和)
云原生落地进入深水区,博云容器云产品族释放四大价值
C DPI adaptation problem
Open source oauth2 framework for SSO single sign on
Quartz调度框架的学习使用
Idea automatically generates serialVersionUID
Xxl-sso enables SSO single sign on
Run typescript code directly using TS node
312. poke the balloon
[STL] summary of pair usage
C # how to obtain DPI and real resolution (can solve the problem that has been 96)
306. 累加数
301. delete invalid brackets