ImportError Traceback (most recent call last)
in
----> 1 from keras.optimizers import schedules
ImportError: cannot import name 'schedules' from 'keras.optimizers' (C:\Users\hp\Roaming\anaconda3-64bit\lib\site-packages\keras\optimizers.py)
I have tensorflow version 1.13.1 and keras version 2.3.1 and conda version 4.6.14. I can import keras.optimizers. I can import every function in it except schedules. Even from keras.optimizers import * works.
Try this sequence to see if you have everything installed:
from tensorflow import keras
from tensorflow.keras import optimizers
from tensorflow.keras.optimizers import schedules
if this doesn't work, also check the output of conda list:
(ds_tensorflow) c:\temp>conda list | grep keras
keras 2.3.1 0
keras-applications 1.0.8 py_0
keras-base 2.3.1 py37_0
keras-preprocessing 1.1.0 py_1
Version 2 of Tensorflow
"Lib/site-packages/tensorflow_core/python/keras/api/_v2/keras/optimizers/schedules/init.py
10 from . import schedules
Version 1.13.1 of Tensorflow
/tf11/Lib/site-packages/tensorflow/contrib/keras/api/keras/optimizers/init.py
# Optimizer classes.
22 from tensorflow.python.keras.optimizers import Adadelta
23 from tensorflow.python.keras.optimizers import Adagrad
24 from tensorflow.python.keras.optimizers import Adam
25 from tensorflow.python.keras.optimizers import Adamax
26 from tensorflow.python.keras.optimizers import Nadam
27 from tensorflow.python.keras.optimizers import Optimizer
28 from tensorflow.python.keras.optimizers import RMSprop
29 from tensorflow.python.keras.optimizers import SGD
30
31 # Auxiliary utils.
32 # pylint: disable=g-bad-import-order
33 from tensorflow.python.keras.optimizers import deserialize
34 from tensorflow.python.keras.optimizers import serialize
35 from tensorflow.python.keras.optimizers import get
Related
code:
import warnings
warnings.filterwarnings("ignore")
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import os
from PIL import Image
import matplotlib
from sklearn.model_selection import train_test_split
import tensorflow as tf
import keras
from keras.models import *
from keras.layers import *
from keras.callbacks import *
from tensorflow.keras.optimizers import *
from tqdm import tqdm
import cv2
import segmentation_models as sm
%load_ext tensorboard
import datetime
from keras.utils.vis_utils import plot_model
error:
AttributeError Traceback (most recent call last)
<ipython-input-19-fb82ca23d179> in <module>
20 import cv2
21 import tensorflow as tf
---> 22 import segmentation_models as sm
23 model = tf.keras.applications.segmentation_model(model_name='mask_rcnn', weights='imagenet')
24 get_ipython().run_line_magic('load_ext', 'tensorboard')
3 frames
/usr/local/lib/python3.8/dist-packages/efficientnet/__init__.py in init_keras_custom_objects()
69 }
70
---> 71 tensorflow.python.keras.utils.generic_utils.get_custom_objects().update(custom_objects)
72
73
AttributeError: module 'keras.utils.generic_utils' has no attribute 'get_custom_objects'
libraries should be imported successfully.
my tensorflow version is 2.11.0
and i am using google colab
Either downgrade tensorflow to version 1.x or upgrade segmentation_models so that it can work with tensorflow_2.x. If you can't do both of these then use the compat behavior of TensorFlow as follows.
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
How should I fix the following error?
ImportError: cannot import name 'Deconvolution3D' from 'keras.layers' (/home/mona/venv/fall/lib/python3.8/site-packages/keras/layers/__init__.py)
I get:
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-14-8ac95d90a6a0> in <module>
4 import config
5 from trainer.fusiondiffroigan import Params,Fusion_Diff_ROI_3DCAE_GAN3D
----> 6 from models import diff_ROI_C3D_AE_no_pool
~/research/code/GAN-fall/Fall-detection/mrfd/models.py in <module>
5 from keras.layers import Activation, Dropout, Flatten, Dense, Input, Reshape, BatchNormalization
6 # from keras.layers import Conv3DTranspose as Deconvolution3D
----> 7 from keras.layers import Deconvolution3D
8 from keras.optimizers import SGD
9 from keras import regularizers
ImportError: cannot import name 'Deconvolution3D' from 'keras.layers' (/home/mona/venv/fall/lib/python3.8/site-packages/keras/layers/__init__.py)
I have:
Python 3.8.5 (default, Jan 27 2021, 15:41:15)
[GCC 9.3.0] on Linux
$ lsb_release -a
LSB Version: core-11.1.0ubuntu2-noarch:security-11.1.0ubuntu2-noarch
Distributor ID: Ubuntu
Description: Ubuntu 20.04.2 LTS
Release: 20.04
Codename: focal
Actually, on TensorFlow 2, Deconvolution3D has been renamed Conv3DTranspose.
So you can use:
from tensorflow.keras.layers import Conv3DTranspose
So we now have a cleaner:
Conv2D and its "reversed" Conv2DTranspose
Conv3D and its "reversed" Conv3DTranspose
try this
import tensorflow as tf
from tensorflow.keras.layers import Deconvolution3D
I am running TensorFlow 2.0 GPU (CUDA), Keras, & Python 3.7 in Windows 10. i am getting the following error:
ImportError Traceback (most recent call last)
<ipython-input-8-2fd313bcdf5e> in <module>
9 from tensorflow.keras.initializers import RandomNormal
10 from tensorflow.keras.models import Model
---> 11 from tensorflow.keras.models import Input
12 from tensorflow.keras.layers import Conv2D
13 from tensorflow.keras.layers import Conv2DTranspose
ImportError: cannot import name 'Input' from 'tensorflow.keras.models' (C:\Users\Syd_R\anaconda3\envs\tensorflow\lib\site-packages\tensorflow_core\python\keras\api\_v2\keras\models\__init__.py)
It should be from tensorflow.keras.layers import Input.
This works for me:
from tensorflow.python.keras.models import Input
They change location in newer version:
from tensorflow.keras import Input
Background
I'm trying to work on a GAN neural network (I'm a beginner for both Python and Machine-Learning), and I need Tensorflow.
Problem
I have tried to use TensorFlow but can't install. I have read questions and answers on SO about various errors, and have tested out those solutions, but I believe this case is different.
What I have tried (in chronological order)
1. Plain Reboot
a) Close all tabs for Jupyter Notebook
b) Close Anaconda Navigator
c) Restart Jupyter Notebook
d) Rerun code
Result: ImportError: no module
2. Reinstall tf
a) Repeat 1a and 1b
b) Open Anaconda Prompt
c) pip install tensorflow
Result: module installed
3. Check out Navigator
Tensorflow installed in all environments I have.
4. Reinstall tf (Take 2)
a) Repeat 1a and 1b
b) Open Anaconda Prompt
c) conda install -c conda-forge tensorflow
Result: EnvironmentNotWritableError:The current user does not have write permissions to the target environment. environment location: C:\ProgramData\<my username>
5. Run as admin (from this question)
a) Repeat 1a and 1b
b) Open Anaconda Prompt
c) conda install -c conda-forge tensorflow
Result:
Preparing transaction: done
Verifying transaction: done
Executing transaction: <after some text> done
6. Run in Jupyter
The code has nothing to do with TF at the moment, but still, it doesn't work.
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import os
import glob2
font_lib = glob2.glob('**/*.ttf', recursive=True)
count = 0
for f in font_lib:
count = count + 1
if count < 10:
print (f)
else:
break
print ("done")
Result:
AttributeError Traceback (most recent call last)
<ipython-input-6-efbffb1990be> in <module>
----> 1 import tensorflow as tf
2 import numpy as np
3 import matplotlib.pyplot as plt
4 import os
5 import glob2
C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\__init__.py in <module>
22
23 # pylint: disable=g-bad-import-order
---> 24 from tensorflow.python import pywrap_tensorflow # pylint: disable=unused-import
25
26 from tensorflow._api.v1 import app
C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\__init__.py in <module>
80 from tensorflow.python import data
81 from tensorflow.python import distribute
---> 82 from tensorflow.python import keras
83 from tensorflow.python.feature_column import feature_column_lib as feature_column
84 from tensorflow.python.layers import layers
C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\keras\__init__.py in <module>
23
24 from tensorflow.python.keras import activations
---> 25 from tensorflow.python.keras import applications
26 from tensorflow.python.keras import backend
27 from tensorflow.python.keras import callbacks
C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\keras\applications\__init__.py in <module>
24 from tensorflow.python.keras import backend
25 from tensorflow.python.keras import engine
---> 26 from tensorflow.python.keras import layers
27 from tensorflow.python.keras import models
28 from tensorflow.python.keras import utils
C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\keras\layers\__init__.py in <module>
27
28 # Advanced activations.
---> 29 from tensorflow.python.keras.layers.advanced_activations import LeakyReLU
30 from tensorflow.python.keras.layers.advanced_activations import PReLU
31 from tensorflow.python.keras.layers.advanced_activations import ELU
C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\keras\layers\advanced_activations.py in <module>
25 from tensorflow.python.keras.engine.base_layer import Layer
26 from tensorflow.python.keras.engine.input_spec import InputSpec
---> 27 from tensorflow.python.keras.utils import tf_utils
28 from tensorflow.python.ops import math_ops
29 from tensorflow.python.util.tf_export import tf_export
C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\keras\utils\__init__.py in <module>
36 from tensorflow.python.keras.utils.layer_utils import get_source_inputs
37 from tensorflow.python.keras.utils.losses_utils import squeeze_or_expand_dimensions
---> 38 from tensorflow.python.keras.utils.multi_gpu_utils import multi_gpu_model
39 from tensorflow.python.keras.utils.np_utils import normalize
40 from tensorflow.python.keras.utils.np_utils import to_categorical
C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\keras\utils\multi_gpu_utils.py in <module>
20 from tensorflow.python.framework import ops
21 from tensorflow.python.keras import backend as K
---> 22 from tensorflow.python.keras.engine.training import Model
23 from tensorflow.python.ops import array_ops
24 from tensorflow.python.util.tf_export import tf_export
C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py in <module>
40 from tensorflow.python.keras.engine import training_generator
41 from tensorflow.python.keras.engine import training_utils
---> 42 from tensorflow.python.keras.engine.network import Network
43 from tensorflow.python.keras.optimizer_v2 import optimizer_v2
44 from tensorflow.python.keras.utils import data_utils
C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\network.py in <module>
38 from tensorflow.python.keras.engine import base_layer
39 from tensorflow.python.keras.engine import base_layer_utils
---> 40 from tensorflow.python.keras.engine import saving
41 from tensorflow.python.keras.engine import training_utils
42 from tensorflow.python.keras.utils import generic_utils
C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\saving.py in <module>
36 # pylint: disable=g-import-not-at-top
37 try:
---> 38 import h5py
39 HDF5_OBJECT_HEADER_LIMIT = 64512
40 except ImportError:
C:\ProgramData\Anaconda3\lib\site-packages\h5py\__init__.py in <module>
34 _errors.silence_errors()
35
---> 36 from ._conv import register_converters as _register_converters
37 _register_converters()
38
h5py\h5r.pxd in init h5py._conv()
h5py\h5r.pyx in init h5py.h5r()
AttributeError: type object 'h5py.h5r.Reference' has no attribute '__reduce_cython__'
7. Update h5py
a) Followed #Alireza Tajadod's instructions and tried.
b) Run code in Jupyter
Result: Same as 6)...
I have tried every method I could, and any help would be highly appreciated. Thank you in advance!
Edit:
Reminded by the answer by #GarytheIceBreaker: Sorry that I forgot to mention, but I have everything installed and set up in Windows. Although this might be frustrating to some, please suggest solutions that can be done within Windows OS premises. Thanks!
I tried jumping through hoops on Windows, and did get Anaconda working, but not Tensorflow. I recommend running Ubuntu virtually, on WSL if you don't want to make any major changes to your machine. Ubuntu is pretty user friendly these days, even if you use it without any graphical shell enabled.
Enable WSL, install Ubuntu from the Microsoft store, and apt-get install tensorflow.
I am trying to load tensorflow within jupyter notebook and receive the following error:
ImportError Traceback (most recent call last) <ipython-input-1-28a19874e1dd> in <module>()
5 import tensorflow as tf
6 from tensorflow.python.framework import ops
----> 7 from tf_utils import load_dataset, random_mini_batches, convert_to_one_hot, predict
8
9 get_ipython().magic(u'matplotlib inline')
/Pathway/tf_utils.py in <module>()
17 import sys
18 import tensorflow as tf
---> 19 import src.utils as utils
20 import logging
21 from tensorflow.contrib import slim
ImportError: No module named src.utils
I have the latest tensorflow installed and have also added models to the PYTHONPATH via:
export PYTHONPATH="$PYTHONPATH:/Pathway/tfmodels-1.9.0"
Do you know how I can resolve this import error?
To resolve this import error I manually imported the missing functions from the following github repo > https://github.com/andersy005/deep-learning-specialization-coursera/blob/master/02-Improving-Deep-Neural-Networks/week3/Programming-Assignments/tf_utils.py