Context - I was getting errors when saving/loading EfficientNetB7 and one of the articles I saw suggested installing tf-nightly. Well that really blew things up in my Google Colab! I uninstalled tf-nightly, restarted my runtime, and now when I go to run my import cell, I get the errors below.
I reinstalled tensorflow 2.6.0.
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-25-5d853ee59f92> in <module>()
11 import tensorflow as tf
12 from tensorflow import keras
---> 13 from tensorflow.keras import Model
14 from tensorflow.keras.models import Sequential
15 from tensorflow.keras.utils import to_categorical
11 frames
/usr/local/lib/python3.7/dist-packages/keras/layers/core/dropout.py in <module>()
24
25 #keras_export('keras.layers.Dropout')
---> 26 class Dropout(base_layer.BaseRandomLayer):
27 """Applies Dropout to the input.
28
AttributeError: module 'keras.engine.base_layer' has no attribute 'BaseRandomLayer'
I also had an issue with this import before:
from tensorflow.keras import backend as keras
Here are all my imports:
import os
import glob
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from skimage.io import imread
from skimage.transform import resize
from sklearn.preprocessing import OneHotEncoder
from sklearn.model_selection import train_test_split
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import Model
from tensorflow.keras.models import Sequential
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.losses import categorical_crossentropy
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.layers import Dense, Flatten, Conv2D, MaxPooling2D, Dropout, BatchNormalization
from tensorflow.keras.callbacks import ModelCheckpoint, LearningRateScheduler
from datetime import datetime
from sklearn.metrics import confusion_matrix, accuracy_score, f1_score, precision_score, recall_score
import skimage.io as io
import skimage.transform as trans
from tensorflow.keras import models
from tensorflow.keras import layers
from tensorflow.keras import optimizers
from tensorflow.keras.models import *
from tensorflow.keras.layers import *
from tensorflow.keras.optimizers import *
from tensorflow.keras.callbacks import ModelCheckpoint, LearningRateScheduler
from tensorflow.keras import backend as keras
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from keras_preprocessing.image import ImageDataGenerator
from tensorflow.keras.optimizers.schedules import ExponentialDecay
import scipy.misc as sc
import h5py
from sklearn.semi_supervised import LabelSpreading
from sklearn.model_selection import GridSearchCV
from tensorflow.keras.applications import * #Efficient Net included here
import shutil
from sklearn import model_selection
from tqdm import tqdm
# import warnings
# warnings.filterwarnings("ignore", category=DeprecationWarning)
Try rolling back the python version to 3.6. If you are using Anaconda run
conda install python=3.6
Then install keras
pip install keras==2.0.8
Worked for me in Ubuntu.
I was able to get rid of the issue after installing tensorflow==2.8.0.
Related
I'm implementing a UNet neural network but I'm having some issues while importing libraries. I found a solution for a couple of them, but I still have a problem with these two imports:
from tensorflow.keras.optimizers import Adam
#Import "tensorflow.keras.optimizers" could not be resolved(reportMissingImports)
from tensorflow.keras.preprocessing.image import ImageDataGenerator
#Import "tensorflow.keras.preprocessing.image" could not be resolved(reportMissingImports)
I've already imported:
import tensorflow as tf
from tensorflow import keras
from keras import layers
import tensorflow_datasets as tfds
import matplotlib.pyplot as plt
import numpy as np
Any idea? I've tried to change but it still raises an error.
I have imported the following libraries for my machine learning project but have problem when I try to run my model in the command prompt...
from tensorflow.python.keras import Model
from tensorflow.python.keras.layers import Layer, Input, Conv2D, MaxPooling2D, Conv2DTranspose, concatenate, Lambda
from tensorflow.python.keras.initializers import TruncatedNormal
from keras.optimizers import Adam
from tensorflow.python.keras.callbacks import ModelCheckpoint, LearningRateScheduler, CSVLogger, Callback
from tensorflow.python.keras.models import load_model
from tensorflow.python.keras.utils import Sequence
This is the error message which I get when trying to run the model in the command prompt.
ImportError: cannot import name '__version__' from partially initialized module 'keras' (most likely due to a circular import) (C:\Users\gurun\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\__init__.py)
It is always recommend to use tensorflow.keras.* instead of tensorflow.python.*.
This is because anything under tensorflow.python.* is private, intended for development only, rather than for public use.
import tensorflow as tf
print(tf.__version__)
from tensorflow.keras import Model
from tensorflow.keras.layers import Layer, Input, Conv2D, MaxPooling2D, Conv2DTranspose, concatenate, Lambda
from tensorflow.keras.initializers import TruncatedNormal
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.callbacks import ModelCheckpoint, LearningRateScheduler, CSVLogger, Callback
from tensorflow.keras.models import load_model
from tensorflow.keras.utils import Sequence
Output:
2.5.0
You should be consistent and import keras only from one source and the recommended way, as #TFer2 said, is from tensorflow.keras . Your editor maybe can complain that tensorflow.keras cannot be resolved but usually that warning can be ignored and all works fine. If not try to uninstall keras and reinstall it accordingly to the version of your tensorflow installation.
When I run my code on Colab and on my machine using the CPU the accuracy of the network improves but when running the same code using PlaidML the accuracy never improves.
My requirements.txt file is as follows:
scikit-learn
scikit-image
matplotlib
seaborn
ipython
jupyter
scipy
numpy
pandas
pillow
pydot
coremltools==3.0
plaidml==0.6.4
plaidbench==0.6.4
keras==2.2.4
tensorflow==1.14
My imports are as follows:
import os
os.environ["KERAS_BACKEND"] = "plaidml.keras.backend"
import keras
import numpy as np
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import *
from keras import optimizers, callbacks
%matplotlib inline
import matplotlib.pyplot as plt
from keras.applications.mobilenet import MobileNet
There must be an issue with my requirements or my imports.
I'm having problem with tensorflow. I want to use ImageDataGenerator, but I'm receiving error ModuleNotFoundError: No module named 'tf'. Not sure what is the problem. I added this tf.version to test will it work, and it shows the version of tensorflow.
import tensorflow as tf
from tensorflow import keras
print(tf.__version__)
from tf.keras.preprocessing.image import ImageDataGenerator
When I run this code, I get this:
2.1.0
Traceback (most recent call last):
File "q:/TF/Kamen papir maaze/rks.py", line 14, in <module>
from tf.keras.preprocessing.image import ImageDataGenerator
ModuleNotFoundError: No module named 'tf'
The line
import tensorflow as tf
means you are importing tensorflow with an alias as tf to call it modules/functions.
You cannot use the alias to import other modules.
For your case, if you call directly
tf.keras.preprocessing.image.ImageDataGenerator(...)
then it will work.
or
you need to import the module with the right module name. i.e.
from tensorflow.keras.preprocessing.image import ImageDataGenerator
This works, tested on kaggle with tf_v2.6 and tf_v2.7
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.models import Sequential
...
in Tensorflow 2.0+, to use keras instead of tf use tensorflow always-
import tensorflow
from tensorflow.keras.preprocessing.image import ImageDataGenerator
I just try to import the packages first on Jupyter Notebook
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from sklearn.model_selection import train_test_split
import numpy as np
import matplotlib.pyplot as plt
but I got this error
AttributeError: module 'theano' has no attribute 'compile'