How to use densenet in Keras - python-3.x

I notice densenet has been added to keras (https://github.com/keras-team/keras/tree/master/keras/applications)and I want to apply it in my project but when I tried to import it in jupyter anaconda, I got an error saying:
module 'keras.applications' has no attribute 'densenet'
it seems like densenet has not been incorporated into current version of keras.
Any idea how can I add it myself?

Densenet was added in keras version 2.1.3. What version of keras are you running?
Have you tried to update keras with pip install keras --upgrade since January?

Related

AttributeError: module 'tensorflow_core.keras.layers.experimental.preprocessing' has no attribute 'RandomFlip'

I use Tensorflow 2.1.0
In this code
data_augmentation = tf.keras.Sequential([
tf.keras.layers.experimental.preprocessing.RandomFlip('horizontal'),
tf.keras.layers.experimental.preprocessing.RandomRotation(0.3)
])
I find this error:
AttributeError: module 'tensorflow_core.keras.layers.experimental.preprocessing' has no attribute 'RandomFlip'
So how can I change it without changing version of tensorflow
To work your code as expected, firstly Tensorflow has to be upgrade to the latest version
! pip install tensorflow --upgrade
If you are looking for solution in TF 2.1.0, then there are two options are available
First solution: tf.image.random_flip_left_right ( horizontal flip)
tf.image.random_flip_left_right(
image, seed=None)
Second solution: tf.keras.preprocessing.image.ImageDataGenerator
tf.keras.preprocessing.image.ImageDataGenerator(
rotation_range=30, horizontal_flip=True)
! pip install tensorflow --upgrade --user
--user option can help you without the permission problem
Add this line to the importing section (of course after import tensorflow as tf)
tf.config.experimental_run_functions_eagerly(True)
Almost any tf.keras.layers.experimental.preprocessing.SomeClass in the listed classes here, should work.
But need to do sanity check with plotting results.

AttributeError: module 'tensorflow' has no attribute 'get_default_graph'. I am getting this error when adding a new layer to a model

ml = Sequential()
ml.add(LSTM(64,dropout=0.5, recurrent_dropout=0.5,return_sequences=True))
This is giving me a error: AttributeError: module 'tensorflow' has no attribute 'get_default_graph'.
ml is the name of the model. I am unable to add any layers to a model. Please help.
The dependencies that I am using are:
Tensorflow: 2.0.0-beta1
Keras: 2.2.4
Python: 3.7.3
Upgrade to latest Tensorflow version instead of Beta Version.And use the import functions as mentioned below.
To install:
pip install tensorflow==2.2.0
#OR
pip install --upgrade tensorflow
Importing:
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM
ml = Sequential()
ml.add(LSTM(64,dropout=0.5, recurrent_dropout=0.5,return_sequences=True))
Your code should work fine now.

After installing Tensorflow 2.0 in a python 3.7.1 env, do I need to install Keras, or does Keras come bundled with TF2.0?

I need to use Tensorflow 2.0(TF2.0) and Keras but I don't know if it's necessary to install both seperately or just TF2.0 (assuming TF2.0 has Keras bundled inside it). If I need to install TF2.0 only, will installing in a Python 3.7.1 be acceptable?
This is for Ubuntu 16.04 64 bit.
In Tensorflow 2.0 there is strong integration between TensorFlow and the Keras API specification (TF ships its own Keras implementation, that respects the Keras standard), therefore you don't have to install Keras separately since Keras already comes with TF in the tf.keras package.

module 'tensorflow_hub' has no attribute 'KerasLayer'

When I'm trying to retrain the model with tensorflow it shows an error:
**error module 'tensorflow_hub' has no attribute 'KerasLayer'**
The code is:
print("Building model with", MODULE_HANDLE)
model = tf.keras.Sequential([
hub.KerasLayer(MODULE_HANDLE, output_shape=[FV_SIZE],
trainable=do_fine_tuning),
tf.keras.layers.Dropout(rate=0.2),
tf.keras.layers.Dense(train_generator.num_classes,
activation='softmax',
kernel_regularizer=tf.keras.regularizers.l2(0.0001))
])
model.build((None,)+IMAGE_SIZE+(3,))
model.summary()
The error is like:
1 print("Building model with", MODULE_HANDLE)
2 model = tf.keras.Sequential([
----> 3 hub.KerasLayer(MODULE_HANDLE, output_shape=[FV_SIZE],
4 trainable=do_fine_tuning),
5 tf.keras.layers.Dropout(rate=0.2),
AttributeError: module 'tensorflow_hub' has no attribute 'KerasLayer'
by using the tensorflow hub retrain the previous hub model by adding new dence fully connected layers.when run the code it show the above error.is any have idea about that.please help
Please check the tensorflow version. It should be a recent nightly version.
When I use a version like 1.13.1, I see the following warning before the error, no attribute 'KerasLayer':
W0423 20:04:16.453974 139707130586880 __init__.py:56] Some hub symbols are not available because TensorFlow version is less than 1.14
After, doing pip install "tf-nightly", everything works fine.
https://www.tensorflow.org/hub
For the BatchNormalizationv1 issue, you can use tf2.0 nightly which should also take care of the original issue.
pip install -U tf-nightly-2.0-preview
https://github.com/tensorflow/tfjs/issues/1255
hub.KerasLayer works with TF2 pre releases:
pip install tf-nightly-2.0-preview --quiet
pip install tensorflow==2.0.0-alpha
pre-release candidate for GPU:
pip install -U --pre tensorflow-gpu

How do I use a previous version of Keras (0.3.1) on Colaboratory?

I tried pip installing 0.3.1, but when I print the version it outputs 2.1.4.
!pip install keras==0.3.1
import keras
print keras._version__
I am trying to train deepmask (https://github.com/abbypa/NNProject_DeepMask/) for which I specifically need 0.3.1.
Note that if you've already loaded keras, then the second import statement has no effect.
So first !pip install keras==0.3.1, then restart your kernel (ctrl-m . or Runtime -> Restart runtime) and then things should work as expected.

Resources