Using densenet with fastai - python

I am trying to train a densenet model using the fast.ai library. I checked the documentation and I managed to make it work for resnet50. However, for densenet, it seems to be unable to find the module.
I tried to use arch=models.dn121 as stated by this forum. But I get the same error.
Can anyone please help?
Here is the code:
learn = create_cnn(data, base_arch=models.densenet201, metrics=accuracy, model_dir="/tmp/model/")
This is the error:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-23-cb9ab3a79572> in <module>()
----> 1 learn = create_cnn(data, base_arch=models.densenet201, metrics=accuracy, model_dir="/tmp/model/")
AttributeError: module 'fastai.vision.models' has no attribute 'densenet201'

According to this post on the fast.ai forum, this is the solution to use densenet with fast.ai:
from torchvision.models import densenet121
def dn121(pre): return children(densenet121(pre))[0]
learn = create_cnn(data, dn121)

Related

"TypeError: cannot pickle 'weakref' object" error when loading deep learning h5 model and converting to pickle

I'm trying several ways because I'm stuck with capacity limitations during software deployment. Among them, I try to convert the model file into a pickle file, but an error like this appears.
TypeError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_1304/3588776463.py in <module>
----> 1 pickle.dump(model, open('model_t.pkl','wb'))
TypeError: cannot pickle 'weakref' object
The code I ran is this.
import joblib
from keras.models import load_model
import pickle
model = load_model("app/models/model_109600.h5")
pickle.dump(model, open('model_t.pkl','wb'))
I thought it was a file capacity problem and tried other files, but got the same error.
The size of the currently loaded file is 207 MB
If there is a problem and you have a solution, please share.
Or if there is another way, I would appreciate it if you could share it.

Tensorflow Backend - Bug in "model._make_predict_function"

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.

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.

Resolving "AttributeError: module 'dask.bag' has no attribute 'from_filenames'"

I am following a simple tutorial to load Reddit comment data from pushshift.io into a dask bag. I am getting the strange error: "Resolving "AttributeError: module 'dask.bag' has no attribute 'from_filenames'", despite the fact that this is standard procedure as described here: http://dask.pydata.org/en/doc-test-build/bag.html
import dask
import dask.bag as db
data = db.from_filenames("reddit_1_28_2018.txt", chunkbytes=100000).map(json.loads)
AttributeError Traceback (most recent call last)
<ipython-input-17-bcbd31affbfb> in <module>()
2 import dask.bag as db
3
----> 4 data = db.from_filenames("reddit_1_28_2018.txt", chunkbytes=100000).map(json.loads)
AttributeError: module 'dask.bag' has no attribute 'from_filenames'
I suspect that the resource you were looking at was very old. I recommend reading the Dask documentation for up-to-date information.
I suspect that you're looking for db.read_text

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

Categories