Tensorflow Backend - Bug in "model._make_predict_function" - python

there's is a bug while running the Tensorflow code, the error code appears like this:
Traceback (most recent call last):
File "app.py", line 76, in <module>
model = deepmoji_emojis(maxlen, PRETRAINED_PATH)
File "/home/lifeofpy/LifeofPy/AI Photographer Project/Text-to-Color/deepmoji/model_def.py", line 35, in deepmoji_emojis
model._make_predict_function()
AttributeError: 'Functional' object has no attribute '_make_predict_function
and the file app.py is like this:
# print('Loading model from {}.'.format(PRETRAINED_PATH))
model = deepmoji_emojis(maxlen, PRETRAINED_PATH)
model.summary()
model._make_predict_function()
I think the error message is occured by the function 'model._make_predict_function',
I would appreciate any comments on this issue. Thanks!

Use: model.make_predict_function()
instead of: model._make_predict_function()

I tried to find _make_predict_function() with Google and it seems it was private function in old Keras in keras.engine.training.py but now Keras is part of tensorflow and function was removed from code. I can't find _make_predict_function() in tensorflow.keras.engine.training.py
Some old posts suggest to use model.predict() instead of model._make_predict_function() before threads but other posts suggest to duplicate model for every thread. But maybe new code in tensorflow resolved problem with running it in threads and maybe it doesn't need this function any more.

Related

'tensorflow' has no attribute 'space_to_depth' error with tensorflow 2.3 when running yad2k to generate model h5 file

I am trying to generate YOLOv2 model yolo.h5 so that I can load this pre-trained model. I am trying to port Andrew Ng coursera Yolo assignment ( which runs in tensorflow 1.x) to tensorflow 2.3.
I was able to cleanly port it thanks to tensorflow uprade (https://www.tensorflow.org/guide/upgrade), But little did I realize that I cannot download the yolo.h5 file ( either its get corrupted or the download times out) and therefore I thought I should build one and I followed instructions from https://github.com/JudasDie/deeplearning.ai/issues/2.
It looked pretty straight forward as I cloned YAD2k repo and downloaded both the yolo.weights and yolo.cfg.
I ran the following the command as per the instructions:
python yad2k.py yolo.cfg yolo.weights model_data/yolo.h5
But I got the following error:-
Traceback (most recent call last):
_main(parser.parse_args())
File "yad2k.py", line 233, in _main
Lambda(
File "/home/sunny/miniconda3/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer.py", line
925, in __call__
return self._functional_construction_call(inputs, args, kwargs,
File "/home/sunny/miniconda3/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer.py", line
1117, in _functional_construction_call
outputs = call_fn(cast_inputs, *args, **kwargs)
File "/home/sunny/miniconda3/lib/python3.8/site-packages/tensorflow/python/keras/layers/core.py", line 903, i
n call
result = self.function(inputs, **kwargs)
File "/home/sunny/YAD2K/yad2k/models/keras_yolo.py", line 32, in space_to_depth_x2
return tf.space_to_depth(x, block_size=2)
AttributeError: module 'tensorflow' has no attribute 'space_to_depth'
From the all chats I figured out that the above needs to run in tensorflow 1.x . However it puts me back where I started which is to run it in tensorflow 1.x. I would love to stick with tensorflow 2.3.
Wondering if someone can guide me here. Frankly, to get me going all I need is an model hd5 file. But I thought generating one would be a better learning than to get one.
The above problem goes away when you upgrade all of your code under yad2k repo ( particularly yad2k.py and python files under models folder to tensorflow 2.x. The beautiful upgrade utility provided by tensorflow does the magic for you by replacing the original call to the compatible tf.compat.v1.space_to_depth(input=x, block_size=...)
Therefore for those who are planning to do the hard job of downgrading their tensorflow and keras, I would recommend them to try the tensorflow upgrade. This saves a lot of time.
This takes care of my model h5 file creation. My bad - I didn't think about it when I asking the question.

pytorch torch.jit.trace returns function instead of torch.jit.ScriptModule

I need to run in c++ a pre-trained pytorch nn model (trained in python) to make predictions.
To do so, I'm following the instructions on how to load a pytorch model in c++ given here: https://pytorch.org/tutorials/advanced/cpp_export.html
But when I try to get the torch.jit.ScriptModule via tracing as stated in the first step of the tutorial:
traced_script_module =
torch.jit.trace(model, (input_tensor_1, input_tensor_2))
Instead of returning a torch.jit.ScriptModule, it returns a function:
print(type(traced_script_module))
<type 'function'>
Which, when I run:
traced_script_module.save("model.pt")
then leads into the following error:
Traceback (most recent call last):
File "serialize_model.py", line 60, in <module>
traced_script_module.save("model.pt")
AttributeError: 'function' object has no attribute 'save'
Any ideas on what I'm doing wrong?
Thanks for asking Jatentaki. I was using PyTorch 0.4 in Python and when I updated to 1.0 it worked.

Setting up VGG-Face Descriptor in PyTorch

I've been trying to use the VGG-Face descriptor model (http://www.robots.ox.ac.uk/~vgg/software/vgg_face/) for a project of mine. All I want to do is to simply obtain the outputs of the network from an input image.
I haven't used any of MatConvNet, Caffe or PyTorch before and so I picked PyTorch at random. It turns out that the model (of class torch.legacy.nn.Sequential.Sequential) was saved in an older version of PyTorch and the syntax was thus slightly different to the ones on PyTorch's documentation.
I was able to load the lua .t7 model like so:
vgg_net = load_lua('./vgg_face_torch/VGG_FACE.t7', unknown_classes=True)
And loading in the input image:
# load image
image = imread('./ak.png')
# convert to tensor
input = torch.from_numpy(image).float()
Gleefully, I loaded in the image into the model with much anticipation:
# load into vgg_net
output = vgg_net.forward(input)
However, my hopes of it cooperating at all was quickly dashed when the code fails to compile. Leaving behind a cryptic error message:
Traceback (most recent call last):
File "~/Documents/python/vgg-face-test/vgg-pytorch.py", line 25, in <module>
output = vgg_net.forward(input)
File "~/.local/lib/python3.6/site-packages/torch/legacy/nn/Module.py", line 33, in forward
return self.updateOutput(input)
File "~/.local/lib/python3.6/site-packages/torch/utils/serialization/read_lua_file.py", line 235, in updateOutput_patch
return obj.updateOutput(*args)
File "~/.local/lib/python3.6/site-packages/torch/legacy/nn/Sequential.py", line 36, in updateOutput
currentOutput = module.updateOutput(currentOutput)
TypeError: 'NoneType' object is not callable
Of which I am absolutely dumbfounded by.
This is why I sought help on Stackoverflow. I hope someone here could perhaps lend me a hand in setting up the model - not even necessarily in Torch, in fact any working model will do, where I can simply get the description for any particular image.
try output = vgg_net(input) without the forward.
This apparently calls a default method defined in the module but I'm having trouble understanding why this is necessary.

Tensorflow's API: seq2seq

I have been following https://github.com/kvfrans/twitch/blob/master/main.py tutorial to create and train a chatbot based on rnn using tensorflow. From what I understand, the tutorials was written on an older version of tensorflow, so some parts are outdated and give me an error like:
Traceback (most recent call last):
File "main.py", line 33, in <module>
outputs, last_state = tf.nn.seq2seq.rnn_decoder(inputs, initialstate, cell, loop_function=None, scope='rnnlm')
AttributeError: 'module' object has no attribute 'seq2seq'
I fixed some of them, but can't figure out what is the alternative to tf.nn.seq2seq.rnn_decoder and what should be the new module's parameters. What I currently fixed:
tf.nn.rnn_cell.BasicLSTMCell(embedsize) changed to
tf.contrib.rnn.BasicLSTMCell(embedsize)
tf.nn.rnn_cell.DropoutWrapper(lstm_cell,keep_prob) changed to tf.contrib.rnn.DropoutWrapper(lstm_cell,keep_prob)
tf.nn.rnn_cell.MultiRNNCell([lstm_cell] * numlayers) changed to
tf.contrib.rnn.MultiRNNCell([lstm_cell] * numlayers)
Can someone please help me figure out what tf.nn.seq2seq.rnn_decoder will be?
I think this is the one you need:
tf.contrib.legacy_seq2seq.rnn_decoder

NameError: name 'custom_data_home' is not defined

from sklearn.datasets import fetch_mldata
mnist = fetch_mldata('MNIST original', data_home=custom_data_home)
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
mnist = fetch_mldata('MNIST original', data_home=custom_data_home)
NameError: name 'custom_data_home' is not defined
i am getting NameError, i searched in net for solutions, i didn't get any relevant answers.
i even tries installing "custom_data_home" using easy_install . it says it could not find.
pls help me on this.
I don't know anything about sklearn, but it looks like you are trying to use the example from this page: http://scikit-learn.org/stable/datasets/mldata.html
In that example custom_data_home is a variable containing the path to where you want the data stored. If you leave it off it says it should default to just data.
Basically in your script you have not defined custom_data_home. That is what NameError is telling you.
If you are going to use a variable, like custom_data_home you have to define it in some way. Your script doesn't know what custom_data_home is.
custom_data_home = '/path/to/my/data'
mnist = fetch_mldata('MNIST original', data_home=custom_data_home)
That should work.

Categories