Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'Sequential' object has no attribute '_is_graph_network' when exporting Keras model to TensorFlow

I'm trying to export a Keras model to TensorFlow.

Keras Version 2.1.4 TF Version 1.3.0 Numpy Version 1.13.3

This is the model:

img_width, img_height = 150, 150
batch_size = 32
samples_per_epoch = 1000
validation_steps = 300
nb_filters1 = 32
nb_filters2 = 64
conv1_size = 3
conv2_size = 2
pool_size = 2
classes_num = 3
lr = 0.0004

model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=(img_width, img_height, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Flatten())  # this converts our 3D feature maps to 1D feature vectors
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(1))
model.add(Dense(classes_num, activation='softmax'))

model.compile(loss='categorical_crossentropy',
              optimizer=optimizers.RMSprop(lr=lr),
metrics=['accuracy'])

This is the export code:

from tensorflow.python import keras
estimator_model = keras.estimator.model_to_estimator(keras_model=model)

This is the error:

INFO:tensorflow:Using the Keras model provided. INFO:tensorflow:Using default config. WARNING:tensorflow:Using temporary folder as model directory: /home/dsxuser/.tmp/tmpbgYQQa INFO:tensorflow:Using config: {'_save_checkpoints_secs': 600, '_session_config': None, '_keep_checkpoint_max': 5, '_task_type': 'worker', '_global_id_in_cluster': 0, '_is_chief': True, '_cluster_spec': , '_evaluation_master': '', '_save_checkpoints_steps': None, '_keep_checkpoint_every_n_hours': 10000, '_service': None, '_num_ps_replicas': 0, '_tf_random_seed': None, '_master': '', '_num_worker_replicas': 1, '_task_id': 0, '_log_step_count_steps': 100, '_model_dir': '/home/dsxuser/.tmp/tmpbgYQQa', '_save_summary_steps': 100}

AttributeErrorTraceback (most recent call last) in () 1 from tensorflow.python import keras ----> 2 estimator_model = keras.estimator.model_to_estimator(keras_model=model)

/opt/conda/envs/DSX-Python27/lib/python2.7/site-packages/tensorflow/python/keras/_impl/keras/estimator.pyc in model_to_estimator(keras_model, keras_model_path, custom_objects, model_dir, config) 476 477 keras_weights = keras_model.get_weights() --> 478 if keras_model._is_graph_network: 479 # TODO(yifeif): move checkpoint initialization to scaffold.init_fn 480 _save_first_checkpoint(keras_model,

AttributeError: 'Sequential' object has no attribute '_is_graph_network'

Any ideas?

like image 244
Romeo Kienzler Avatar asked Apr 12 '18 16:04

Romeo Kienzler


1 Answers

You need this from tensorflow.python.keras import Sequential you should use keras api implemented in tensorflow instead of using keras api directly.

like image 119
Guozhen Avatar answered Nov 14 '22 23:11

Guozhen