Search code examples
pythontensorflowkerasconv-neural-networkdensenet

How to solve `NameError: name 'compression' is not defined`?


I am trying to implement DenseNet model and I am using image dataset with 4 classes.

Code snippets:

For building model:

def denseblock(input, num_filter = 12, dropout_rate = 0.2):
global compression
temp = input
for _ in range(l): 
    BatchNorm = BatchNormalization()(temp)
    relu = Activation('relu')(BatchNorm)
    Conv2D_3_3 =Conv2D(int(num_filter*compression), (3,3), use_bias=False ,padding='same')(relu)
    if dropout_rate>0:
        Conv2D_3_3 = Dropout(dropout_rate)(Conv2D_3_3)
    concat = Concatenate(axis=-1)([temp,Conv2D_3_3])
    temp = concat
return temp
## transition Block
def transition(input, num_filter = 12, dropout_rate = 0.2):
    global compression
    BatchNorm = BatchNormalization()(input)
    relu = Activation('relu')(BatchNorm)
    Conv2D_BottleNeck = Conv2D(int(num_filter*compression), (1,1), use_bias=False ,padding='same')(relu)
    if dropout_rate>0:
         Conv2D_BottleNeck = Dropout(dropout_rate)(Conv2D_BottleNeck)
    avg = AveragePooling2D(pool_size=(2,2))(Conv2D_BottleNeck)
    return avg
#output layer
def output_layer(input):
    global compression
    BatchNorm = BatchNormalization()(input)
    relu = Activation('relu')(BatchNorm)
    AvgPooling = AveragePooling2D(pool_size=(2,2))(relu)
    flat = Flatten()(AvgPooling)
    output = Dense(categories, activation='softmax')(flat)
    return output

creating a model with the two DenseNet blocks:

l = 7
input = Input(shape=(height, width, 3))
First_Conv2D = Conv2D(30, (3,3), use_bias=False ,padding='same')(input)
First_Block = denseblock(First_Conv2D, 30, 0.5)
First_Transition = transition(First_Block, 30, 0.5)
Last_Block = denseblock(First_Transition, 30, 0.5)
output = output_layer(Last_Block)
model = Model(inputs=[input], outputs=[output])

The error is: NameError: name 'compression' is not defined


Solution

  • I am guessing compression is your bottleneck width, but is does not seem defined, maybe try:

    compression = 4
    def denseblock(input, num_filter = 12, dropout_rate = 0.2):
        temp = input
        for _ in range(l): 
            BatchNorm = layers.BatchNormalization()(temp)
            relu = layers.Activation('relu')(BatchNorm)
            Conv2D_3_3 = layers.Conv2D(int(num_filter*compression), (3,3), use_bias=False ,padding='same')(relu)
            if dropout_rate>0:
                Conv2D_3_3 = layers.Dropout(dropout_rate)(Conv2D_3_3)
            concat = layers.Concatenate(axis=-1)([temp,Conv2D_3_3])
            temp = concat
        return temp
    ## transition Blosck
    def transition(input, num_filter = 12, dropout_rate = 0.2):
        BatchNorm = layers.BatchNormalization()(input)
        relu = layers.Activation('relu')(BatchNorm)
        Conv2D_BottleNeck = layers.Conv2D(int(num_filter*compression), (1,1), use_bias=False ,padding='same')(relu)
        if dropout_rate>0:
             Conv2D_BottleNeck = layers.Dropout(dropout_rate)(Conv2D_BottleNeck)
        avg = layers.AveragePooling2D(pool_size=(2,2))(Conv2D_BottleNeck)
        return avg
    #output layer
    def output_layer(input):
        BatchNorm = layers.BatchNormalization()(input)
        relu = layers.Activation('relu')(BatchNorm)
        AvgPooling = layers.AveragePooling2D(pool_size=(2,2))(relu)
        flat = layers.Flatten()(AvgPooling)
        output = layers.Dense(10, activation='softmax')(flat)
        return output
    
    l = 7
    input = layers.Input(shape=(28, 28, 3,))
    First_Conv2D = layers.Conv2D(30, (3,3), use_bias=False ,padding='same')(input)
    First_Block = denseblock(First_Conv2D, 30, 0.5)
    First_Transition = transition(First_Block, 30, 0.5)
    Last_Block = denseblock(First_Transition, 30, 0.5)
    output = output_layer(Last_Block)
    model = Model(inputs=[input], outputs=[output])