forked from LongxingTan/Time-series-prediction
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunet.py
89 lines (66 loc) · 2.94 KB
/
unet.py
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
79
80
81
82
83
84
85
86
87
88
# -*- coding: utf-8 -*-
# @author: Longxing Tan, [email protected]
# @date: 2020-03
# paper:
# other implementations: https://www.kaggle.com/super13579/u-net-1d-cnn-with-pytorch
# https://www.kaggle.com/kmat2019/u-net-1d-cnn-with-keras
import tensorflow as tf
from tensorflow.keras.layers import (Input, AveragePooling1D, Add, UpSampling1D, Concatenate, Lambda)
from deepts.layers.unet_layer import *
params={}
class Unet(object):
def __init__(self, custom_model_params):
self.params=params.update(custom_model_params)
self.AvgPool1D1 = AveragePooling1D(pool_size=2)
self.AvgPool1D2 = AveragePooling1D(pool_size=4)
self.encoder = Encoder()
self.decoder = Decoder()
def __call__(self, x, predict_seq_length, training=True):
pool1=self.AvgPool1D1(x)
pool2=self.AvgPool1D2(x)
encoder_output=self.encoder([x,pool1,pool2])
decoder_output=self.decoder(encoder_output, predict_seq_length=predict_seq_length)
return decoder_output
class Encoder(object):
def __init__(self):
pass
def __call__(self, input_tensor,units=64,kernel_size=2,depth=2):
x,pool1,pool2=input_tensor
x = conv_br(x, units, kernel_size, 1, 1) # => batch_size * sequence_length * units
for i in range(depth):
x = re_block(x, units, kernel_size, 1, 1)
out_0 = x # => batch_size * sequence_length * units
x = conv_br(x, units * 2, kernel_size, 2, 1)
for i in range(depth):
x = re_block(x, units * 2, kernel_size, 1,1)
out_1 = x # => batch_size * sequence/2 * units*2
x = Concatenate()([x, pool1])
x = conv_br(x, units * 3, kernel_size, 2, 1)
for i in range(depth):
x = re_block(x, units * 3, kernel_size, 1,1)
out_2 = x # => batch_size * sequence/2, units*3
x = Concatenate()([x, pool2])
x = conv_br(x, units * 4, kernel_size, 4, 1)
for i in range(depth):
x = re_block(x, units * 4, kernel_size, 1,1)
return [out_0,out_1,out_2, x]
class Decoder(object):
def __init__(self):
pass
def __call__(self, input_tensor, units=64, kernel_size=2, predict_seq_length=1):
out_0,out_1,out_2,x=input_tensor
x = UpSampling1D(4)(x)
x = Concatenate()([x, out_2])
x = conv_br(x, units * 3, kernel_size, 1, 1)
x = UpSampling1D(2)(x)
x = Concatenate()([x, out_1])
x = conv_br(x, units * 2, kernel_size, 1, 1)
x = UpSampling1D(2)(x)
x = Concatenate()([x, out_0])
x = conv_br(x, units, kernel_size, 1, 1)
# regression
x = Conv1D(1, kernel_size=kernel_size, strides=1, padding="same")(x)
out = Activation("sigmoid")(x)
out = Lambda(lambda x: 12*x)(out)
out = AveragePooling1D(strides=4)(out) # Todo: just a tricky way to change the batch*input_seq*1 -> batch_out_seq*1, need a more general way
return out