cannot import name 'Input' from 'tensorflow.keras.models' - python

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

Related

Why I can't import this module?

I'm trying to import img_to_array even though all packages are there. This error still appears:
Traceback (most recent call last):
File "c:\Users\Usuario\Downloads\keras-api\keras-main.py", line 11, in <module> from keras.preprocessing.image import img_to_array
ImportError: cannot import name 'img_to_array' from 'keras.preprocessing.image'
This is how I am calling the packages from the keras package.
from keras.preprocessing.image import img_to_array
On the other hand, this is what I'm trying to do with the code.
image = img_to_array(image)
However, I cannot even continue because I'm not able to import img_to_array
but this is how you import img_to_array if you have installed the keras package via e.g. pip install keras
from keras.utils import img_to_array
img_to_array
check if the keras version is the correct one:
Keras Installation Guide

i am having problem while importing libraries in python

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()

ImportError: cannot import name 'Deconvolution3D' from 'keras.layers' (/home/mona/venv/fall/lib/python3.8/site-packages/keras/layers/__init__.py)

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

.Error in importing keras.optimizers.schedules

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

tensorflow import error can somebody help me

import keras
from keras.models import Model
from keras.layers import Dense, Dropout, LSTM, Input, Activation
from keras import optimizers
import numpy as np
np.random.seed(4)
from tensorflow import set_random_seed
set_random_seed(4)
from util import csv_to_dataset, history_points
Using TensorFlow 2.0 as backend and got following errors.
Traceback (most recent call last):
File "C:/Users/sander/PycharmProjects/autotrader/basic_model.py", line 7, in <module>
from tensorflow import set_random_seed
ImportError: cannot import name 'set_random_seed' from 'tensorflow' (C:\Users\sander\venv\lib\site-packages\tensorflow\__init__.py)
What does this error mean?
set_random_seed is a function not a module, have you tried following steps?
In tf 1.x, do it as this:
import tensorflow as tf
tf.set_random_seed(4)
In tf 2.0, do this as this:
import tensorflow as tf
tf.compat.v1.set_random_seed(4)

Categories

Resources