ModuleNotFoundError:没有名为“tensorflow.python.keras.engine.base_layer_v1”的模块

Posted

技术标签:

【中文标题】ModuleNotFoundError:没有名为“tensorflow.python.keras.engine.base_layer_v1”的模块【英文标题】:ModuleNotFoundError: No module named 'tensorflow.python.keras.engine.base_layer_v1 【发布时间】:2020-12-10 01:27:39 【问题描述】:
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 install 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'

【问题讨论】:

可以添加完整的回溯吗? 【参考方案1】:

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

【讨论】:

这个确实有效。但我想知道为什么?【参考方案2】:

试试这个命令,我有同样的错误:

%env SM_FRAMEWORK=tf.keras
pip install keras==2.3.1

【讨论】:

点评来源: 你能解释一下为什么这段代码有效吗?【参考方案3】:

我通过安装最新版本的 tensorflow 和 keras 解决了这个错误。

pip install tensorflow==2.4.1
pip install keras==2.4.3

【讨论】:

以上是关于ModuleNotFoundError:没有名为“tensorflow.python.keras.engine.base_layer_v1”的模块的主要内容,如果未能解决你的问题,请参考以下文章

ModuleNotFoundError:没有名为“dataviz”的模块

ModuleNotFoundError:没有名为“numpy”的模块

错误:ModuleNotFoundError:没有名为“图像”的模块

ModuleNotFoundError:没有名为“knox”的模块

ModuleNotFoundError:没有名为“memoize”的模块

ModuleNotFoundError:没有名为“pssh”的模块