i am having problem while importing libraries in python - 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()

Related

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

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

Python: 'ModuleNotFoundError' when trying to import module in google colab

I am using Google Colab python 3, when import from python.aux.plot import choose_proj_from_xar i got error ModuleNotFoundError
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import xarray as xr
from matplotlib import animation, rc
from IPython.display import HTML
from python.aux.plot import choose_proj_from_xar
from python.aux.ml_flood_config import path_to_data
from python.aux.plot import Map
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-42-e570b5b49079> in <module>()
from IPython.display import HTML
---> from python.aux.plot import choose_proj_from_xar
from python.aux.ml_flood_config import path_to_data
from python.aux.plot import Map
ModuleNotFoundError: No module named 'python'
how to resolve this issue ? Thanks!

.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

Problem importing module: ModuleNotFoundError: No module named 'torch.utils.checkpoint'

I am following this example of ULMFiT. Here is the github.
I am running the model_forward_pass.ipynb in Google Colab. I am running the first code block which is this:
import pandas as pd
import numpy as np
import torch
!pip install fastai==0.7.0
!pip install torch
import fastai
from fastai.imports import *
from fastai.torch_imports import *
from fastai.core import *
from fastai.model import fit
from fastai.dataset import *
import torchtext
from torchtext import vocab, data
from torchtext.datasets import language_modeling
from fastai.rnn_reg import *
from fastai.rnn_train import *
from fastai.nlp import *
from fastai.lm_rnn import *
from fastai.text import *
import dill as pickle
!pip install spacy
!python -m spacy download en
import spacy
import html
import pickle
import collections
#!pip install nbimporter
import nbimporter
I am getting the following error:
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-bd3be060f151> in <module>()
6 import fastai
7 from fastai.imports import *
----> 8 from fastai.torch_imports import *
9 from fastai.core import *
10 from fastai.model import fit
3 frames
/usr/local/lib/python3.6/dist-packages/fastai/torch_imports.py in <module>()
1 import os
----> 2 import torch, torchvision, torchtext
3 from torch import nn, cuda, backends, FloatTensor, LongTensor, optim
4 import torch.nn.functional as F
5 from torch.autograd import Variable
/usr/local/lib/python3.6/dist-packages/torchvision/__init__.py in <module>()
1 import warnings
2
----> 3 from torchvision import models
4 from torchvision import datasets
5 from torchvision import ops
/usr/local/lib/python3.6/dist-packages/torchvision/models/__init__.py in <module>()
4 from .squeezenet import *
5 from .inception import *
----> 6 from .densenet import *
7 from .googlenet import *
8 from .mobilenet import *
/usr/local/lib/python3.6/dist-packages/torchvision/models/densenet.py in <module>()
3 import torch.nn as nn
4 import torch.nn.functional as F
----> 5 import torch.utils.checkpoint as cp
6 from collections import OrderedDict
7 from .utils import load_state_dict_from_url
ModuleNotFoundError: No module named 'torch.utils.checkpoint'
I found a similar question here, but I am still not sure how to fix my problem.
What should I do to fix this?

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