Attribute Error: module 'tensorflow' has no attribute 'contrib' - python

I am trying to load the tensorflow zoo model but I encountered this error and I am not able to fix this I am new to ai/ml. This is the code for loading the zoo model:
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # Suppress TensorFlow logging
import tensorflow as tf
from object_detection.utils import label_map_util
from object_detection.utils import config_util
from object_detection.utils import visualization_utils as viz_utils
from object_detection.builders import model_builder
tf.get_logger().setLevel('ERROR') # Suppress TensorFlow logging (2)
# Enable GPU dynamic memory allocation
gpus = tf.config.experimental.list_physical_devices('GPU')
for gpu in gpus:
tf.config.experimental.set_memory_growth(gpu, True)
# Load pipeline config and build a detection model
configs = config_util.get_configs_from_pipeline_file(PATH_TO_CFG)
model_config = configs['model']
detection_model = model_builder.build(model_config=model_config,
is_training=False)
# Restore checkpoint
ckpt = tf.compat.v2.train.Checkpoint(model=detection_model)
ckpt.restore(os.path.join(PATH_TO_CKPT, 'ckpt-0')).expect_partial()
#tf.function
def detect_fn(image):
"""Detect objects in image."""
image, shapes = detection_model.preprocess(image)
prediction_dict = detection_model.predict(image, shapes)
detections = detection_model.postprocess(prediction_dict, shapes)
return detections, prediction_dict, tf.reshape(shapes, [-1])

As mentioned by #yudhiesh, tf.contrib is deprecated in Tensorflow >=2.0.
tf.contrib libraries are moved to Tensorflow addons.
Take a look at Tensorflow release note.

Related

cannot train nlu.yml file rasa in google colab

i'm triying to train nlu model in colab, then i gets this error. I use rasa 2.6.3
ValueError: Unknown data format for file /content/drive/MyDrive/my_project/data/nlu/nlu.yml
here is my code
# Import modules for training
from rasa_nlu.training_data import load_data
from rasa_nlu.config import RasaNLUModelConfig
from rasa_nlu.model import Trainer
from rasa_nlu import config
# loading the nlu training samples
training_data = load_data("/content/drive/MyDrive/my_project/data/nlu/nlu.yml")
trainer = Trainer(config.load("/content/drive/MyDrive/my_project/config.yml"))
# training the nlu
interpreter = trainer.train(training_data)
model_directory = trainer.persist("/content/drive/MyDrive/my_project/models/", fixed_model_name="current")

saved_model from AutoML Vision Edge not loading properly

I've been using AutoML Vision Edge for some image classification tasks with great results when exporting the models in TFLite format. However, I just tried exporting the saved_model.pb file and running it with Tensorflow 2.0 and seem to be running into some issues.
Code snippet:
import numpy as np
import tensorflow as tf
import cv2
from tensorflow import keras
my_model = tf.keras.models.load_model('saved_model')
print(my_model)
print(my_model.summary())
'saved_model' is the directory containing my downloaded saved_model.pb file. Here's what I'm seeing:
2019-10-18 23:29:08.801647: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
2019-10-18 23:29:08.829017: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x7ffc2d717510 executing computations on platform Host. Devices:
2019-10-18 23:29:08.829038: I tensorflow/compiler/xla/service/service.cc:175] StreamExecutor device (0): Host, Default Version
Traceback (most recent call last):
File "classify_in_out_tf2.py", line 81, in
print(my_model.summary())
AttributeError: 'AutoTrackable' object has no attribute 'summary'
I'm not sure if it's an issue with how I'm exporting the model, or with my code to load the model, or if these models aren't compatible with Tensorflow 2.0, or some combination.
Any help would be greatly appreciated!
I've got my saved_model.pb working outside of the docker container (for object detection, not classification - but they should be similar, change the outputs and maybe the inputs for tf 1.14), here is how:
tensorflow 1.14.0:
image encoded as bytes
import cv2
import tensorflow as tf
cv2.imread(filepath)
flag, bts = cv.imencode('.jpg', img)
inp = [bts[:,0].tobytes()]
with tf.Session(graph=tf.Graph()) as sess:
tf.saved_model.loader.load(sess, ['serve'], 'directory_of_saved_model')
graph = tf.get_default_graph()
out = sess.run([sess.graph.get_tensor_by_name('num_detections:0'),
sess.graph.get_tensor_by_name('detection_scores:0'),
sess.graph.get_tensor_by_name('detection_boxes:0'),
sess.graph.get_tensor_by_name('detection_classes:0')],
feed_dict={'encoded_image_string_tensor:0': inp})
image as numpy array
import cv2
import tensorflow as tf
import numpy as np
with tf.Session(graph=tf.Graph()) as sess:
tf.saved_model.loader.load(sess, ['serve'], 'directory_of_saved_model')
graph = tf.get_default_graph()
# Read and preprocess an image.
img = cv2.imread(filepath)
# Run the model
out = sess.run([sess.graph.get_tensor_by_name('num_detections:0'),
sess.graph.get_tensor_by_name('detection_scores:0'),
sess.graph.get_tensor_by_name('detection_boxes:0'),
sess.graph.get_tensor_by_name('detection_classes:0')],
feed_dict={'map/TensorArrayStack/TensorArrayGatherV3:0': img[np.newaxis, :, :, :]})
I used netron to find my input.
tensorflow 2.0:
import cv2
import tensorflow as tf
img = cv2.imread('path_to_image_file')
flag, bts = cv2.imencode('.jpg', img)
inp = [bts[:,0].tobytes()]
loaded = tf.saved_model.load(export_dir='directory_of_saved_model')
infer = loaded.signatures["serving_default"]
out = infer(key=tf.constant('something_unique'), image_bytes=tf.constant(inp))

Tensorflow==2.0.0a0 - AttributeError: module 'tensorflow' has no attribute 'global_variables_initializer'

I'm using Tensorflow==2.0.0a0 and want to run the following script:
import tensorflow as tf
import tensorboard
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import tensorflow_probability as tfp
from tensorflow_model_optimization.sparsity import keras as sparsity
from tensorflow import keras
tfd = tfp.distributions
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
model = tf.keras.Sequential([
tf.keras.layers.Dense(1,kernel_initializer='glorot_uniform'),
tfp.layers.DistributionLambda(lambda t: tfd.Normal(loc=t, scale=1))
])
All my older notebooks work with TF 1.13. However, I want to develop a notebook where I use Model Optimization (Neural net pruning) + TF Probability, which require Tensorflow > 1.13.
All libraries are imported but init = tf.global_variables_initializer() generates the error:
AttributeError: module 'tensorflow' has no attribute 'global_variables_initializer'
Also, tf.Session() generates the error:
AttributeError: module 'tensorflow' has no attribute 'Session'
So I guess it may be something related to Tensorflow itself, but I don't have older versions confliciting in my Anaconda environment.
Outputs for libraries' versions:
tf.__version__
Out[16]: '2.0.0-alpha0'
tfp.__version__
Out[17]: '0.7.0-dev20190517'
keras.__version__
Out[18]: '2.2.4-tf'
Any ideas on this issue ?
Tensorflow 2.0 goes away from session and switches to eager execution. You can still run your code using session if you refer to tf.compat library and disable eager execution:
import tensorflow as tf
import tensorboard
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import tensorflow_probability as tfp
from tensorflow_model_optimization.sparsity import keras as sparsity
from tensorflow import keras
tf.compat.v1.disable_eager_execution()
tfd = tfp.distributions
init = tf.compat.v1.global_variables_initializer()
with tf.compat.v1.Session() as sess:
sess.run(init)
model = tf.keras.Sequential([
tf.keras.layers.Dense(1,kernel_initializer='glorot_uniform'),
tfp.layers.DistributionLambda(lambda t: tfd.Normal(loc=t, scale=1))
])
You can convert any python script in that manner using:
tf_upgrade_v2 --infile in.py --outfile out.py
I believe "Session()" has been removed with TF 2.0.
Instead, use Functions to graph (as per TensorFlow documentation):
https://www.tensorflow.org/alpha/tutorials/eager/tf_function
Log of similar issue: https://github.com/tensorflow/community/pull/20/commits/9645a1249d3bdbe8e930af62d1958120a940c31d
use this
init = tf.compat.v1.global_variables_initializer()
event you get error after this then run the following
tf.compat.v1.disable_eager_execution()
init = tf.compat.v1.global_variables_initializer()

Issue with converting tensorflow model to Intel Movidius graph

Hello I faced with the problem when trying to use Intel Movidius Neural Stick with tensorflow. I have keras model and I convert it to tensorflow model. When I convert it to Movidius graph I got error:
Traceback (most recent call last):
File "/usr/local/bin/mvNCCompile", line 118, in
create_graph(args.network, args.inputnode, args.outputnode, args.outfile, args.nshaves, args.inputsize, args.weights)
File "/usr/local/bin/mvNCCompile", line 104, in create_graph
net = parse_tensor(args, myriad_config)
File "/usr/local/bin/ncsdk/Controllers/TensorFlowParser.py", line 290, in parse_tensor
if have_first_input(strip_tensor_id(node.outputs[0].name)):
IndexError: list index out of range
Here is my code:
from keras.models import model_from_json
from keras.models import load_model
from keras import backend as K
import tensorflow as tf
import nn
import os
weights_file = "weights.h5"
sess = K.get_session()
K.set_learning_phase(0)
model = nn.alexnet_model() # get keras model
model.load_weights(weights_file)
saver = tf.train.Saver()
saver.save(sess, "./TF_Model/tf_model") # convert keras to tensorflow model
tf_model_path = "./TF_Model/tf_model"
fw = tf.summary.FileWriter('logs', sess.graph)
fw.close()
os.system('mvNCCompile TF_Model/tf_model.meta -in=conv2d_1_input -on=activation_7/Softmax') # get Movidius graph
Python version: 2.7
OS: Ubuntu 16.04
Tensorflow version: 1.12
As I know, the ncsdk compiler does not resolve every part of a normal tensorflow network, so you have to modify the network and re-save it in an NCS-friendly way in order to successfully make a Movidius graph.
For more information about how to modify tensorflow network, have a look at the official guidance.
Hope it will help you.

Failed precondition error when using Tensorflow serving to serve pretrained keras xception model

This is the code that i am using to export the keras model into tensorflow serving format.The exported model loads up successfully in tensorflow serving( without any warnings or errors). But when i use my client to make a request to the server, i get a FailedPrecondition error.
grpc._channel._Rendezvous: <_Rendezvous of RPC that terminated with:
status = StatusCode.FAILED_PRECONDITION
details = "Attempting to use uninitialized value block11_sepconv2_bn/moving_mean
import sys
import os
import tensorflow as tf
from keras import backend as K
from keras.models import Model
from keras.models import load_model
from tensorflow.python.saved_model import builder as saved_model_builder
from tensorflow.python.saved_model import utils
from tensorflow.python.saved_model import tag_constants, signature_constants
from tensorflow.python.saved_model.signature_def_utils_impl import
build_signature_def, predict_signature_def
from tensorflow.contrib.session_bundle import exporter
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
config = tf.ConfigProto( device_count = {'GPU': 2 , 'CPU': 12} )
sess = tf.Session(config=config)
K.set_session(sess)
K._LEARNING_PHASE = tf.constant(0)
K.set_learning_phase(0)
xception = load_model('models/xception/model.h5')
config = xception.get_config()
weights = xception.get_weights()
new_xception = Model.from_config(config)
new_xception.set_weights(weights)
export_path = 'prod_models/2'
builder = saved_model_builder.SavedModelBuilder(export_path)
signature = predict_signature_def(inputs={'images': new_xception.input},
outputs={'scores': new_xception.output})
with K.get_session() as sess:
builder.add_meta_graph_and_variables(sess=sess,
tags=[tag_constants.SERVING],
signature_def_map={'predict':
signature})
builder.save()
Package versions
Python 3.6.3
tensorflow-gpu 1.8.0
Keras 2.1.5
CUDA 9.0.176
I tried to replicate your problem with the following model as I do not have access to the model file that you are using:
from keras.applications.xception import Xception
new_xception = Xception()
I can make requests to this model without issues (python 3.6.4, tf 1.8.0, keras 2.2.0). What version of TF-serving are you using?

Categories