Keras: How to load a model with pretrained ELMO Layer - python

I have trained a Deep Learning network that has a pretrained ELMO layer. I've saved the model and weights using the code below.
model.save("model.h5")
model.save_weights("weights.h5")
I now need to load the load but I'm not sure whats the right way. I've tried two techniques and both of them fail.
1: Tried just loading the model but fails with a get_config error
import numpy as np
import io
import re
from tensorflow import keras
elmo_BiDirectional_model = keras.models.load_model("model.h5")
x_data = np.zeros((1, 1), dtype='object')
x_data[0] = "test token"
with tf.Session() as session:
session.run(tf.global_variables_initializer())
session.run(tf.tables_initializer())
print( elmo_BiDirectional_model.predict(x_data) )
File "C:\temp\Simon\perdict_elmo.py", line 36, in
elmo_BiDirectional_model = keras.models.load_model("model.h5")
File
"C:\ProgramData\Anaconda3\lib\site-packages\tensorflow_core\python\keras\saving\save.py",
line 143, in load_model
return hdf5_format.load_model_from_hdf5(filepath, custom_objects, compile)
File
"C:\ProgramData\Anaconda3\lib\site-packages\tensorflow_core\python\keras\saving\hdf5_format.py",
line 159, in load_model_from_hdf5
raise ValueError('No model found in config file.')
ValueError: No model found in config file.
2: Tried building the model and just setting the weights:
import tensorflow_hub as hub
import tensorflow as tf
elmo = hub.Module("https://tfhub.dev/google/elmo/3", trainable=False)
from tensorflow.keras.layers import Input, Lambda, Bidirectional, Dense, Dropout, Flatten, LSTM
from tensorflow.keras.models import Model
def ELMoEmbedding(input_text):
return elmo(tf.reshape(tf.cast(input_text, tf.string), [-1]), signature="default", as_dict=True)["elmo"]
def build_model():
input_layer = Input(shape=(1,), dtype="string", name="Input_layer")
embedding_layer = Lambda(ELMoEmbedding, output_shape=(1024, ), name="Elmo_Embedding")(input_layer)
BiLSTM = Bidirectional(LSTM(128, return_sequences= False, recurrent_dropout=0.2, dropout=0.2), name="BiLSTM")(embedding_layer)
Dense_layer_1 = Dense(64, activation='relu')(BiLSTM)
Dropout_layer_1 = Dropout(0.5)(Dense_layer_1)
Dense_layer_2 = Dense(32, activation='relu')(Dropout_layer_1)
Dropout_layer_2 = Dropout(0.5)(Dense_layer_2)
output_layer = Dense(3, activation='sigmoid')(Dropout_layer_2)
model = Model(inputs=[input_layer], outputs=output_layer, name="BiLSTM with ELMo Embeddings")
model.summary()
model.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'])
return model
elmo_BiDirectional_model = build_model()
elmo_BiDirectional_model.load_weights('weights.h5')
import numpy as np
import io
import re
from tensorflow import keras
x_data = np.zeros((1, 1), dtype='object')
x_data[0] = "test token"
with tf.Session() as session:
session.run(tf.global_variables_initializer())
session.run(tf.tables_initializer())
print( elmo_BiDirectional_model.predict(x_data) )
But this failed with error:
File "C:\temp\Simon\perdict_elmo.py", line 28, in
elmo_BiDirectional_model.load_weights('weights.h5')
File
"C:\ProgramData\Anaconda3\lib\site-packages\tensorflow_core\python\keras\engine\training.py",
line 182, in load_weights
return super(Model, self).load_weights(filepath, by_name)
File
"C:\ProgramData\Anaconda3\lib\site-packages\tensorflow_core\python\keras\engine\network.py",
line 1373, in load_weights
saving.load_weights_from_hdf5_group(f, self.layers)
File
"C:\ProgramData\Anaconda3\lib\site-packages\tensorflow_core\python\keras\saving\hdf5_format.py",
line 645, in load_weights_from_hdf5_group
original_keras_version = f.attrs['keras_version'].decode('utf8')
AttributeError: 'str' object has no attribute 'decode'
Versions:
keras.__version__
'2.2.4-tf'
tensorflow.__version__
'1.15.0'

Finally! I had to downgrade two dependencies, then I used strategy #2 to load the weights into the model.
pip install astroid==2.3.0 --force-reinstall --user
pip install h5py==2.10.0 --force-reinstall --user

Related

bad zip file error while loading the trained deep learning model

I trained my model in colab and save it with torch.save('model.pth')
and then when i wanted to load it in my pycharm i get this error:
File "C:\Users\Amin\AppData\Local\Programs\Python\Python310\lib\zipfile.py", line 1334, in _RealGetContents
raise BadZipFile("File is not a zip file")
zipfile.BadZipFile: File is not a zip file`
can anyone help me to fix this error please
i could not find any solution for it on internet
i used tensorflow for training my model and used these imports :
from tensorflow.keras.preprocessing.text import text_to_word_sequence
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing import sequence
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Activation
from tensorflow.keras.layers import Embedding
from tensorflow.keras.layers import Conv1D, GlobalMaxPooling1D
my program can load the tokenizer that i have built but it wont load the model
this is my model :
max_features = 1000
maxlen = 650
embedding_dims = 50
filters = 250
kernel_size = 3
hidden_dims = 250
model5 = Sequential()
model5.add(Embedding(max_features, embedding_dims ))
model5.add(Dropout(0.2))
model5.add(Conv1D(filters, kernel_size, padding='valid', activation='relu', strides=1))
model5.add(GlobalMaxPooling1D())
model5.add(Dense(hidden_dims)) model5.add(Dropout(0.2)) model5.add(Activation('relu'))
model5.add(Dense(5)) model5.add(Activation('softmax'))
model5.compile(loss='categorical_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
model5.fit(X_train, y_train,
batch_size=32,
epochs=14,
validation_data=(X_test, y_test))
torch.save(model5,'model.pth')
i loaded my model in colab and it was fine but it didn't work in pycharm
relative_model_path = "model.pth"
full_model_path = os.path.join(absolute_path, relative_model_path)
model = torch.load(full_model_path)
Traceback (most recent call last):
File "C:\\Users\\Amin\\PycharmProjects\\src\\model\\categorizer.py", line 25, in \<module\>
model = torch.load(full_model_path)
File "C:\\Users\\Amin\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\torch\\serialization.py", line 789, in load
return \_load(opened_zipfile, map_location, pickle_module, \*\*pickle_load_args)
File "C:\\Users\\Amin\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\torch\\serialization.py", line 1131, in \_load
result = unpickler.load()
File "C:\\Users\\Amin\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\keras\\saving\\pickle_utils.py", line 48, in deserialize_model_from_bytecode
raise e
File "C:\\Users\\Amin\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\keras\\saving\\pickle_utils.py", line 46, in deserialize_model_from_bytecode
model = saving_lib.load_model(filepath)
File "C:\\Users\\Amin\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\keras\\saving\\experimental\\saving_lib.py", line 196, in load_model
raise e
File "C:\\Users\\Amin\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\keras\\saving\\experimental\\saving_lib.py", line 173, in load_model
with zipfile.ZipFile(filepath, "r") as zipfile_to_load:
File "C:\\Users\\Amin\\AppData\\Local\\Programs\\Python\\Python310\\lib\\zipfile.py", line 1267, in __init__
self.\_RealGetContents()
File "C:\\Users\\Amin\\AppData\\Local\\Programs\\Python\\Python310\\lib\\zipfile.py", line 1334, in \_RealGetContents
raise BadZipFile("File is not a zip file")`your text`
zipfile.BadZipFile: File is not a zip file
I just needed to save the model with
keras.save('model')
not torch because the model was built in tensorflow keras

Error `Cannot import name 'wrappers' from 'tensorflow.python.keras.layers'`?

The code is giving the following error message
Cannot import name 'wrappers' from 'tensorflow.python.keras.layers' - and ImportError: graphviz or pydot are not available.
Even after installing the graphviz and pydot using the !apt-get -qq install -y graphviz && pip install pydot still not able to genrate model.png.
i m running the following code in colab
try:
# %tensorflow_version only exists in Colab.
%tensorflow_version 2.x
except Exception:
pass
import tensorflow as tf
from tensorflow.python.keras.utils.vis_utils import plot_model
import pydot
from tensorflow.keras.models import Model
def build_model_with_sequential():
# instantiate a Sequential class and linearly stack the layers of your model
seq_model = tf.keras.models.Sequential([tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(128, activation=tf.nn.relu),
tf.keras.layers.Dense(10, activation=tf.nn.softmax)])
return seq_model
def build_model_with_functional():
# instantiate the input Tensor
input_layer = tf.keras.Input(shape=(28, 28))
# stack the layers using the syntax: new_layer()(previous_layer)
flatten_layer = tf.keras.layers.Flatten()(input_layer)
first_dense = tf.keras.layers.Dense(128, activation=tf.nn.relu)(flatten_layer)
output_layer = tf.keras.layers.Dense(10, activation=tf.nn.softmax)(first_dense)
# declare inputs and outputs
func_model = Model(inputs=input_layer, outputs=output_layer)
return func_model
model = build_model_with_functional()
#model = build_model_with_sequential()
# Plot model graph
plot_model(model, show_shapes=True, show_layer_names=True, to_file='model.png')
ERROR
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-5-aaa8f6f7cc6e> in <module>
3
4 # Plot model graph
----> 5 plot_model(model, show_shapes=True, show_layer_names=True, to_file='model.png')
1 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/utils/vis_utils.py in plot_model(model, to_file, show_shapes, show_dtype, show_layer_names, rankdir, expand_nested, dpi)
327 rankdir=rankdir,
328 expand_nested=expand_nested,
--> 329 dpi=dpi)
330 to_file = path_to_string(to_file)
331 if dot is None:
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/utils/vis_utils.py in model_to_dot(model, show_shapes, show_dtype, show_layer_names, rankdir, expand_nested, dpi, subgraph)
96 ImportError: if graphviz or pydot are not available.
97 """
---> 98 from tensorflow.python.keras.layers import wrappers
99 from tensorflow.python.keras.engine import sequential
100 from tensorflow.python.keras.engine import functional
ImportError: cannot import name 'wrappers' from 'tensorflow.python.keras.layers' (/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/layers/__init__.py)
---------------------------------------------------------------------------
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.
-------------------------------------------------------------------------
It's the version issue.
According to the latest tensorflow doc (at least from 2.8.1). there's no tensorflow.python package. plot_model module has been moved to tensorflow.keras.utils.
so simply replacing
from tensorflow.python.keras.utils.vis_utils import plot_model
with
from tensorflow.keras.utils import plot_model
could solve the issue.
Seems to be some import conflicts. Rather use tf.keras for everything:
try:
# %tensorflow_version only exists in Colab.
%tensorflow_version 2.x
except Exception:
pass
import tensorflow as tf
import pydot
def build_model_with_sequential():
# instantiate a Sequential class and linearly stack the layers of your model
seq_model = tf.keras.models.Sequential([tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(128, activation=tf.nn.relu),
tf.keras.layers.Dense(10, activation=tf.nn.softmax)])
return seq_model
def build_model_with_functional():
# instantiate the input Tensor
input_layer = tf.keras.Input(shape=(28, 28))
# stack the layers using the syntax: new_layer()(previous_layer)
flatten_layer = tf.keras.layers.Flatten()(input_layer)
first_dense = tf.keras.layers.Dense(128, activation=tf.nn.relu)(flatten_layer)
output_layer = tf.keras.layers.Dense(10, activation=tf.nn.softmax)(first_dense)
# declare inputs and outputs
func_model = tf.keras.Model(inputs=input_layer, outputs=output_layer)
return func_model
model = build_model_with_functional()
tf.keras.utils.plot_model(model, show_shapes=True, show_layer_names=True, to_file='model.png')

Cannot run Carlini and Wagner Attack using foolbox on a tensorflow Model

I am using the latest version of foolbox (3.3.1), and my code simply load a RESNET-50 CNN, adds some layers for a transferred learning application, and loads the weights as follows.
from numpy.core.records import array
import tensorflow as tf
from keras.applications.resnet50 import ResNet50, preprocess_input
from tensorflow.keras.layers import Dense, Dropout, Flatten
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input
import cv2
import os
import numpy as np
import foolbox as FB
from sklearn.metrics import accuracy_score
from scipy.spatial.distance import cityblock
from sklearn.metrics import plot_confusion_matrix
from sklearn.metrics import confusion_matrix
from PIL import Image
import foolbox as FB
import math
from foolbox.criteria import Misclassification
#load model
num_classes = 12
#Load model and prepare it for testing
print("Step 1: Load model and weights")
baseModel = ResNet50(weights=None, include_top=False, input_tensor=Input(shape=(224, 224, 3)))
headModel = baseModel.output
headModel = Flatten(name="flatten")(headModel)
headModel = Dense(512, activation="relu")(headModel)
headModel = Dropout(0.5)(headModel)
headModel = Dense(num_classes, activation="softmax")(headModel)
model = Model(inputs=baseModel.input, outputs=headModel)
model.load_weights("RESNET-50/weights/train1-test1.h5")
print("Step 2: prepare testing data")
#features is a set of (1200,10,224,224,3) images
features=np.load("features.npy")
labels=np.load("labels.npy")
Now I would like to attack it using the foolbox 3.3.1 Carlini and Wagner attack, here is the way I load the model for foolbox
#Lets test the foolbox model
bounds = (0, 1)
fmodel = fb.TensorFlowModel(model, bounds=bounds)
My dataset is split into 10 images per document, I will attack these 10 images using a batch size of 10 for foolbox using Carlini and Wagner attack
#for each i, I have 10 images
for i in range(0, features.shape[0]):
print("document "+str(i))
#Receive current values
#This is a batch of (10,224,224,3) images
features_to_test=features[i,:]
#Get their labels
labels_to_test=labels[i,:]
######################ATTACK IN THE NORMALIZED DOMAIN###########################
#lets do the attack
#We use an interval of epsilons
epsilons = np.linspace(0.01, 1, num=2)
attack = fb.attacks.L2CarliniWagnerAttack(fmodel)
adversarials = attack(features_to_test, labels_to_test, criterion=Misclassification(labels=labels_to_test), epsilons=epsilons)
However, whenever I run the code, here is the error that is returned to me
Traceback (most recent call last):
File "test_carlini_wagner.py", line 161, in <module>
adversarials = attack(features_to_test, labels_to_test,
criterion=Misclassification(labels=labels_to_test), epsilons=epsilons)
File "/usr/local/lib/python3.8/dist-packages/foolbox/attacks/base.py", line 410, in
__call__
xp = self.run(model, x, criterion, early_stop=early_stop, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/foolbox/attacks/carlini_wagner.py", line 100, in run
bounds = model.bounds
AttributeError: 'tensorflow.python.framework.ops.EagerTensor' object has no attribute
'bounds'
What is supposed to be the error? am I loading my model wrongly? should I add new parameters for the attack called? as previously stated, I am on foolbox 3.3.1.
I think you might have mixed up the parameters of the L2CarliniWagnerAttack. Here is a simplified working example with dummy data:
import tensorflow as tf
import numpy as np
from tensorflow.keras.applications.resnet50 import ResNet50, preprocess_input
from tensorflow.keras.layers import Dense, Dropout, Flatten
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input
from sklearn.metrics import accuracy_score
from scipy.spatial.distance import cityblock
from sklearn.metrics import plot_confusion_matrix
from sklearn.metrics import confusion_matrix
from foolbox import TensorFlowModel
from foolbox.criteria import Misclassification
from foolbox.attacks import L2CarliniWagnerAttack
num_classes = 12
print("Step 1: Load model and weights")
baseModel = ResNet50(weights=None, include_top=False, input_tensor=Input(shape=(224, 224, 3)))
headModel = baseModel.output
headModel = Flatten(name="flatten")(headModel)
headModel = Dense(512, activation="relu")(headModel)
headModel = Dropout(0.5)(headModel)
headModel = Dense(num_classes, activation="softmax")(headModel)
model = Model(inputs=baseModel.input, outputs=headModel)
bounds = (0, 1)
fmodel = TensorFlowModel(model, bounds=bounds)
images, labels = tf.random.normal((64, 10, 224, 224, 3)), tf.random.uniform((64, 10,), maxval=13, dtype=tf.int32)
for i in range(0, images.shape[0]):
print("document "+str(i))
features_to_test=images[i,:]
labels_to_test=labels[i,:]
epsilons = np.linspace(0.01, 1, num=2)
attack = L2CarliniWagnerAttack()
adversarials = attack(fmodel, features_to_test, criterion=Misclassification(labels_to_test), epsilons=epsilons)
Step 1: Load model and weights
document 0
document 1
document 2
document 3
document 4
document 5
document 6
...

OSError: Unable to open file (unable to open file: name = 'DLCB_code_output/Results_mmWave_ML0', errno = 13, error message = 'Permission denied')

Now, I am trying to run the CNN code regarding "Deep Learning Coordinated Beamforming."
At first, there is no directly of "DLCB_code_output/Results_mmWave_ML0". And there was an error of "there is no directory of ...". Thereby, I created new directories of "DLCB_code_output/Results_mmWave_ML0".
So, as you can see from the title, there is a permission error in that directories.
from __future__ import division
import os, keras
os.environ["KERAS_BACKEND"] = "theano"
os.environ["THEANO_FLAGS"] = "device=gpu%d"%(1)
import numpy as np
import theano as th
import theano.tensor as T
from keras.utils import np_utils
import keras.models as models
from keras.layers.core import Reshape,Dense,Dropout,Activation
from keras.optimizers import adam
from scipy.io import loadmat, savemat
import os.path
from keras import backend as K
#from tensorflow.python.keras.layers import Input,Dense
#from tensorflow.python.keras.models import Sequential
# Model training function
def train(In_train, Out_train, In_test, Out_test,
nb_epoch, batch_size,dr,
num_hidden_layers, nodes_per_layer,
loss_fn,n_BS,n_beams):
in_shp = list(In_train.shape[1:])
AP_models = []
for idx in range(0, n_BS*n_beams-2, n_beams):
idx_str = str(idx / n_beams + 1)
model = models.Sequential()
model.add(Dense(nodes_per_layer, activation='relu', init='he_normal',
name="dense" + idx_str + "1", input_shape=in_shp))
model.add(Dropout(dr))
for h in range(num_hidden_layers):
model.add(Dense(nodes_per_layer, activation='relu',
init='he_normal', name="dense" + idx_str + "h" + str(h)))
model.add(Dropout(dr))
model.add(Dense(n_beams, activation='relu', init='he_normal',
name="dense" + idx_str + "o"))
model.compile(loss=loss_fn, optimizer='adam')
model.summary()
# perform training ...
earlyStoppingCallback = \
keras.callbacks.EarlyStopping(monitor='val_loss',
patience=5,
verbose=0,
mode='auto')
filepath = 'DLCB_code_output/Results_mmWave_ML'+str(idx)
#filepath = 'DLCB_code_output/DL_Result'
#filepath = 'DLCB_code_output'
history = model.fit(In_train,
Out_train[:, idx:idx + n_beams],
batch_size=batch_size,
nb_epoch=nb_epoch,
verbose=2,
validation_data=(In_test, Out_test[:,idx:idx + n_beams]),
callbacks = [
keras.callbacks.ModelCheckpoint(filepath, monitor='val_loss', verbose=0, save_best_only=True, mode='auto'),
keras.callbacks.EarlyStopping(monitor='val_loss', patience=5, verbose=0, mode='auto')
])
# we re-load the best weights once training is finished
model.load_weights(filepath)
AP_models.append(model)
return AP_models
# Reading input and output sets generated from MATLAB
In_set_file=loadmat('DLCB_dataset/DLCB_input.mat')
Out_set_file=loadmat('DLCB_dataset/DLCB_output.mat')
In_set=In_set_file['DL_input']
Out_set=Out_set_file['DL_output']
# Parameter initialization
num_user_tot=In_set.shape[0]
n_DL_size=[.001,.05,.1,.15,.2,.25,.3,.35,.4,.45,.5,.55,.6,.65,.7,.75,.8]
count=0
num_tot_TX=4
num_beams=128
for DL_size_ratio in n_DL_size:
print (DL_size_ratio)
count=count+1
DL_size=int(num_user_tot*DL_size_ratio)
np.random.seed(2016)
n_examples = DL_size
num_train = int(DL_size * 0.8)
num_test = int(num_user_tot*.2)
train_index = np.random.choice(range(0,num_user_tot), size=num_train, replace=False)
rem_index = set(range(0,num_user_tot))-set(train_index)
test_index= list(set(np.random.choice(list(rem_index), size=num_test, replace=False)))
In_train = In_set[train_index]
In_test = In_set[test_index]
Out_train = Out_set[train_index]
Out_test = Out_set[test_index]
# Learning model parameters
nb_epoch = 10
batch_size = 100
dr = 0.05 # dropout rate
num_hidden_layers=4
nodes_per_layer=In_train.shape[1]
loss_fn='mean_squared_error'
# Model training
AP_models = train(In_train, Out_train, In_test, Out_test,
nb_epoch, batch_size,dr,
num_hidden_layers, nodes_per_layer,
loss_fn,num_tot_TX,num_beams)
# Model running/testing
DL_Result={}
for id in range(0,num_tot_TX,1):
beams_predicted=AP_models[id].predict( In_test, batch_size=10, verbose=0)
DL_Result['TX'+str(id+1)+'Pred_Beams']=beams_predicted
DL_Result['TX'+str(id+1)+'Opt_Beams']=Out_test[:,id*num_beams:(id+1)*num_beams]
DL_Result['user_index']=test_index
savemat('DLCB_code_output/DL_Result'+str(count),DL_Result)
I can't deal with this problem.
Please give me some advice.
Now, I use the Windows OS, Python 3.6.8, Keras 2.3.1 and Tensorflow 2.0.0
"C:\Users\kohei\AppData\Local\Programs\Python\Python36\lib\site-packages\h5py_hl\files.py", line 173, in make_fid fid = h5f.open(name, flags, fapl=fapl) File "h5py_objects.pyx", line 54,
in h5py._objects.with_phil.wrapper File "h5py_objects.pyx", line 55,
in h5py._objects.with_phil.wrapper File "h5py\h5f.pyx", line 88,
in h5py.h5f.open OSError: Unable to open file (unable to open file:
name = 'DLCB_code_output/Results_mmWave_ML0',
errno = 13, error message = 'Permission denied', flags = 0, o_flags = 0)```

OpenCV DNN fails with keras DenseNet121

following http://answers.opencv.org/question/183507/opencv-dnn-import-error-for-keras-pretrained-vgg16-model/ I'm trying to get densenet to work on openCV DNN but getting:
"error: OpenCV(3.4.2)
/io/opencv/modules/dnn/src/tensorflow/tf_graph_simplifier.cpp:712:
error: (-2:Unspecified error) Tensor's
data type is not supported in function
'getTensorContent'"
import numpy as np
from keras import applications
from keras import backend as K
import cv2 as cv
import tensorflow as tf
model = applications.densenet.DenseNet121(input_shape=(224, 224, 3), weights='imagenet', include_top=True)
sess = K.get_session()
print(model.input, model.outputs)
## Tensor("input_1:0", shape=(?, 224, 224, 3), dtype=float32) [<tf.Tensor 'fc1000/Softmax:0' shape=(?, 1000) dtype=float32>]
from tensorflow.python.tools import freeze_graph
from tensorflow.python.tools import optimize_for_inference_lib
MODEL_PATH = 'out'
MODEL_NAME = 'test'
input_node_name = 'input_1'
output_node_name = 'fc1000/Softmax'
!rm -rf {MODEL_PATH}/
tf.train.write_graph(sess.graph_def, MODEL_PATH, f'{MODEL_NAME}_graph.pb', as_text=False)
tf.train.write_graph(sess.graph_def, MODEL_PATH, f'{MODEL_NAME}_graph.pbtxt')
tf.train.Saver().save(sess, f'{MODEL_PATH}/{MODEL_NAME}.chkp')
freeze_graph.freeze_graph(f'{MODEL_PATH}/{MODEL_NAME}_graph.pbtxt',
None, False,
f'{MODEL_PATH}/{MODEL_NAME}.chkp',
output_node_name,
"save/restore_all",
"save/Const:0",
f'{MODEL_PATH}/frozen_{MODEL_NAME}.pb',
True, "")
graph_def = tf.GraphDef()
with tf.gfile.Open(f'{MODEL_PATH}/frozen_{MODEL_NAME}.pb', "rb") as f:
graph_def.ParseFromString(f.read())
output_graph_def = optimize_for_inference_lib.optimize_for_inference(
graph_def, [input_node_name], [output_node_name], tf.float32.as_datatype_enum)
with tf.gfile.GFile(f'{MODEL_PATH}/opt_{MODEL_NAME}.pb', "wb") as f:
f.write(output_graph_def.SerializeToString())
# Strip Const nodes.
for i in reversed(range(len(graph_def.node))):
if graph_def.node[i].op == 'Const':
del graph_def.node[i]
# for attr in ['T', 'data_format', 'Tshape', 'N', 'Tidx', 'Tdim',
# 'use_cudnn_on_gpu', 'Index', 'Tperm', 'is_training',
# 'Tpaddings']:
# if attr in graph_def.node[i].attr:
# del graph_def.node[i].attr[attr]
# Save stripped model.
tf.train.write_graph(graph_def, "", f'{MODEL_PATH}/stripped_{MODEL_NAME}.pbtxt', as_text=True)
net = cv.dnn.readNetFromTensorflow(f'{MODEL_PATH}/opt_{MODEL_NAME}.pb', f'{MODEL_PATH}/stripped_{MODEL_NAME}.pbtxt')
## error: OpenCV(3.4.2) /io/opencv/modules/dnn/src/tensorflow/tf_graph_simplifier.cpp:712: error: (-2:Unspecified error) Tensor's data type is not supported in function 'getTensorContent'
It's hard to tell which node is causing that from the error.
Any idea please?
Cheers
tensorflow 1.12.0
opencv 3.4.3
Although, I still wasn't able to get optimize_for_inference to work due to the FusedBatchNorm, thanks for the feedback from #dkurt and https://github.com/keras-team/keras/issues/6775 which explains the keras learning_phase. You have to set the learning phase before loading the model!
load the model with set_learning_phase(0):
import numpy as np
from keras import applications
from keras import backend as K
import tensorflow as tf
K.set_learning_phase(0) ##
model = applications.densenet.DenseNet121(input_shape=(224, 224, 3), weights='imagenet', include_top=True)
sess = K.get_session()
print(model.input, model.outputs)
## Tensor("input_1:0", shape=(?, 224, 224, 3), dtype=float32) [<tf.Tensor 'fc1000/Softmax:0' shape=(?, 1000) dtype=float32>]
freeze it:
from tensorflow.python.tools import freeze_graph
from tensorflow.python.tools import optimize_for_inference_lib
MODEL_PATH = 'out'
MODEL_NAME = 'test'
input_node_name = 'input_1'
output_node_name = 'fc1000/Softmax'
!rm -rf {MODEL_PATH}/
tf.train.write_graph(sess.graph_def, MODEL_PATH, f'{MODEL_NAME}_graph.pb', as_text=False)
tf.train.write_graph(sess.graph_def, MODEL_PATH, f'{MODEL_NAME}_graph.pbtxt')
tf.train.Saver().save(sess, f'{MODEL_PATH}/{MODEL_NAME}.chkp')
freeze_graph.freeze_graph(f'{MODEL_PATH}/{MODEL_NAME}_graph.pbtxt',
None, False,
f'{MODEL_PATH}/{MODEL_NAME}.chkp',
output_node_name,
"save/restore_all",
"save/Const:0",
f'{MODEL_PATH}/frozen_{MODEL_NAME}.pb',
True, "")
then load it with dnn:
import cv2 as cv
net = cv.dnn.readNetFromTensorflow(f'{MODEL_PATH}/frozen_{MODEL_NAME}.pb')
# Smoke test
inp = np.ones([1, 3, 224, 224]).astype(np.float32)
net.setInput(inp)
dnn_out = net.forward()
print(dnn_out.shape, dnn_out[0,:5])
## (1, 1000) [2.0760612e-04 2.6876197e-04 5.9680151e-05 5.5908626e-05 1.4762023e-04]
As said, I wasn't able to get optimize_for_inference to work due to the FusedBatchNorm:
WARNING:tensorflow:Didn't find expected Conv2D input to 'conv2_block1_0_bn/FusedBatchNorm_1'
opencv-4.0.0/modules/dnn/src/tensorflow/tf_importer.cpp:497: error: (-2:Unspecified error) Input layer not found: conv2_block1_1_bn/FusedBatchNorm_1 in function 'connect'
So please let me know if you know a solution for that. Thanks

Categories

Resources