Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

my versions are:

pip install q tensorflow==2.1

pip install q keras==2.3.1

pip install imgaug

pip install -U segmentation-models

I am using UNET using dense block instead of convulational layer with dilated spatial pooling layer in bottlenack layer. but i am getting ModuleNotFoundError: No module named 'tensorflow.python.keras.engine.base_layer_v1'

like image 351
keshav swami Avatar asked Aug 20 '20 16:08

keshav swami


3 Answers

I came across similar error some time back and resolved this by importing all modules from tensorflow.

Please refer working code in below

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()
like image 57
TFer2 Avatar answered Oct 04 '22 21:10

TFer2


Try this command, I had the same error:

%env SM_FRAMEWORK=tf.keras
pip install keras==2.3.1
like image 32
Om. Sa223 Avatar answered Oct 04 '22 21:10

Om. Sa223


I resolved this error by installing latest version of tensorflow and keras.

pip install tensorflow==2.4.1
pip install keras==2.4.3
like image 23
Srikanth Reddy Avatar answered Oct 04 '22 21:10

Srikanth Reddy