cannot import name 'imagenet_utils' from 'tensorflow.keras.applications' - python

i have the following python imports with in Jupyter Notebook.
import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.layers import Dense, Activation
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.metrics import categorical_crossentropy
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.preprocessing import image
from tensorflow.keras.models import Model
from tensorflow.keras.applications import imagenet_utils
from sklearn.metrics import confusion_matrix
import itertools
import os
import shutil
import random
import matplotlib.pyplot as plt
%matplotlib inline
But i keep getting the following error
ImportError: cannot import name 'imagenet_utils' from 'tensorflow.keras.applications' (C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\keras\api_v2\keras\applications_init_.py)
when i search for **cannot import name 'imagenet_utils' from 'tensorflow.keras.applications' **in google i dont get much helpful information.
Has anyone come across this at all?

change
from tensorflow.keras.applications import imagenet_utils
to
from keras.applications import imagenet_utils

i managed to solve my issue.
first i ran the following to update all modules.
conda update --all
Then i used 'from keras.applications import imagenet_utils'
instead of '#from tensorflow.keras import imagenet_utils'

Related

Module 'dateutil' has no attribute 'parser' - Dateutil - Python

I am trying to download a pre-trained tensorflow model. I am using the below code
import numpy as np
import time
import PIL.Image
import IPython.display as display
import matplotlib.pylab as plt
import tensorflow as tf
import tensorflow_hub as hub
import datetime
from tensorflow.keras.preprocessing import image
from dateutil import parser
from keras.applications.inception_v3 import InceptionV3
model = InceptionV3()
model.summary()
I am getting the following error
AttributeError: module 'dateutil' has no attribute 'parser'
I am using python -3.7 and TF-2.7, python-dateutil-2.8.1
Please help me fix this. Thank You :)
The correct import syntax is:
import dateutil.parser
and then:
parser.parse(time_string)
or:
from dateutil.parser import parse
parse(time_string)
Documentation: https://dateutil.readthedocs.io/en/stable/parser.html

ImportError: cannot import name 'LayerNormalization' from 'tensorflow.python.keras.layers.normalization' mismatch versions fix?

Have not posted much on stackoverflow and new to tensorflow. I followed this tutorial on how to install tensorflow for use on jupyter notebooks (https://github.com/jeffheaton/t81_558_deep_learning/blob/master/install/tensorflow-install-mac-jan-2021.ipynb).
When testing the environment I get Python 3.8.11, Tensor Flow 2.5.1 and Keras 2.5.0
Code I am trying to run:
import pandas as pd
import numpy as np
import tensorflow as tf
import re
from nltk.corpus import stopwords
from sklearn.model_selection import train_test_split
from sklearn.metrics import median_absolute_error as mae
from sklearn.metrics import mean_squared_error as mse
from sklearn.metrics import accuracy_score as acc
import matplotlib.pyplot as plt
from keras.models import Sequential
from keras import initializers
from keras.layers import Dropout, Activation, Embedding, Convolution1D, MaxPooling1D, Input, Dense, Merge, \
BatchNormalization, Flatten, Reshape, Concatenate
from keras.layers.recurrent import LSTM, GRU
from keras.callbacks import Callback, ModelCheckpoint, EarlyStopping, ReduceLROnPlateau
from keras.models import Model
from keras.optimizers import Adam, SGD, RMSprop
from keras import regularizers
The error I get is
ImportError Traceback (most recent call last)
<ipython-input-4-3a64b96c6d78> in <module>
10 import matplotlib.pyplot as plt
11
---> 12 from keras.models import Sequential
13 from keras import initializers
14 from keras.layers import Dropout, Activation, Embedding, Convolution1D, MaxPooling1D, Input, Dense, Merge, \
/opt/miniconda3/envs/tensorflow/lib/python3.8/site-packages/keras/__init__.py in <module>
23
24 # See b/110718070#comment18 for more details about this import.
---> 25 from keras import models
26
27 from keras.engine.input_layer import Input
/opt/miniconda3/envs/tensorflow/lib/python3.8/site-packages/keras/models.py in <module>
18 import tensorflow.compat.v2 as tf
19 from keras import backend
---> 20 from keras import metrics as metrics_module
21 from keras import optimizer_v1
22 from keras.engine import functional
/opt/miniconda3/envs/tensorflow/lib/python3.8/site-packages/keras/metrics.py in <module>
25
26 import numpy as np
---> 27 from keras import activations
28 from keras import backend
29 from keras.engine import base_layer
/opt/miniconda3/envs/tensorflow/lib/python3.8/site-packages/keras/activations.py in <module>
18
19 from keras import backend
---> 20 from keras.layers import advanced_activations
21 from keras.utils.generic_utils import deserialize_keras_object
22 from keras.utils.generic_utils import serialize_keras_object
/opt/miniconda3/envs/tensorflow/lib/python3.8/site-packages/keras/layers/__init__.py in <module>
146
147 # Normalization layers.
--> 148 from keras.layers.normalization import LayerNormalization
149 from keras.layers.normalization_v2 import SyncBatchNormalization
150
ImportError: cannot import name 'LayerNormalization' from 'keras.layers.normalization' (/opt/miniconda3/envs/tensorflow/lib/python3.8/site-packages/keras/layers/normalization/__init__.py)
I am aware that there have been posts about mismatch versions being the reason for this not working but not sure how I would go about independently changing the versions for tensorflow etc.. to make this work.
Try again with Tensorflow 2.5 and Keras 2.4.3 .
providing gist for reference

KeyError: 'input_1_ib-0' when i save my autoencoder model

I get the error when save my Autoencoder model.
This is my code. I have searched about this error but no solution help me solve this problem.
The error photo:
.
Probably it is due to mixing keras and tesnorflow libraries. Use from tensorflow.keras.optimizers import Adam and from tensorflow.keras.models import Model, load_model instead of keras ones.
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.keras.layers import Conv2D, Input, BatchNormalization, MaxPooling2D, UpSampling2D, Dropout, BatchNormalization
#from keras.models import Model, load_model
from tensorflow.keras.models import Model, load_model # tensorflow.keras
from tensorflow.keras.callbacks import ModelCheckpoint
from google.colab.patches import cv2_imshow
from tensorflow.keras.preprocessing.image import ImageDataGenerator
#from keras.optimizers import Adam
from tensorflow.keras.optimizers import Adam # tensorflow.keras

getting error while freezing first few layers in vgg19

import tensorflow as tf
from tensorflow.keras import Sequential,Model
from tensorflow.keras.layers import
Dense,Dropout,Flatten,BatchNormalization,Conv2D,MaxPool2D
from tensorflow.keras.applications.vgg19 import VGG19,preprocess_input
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import numpy as np
#defining the image size
num_classes=7
img_width=224
img_height=224
batch_size_training=32
batch_size_validation=32
data_generator=ImageDataGenerator(preprocessing_function=preprocess_inpu)
Fetching the image from directory:
train_generator=data_generator.flow_from_directory
('C:/Users/pauls/Desktop/Python Rev1/Dataset/Train/',target_size=
(224,224),batch_size=20,class_mode='categorical')
test_generator=data_generator.flow_from_directory
('C:/Users/pauls/Desktop/Python Rev1/Dataset/Test/',target_size=
(224,224),batch_size=20,class_mode='categorical')

How to solve a below error to import merge from keras?

import re
from collections import Counter
from itertools import chain
import numpy as np
import pandas as pd
from nltk.corpus import stopwords
from sklearn.metrics import mean_absolute_error as mse
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression,huber
from sklearn.svm import SVR
from sklearn.ensemble import RandomForestRegressor
from sklearn.neighbors import KNeighborsRegressor
from sklearn.ensemble import AdaBoostRegressor, GradientBoostingRegressor
from util import shortWord_Dictionary, load_glove
These are the libraries i am trying to import but i am getting this error.
i have tried few other solutions such as importing subclasses like add and concentrate but still it is not working, Help me out here
Error is:
ImportError Traceback (most recent call last)
<ipython-input-9-acc83d608c96> in <module>
15 import tensorflow.contrib.keras as keras
16
---> 17 from util import shortWord_Dictionary, load_glove
~\Downloads\thesis-final\thesis-final\Microsoft\util.py in <module>
6 from keras import initializers
7 from keras.callbacks import ModelCheckpoint, EarlyStopping, ReduceLROnPlateau
----> 8 from keras.layers import Dropout, Embedding, Convolution1D, Dense, Merge
9 from keras.layers.recurrent import LSTM
10 from keras.models import Sequential
ImportError: cannot import name 'Merge' from 'keras.layers' (C:\Users\DARSHIT\Anaconda3\Anaconda\lib\site-packages\keras\layers\__init__.py)
keras doesn't have Merge layers, see the docs. Merge is just the name that they gave to a subset of the layers, namely Add, Multiply, or Average.

Categories