I am trying to implement DenseNet-121 model using tensorflow. I have already implemented the data pipeline and have already pre-processed the data which I have indicated by "some codes here" comment due to it being a bit lengthy(Will upload if needed) My code snippet is as follows:
import tensorflow as tf
''' Some Codes Here '''
from tensorflow.keras.layers import Input, Conv2D, BatchNormalization, Dense, AvgPool2D, GlobalAveragePooling2D, MaxPool2D, ReLU, concatenate
# Creating Densenet121
def densenet(input_shape, n_classes, filters = 32):
#batch norm + relu + conv
def bn_rl_conv(x,filters,kernel=1,strides=1):
x = BatchNormalization()(x)
x = ReLU()(x)
x = Conv2D(filters, kernel, strides=strides,padding = 'same')(x)
return x
def dense_block(x, repetition):
for _ in range(repetition):
y = bn_rl_conv(x, 4*filters)
y = bn_rl_conv(y, filters, 3)
x = concatenate([y,x])
return x
def transition_layer(x):
x = bn_rl_conv(x, tf.keras.backend.int_shape(x)[-1] //2 )
x = AvgPool2D(2, strides = 2, padding = 'same')(x)
return x
inp = Input (input_shape)
x = Conv2D(64, 7, padding = 'same', input_shape=input_shape)(inp)
x = MaxPool2D(3, strides = 2, padding = 'same')(x)
for repetition in [6,12,24,16]:
d = dense_block(x, repetition)
x = transition_layer(d)
x = GlobalAveragePooling2D()(d)
output = Dense(n_classes, activation = tf.nn.relu)(x)
model = tf.keras.Model(inp, output) # Line of error
return model
input_shape = (224, 224, 3)
n_classes = 3
model1 = densenet(input_shape,n_classes)
model1.add(Dense(1, activation='sigmoid'))
model1.summary()
The error I am receiving from the above line marked by comment is as follows:
File "C:\Users\USER\Ss\Image_Classification using DenseNet\main.py", line 131, in <module>
model1 = densenet(input_shape,n_classes)
File "C:\Users\USER\Ss\Image_Classification using DenseNet\main.py", line 125, in densenet
model = tf.keras.Model(inp, output) # Line of error
File "C:\Users\USER\AppData\Local\Programs\Python\Python39\Lib\site-packages\keras\engine\training.py", line 191, in __new__
from keras.engine import functional
File "C:\Users\USER\AppData\Local\Programs\Python\Python39\Lib\site-packages\keras\__init__.py", line 20, in <module>
from keras import distribute
File "C:\Users\USER\AppData\Local\Programs\Python\Python39\Lib\site-packages\keras\distribute\__init__.py", line 18, in <module>
from keras.distribute import sidecar_evaluator
File "C:\Users\USER\AppData\Local\Programs\Python\Python39\Lib\site-packages\keras\distribute\sidecar_evaluator.py", line 22, in <module>
from keras.optimizers.optimizer_experimental import (
File "C:\Users\USER\AppData\Local\Programs\Python\Python39\Lib\site-packages\keras\optimizers\__init__.py", line 26, in <module>
from keras.optimizers.legacy import adadelta as adadelta_legacy
File "C:\Users\USER\AppData\Local\Programs\Python\Python39\Lib\site-packages\keras\optimizers\legacy\adadelta.py", line 17, in <module>
from keras.optimizers.optimizer_v2 import adadelta
File "C:\Users\USER\AppData\Local\Programs\Python\Python39\Lib\site-packages\keras\optimizers\optimizer_v2\adadelta.py", line 21, in <module>
from keras.optimizers.optimizer_v2 import optimizer_v2
File "C:\Users\USER\AppData\Local\Programs\Python\Python39\Lib\site-packages\keras\optimizers\optimizer_v2\optimizer_v2.py", line 38, in <module>
keras_optimizers_gauge = tf.__internal__.monitoring.BoolGauge(
File "C:\Users\USER\AppData\Local\Programs\Python\Python39\Lib\site-packages\tensorflow\python\eager\monitoring.py", line 356, in __init__
super(BoolGauge, self).__init__('BoolGauge', _bool_gauge_methods,
File "C:\Users\USER\AppData\Local\Programs\Python\Python39\Lib\site-packages\tensorflow\python\eager\monitoring.py", line 131, in __init__
self._metric = self._metric_methods[self._label_length].create(*args)
AlreadyExistsError: Another metric with the same name already exists.
2022-10-27 01:01:14.175371: E tensorflow/core/lib/monitoring/collection_registry.cc:81] Cannot register 2 metrics with the same name: /tensorflow/api/keras/optimizers
Tensorflow version: 2.10.0
Keras version: 2.10.0
Please Help.
Edit: Found that after its execution and error in spyder, if I type import keras in terminal, it generates the same error, while if I type the same in say another IDE terminal alone, it generates no error during importing.