Related
I am training a VQVAE with this dataset (64x64x3). I have downloaded it locally and loaded it with keras in Jupyter notebook. The problem is that when I ran fit() to train the model I get this error: ValueError: Layer "vq_vae" expects 1 input(s), but it received 2 input tensors. Inputs received: [<tf.Tensor 'IteratorGetNext:0' shape=(None, 64, 64, 3) dtype=float32>, <tf.Tensor 'IteratorGetNext:1' shape=(None,) dtype=int32>] . I have taken most of the code from here and adapted it myself. But for some reason I can't make it work for other datasets. You can ignore most of the code here and check it in the page, help is much appreciated.
The code I have so far:
img_height = 64
img_width = 64
dataset = tf.keras.utils.image_dataset_from_directory(directory="PATH",
image_size=(64, 64), batch_size=64, shuffle=True)
normalization_layer = tf.keras.layers.Rescaling(1./255)
normalized_ds = dataset.map(lambda x, y: (normalization_layer(x), y))
AUTOTUNE = tf.data.AUTOTUNE
train_ds = normalized_ds.cache().prefetch(buffer_size=AUTOTUNE)
VQVAE code:
class VectorQuantizer(layers.Layer):
def __init__(self, num_embeddings, embedding_dim, beta=0.25, **kwargs):
super().__init__(**kwargs)
self.embedding_dim = embedding_dim
self.num_embeddings = num_embeddings
self.beta = (beta)
w_init = tf.random_uniform_initializer()
self.embeddings = tf.Variable(initial_value=w_init(shape=(self.embedding_dim, self.num_embeddings), dtype="float32"),
trainable=True,
name="embeddings_vqvae",)
def call(self, x):
input_shape = tf.shape(x)
flattened = tf.reshape(x, [-1, self.embedding_dim])
# Quantization.
encoding_indices = self.get_code_indices(flattened)
encodings = tf.one_hot(encoding_indices, self.num_embeddings)
quantized = tf.matmul(encodings, self.embeddings, transpose_b=True)
quantized = tf.reshape(quantized, input_shape)
commitment_loss = self.beta * tf.reduce_mean((tf.stop_gradient(quantized) - x) ** 2)
codebook_loss = tf.reduce_mean((quantized - tf.stop_gradient(x)) ** 2)
self.add_loss(commitment_loss + codebook_loss)
# Straight-through estimator.
quantized = x + tf.stop_gradient(quantized - x)
return quantized
def get_code_indices(self, flattened_inputs):
# Calculate L2-normalized distance between the inputs and the codes.
similarity = tf.matmul(flattened_inputs, self.embeddings)
distances = (
tf.reduce_sum(flattened_inputs ** 2, axis=1, keepdims=True)
+ tf.reduce_sum(self.embeddings ** 2, axis=0)
- 2 * similarity
)
# Derive the indices for minimum distances.
encoding_indices = tf.argmin(distances, axis=1)
return encoding_indices
def get_vqvae(latent_dim=16, num_embeddings=64):
vq_layer = VectorQuantizer(num_embeddings, latent_dim,
name="vector_quantizer")
encoder = get_encoder(latent_dim)
decoder = get_decoder(latent_dim)
inputs = keras.Input(shape=(64, 64, 3))
encoder_outputs = encoder(inputs)
quantized_latents = vq_layer(encoder_outputs)
reconstructions = decoder(quantized_latents)
return keras.Model(inputs, reconstructions, name="vq_vae")
class VQVAETrainer(keras.models.Model):
def __init__(self, train_variance, latent_dim=32, num_embeddings=128, **kwargs):
super(VQVAETrainer, self).__init__(**kwargs)
self.train_variance = train_variance
self.latent_dim = latent_dim
self.num_embeddings = num_embeddings
self.vqvae = get_vqvae(self.latent_dim, self.num_embeddings)
self.total_loss_tracker = keras.metrics.Mean(name="total_loss")
self.reconstruction_loss_tracker = keras.metrics.Mean(
name="reconstruction_loss"
)
self.vq_loss_tracker = keras.metrics.Mean(name="vq_loss")
#property
def metrics(self):
return [
self.total_loss_tracker,
self.reconstruction_loss_tracker,
self.vq_loss_tracker,]
def train_step(self, x):
with tf.GradientTape() as tape:
# Outputs from the VQ-VAE.
reconstructions = self.vqvae(x)
# Calculate the losses.
reconstruction_loss = (
tf.reduce_mean((x - reconstructions) ** 2) / self.train_variance
)
total_loss = reconstruction_loss + sum(self.vqvae.losses)
# Backpropagation.
grads = tape.gradient(total_loss, self.vqvae.trainable_variables)
self.optimizer.apply_gradients(zip(grads, self.vqvae.trainable_variables))
# Loss tracking.
self.total_loss_tracker.update_state(total_loss)
self.reconstruction_loss_tracker.update_state(reconstruction_loss)
self.vq_loss_tracker.update_state(sum(self.vqvae.losses))
# Log results.
return {
"loss": self.total_loss_tracker.result(),
"reconstruction_loss": self.reconstruction_loss_tracker.result(),
"vqvae_loss": self.vq_loss_tracker.result(),
}
get_vqvae().summary()
Encoder and Decoder (I have made changes here but I dont think this is the problem):
def get_encoder(latent_dim=16):
encoder_inputs = keras.Input(shape=(64, 64, 3))
x = layers.Conv2D(64, 3, padding="same")(encoder_inputs)
x = layers.Dropout(0.25)(x)
x = layers.Activation("relu")(x)
x = layers.UpSampling2D()(x)
x = layers.BatchNormalization(momentum=0.8)(x)
x = layers.Conv2D(64, 3, strides=2, padding="same")(x)
x = layers.Dropout(0.25)(x)
x = layers.Activation("relu")(x)
x = layers.BatchNormalization(momentum=0.8)(x)
x = layers.Conv2D(128, 3, strides=2, padding="same")(x)
x = layers.BatchNormalization(momentum=0.8)(x)
x = layers.Activation("relu")(x)
x = layers.Conv2D(128, 3, strides=2, padding="same")(x)
x = layers.BatchNormalization(momentum=0.8)(x)
x = layers.Activation("relu")(x)
x = layers.Conv2D(256, 3, strides=2, padding="same")(x)
x = layers.Activation("relu")(x)
x = layers.UpSampling2D()(x)
x = layers.BatchNormalization(momentum=0.8)(x)
x = layers.Dense(4*4*128)(x)
encoder_outputs = layers.Conv2D(latent_dim, 1, padding="same")(x)
return keras.Model(encoder_inputs, encoder_outputs, name="encoder")
get_encoder().summary()
def get_decoder(latent_dim=16):
latent_inputs = keras.Input(shape=get_encoder().output.shape[1:])
x = layers.Conv2DTranspose(32, 3, padding="same")(latent_inputs)
x = layers.Dropout(0.25)(x)
x = layers.Activation("relu")(x)
x = layers.BatchNormalization(momentum=0.8)(x)
x = layers.Conv2DTranspose(32, 3, padding="same")(latent_inputs)
x = layers.Dropout(0.25)(x)
x = layers.Activation("relu")(x)
x = layers.BatchNormalization(momentum=0.8)(x)
x = layers.Conv2DTranspose(64, 3, padding="same")(x)
x = layers.Dropout(0.25)(x)
x = layers.Activation("relu")(x)
x = layers.BatchNormalization(momentum=0.8)(x)
x = layers.Conv2DTranspose(128, 3, strides=2, padding="same")(x)
x = layers.Dropout(0.25)(x)
x = layers.Activation("relu")(x)
x = layers.BatchNormalization(momentum=0.8)(x)
x = layers.Conv2DTranspose(256, 3, padding="same")(x)
x = layers.Dropout(0.25)(x)
x = layers.Activation("relu")(x)
x = layers.BatchNormalization(momentum=0.8)(x)
x = layers.Conv2DTranspose(512, 3, strides=2, padding="same")(x)
x = layers.Dropout(0.25)(x)
x = layers.Activation("relu")(x)
x = layers.BatchNormalization(momentum=0.8)(x)
decoder_outputs = layers.Conv2DTranspose(1, 3, padding="same")(x)
return keras.Model(latent_inputs, decoder_outputs, name="decoder")
get_decoder().summary()
I get the error here:
vqvae_trainer = VQVAETrainer(data_variance, latent_dim=16,
num_embeddings=128)
vqvae_trainer.compile(optimizer=tf.keras.optimizers.Adam())
vqvae_trainer.fit(train_ds, epochs=30, batch_size=128)
This kind of model does not work with labels. Try running:
normalized_ds = dataset.map(lambda x, y: normalization_layer(x))
to discard the labels, since you are actually only interested in x.
This Model is a variety of CNN and uses Causal Dilational Convolution Layer.
I can train and predict with 0 error, but when I use model.save() to save model, it throws Exception.
So I use save_weights and load_weights to save and load model.
I wonder why this error appears:
model.save("path")
out:
ValueError: Dimension size must be evenly divisible by 2 but is 745 for '{{node conv1d_5/SpaceToBatchND}} = SpaceToBatchND[T=DT_FLOAT, Tblock_shape=DT_INT32, Tpaddings=DT_INT32](conv1d_5/Pad, conv1d_5/SpaceToBatchND/block_shape, conv1d_5/SpaceToBatchND/paddings)' with input shapes: [?,745,32], [1], [1,2] and with computed input tensors: input[1] = <2>, input[2] = <[0 0]>.
Input shape is (None,743,27)
Output shape is (None,24,1)
def slice(x, seq_length):
return x[:, -seq_length:, :]
class ResidualBlock(tf.keras.layers.Layer):
def __init__(self, n_filters, filter_width, dilation_rate):
super(ResidualBlock, self).__init__()
self.n_filters = n_filters
self.filter_width = filter_width
self.dilation_rate = dilation_rate
# preprocessing - equivalent to time-distributed dense
self.x = Conv1D(32, 1, padding='same', activation='relu')
# filter convolution
self.x_f = Conv1D(filters=n_filters,
kernel_size=filter_width,
padding='causal',
dilation_rate=dilation_rate,
activation='tanh')
# gating convolution
self.x_g = Conv1D(filters=n_filters,
kernel_size=filter_width,
padding='causal',
dilation_rate=dilation_rate,
activation='sigmoid')
# postprocessing - equivalent to time-distributed dense
self.z_p = Conv1D(32, 1, padding='same', activation='relu')
def call(self, inputs):
x = self.x(inputs)
f = self.x_f(x)
g = self.x_g(x)
z = tf.multiply(f, g)
z = self.z_p(z)
return tf.add(x, z), z
def get_config(self):
config = super(ResidualBlock, self).get_config()
config.update({"n_filters": self.n_filters,
"filter_width": self.filter_width,
"dilation_rate": self.dilation_rate})
return config
class WaveNet(tf.keras.Model):
def __init__(self, n_filters=32, filter_width=2, dilation_rates=None, drop_out=0.2, pred_length=24):
super().__init__(name='WaveNet')
# Layer Parameter
self.n_filters = n_filters
self.filter_width = filter_width
self.drop_out = drop_out
self.pred_length = pred_length
if dilation_rates is None:
self.dilation_rates = [2 ** i for i in range(8)]
else:
self.dilation_rates = dilation_rates
# Layer
self.residual_stacks = []
for dilation_rate in self.dilation_rates:
self.residual_stacks.append(ResidualBlock(self.n_filters, self.filter_width, dilation_rate))
# self.add = Add()
self.cut = Lambda(slice, arguments={'seq_length': pred_length})
self.conv_1 = Conv1D(128, 1, padding='same')
self.relu = Activation('relu')
self.drop = Dropout(drop_out)
self.skip = Lambda(lambda x: x[:, -2 * pred_length + 1:-pred_length + 1, :1])
self.conv_2 = Conv1D(1, 1, padding='same')
def _unroll(self, inputs, **kwargs):
outputs = inputs
skips = []
for residual_block in self.residual_stacks:
outputs, z = residual_block(outputs)
skips.append(z)
outputs = self.relu(Add()(skips))
outputs = self.cut(outputs)
outputs = self.conv_1(outputs)
outputs = self.relu(outputs)
outputs = self.drop(outputs)
outputs = Concatenate()([outputs, self.skip(inputs)])
outputs = self.conv_2(outputs)
outputs = self.cut(outputs)
return outputs
def _get_output(self, input_tensor):
pass
def call(self, inputs, training=False, **kwargs):
if training:
return self._unroll(inputs)
else:
return self._get_output(inputs)
Train step
model = WaveNet()
model.compile(Adam(), loss=loss)
# ok
history = model.fit(train_x, train_y,
batch_size=batch_size,
epochs=epochs,
callbacks=[cp_callback] if save else None)
# ok
result = model.predict(test_x)
# error
model.save("path")
Trying to implement resnet 50 over poly-u dataset.
what i am trying to do over here is that i want to make a model which classifies the palm print of individuals into seperate identities , i found that resnet is working best for this kind of problems
cant seems to get over this error tried many solutions online but cant resolve for some reason :
TypeError: Failed to convert object of type <class 'tensorflow.python.framework.sparse_tensor.SparseTensor'> to Tensor. Contents: SparseTensor(indices=Tensor("DeserializeSparse:0", shape=(None, 2), dtype=int64), values=Tensor("DeserializeSparse:1", shape=(None,), dtype=float32), dense_shape=Tensor("stack:0", shape=(2,), dtype=int64)). Consider casting elements to a supported type.
identity block
import keras
def identity_block(X,f,filters):
# retrieve filters
F1,F2,F3 = filters
X_shortcut = X
# first layer
X = Conv2D(filters = F1, kernel_size = (1,1),strides= (1,1),padding ='valid') (X)
X = BatchNormalization(axis = 3 )(X)
X = Activation('relu')(X)
# second layer
X = Conv2D(filters = F2, kernel_size = (f,f),strides= (1,1),padding ='same') (X)
X = BatchNormalization(axis = 3 )(X)
X = Activation('relu')(X)
# third layer
X = Conv2D(filters = F3, kernel_size = (1,1),strides= (1,1),padding ='valid') (X)
X = BatchNormalization(axis = 3 )(X)
# final step > adding shortcut value through relu activation
# X = Add()([X,X_shortcut])
X = tf.keras.layers.Add()([X,X_shortcut])
# X = keras.layers.concatenate() ([X,X_shortcut])
X = Activation('relu')(X)
return X
convolution block
def convolutional_block(X,f,filters,s =2):
# retrieve filters
F1,F2,F3 = filters
# save the input values
X_shortcut = X
# first layer
X = Conv2D(F1,(1,1),strides=(s,s))(X)
X = BatchNormalization(axis =3)(X)
X = Activation('relu')(X)
# second layer
X = Conv2D(filters = F2,kernel_size =(f,f),strides = (1,1), padding ="same")(X)
X = BatchNormalization(axis =3)(X)
X = Activation('relu')(X)
# third layer
X = Conv2D(filters = F3,kernel_size =(1,1),strides =(1,1), padding ="valid")(X)
X = BatchNormalization(axis = 3 )(X)
# shortcut path
X_shortcut = Conv2D(filters = F3,kernel_size =(1,1),strides = (s,s),padding ='valid')(X_shortcut)
X_shortcut = BatchNormalization( axis =3)(X_shortcut)
# final step > adding shortcut value through relu activation
# X = ADD()([X,X_shortcut])
X = tf.keras.layers.Add()([X,X_shortcut])
# X = keras.layers.concatenate() ([X,X_shortcut])
X = Activation('relu')(X)
return X
resnet50 architecture
def Resnet50(input_shape= (224,224,3),classes = 309 ):
# implementing the resnet 50 achitechture over here
# define the input with shape input_shape
X_input = Input(input_shape)
# zero padding
X = ZeroPadding2D((3,3))(X_input)
# stage 1
X = Conv2D(64,(7,7),strides = (2,2))(X)
X = BatchNormalization(axis =3 )(X)
X = Activation('relu')(X)
X = MaxPooling2D((3,3),strides =(2,2))(X)
# stage 2
X = convolutional_block(X,f=3 , filters = [64,64,256],s=1)
# these line of code are the conv laters from convolution block function
# X = Conv2d(F1,(1,1),strides=(s,s))(X)
# X = Conv2D(filters = F2,kernel_size =(f,f),strides = (1,1), padding ="same")(X)
# X = conv2D(F3 ,(1,1),strides = (s,s),name = conv_name_base +'2a')(X)
X = identity_block(X ,3,[ 64, 64, 256])
# same line from identity block
X = identity_block(X, 3, [ 64, 64,256])
# same line from identity block
# stage 3
X = convolutional_block(X,f =3 , filters = [128,128,512],s=2)
X = identity_block(X,3,[128,128,512])
X = identity_block(X,3,[128,128,512])
X = identity_block(X,3,[128,128,512])
# stage 4
X = convolutional_block(X,f = 3 , filters = [ 256, 256, 1024], s = 2 )
X = identity_block(X,3,[256,256,1024])
X = identity_block(X,3,[256,256,1024])
X = identity_block(X,3,[256,256,1024])
X = identity_block(X,3,[256,256,1024])
X = identity_block(X,3,[256,256,1024])
# stage 5
X = convolutional_block(X, f = 3 ,filters = [512,512,2048], s = 2 )
# X = identity_block(X,3 , [512,512,1024])
# X = identity_block(X,3 , [512,512,1024])
# AVGPOOL
X = AveragePooling2D((2,2),name='avg_pool')(X)
# achitech complete
# output
X = Flatten()(X)
X = Dense (classes, activation = 'softmax',name = 'fc'+str(classes),kernel_initializer = glorot_uniform(seed = 0))(X)
# Create model
model = Model(inputs = X_input , outputs =X , name='Resnet50')
return model
model = Resnet50(input_shape = (224,224,3), classes=309)
model.compile(optimizer = 'adam', loss='categorical_crossentropy', metrics = ['accuracy'])
model.fit(train_x,train_y,epochs= 10 , batch_size =32)
Looking at your resnet50 it seems your missing the last two identity blocks of stage 5
X = convolutional_block(X, f = 3 ,filters = [512,512,2048], s = 2 )
X = identity_block(X,3,[512,512,2048]]) # this part seems missing
X = identity_block(X,3,[512,512,2048]])
Btw you can just use keras applications for a shortcut
import tensorflow as tf
tf.keras.applications.ResNet50(include_top=True, classes=200, weights=None)
In the code here below the vector rand is initialized when I call the first time the function create_model().
def create_model(num_columns):
inp_layer = tfl.Input((num_columns,))
rand = tf.random.uniform((1,num_columns), minval = 0, maxval = 2, dtype = tf.int32), tf.float32))
inp_rand = tfl.Multiply()([inp_layer, rand])
dense = tfl.Dense(256, activation = 'relu')(inp_rand)
dense = tfl.Dense(128, activation = 'relu')(dense)
dense = tfl.Dense(64, activation = 'sigmoid')(dense)
model = tf.keras.Model(inputs = inp_layer, outputs = dense)
model.compile(optimizer = 'adam', loss = 'binary_crossentropy')
model = create_model(num_columns)
model.fit()
I would like it to be regenerated with new random values every time I call the function model.fit(), or even better, at any batch during the execution of model.fit().
Would you know how I can do this?
You can change what happens in the call() method of a subclassed Keras model.
def call(self, x, training=None, **kwargs):
rand = tf.cast(tf.random.uniform((1, *x.shape[1:]), 0, 2, tf.int32), tf.float32)
x = tf.multiply(x, rand)
x = self.conv1(x)
x = self.maxp1(x)
x = self.conv2(x)
x = self.maxp2(x)
x = self.flatt(x)
x = self.dens1(x)
x = self.drop1(x)
x = self.dens2(x)
return x
Here I did it using the MNIST, where I multipled the input tensor with a random tensor of the same shape:
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
import tensorflow as tf
import tensorflow_datasets as tfds
from tensorflow import keras as K
from tensorflow.keras.layers import Conv2D, Flatten, Dense, MaxPooling2D, Dropout
from tensorflow import nn as nn
from functools import partial
dataset, info = tfds.load('mnist', with_info=True)
train, test = dataset['train'], dataset['test']
def prepare(dataset):
inputs = tf.divide(x=dataset['image'], y=255)
targets = tf.one_hot(indices=dataset['label'], depth=10)
return inputs, targets
train = train.take(5_000).batch(4).map(prepare)
test = test.take(1_000).batch(4).map(prepare)
class MyCNN(K.Model):
def __init__(self):
super(MyCNN, self).__init__()
Conv = partial(Conv2D, kernel_size=(3, 3), activation=nn.relu)
MaxPool = partial(MaxPooling2D, pool_size=(2, 2))
self.conv1 = Conv(filters=8)
self.maxp1 = MaxPool()
self.conv2 = Conv(filters=16)
self.maxp2 = MaxPool()
self.flatt = Flatten()
self.dens1 = Dense(64, activation=nn.relu)
self.drop1 = Dropout(.5)
self.dens2 = Dense(10, activation=nn.softmax)
def call(self, x, training=None, **kwargs):
rand = tf.cast(tf.random.uniform((1, *x.shape[1:]), 0, 2, tf.int32), tf.float32)
x = tf.multiply(x, rand)
x = self.conv1(x)
x = self.maxp1(x)
x = self.conv2(x)
x = self.maxp2(x)
x = self.flatt(x)
x = self.dens1(x)
x = self.drop1(x)
x = self.dens2(x)
return x
model = MyCNN()
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(train, validation_data=test, epochs=10,
steps_per_epoch=1250, validation_steps=250)
What I was trying to implement is actually the Dropout layer. So no point in searching further.
I'm trying to create a densenet but when I try to compile the model I get this error message. Here's my model:
from tensorflow import keras
from keras.utils import plot_model
dropoutRate = 0.2
def globalAvgPooling(x):
height = np.shape(x)[2]
width = np.shape(x)[1]
poolSize = [width, height]
return tf.keras.layers.AveragePooling2D(pool_size=poolSize, strides=1)(x)
def concatenation(layers):
return tf.keras.layers.concatenate(layers, axis=3)
class DenseNet():
def __init__(self, filters, numBlocks, numClasses, training):
self.filters = filters
self.numBlocks = numBlocks
self.training = training
self.numClasses = numClasses
self.model = self.denseNet()
def bottleneckLayer(self, inputX):
x = tf.keras.layers.BatchNormalization()(inputs=inputX, training=self.training)
x = tf.keras.activations.relu(x)
x = tf.keras.layers.Conv2D(use_bias=False, filters=self.filters, kernel_size=1, strides=1, padding='same')(x)
x = tf.layers.dropout(inputs=x, rate=dropoutRate, training=self.training)
x = tf.keras.layers.BatchNormalization()(inputs=inputX, training=self.training)
x = tf.keras.activations.relu(x)
x = tf.keras.layers.Conv2D(use_bias=False, filters=self.filters, kernel_size=3, strides=1, padding='same')(x)
x = tf.layers.dropout(inputs=x, rate=dropoutRate, training=self.training)
return x
def denseBlock(self, inputX, numLayers):
concatLayers = list()
concatLayers.append(inputX)
x = self.bottleneckLayer(inputX=inputX)
concatLayers.append(x)
for i in range(self.numBlocks - 1):
x = concatenation(concatLayers)
x = self.bottleneckLayer(inputX=x)
concatLayers.append(x)
x = concatenation(concatLayers)
return x
def transitionLayer(self, inputX):
x = tf.keras.layers.BatchNormalization()(inputs=inputX, training=self.training)
x = tf.keras.activations.relu(x)
x = tf.keras.layers.Conv2D(use_bias=False, filters=self.filters, kernel_size=1, strides=1, padding='same')(x)
x = tf.layers.dropout(inputs=x, rate=dropoutRate, training=self.training)
x = tf.keras.layers.AveragePooling2D(pool_size=[2,2], strides=2, padding='valid')(x)
return x
def denseNet(self):
inputs = keras.Input(shape=(32,32,3))
x = tf.keras.layers.Conv2D(use_bias=False, filters=self.filters, kernel_size=7, strides=1, padding='same')(inputs)
x = self.denseBlock(inputX=x, numLayers=6) #Dense block 1 with 6 layers
x = self.transitionLayer(inputX=x)
x = self.denseBlock(inputX=x, numLayers=12) #Dense block 2 with 12 layers
x = self.transitionLayer(inputX=x)
x = self.denseBlock(inputX=x, numLayers= 48) #Dense block 3 with 48 layers
x = self.transitionLayer(inputX=x)
x = self.denseBlock(inputX=x, numLayers=32) #Dense block 4 with 32 layers (final block)
x = globalAvgPooling(x=x)
x = tf.keras.layers.Softmax()(x)
outputs = tf.keras.layers.Dense(units=self.numClasses)(x)
model = keras.Model(inputs=inputs, outputs=outputs)
return x
tf.compat.v1.disable_eager_execution()
growthK = 24
numBlock = 2
cameraModel = DenseNet(filters=growthK, numBlocks=numBlock, numClasses=4, training=True).model
Here is the error message I get:
ValueError: Graph disconnected: cannot obtain value for tensor
Tensor("dropout_109/dropout/mul_1:0", shape=(?, 32, 32, 24), dtype=float32)
at layer "concatenate_48". The following previous layers were accessed without issue:
['input_6', 'conv2d_120', 'batch_normalization_115', 'tf_op_layer_Relu_115', 'conv2d_122', 'dropout_108']
What am I doing wrong?
The error comes from the line:
x = tf.keras.layers.BatchNormalization()(inputs=inputX, training=self.training)
as I think you have a bad inputs parameter in it.
It should looks like:
x = tf.keras.layers.BatchNormalization()(inputs=x, training=self.training)
That is why value the Graph disconnected.