Tuesday, May 9, 2017

z = keras.layers.add([x, y]) NameError: name 'keras' is not defined

https://keras.io/getting-started/functional-api-guide/#more-examples

from keras.layers import Conv2D, MaxPooling2D, Input
#import keras

input_img = Input(shape=(3, 256, 256))

tower_1 = Conv2D(64, (1, 1), padding='same', activation='relu')(input_img)
tower_1 = Conv2D(64, (3, 3), padding='same', activation='relu')(tower_1)

tower_2 = Conv2D(64, (1, 1), padding='same', activation='relu')(input_img)
tower_2 = Conv2D(64, (5, 5), padding='same', activation='relu')(tower_2)

tower_3 = MaxPooling2D((3, 3), strides=(1, 1), padding='same')(input_img)
tower_3 = Conv2D(64, (1, 1), padding='same', activation='relu')(tower_3)

output = keras.layers.concatenate([tower_1, tower_2, tower_3], axis=1)


$ python concat.py Using TensorFlow backend.
Traceback (most recent call last):
  File "concat.py", line 15, in
    output = keras.layers.concatenate([tower_1, tower_2, tower_3], axis=1)
NameError: name 'keras' is not defined

#######################################
>>> keras
Traceback (most recent call last):
  File "", line 1, in
NameError: name 'keras' is not defined
>>> import keras
>>> keras
>>> 
#######################################
# Fix

from keras.layers import Conv2D, MaxPooling2D, Input
import keras # <<<<< Fix

input_img = Input(shape=(3, 256, 256))

tower_1 = Conv2D(64, (1, 1), padding='same', activation='relu')(input_img)
tower_1 = Conv2D(64, (3, 3), padding='same', activation='relu')(tower_1)

tower_2 = Conv2D(64, (1, 1), padding='same', activation='relu')(input_img)
tower_2 = Conv2D(64, (5, 5), padding='same', activation='relu')(tower_2)

tower_3 = MaxPooling2D((3, 3), strides=(1, 1), padding='same')(input_img)
tower_3 = Conv2D(64, (1, 1), padding='same', activation='relu')(tower_3)

output = keras.layers.concatenate([tower_1, tower_2, tower_3], axis=1)