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

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.

Related

Error: Module 'tensorflow' has no attribute 'gfile' error while running tensorflow object detection api tutorial

I am trying to use the object detection tutorial from tensor flow api. I am using python 3 and tensor flow version 2. But getting the below error.I tried several ways:
File "C:\Aniruddhya\object_detection\object_detection\utils\label_map_util.py", line 137, in load_labelmap
with tf.gfile.GFile(path, 'r') as fid:
AttributeError: module 'tensorflow' has no attribute 'gfile'
can someone help me to run this?
code link: https://drive.google.com/drive/u/3/folders/1XHpnr5rsENzOOSzoWNTvRqhEbLKXaenL
It's not called that in TensorFlow 2. You might be using a TensorFlow 1 tutorial.
Version 1
tf.gfile.GFile
https://www.tensorflow.org/versions/r1.15/api_docs/python/tf/io/gfile/GFile
Version 2
tf.io.gfile.GFile
https://www.tensorflow.org/api_docs/python/tf/io/gfile/GFile
If you have Tensorflow version 2. You can use the next module compatible with the version 1, too.
import tensorflow.compat.v1 as tf
I solved this problem by reinstalling tensor using the previous version: sudo pip3 install tensorflow==1.14.0
You may optionally downgrade to previous version of tensorflow:
!pip install tensorflow==1.12.0
import tensorflow as tf
print(tf.__version__)
otherwise , make if tf.io.gfile and import tf.io

Pandas-profiling error AttributeError: 'DataFrame' object has no attribute 'profile_report'

I wanted to use pandas-profiling to do some eda on a dataset but I'm getting an error : AttributeError: 'DataFrame' object has no attribute 'profile_report'
I have created a python script on spyder with the following code :
import pandas as pd
import pandas_profiling
data_abc = pd.read_csv('abc.csv')
profile = data_abc.profile_report(title='Pandas Profiling Report')
profile.to_file(output_file="abc_pandas_profiling.html")
AttributeError: 'DataFrame' object has no attribute 'profile_report'
The df.profile_report() entry point is available from v2.0.0. soln from here
Did you install pandas-profiling via pip or conda?
use : pip install -U pandas-profiling to solve this and restart your kernel
The issue is that the team hasn't updated the pip or conda installations yet (described here). If you installed using one of these, try this for the time being.
profile = pandas_profiling.ProfileReport(df)
print(profile)
This should work for those who want to use the latest version:
Run pip uninstall pandas_profiling from anaconda prompt (given you're using Spyder, I'd guess this would be your case) / or command prompt
Run pip install https://github.com/pandas-profiling/pandas-profiling/archive/master.zip
If you're using something like a Jupyter Notebook/Jupyter Lab, be sure to restart your kernel and re-import your packages.
I hope this helps.
For the those using google colabs, the profiling library is outdated, hence use the command below and restart the runtime
! pip install https://github.com/pandas-profiling/pandas-profiling/archive/master.zip
The only workaround I found was that the python script I made is getting executed from the command prompt and giving the correct output but the code is still giving an error in Spyder.
Some of the version of the pandas-profiling does not work for me and I installed 2.8.0 version and it work for me.
!pip install pandas-profiling==2.8.0
import numpy as np
import pandas as pd
import pandas_profiling as pp
df = pd.read_csv('/content/sample_data/california_housing_train.csv')
profile = df.profile_report(title = "Data Profiling Report")
profile.to_file("ProfileReportTest.html")
If none of the above worked, can you check by setting the encoding to unicode_escape in read_csv? It may be due to one of your columns
encoding = 'unicode_escape'
My solution
For me installation via pip was giving errors, therefore I installed it via conda from here.
Code Example
And here is the code example to use profile report:
import pandas as pd
from pandas_profiling import ProfileReport
data_abc = pd.read_csv('abc.csv')
profile = ProfileReport(data_abc, minimal=True)
profile.to_file("abc_pandas_profiling.html")
To read the html file I used the following code
df = pd.read_html("abc_pandas_profiling.html")
print(df[0])
Try in conda environment
!pip install --user pandas-profiling
import pandas_profiling
data.profile_report()

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

AttributeError: module 'networkx.algorithms.community' has no attribute 'best_partition'

well i am trying to use community detection algorithms by networkx on famous facebook snap data set.
here are my codes :
import networkx as nx
import matplotlib.pyplot as plt
from networkx.algorithms import community
from networkx.algorithms.community.centrality import girvan_newman
G_fb = nx.read_edgelist("./facebook_combined.txt",create_using = nx.Graph(), nodetype=int)
parts = community.best_partition(G_fb)
values = [parts.get(node) for node in G_fb.nodes()]
but when i'm run the cell i face with the title error which is :
AttributeError: module 'networkx.algorithms.community' has no attribute 'best_partition'
any advice ?
I think you're confusing the community module in networkx proper with the community detection in the python-louvain module which uses networkx.
If you install python-louvain, the example in its docs works for me, and generates images like
Note that you'll be importing community, not networkx.algorithms.community. That is,
import community
[.. code ..]
partition = community.best_partition(G_fb)
I faced this in CS224W
AttributeError: module 'community' has no attribute 'best_partition'
Pls change this file karate.py
replace import to
import community.community_louvain as community_louvain
then it works for me.
I had the same problem. In my case, it was solved importing the module in a different manner:
import community.community_louvain
Source
I also faced this in CS224W
but changing the karate.py or other solutions didn't work.
For me (in colab) using the new PyG installation code worked.
this code, will install the last version:
!pip install -q torch-scatter -f https://pytorch-geometric.com/whl/torch-1.9.0+cu102.html
!pip install -q torch-sparse -f https://pytorch-geometric.com/whl/torch-1.9.0+cu102.html
!pip install -q git+https://github.com/rusty1s/pytorch_geometric.git
I had a similar issue.
In my case, it was because on the other machine the library networkx was obsolete.
With the following command, the issues was solved.
pip3 install --upgrade networkx
I naively thought that pip install community was the package I was looking for but rather I needed pip install python-louvain which is then imported as import community.
This has helped me to run the code without errors:
pip uninstall community
import community.community_louvain as cl
partition = cl.best_partition(G_fb)

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