gpt4 book ai didi

tensorflow - ModuleNotFoundError : No module named 'tensorflow. python.keras.engine.base_layer_v1

转载 作者:行者123 更新时间:2023-12-04 11:30:29 28 4
gpt4 key购买 nike

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()

我的版本是:
pip 安装 q tensorflow ==2.1
pip install q keras==2.3.1
pip 安装 imgaug
pip install -U 分段模型
我正在使用使用密集块的 UNET,而不是在瓶颈层中使用膨胀空间池化层的卷积层。
但我收到 ModuleNotFoundError: No module named 'tensorflow.python.keras.engine.base_layer_v1'

最佳答案

前段时间我遇到了类似的错误,并通过从 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com