ImportError: cannot import name 'builder' from 'google.protobuf.internal' while importing tf2onnx - tensorflow

I am following a tutorial to convert keras (.hdf5/.h5) model to onnx model. the link is (https://www.youtube.com/watch?v=7ndUGBzGVvg)
So, the code is
import tensorflow as tf
from tensorflow.keras.models import load_model
print(tf.__version__)
model = load_model('./model/weights.28-3.73.hdf5')
model.summary()
import tf2onnx
I also tried importing import keras2onnx but it generates error saying
module 'tensorflow.python.keras' has no attribute 'applications'. So I tried with tf2onnx.
I am using
keras==2.4.3
tensorflow==2.5.0
python==3.8.5
tf2onnx==1.13.0
onnx==1.13.0

Related

Feature extraction in pretrained model: different result from keras-gpu&tensorflow-gpu and keras&tensorflow base

I'm extracting features from a pretrained keras model (VGG16).The task is simple but I obtain different features (nx4096) when I run the code on GPU (tensorflow-gpu and keras-gpu installed) or CPU (tensorflow and keras installed).
The differences in the features are huge and when I use the different extracted features in a classifier, I obtain very much better results with those obtained on GPU.
Could someone explain me why?
I write the code in case it is useful:
from keras.preprocessing import image
from keras.applications.vgg16 import VGG16
import tensorflow.keras
from keras.applications.vgg16 import preprocess_input
import numpy as np
from sklearn.cluster import KMeans
# %matplotlib inline
import matplotlib.pyplot as plt
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.applications.vgg16 import decode_predictions
from keras.models import Model
from pickle import dump
model = VGG16(weights='imagenet')#, include_top=False
# remove the output layer
model = Model(inputs=model.inputs, outputs=model.layers[-2].output)
model.summary()
trainstack=np.load('.../All_imgs.npy')
trainstack=np.transpose(trainstack,[2,0,1,3])
trainstack=preprocess_input(trainstack)
trainstacklabel=np.load('.../All_labels.npy')
img_data = preprocess_input(trainstack)
vgg16_feature_list=[]
for i in range(trainstack.shape[0]):
v =img_data[i,:,:,:]
v=v[np.newaxis,...]
vgg16_feature = model.predict(v)
print (vgg16_feature.shape)
vgg16_feature_np = np.array(vgg16_feature)
vgg16_feature_list.append(vgg16_feature_np.flatten())
np.save('.../features.npy',vgg16_feature_list)
Thank you very much in advance!!
I try to extract features from a pretrained model for combining them with other variables in a binary classifier.
I obtain very different extracted features when I run my code on GPU and CPU.

Module not found using Input function from tensorflow.keras.layers

Im quite new learning machine learning and my first project is the creation of a Neural Network in order to detect key facial points on google colab. Everything has been working ok but today when I wanted to train my neural network I came accross with an error that has never appeared before when I trained my neural network.
The error is:
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-189-47fd3efd0229> in <module>()
5
6
----> 7 X_input = Input(input_shape)
8
9 # Zero-padding
4 frames
/usr/lib/python3.7/importlib/_bootstrap.py in _find_and_load_unlocked(name, import_)
ModuleNotFoundError: No module named 'keras.engine.base_layer_v1'
---------------------------------------------------------------------------
NOTE: If your import is failing due to a missing package, you can
manually install dependencies using either !pip or !apt.
To view examples of installing some common dependencies, click the
"Open Examples" button below.
I don't understand the line ModuleNotFoundError: No module named 'keras.engine.base_layer_v1' because the line that is not working is when I'm using Input from tensorflow.keras.layers.
I really don't know what is going on because I never got this error before. I've seen that it could be the version of TensorFlow or maybe my libraries.
I am using 2.3.0 versions in TensorFlow and Keras and these are the libraries I am importing:
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.applications import DenseNet121
from tensorflow.keras.models import Model, load_model
from tensorflow.keras.initializers import glorot_uniform
from tensorflow.keras.utils import plot_model
from tensorflow.keras.callbacks import ReduceLROnPlateau, EarlyStopping, ModelCheckpoint, LearningRateScheduler
from IPython.display import display
from tensorflow.python.keras import *
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras import layers, optimizers
from tensorflow.keras.applications.resnet50 import ResNet50
from tensorflow.keras.layers import *
from tensorflow.keras import backend as K
from keras import optimizers
I would really appreciate any help :)
Re-installing tensorflow and keras works for me

ModuleNotFoundError: No module named 'tf' But the TensorFlow v2.3 is already installed

I'm trying to import some regularizers, with the following code:
import tensorflow as tf
from tensorflow import keras
import numpy as np
# importing regularizers
from tf.keras.regularizers import l2, activity_l2
The problem is that I get the following error:
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_8996/2415362791.py in <module>
4 from tensorflow import keras
5 # importing regularisers
----> 6 from tf.keras.regularizers import l2, activity_l2
ModuleNotFoundError: No module named 'tf'
However, I've already installed TensorFlow, and I can build neural networks, with several layers, without any error showing up...
I'm using Anaconda v2.1, Jupyter Notebooks v6.4.3.
Why is this happening?
You can use tensorflow.keras.regularizers instead of tf.keras.regularizers as below:
from tensorflow.keras.regularizers import l2 (to import specific regularizers)
or
from tensorflow.keras import regularizers (to use and import all other regularizers.)
activity_l2 is deleted and is equal to l2. Find the related github reference here.

AttributeError: module 'tensorflow.compat.v2.__internal__' has no attribute 'tf2'

enter image description here
Above is the image:
The error is this:
LOCAL.GENERATED_WITH_V2 = tf.__internal__.tf2.enabled()
AttributeError: module 'tensorflow.compat.v2.__internal__' has no attribute 'tf2'
Thanks
From Tensorflow 2.x onward, keras is no longer maintained and it became a part of Tensorflow. I would recommend instead of import keras, you should try from tensorflow import keras or import tensorflow as tf and use tf.keras.
You can import Sequential module from tensorflow as shown below
import tensorflow as tf
from tf.keras import Sequential
For more information you can refer this and this

Unable to import AttentionLayer in Keras (TF1.13)

I'm trying to import Attention layer for my encoder decoder model but it gives error.
from keras.layers import AttentionLayer
or
from keras.layers import Attention
following is the error
cannot import name 'AttentionLayer' from 'keras.layers'
cannot import name 'Attention' from 'keras.layers'
Any suggestons?
I solved the issue by upgrading to tensorflow 1.14 and importing it as
from tensorflow.keras.layers import Attention
I think you have to use tensorflow if you haven't imported earlier
from tensorflow.keras.layers import Attention