yu4u / convnet-drawer

Python script for illustrating Convolutional Neural Networks (CNN) using Keras-like model definitions
MIT License
596 stars 98 forks source link

convert_drawer_model fail when GlobalAveragePooling2D layer is included #11

Closed xsthunder closed 5 years ago

xsthunder commented 5 years ago

Hi there, I have come across some problem. convert_drawer_model from keras_util fail when GlobalAveragePooling2D layer is included. But it is said here that GlobalAveragePooling2D is supported. Thanks in advance.

update

fix this problem. see pr #12

my code

from keras_util import convert_drawer_model
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Dense,Dropout,Activation,Flatten
from keras.layers import Conv2D,MaxPool2D,GlobalAveragePooling2D,AveragePooling2D
from keras import optimizers
from keras.callbacks import ReduceLROnPlateau,EarlyStopping
from keras.layers.normalization import BatchNormalization
from keras.regularizers import l2

batch_size = 128
epochs = 30
num_classes = 10
weight_decay = 1e-6
nets = 15

model = Sequential()

model.add(Conv2D(192,(5,5),padding='same',kernel_regularizer=l2(weight_decay),kernel_initializer='he_normal',input_shape=(28,28,1)))
#model.add(BatchNormalization())
#model.add(Activation('relu'))
model.add(Conv2D(160,(1,1),padding='same',kernel_regularizer=l2(weight_decay),kernel_initializer='he_normal'))
#model.add(BatchNormalization())
#model.add(Activation('relu'))
model.add(Conv2D(96,(1,1),padding='same',kernel_regularizer=l2(weight_decay),kernel_initializer='he_normal'))
#model.add(BatchNormalization())
#model.add(Activation('relu'))
model.add(MaxPool2D(pool_size=(3,3),strides=(2,2),padding='same'))

#model.add(Dropout(0.2))

model.add(Conv2D(192,(5,5),padding='same',kernel_regularizer=l2(weight_decay),kernel_initializer='he_normal'))
#model.add(BatchNormalization())
#model.add(Activation('relu'))
model.add(Conv2D(192,(1,1),padding='same',kernel_regularizer=l2(weight_decay),kernel_initializer='he_normal'))
#model.add(BatchNormalization())
#model.add(Activation('relu'))
model.add(Conv2D(192,(1,1),padding='same',kernel_regularizer=l2(weight_decay),kernel_initializer='he_normal'))
#model.add(BatchNormalization())
#model.add(Activation('relu'))
model.add(MaxPool2D(pool_size=(3,3),strides=(2,2),padding='same'))

#model.add(Dropout(0.2))

model.add(Conv2D(192,(3,3),padding='same',kernel_regularizer=l2(weight_decay),kernel_initializer='he_normal'))
#model.add(BatchNormalization())
#model.add(Activation('relu'))
model.add(Conv2D(192,(1,1),padding='same',kernel_regularizer=l2(weight_decay),kernel_initializer='he_normal'))
#model.add(BatchNormalization())
#model.add(Activation('relu'))
model.add(Conv2D(10,(1,1),padding='same',kernel_regularizer=l2(weight_decay),kernel_initializer='he_normal'))
#model.add(BatchNormalization())
#model.add(Activation('relu'))  

model.add(GlobalAveragePooling2D())
#model.add(Activation('softmax'))

#adam = optimizers.rmsprop()
#model.compile(loss='categorical_crossentropy',optimizer=adam,metrics=['accuracy'])

Error

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-19-677489d1360a> in <module>
----> 1 convert_drawer_model(model).save_fig('tmp.svg')

~/python_lib/convnet_drawer/keras_util.py in convert_drawer_model(model)
     36         class_config = config.get("config", False)
     37         if class_name and class_config:
---> 38             class_obj = is_class_object(class_name)
     39             if class_name == "Conv2D":
     40                 conv_2d = get_conv2d_obj(class_obj, class_config)

~/python_lib/convnet_drawer/keras_util.py in is_class_object(class_name)
     26 
     27 def is_class_object(class_name):
---> 28     return eval(class_name)
     29 
     30 

~/python_lib/convnet_drawer/keras_util.py in <module>

NameError: name 'GlobalAveragePooling2D' is not defined
yu4u commented 5 years ago

@xsthunder Thx for that PR! I merged it.