I'm using macOS terminal to run train.py file, which can be found in this Github link but I keep getting the following error:
Arabic-NER $ python train.py
Using TensorFlow backend.
Loading Word Embedding model...
loaded model in 368.90753722190857 seconds
2019-12-18 21:11:19.329197: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x7fb88580c330 executing computations on platform Host. Devices:
2019-12-18 21:11:19.329702: I tensorflow/compiler/xla/service/service.cc:175] StreamExecutor device (0): Host, Default Version
Traceback (most recent call last):
File "train.py", line 130, in <module>
train_model, crf_layer = model.build_model()
File "/Name Entity Recognition /HassanAzzam/Arabic-NER/model.py", line 21, in build_model
output_layer = crf_layer(bi_gru)
File "/Name Entity Recognition /HassanAzzam/Arabic-NER/env/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 75, in symbolic_fn_wrapper
return func(*args, **kwargs)
File "/Name Entity Recognition /HassanAzzam/Arabic-NER/env/lib/python3.6/site-packages/keras/engine/base_layer.py", line 475, in __call__
previous_mask = _collect_previous_mask(inputs)
File "/Name Entity Recognition /HassanAzzam/Arabic-NER/env/lib/python3.6/site-packages/keras/engine/base_layer.py", line 1441, in _collect_previous_mask
mask = node.output_masks[tensor_index]
AttributeError: 'Node' object has no attribute 'output_masks'
Also, here's model.py:
import tensorflow.keras
from tensorflow.keras.layers import Dense, Input, GRU, Embedding, Dropout, Activation, Masking
from tensorflow.keras.layers import Bidirectional, GlobalMaxPool1D, TimeDistributed
from tensorflow.keras.models import Model, Sequential
from keras_contrib.layers import CRF
def build_model():
crf_layer = CRF(9)
input_layer = Input(shape=(None,300,))
# embedding = Embedding(212, 20, input_length=None, mask_zero=True)(input_layer)
mask_layer = Masking(mask_value=0., input_shape=(212, 300))(input_layer)
bi_gru = Bidirectional(GRU(10, return_sequences=True))(mask_layer)
bi_gru = TimeDistributed(Dense(10, activation="relu"))(bi_gru)
output_layer = crf_layer(bi_gru)
return Model(input_layer, output_layer), crf_layer
I'm using TensorFlow 2.0 and Python 3.6.6.
Note: in the train.py file, in line 8, I have added tensorflow before keras.
So, it's from tensorflow.keras.utils import to_categorical.
The full Github repository can be found here
I solved the problem by installing the following versions:
Seqeval:
pip install seqeval==0.0.5
Keras:
pip install keras==2.2.4
Tensorflow:
pip install tensorflow==1.14.0
I also added from keras.models import * in the beginning of each file
Related
I have trained a model on google colab by adding a layer to resnet. Here is the model:
import tensorflow_hub as hub # Provides pretrained models
resnet_url = "https://tfhub.dev/google/imagenet/resnet_v2_50/feature_vector/5"
# Download ResNet model and save as Keras layer
# Trainable : False means we don't want to train it further
resnet_layer = hub.KerasLayer(resnet_url,
trainable=False,
input_shape=(256,256,3))
# Create model
resnet_model=tf.keras.Sequential([
# Puts images through downloaded model first
resnet_layer,
# Define we will use 20 classes
Dense(2,
activation="softmax")
])
It works fine on colab notebook. But when I export and want to deploy it into a flask service using the code below:
import base64
import numpy as np
import io
import os
from PIL import Image
import keras
from keras import backend as K
from keras.models import Sequential
from keras.models import load_model
from keras.preprocessing.image import ImageDataGenerator
#from keras.preprocessing.image import img_to_array
import tensorflow as tf
import tensorflow_hub as hub
from tensorflow.keras.utils import img_to_array
from flask import request
from flask import jsonify
from flask import Flask
app = Flask(__name__)
def get_model():
global model
model = load_model('resnet_model_1.h5')
print(" * Model loaded!")
def preprocess_image(image, target_size):
if image.mode != "RGB":
image = image.convert("RGB")
image = image.resize(target_size)
image = img_to_array(image)
image = np.expand_dims(image, axis=0)
return image
print(" * Loading Keras model...")
get_model()
#app.route("/predict", methods=["POST"])
def predict():
message = request.get_json(force=True)
encoded = message['image']
decoded = base64.b64decode(encoded)
image = Image.open(io.BytesIO(decoded))
processed_image = preprocess_image(image, target_size=(256, 256))
prediction = model.predict(processed_image).tolist()
response = {
'prediction': {
'dog': prediction[0][0],
'cat': prediction[0][1]
}
}
return jsonify(response)
I get this nasty error:
Traceback (most recent call last):
File "/usr/local/bin/flask", line 8, in <module>
sys.exit(main())
File "/usr/local/lib/python3.8/dist-packages/flask/cli.py", line 988, in main
cli.main()
File "/usr/local/lib/python3.8/dist-packages/flask/cli.py", line 579, in main
return super().main(*args, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/click/core.py", line 1055, in main
rv = self.invoke(ctx)
File "/usr/local/lib/python3.8/dist-packages/click/core.py", line 1657, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/usr/local/lib/python3.8/dist-packages/click/core.py", line 1404, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/usr/local/lib/python3.8/dist-packages/click/core.py", line 760, in invoke
return __callback(*args, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/click/decorators.py", line 84, in new_func
return ctx.invoke(f, obj, *args, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/click/core.py", line 760, in invoke
return __callback(*args, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/flask/cli.py", line 850, in run_command
app = DispatchingApp(info.load_app, use_eager_loading=eager_loading)
File "/usr/local/lib/python3.8/dist-packages/flask/cli.py", line 299, in __init__
self._load_unlocked()
File "/usr/local/lib/python3.8/dist-packages/flask/cli.py", line 333, in _load_unlocked
self._app = rv = self.loader()
File "/usr/local/lib/python3.8/dist-packages/flask/cli.py", line 389, in load_app
app = locate_app(import_name, name)
File "/usr/local/lib/python3.8/dist-packages/flask/cli.py", line 234, in locate_app
__import__(module_name)
File "/home/pc3/dev/flaskservice/predict_app.py", line 39, in <module>
get_model()
File "/home/pc3/dev/flaskservice/predict_app.py", line 26, in get_model
model = load_model('resnet_model_1.h5')
File "/usr/local/lib/python3.8/dist-packages/keras/utils/traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "/usr/local/lib/python3.8/dist-packages/keras/utils/generic_utils.py", line 562, in class_and_config_for_serialized_keras_object
raise ValueError(
ValueError: Unknown layer: KerasLayer. Please ensure this object is passed to the `custom_objects` argument. See https://www.tensorflow.org/guide/keras/save_and_serialize#registering_the_custom_object for details.
my tensorflow version on colab is 2.8.2 and my local ubuntu machine on which I deply the model it is 2.9.1.
I read here that this error has been resolved by passing ,custom_objects={'KerasLayer':hub.KerasLayer} to load_model. But for me this results in all images are classifed as cat. So clearly it messes up somthing in the model.
So I'm left clueless and appreciate your hints.
Please ensure that these lines are added before calling your inference, as you need to call an argmax to specify which category is the dominant one in your prediction.
list_of_categories = ["dog", "cat"]
pred = list_of_categories[np.argmax(prediction)]
response = pred
I'm implementing SRGAN (and am not very experienced in this field), which uses a pre-trained VGG19 model to extract features. The following code was working fine on Keras 2.1.2 and tf 1.15.0 till yesterday. then it started throwing an "AttributeError: module 'keras.utils.generic_utils' has no attribute 'populate_dict_with_module_objects'" So i updated the keras version to 2.4.3 and tf to 2.5.0. but then its showing a "Input 0 of layer fc1 is incompatible with the layer: expected axis -1 of input shape to have value 25088 but received input with shape (None, 32768)" on the following line
features = vgg(input_layer)
But here the input has to be (256,256,3).
I had downgraded the keras and tf versions to the one I mentioned before to get rid of this error in the first place and it was working well till yesterday.
changing the input shape to (224,224,3) does not work. Any help in solving this error will be very appreciated.
import glob
import time
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
import keras
from keras.layers import Input
from keras.applications.vgg19 import VGG19
from keras.callbacks import TensorBoard
from keras.layers import BatchNormalization, Activation, LeakyReLU, Add, Dense,Flatten
from keras.layers.convolutional import Conv2D, UpSampling2D
from keras.models import Model
from keras.optimizers import Adam
from scipy.misc import imread, imresize
from PIL import Image
def build_vgg():
input_shape = (256, 256, 3)
vgg = VGG19(weights="imagenet")
vgg.outputs = [vgg.layers[9].output]
input_layer = Input(shape=input_shape)
features = vgg(input_layer)
model = Model(inputs=[input_layer], outputs=[features])
return model
vgg = build_vgg()
vgg.trainable = False
vgg.compile(loss='mse', optimizer=common_optimizer, metrics=['accuracy'])
# Build and compile the discriminator
discriminator = build_discriminator()
discriminator.compile(loss='mse', optimizer=common_optimizer, metrics=['accuracy'])
# Build the generator network
generator = build_generator()
The Error message
Im using google colab
Importing keras from tensorflow and setting include_top=False in
vgg = VGG19(weights="imagenet",include_top=False)
seems to work.
I am building a neural network using keras and tensorflow and I get a error at this place
def create_model():
model = Sequential()
model.add(Dense(4, input_dim=2, kernel_initializer='normal', activation='tanh'))
model.add(Dense(6, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer=RAdam(learning_rate), metrics=['accuracy'])
return model
model = create_model()
And I get the following error when I run my code in jupyter notebook,
TypeError Traceback (most recent call last)
<ipython-input-14-2358feb9246f> in <module>
1 # make a shallow neural network
----> 2 model = create_model()
3 model.summary()
<ipython-input-13-7c6ab8b2130e> in create_model()
10
11 # Compile model
---> 12 model.compile(loss='binary_crossentropy', optimizer=RAdam(learning_rate), metrics=['accuracy'])
13 return model
~\anaconda3\envs\tf\lib\site-packages\keras_radam\optimizers.py in __init__(self, learning_rate, beta_1, beta_2, epsilon, decay, weight_decay, amsgrad, total_steps, warmup_proportion, min_lr, **kwargs)
32 total_steps=0, warmup_proportion=0.1, min_lr=0., **kwargs):
33 learning_rate = kwargs.pop('learning_rate', learning_rate)
---> 34 super(RAdam, self).__init__(**kwargs)
35 with K.name_scope(self.__class__.__name__):
36 self.iterations = K.variable(0, dtype='int64', name='iterations')
TypeError: __init__() missing 1 required positional argument: 'name'
And these are the imports I have used for my code to run. I think I have most of the codes imported to build a shallow neural network
import numpy as np
import keras
import tensorflow as tf
from keras.models import Sequential
from keras.layers import Dense
from keras import backend as K
from keras.wrappers.scikit_learn import KerasClassifier
from keras_radam import RAdam
For others who may be looking for another solution.
RAdam is not in tensorflow.keras.optimizers and neither in keras by default, but in tensorflow-addons package, which is a better alternative (IMHO) than the external keras_radam library, considerably less prone to errors.
What you are looking for is here: https://www.tensorflow.org/addons/api_docs/python/tfa/optimizers/RectifiedAdam
#pip install tensorflow-addons
import tensorflow_addons as tfa
optimizer = tfa.optimizers.RectifiedAdam(lr=1e-3)
I was able to reproduce your problem. It happened when you have tf. keras but you load keras-radam with old keras. But this implementation supports both versions of keras or tf. keras. To use it with the new version, as also mentioned here, all you need to do as follows:
import os
os.environ['TF_KERAS']='1'
from keras_radam import RAdam
The package will choose the tf. keras compatible version of RAdam()
from .backend import TF_KERAS
__all__ = ['RAdam']
if TF_KERAS:
from .optimizer_v2 import RAdam
else:
from .optimizers import
So, RAdam() will be imported from this script. But there is another issue. In the very latest version of tf, the following import has been updated
# from
from tensorflow.python import os, math_ops, state_ops, control_flow_ops
# to
from tensorflow.python.ops import math_ops, state_ops, control_flow_ops
From this point, you need to modify this import from the source script and it will solve this issue. Just modify the source script by replacing the above imports.
from keras import Sequential
from keras.layers import Dense
def create_model():
model = Sequential()
model.add(Dense(4, input_dim=2, kernel_initializer='normal', activation='tanh'))
model.add(Dense(6, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy',
optimizer=RAdam(learning_rate),
metrics=['accuracy'])
return model
model = create_model()
I am going to train my model quantization aware. However, when i use it , the tensorflow_model_optimization cannot quantize tf.reshape function , and throws an error.
tensorflow version : '2.4.0-dev20200903'
python version : 3.6.9
the code:
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '3'
from tensorflow.keras.applications import VGG16
import tensorflow_model_optimization as tfmot
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
quantize_model = tfmot.quantization.keras.quantize_model
inputs = keras.Input(shape=(784,))
# img_inputs = keras.Input(shape=(32, 32, 3))
dense = layers.Dense(64, activation="relu")
x = dense(inputs)
x = layers.Dense(64, activation="relu")(x)
outputs = layers.Dense(10)(x)
outputs = tf.reshape(outputs, [-1, 2, 5])
model = keras.Model(inputs=inputs, outputs=outputs, name="mnist_model")
# keras.utils.plot_model(model, "my_first_model.png")
q_aware_model = quantize_model(model)
and the output:
Traceback (most recent call last):
File "<ipython-input-39-af601b78c010>", line 14, in <module>
q_aware_model = quantize_model(model)
File "/home/essys/.local/lib/python3.6/site-packages/tensorflow_model_optimization/python/core/quantization/keras/quantize.py", line 137, in quantize_model
annotated_model = quantize_annotate_model(to_quantize)
File "/home/essys/.local/lib/python3.6/site-packages/tensorflow_model_optimization/python/core/quantization/keras/quantize.py", line 210, in quantize_annotate_model
to_annotate, input_tensors=None, clone_function=_add_quant_wrapper)
...
File "/home/essys/anaconda3/envs/tf_gpu/lib/python3.6/site-packages/tensorflow/python/autograph/impl/api.py", line 667, in wrapper
raise e.ag_error_metadata.to_exception(e)
TypeError: in user code:
TypeError: tf__call() got an unexpected keyword argument 'shape'
If somebody know, please help ?
The reason behind is because your layer is not yet support for QAT at the moment. If you want to quantize it, you have to self writing your quantization by quantize_annotate_layer and pass it through quantize_scope and apply to your model by quantize_apply as describe in here: https://www.tensorflow.org/model_optimization/guide/quantization/training_comprehensive_guide?hl=en#quantize_custom_keras_layer
I have create a batch_norm_layer in here as an example
Tensorflow 2.x is not complete for QAT layer, pls consider using tf1.x by adding FakeQuant after operators.
Trying to load a Keras model using tf.keras.models.load_model I get the following error:
import tensorflow as tf
from tensorflow_addons.optimizers import RectifiedAdam
model = tf.keras.models.load_model('model', custom_objects = {'RectifiedAdam' : RectifiedAdam})
Error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/mylib/python3.7/site-packages/tensorflow_core/python/keras/saving/save.py", line 150, in load_model
return saved_model_load.load(filepath, compile)
File "/mylib/python3.7/site-packages/tensorflow_core/python/keras/saving/saved_model/load.py", line 99, in load
training_config))
File "/mylib/python3.7/site-packages/tensorflow_core/python/keras/saving/saving_utils.py", line 229, in compile_args_from_training_config
optimizer_config, custom_objects=custom_objects)
File "/mylib/python3.7/site-packages/tensorflow_core/python/keras/optimizers.py", line 819, in deserialize
printable_module_name='optimizer')
File "/mylib/python3.7/site-packages/tensorflow_core/python/keras/utils/generic_utils.py", line 292, in deserialize_keras_object
config, module_objects, custom_objects, printable_module_name)
File "/mylib/python3.7/site-packages/tensorflow_core/python/keras/utils/generic_utils.py", line 250, in class_and_config_for_serialized_keras_object
raise ValueError('Unknown ' + printable_module_name + ': ' + class_name)
ValueError: Unknown optimizer: RectifiedAdam
I can load the model with compile to False tf.keras.models.load_model('model', compile=False) and then compile it again with RectifiedAdam optimizer (as suggested here: https://stackoverflow.com/a/56565801) - however that is not ideal...
So any ideas on, what I'm doing wrong?
One quick hack around this is to manually assign RectifiedAdam to an object in the Tensorflow namespace:
import tensorflow as tf
from tensorflow_addons.optimizers import RectifiedAdam
tf.keras.optimizers.RectifiedAdam = RectifiedAdam
...
or do something like this:
models.load_model('myModel.h5', custom_objects={'MyOptimizer': MyOptimizer})
The model was trained on a tensorflow (Keras) version higher than the one you want to use to load the trained model. Find the compatible version of TensorFlow (Keras).