Tensorflow/Keras - ImportError: cannot import name 'Sequence' - python

Trying to run some machine learning examples so am importing some packages
sklearn version 0.22.1
tensorflow version 2.3.0
keras version 2.3.0
in the following way:
import os
import sys
import math
import numpy as np
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
from keras.models import Sequential
from keras.layers import Dense, LSTM
import matplotlip.pyplot as plt
I am getting the import error:
ImportError: cannot import name 'Sequence'
Full traceback:
File "C:\Anaconda\lib\site-packages\keras\__init__.py", line 12, in <module>
from . import callbacks
File "C:\Anaconda\lib\site-packages\keras\callbacks\__init__.py", line 3, in <module>
from .callbacks import Callback
File "C:\Anaconda\lib\site-packages\keras\callbacks\callbacks.py", line 23, in <module>
from ..engine.training_utils import standardize_input_data
File "C:\Anaconda\lib\site-packages\keras\engine\training_utils.py", line 18, in <module>
from ..utils import Sequence
ImportError: cannot import name 'Sequence'
I have looked on line for help trying a few different tensorflow and keras versions. I have also tried:
import tensorflow.keras as keras
and import tensorflow.keras.utils import Sequence
without success

Related

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

Cannot import name SummaryWriter

I'm using Pycharm with python 3.9, torch 1.8.1+cu111 and tensorboard 2.6.0.
When I try to import SummaryWriter by:
from torch.utils.tensorboard import SummaryWriter
I get the following error:
*Traceback (most recent call last):
File "/data/heisery/Yaronhome/PycharmProjects/CSSC_pytorch/tensorboard.py", line 18, in <module>
from torch.utils.tensorboard import SummaryWriter
File "/usr/local/lib/python3.8/dist-packages/torch/utils/tensorboard/__init__.py", line 1, in <module>
import tensorboard
File "/data/heisery/Yaronhome/PycharmProjects/CSSC_pytorch/tensorboard.py", line 18, in <module>
from torch.utils.tensorboard import SummaryWriter*
**ImportError: cannot import name 'SummaryWriter' from partially initialized module 'torch.utils.tensorboard' (most likely due to a circular import) (/usr/local/lib/python3.8/dist-packages/torch/utils/tensorboard/__init__.py)**
This is my import list:
import torch
import torch.nn as nn
from torch.utils.data import DataLoader
import torchvision
import torchvision.transforms as transforms
import matplotlib.pyplot as plt
import sys
from torch.utils.tensorboard import SummaryWriter
writer = SummaryWriter('runs/mnist')
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
Bro
I have just solved a similar problem like yours. Just ensure that your filename is not set as 'tensorboard'.

cannot import name 'BatchNormalization' from 'keras.layers.normalization'

I'm learning ObjectDetection from this website
I have installed ImageAI,Tensorflow and Keras.
Then when I run this in python
from imageai.Detection import ObjectDetection
I got
Traceback (most recent call last):
File "", line 1, in
File "/home/carl/python-environments/env/lib/python3.9/site-packages/imageai/Detection/init.py", line 17, in
from imageai.Detection.YOLOv3.models import yolo_main, tiny_yolo_main
File "/home/carl/python-environments/env/lib/python3.9/site-packages/imageai/Detection/YOLOv3/models.py", line 8, in
from keras.layers.normalization import BatchNormalization
ImportError: cannot import name 'BatchNormalization' from 'keras.layers.normalization' (/home/carl/python-environments/env/lib/python3.9/site-packages/keras/layers/normalization/init.py)
Already tried:
from keras.layers.normalization.batch_normalization import BatchNormalization
from tensorflow.keras.layers import BatchNormalization
But still gave me the same error.
Tensorflow v2.8.0 support tf.keras.layers.BatchNormalization()
import tensorflow as tf
tf.keras.layers.BatchNormalization()

PyInstaller error: ModuleNotFoundError: No module named 'statsmodels.__init__._version'

I am attempting to build an exe file that utilizes statsmodels via PyInstaller, from the terminal. The exe file is built by PyInstaller without error--however when run I get the following error:
ModuleNotFoundError: No module named 'statsmodels.__init__._version'
I have tried installing the statsmodels hook in the PyInstaller "hooks" directory, as described here, however that does not work. I have also attempted to explicitly import statsmodels.init._version in my Python code, but that has also not worked. Below is the top of my code where I import the packages:
import pandas as pd
from sklearn import preprocessing
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.neighbors import NearestNeighbors, KNeighborsClassifier
import numpy as np
import matplotlib.pylab as plt
from sklearn.linear_model import LogisticRegression
import statsmodels.api as sm
from scipy import stats
from sklearn import datasets, neighbors
from mlxtend.plotting import plot_decision_regions
import seaborn as sns
from tqdm import tqdm
import matplotlib.ticker as mtick
from tkinter import *
from tkinter.filedialog import askopenfilename
import sklearn.utils._cython_blas
import sklearn.neighbors.typedefs
import statsmodels.tsa.statespace._filters
import statsmodels.tsa.statespace._filters._conventional
import statsmodels.tsa.statespace._filters._univariate
import statsmodels.tsa.statespace._filters._univariate_diffuse
import statsmodels.tsa.statespace._filters._inversions
import statsmodels.tsa.statespace._smoothers
import statsmodels.tsa.statespace._smoothers._conventional
import statsmodels.tsa.statespace._smoothers._univariate
import statsmodels.tsa.statespace._smoothers._univariate_diffuse
import statsmodels.tsa.statespace._smoothers._classical
import statsmodels.tsa.statespace._smoothers._alternative
import statsmodels.__init__._version
Here is the traceback of the error:
File "LR.py", line 9, in <module>
File "/opt/anaconda3/lib/python3.7/site-packages/PyInstaller/loader/pyimod03_importers.py", line 493, in exec_module
exec(bytecode, module.__dict__)
File "statsmodels/api.py", line 32, in <module>
File "/opt/anaconda3/lib/python3.7/site-packages/PyInstaller/loader/pyimod03_importers.py", line 493, in exec_module
exec(bytecode, module.__dict__)
File "statsmodels/__init__.py", line 2, in <module>
ModuleNotFoundError: No module named 'statsmodels.__init__._version'
What can I do to fix this so that the exe file includes the statsmodels package and can be run?
statsmodels version is accessed through the __version__ attribute.
import statsmodels
print(statsmodels.__version__)
which shows
v0.12.0rc0+20.g98dfc0073
on my system.
You should replace import statsmodels.__init__._version with from statsmodels.__init__ import __version__

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