作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
from keras import backend as K
from tensorflow.keras.layers import MaxPooling2D,Conv2D,Input,Add,Flatten,AveragePooling2D,Dense,BatchNormalization,ZeroPadding2D,Activation
from tensorflow.keras.models import Model
def Dense_Layer(x,k):
x = BatchNormalization(axis = 3)(x)
x = Activation('relu')(x)
x = Conv2D(4*k,(1,1),strides = (1,1))(x)
x = BatchNormalization(axis = 3)(x)
x = Activation('relu')(x)
x = Conv2D(k,(1,1),strides = (1,1))(x)
return x
def Dense_Block(x,k):
x1 = Dense_Layer(x,k)
x1_add = keras.layers.Concatenate()([x1,x])
x2 = Dense_Layer(x1_add,k)
x2_add = keras.layers.Concatenate()([x1,x2])
return x2_add
def Dilated_Spatial_Pyramid_Pooling(x,k):
x = BatchNormalization(axis = 3)(x)
d1 = Conv2D(k, (1,1), dilation_rate = 2)(x)
d2 = Conv2D(k, (1,1), dilation_rate = 4)(d1)
d3 = Conv2D(k, (1,1), dilation_rate = 8)(d2)
d4 = Conv2D(k, (1,1), dilation_rate = 16)(d3)
c = keras.layers.Concatenate()([d1,d2,d3,d4])
return c
def down_block(x,filters, kernel_size = (3, 3), padding = "same",strides =1 ):
c = Dense_Block(x,filters)
c = Dense_Block(c,filters)
p = keras.layers.MaxPool2D((2,2),(2,2))(c)
return c,p
def up_block(x,skip,filters, kernel_size = (3, 3), padding = "same",strides =1 ):
us = keras.layers.UpSampling2D((2,2))(x)
concat = keras.layers.Concatenate()([us,skip])
c = Dense_Block(concat,filters)
c = Dense_Block(c,filters)
return c
def bottleneck(x,filters, kernel_size = (3, 3), padding = "same",strides =1 ):
c = Dense_Block(x,filters)
c = Dense_Block(c,filters)
c = Dilated_Spatial_Pyramid_Pooling(c,filters)
return c
def UNet():
f = [32,64,128,256]
input = keras.layers.Input((128,128,1))
p0 = input
c1,p1 = down_block(p0,f[0])
c2,p2 = down_block(p1,f[1])
c3,p3 = down_block(p2,f[2])
bn = bottleneck(p3,f[3])
u1 = up_block(bn,c3,f[2])
u2 = up_block(u1,c2,f[1])
u3 = up_block(u2,c1,f[0])
outputs = keras.layers.Conv2D(1,(1,1),padding= "same",activation = "sigmoid")(u3)
model = keras.models.Model(input,outputs)
return model
model=UNet()
model.summary()
我的版本是:
最佳答案
前段时间我遇到了类似的错误,并通过从 tensorflow 导入所有模块解决了这个问题。
请引用下面的工作代码
from tensorflow.keras.layers import MaxPooling2D,Conv2D,Input,Add,MaxPool2D,Flatten,AveragePooling2D,Dense,BatchNormalization,ZeroPadding2D,Activation,Concatenate,UpSampling2D
from tensorflow.keras.models import Model
def Dense_Layer(x,k):
x = BatchNormalization(axis = 3)(x)
x = Activation('relu')(x)
x = Conv2D(4*k,(1,1),strides = (1,1))(x)
x = BatchNormalization(axis = 3)(x)
x = Activation('relu')(x)
x = Conv2D(k,(1,1),strides = (1,1))(x)
return x
def Dense_Block(x,k):
x1 = Dense_Layer(x,k)
x1_add = Concatenate()([x1,x])
x2 = Dense_Layer(x1_add,k)
x2_add = Concatenate()([x1,x2])
return x2_add
def Dilated_Spatial_Pyramid_Pooling(x,k):
x = BatchNormalization(axis = 3)(x)
d1 = Conv2D(k, (1,1), dilation_rate = 2)(x)
d2 = Conv2D(k, (1,1), dilation_rate = 4)(d1)
d3 = Conv2D(k, (1,1), dilation_rate = 8)(d2)
d4 = Conv2D(k, (1,1), dilation_rate = 16)(d3)
c = Concatenate()([d1,d2,d3,d4])
return c
def down_block(x,filters, kernel_size = (3, 3), padding = "same",strides =1 ):
c = Dense_Block(x,filters)
c = Dense_Block(c,filters)
p = MaxPool2D((2,2),(2,2))(c)
return c,p
def up_block(x,skip,filters, kernel_size = (3, 3), padding = "same",strides =1 ):
us = UpSampling2D((2,2))(x)
concat = Concatenate()([us,skip])
c = Dense_Block(concat,filters)
c = Dense_Block(c,filters)
return c
def bottleneck(x,filters, kernel_size = (3, 3), padding = "same",strides =1 ):
c = Dense_Block(x,filters)
c = Dense_Block(c,filters)
c = Dilated_Spatial_Pyramid_Pooling(c,filters)
return c
def UNet():
f = [32,64,128,256]
input = Input((128,128,1))
p0 = input
c1,p1 = down_block(p0,f[0])
c2,p2 = down_block(p1,f[1])
c3,p3 = down_block(p2,f[2])
bn = bottleneck(p3,f[3])
u1 = up_block(bn,c3,f[2])
u2 = up_block(u1,c2,f[1])
u3 = up_block(u2,c1,f[0])
outputs = Conv2D(1,(1,1),padding= "same",activation = "sigmoid")(u3)
model = Model(input,outputs)
return model
model=UNet()
model.summary()
关于tensorflow - ModuleNotFoundError : No module named 'tensorflow. python.keras.engine.base_layer_v1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63509657/
我是一名优秀的程序员,十分优秀!