Hey my code is not running locally and throwing remapper failed error while the same code is working on google colab

Hey I am making a project on Diabetic retinopathic detection using GAN but my code is not working on my local machine even though I have all the dependencies installed it throwing this error while training the model:-

> E tensorflow/core/grappler/optimizers/meta_optimizer.cc:961] remapper failed: INVALID_ARGUMENT: Mutation::Apply error: fanout 'StatefulPartitionedCall/gradient_tape/sequential_2_1/sequential_2/leaky_re_lu_3_1/LeakyRelu/LeakyReluGrad' exist for missing node 'StatefulPartitionedCall/sequential_2_1/sequential_2/conv2d_3_1/add'.
and
image this fake is always 0% while on colab it is not always 0% and thus images generated are very noisy like:-


while online generated image is clearer:-

what is this error?
the code is:-

from numpy import expand_dims
from numpy import zeros
from numpy import ones
from numpy import vstack
from numpy.random import randn
from numpy.random import randint
from keras.optimizers import Adam
from keras.models import Sequential
import keras
from keras.layers import Dense,Reshape,Flatten,Conv2D,Conv2DTranspose,LeakyReLU,Dropout
from matplotlib import pyplot
import numpy as np
import cv2

# function to generate discriminator model
def define_discriminator(in_shape=(32,32,3)):
	model = Sequential()
	# normal
	inputs=keras.Input(shape=(32,32,3),name='input_layer')
	model.add(Conv2D(64, (3,3), padding='same', input_shape=in_shape))
	model.add(LeakyReLU(negative_slope=0.2))
	# downsample
	model.add(Conv2D(128, (3,3), strides=(2,2), padding='same'))
	model.add(LeakyReLU(negative_slope=0.2))
	# downsample
	model.add(Conv2D(128, (3,3), strides=(2,2), padding='same'))
	model.add(LeakyReLU(negative_slope=0.2))
	# downsample
	model.add(Conv2D(256, (3,3), strides=(2,2), padding='same'))
	model.add(LeakyReLU(negative_slope=0.2))
	# classifier
	model.add(Flatten())
	model.add(Dropout(0.4))
	model.add(Dense(1, activation='sigmoid'))
	# compile model
	opt = Adam(learning_rate=0.0002, beta_1=0.5)
	model.compile(loss='binary_crossentropy', optimizer=opt, metrics=['accuracy'])
	return model

# function to generate standalone generator model
def define_generator(latent_dim):
	model = Sequential()
	# foundation for 4x4 image
	n_nodes = 256 * 4 * 4
	model.add(Dense(n_nodes, input_dim=latent_dim))
	model.add(LeakyReLU(negative_slope=0.2))
	model.add(Reshape((4, 4, 256)))
	# upsample to 8x8
	model.add(Conv2DTranspose(128, (4,4), strides=(2,2), padding='same'))
	model.add(LeakyReLU(negative_slope=0.2))
	# upsample to 16x16
	model.add(Conv2DTranspose(128, (4,4), strides=(2,2), padding='same'))
	model.add(LeakyReLU(negative_slope=0.2))
	# upsample to 32x32
	model.add(Conv2DTranspose(128, (4,4), strides=(2,2), padding='same'))
	model.add(LeakyReLU(negative_slope=0.2))
	# output layer
	model.add(Conv2D(3, (3,3), activation='tanh', padding='same'))
	return model

# define the combined generator and discriminator model, for updating the generator
def define_gan(g_model, d_model):
	# make weights in the discriminator not trainable
	d_model.trainable = False
	# connect them
	model = Sequential()
	# add generator
	model.add(g_model)
	# add the discriminator
	model.add(d_model)
	# compile model
	opt = Adam(learning_rate=0.0002, beta_1=0.5)
	model.compile(loss='binary_crossentropy', optimizer=opt)
	return model

# load and prepare cifar10 training images
def load_real_samples():
	# load cifar10 dataset
	X = np.load('img_data.txt.npy')
	# convert from unsigned ints to floats
	#X = trainX.astype('float32')
	# scale from [0,255] to [-1,1]
	#X = (X - 127.5) / 127.5
	return X

# select real samples
def generate_real_samples(dataset, n_samples):
	# choose random instances
	ix = randint(0, dataset.shape[0], n_samples)
	# retrieve selected images
	X = dataset[ix]
	# generate 'real' class labels (1)
	y = ones((n_samples, 1))
	return X, y

# generate points in latent space as input for the generator
def generate_latent_points(latent_dim, n_samples):
	# generate points in the latent space
	x_input = randn(latent_dim * n_samples)
	# reshape into a batch of inputs for the network
	x_input = x_input.reshape(n_samples, latent_dim)
	return x_input

# use the generator to generate n fake examples, with class labels
def generate_fake_samples(g_model, latent_dim, n_samples):
	# generate points in latent space
	x_input = generate_latent_points(latent_dim, n_samples)
	# predict outputs
	X = g_model.predict(x_input)
	# create 'fake' class labels (0)
	y = zeros((n_samples, 1))
	return X, y

# create and save a plot of generated images
def save_plot(examples, epoch, n=7):
	# scale from [-1,1] to [0,1]
	examples = (examples + 1) / 2.0
	# plot images
	for i in range(n * n):
		# define subplot
		pyplot.subplot(n, n, 1 + i)
		# turn off axis
		pyplot.axis('off')
		# plot raw pixel data
		pyplot.imshow(examples[i])
		cv2.imwrite("img/"+str(i)+".jpg",examples[i])
	# save plot to file
	filename = 'model/generated_plot_e%03d.png' % (epoch+1)
	pyplot.savefig(filename)
	pyplot.close()

# evaluate the discriminator, plot generated images, save generator model
def summarize_performance(epoch, g_model, d_model, dataset, latent_dim, n_samples=150):
	# prepare real samples
	X_real, y_real = generate_real_samples(dataset, n_samples)
	# evaluate discriminator on real examples
	_, acc_real = d_model.evaluate(X_real, y_real, verbose=0)
	# prepare fake examples
	x_fake, y_fake = generate_fake_samples(g_model, latent_dim, n_samples)
	# evaluate discriminator on fake examples
	_, acc_fake = d_model.evaluate(x_fake, y_fake, verbose=0)
	# summarize discriminator performance
	print('>Accuracy real: %.0f%%, fake: %.0f%%' % (acc_real*100, acc_fake*100))
	# save plot
	save_plot(x_fake, epoch)
	# save the generator model tile file
	filename = 'model/generator_model_%03d.h5' % (epoch+1)
	g_model.save(filename)

# train the generator and discriminator
def train(g_model, d_model, gan_model, dataset, latent_dim, n_epochs=100, n_batch=128):
	bat_per_epo = int(dataset.shape[0] / n_batch)
	half_batch = int(n_batch / 2)
	# manually enumerate epochs
	for i in range(n_epochs):
		# enumerate batches over the training set
		for j in range(bat_per_epo):
			# get randomly selected 'real' samples
			X_real, y_real = generate_real_samples(dataset, half_batch)
			# update discriminator model weights
			d_loss1, _ = d_model.train_on_batch(X_real, y_real)
			# generate 'fake' examples
			X_fake, y_fake = generate_fake_samples(g_model, latent_dim, half_batch)
			# update discriminator model weights
			d_loss2, _ = d_model.train_on_batch(X_fake, y_fake)
			# prepare points in latent space as input for the generator
			X_gan = generate_latent_points(latent_dim, n_batch)
			# create inverted labels for the fake samples
			y_gan = ones((n_batch, 1))
			# update the generator via the discriminator's error
			g_loss = gan_model.train_on_batch(X_gan, y_gan)
			# summarize loss on this batch
			print('>%d, %d/%d, d1=%.3f, d2=%.3f g=%.3f' %
				(i+1, j+1, bat_per_epo, d_loss1, d_loss2, g_loss))
		# evaluate the model performance, sometimes
		if (i+1) % 10 == 0:
			summarize_performance(i, g_model, d_model, dataset, latent_dim)

# size of the latent space
latent_dim = 200
# create the discriminator
d_model = define_discriminator()
# create the generator
g_model = define_generator(latent_dim)
# create the gan
gan_model = define_gan(g_model, d_model)
# load image data
dataset = load_real_samples()
# train model
train(g_model, d_model, gan_model, dataset, latent_dim)

please help with the same

Hi again @Arpit_08dev

Can you tell me the reason of choice for LeakyRelu in your model

Next question is

when you have a categorical classification of more than 2 types (i.e. in your case 5 classes), why are you using a loss of binary crossentropy??

Try using sparse categorical crossentropy

Compile the model
model.compile(loss=tensorflow.keras.losses.sparse_categorical_crossentropy,
optimizer=tensorflow.keras.optimizers.Adam(),
metrics=[‘accuracy’])

Let me know if you get a different output

Regards
DP

Hey now its giving this error:-
Graph execution error:

> Detected at node compile_loss/sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits defined at (most recent call last):
and like why the same code works on colab and not on my machine like that issue I am not able to address

Arpit

based on your error output, I found the below link

So basically this link tells your labels cannot be a float if you are using sparse_categorical_crossentropy, it need to be an integer.

Hey correction, change the loss=tensorflow.keras.losses.sparse_categorical_crossentropy to ‘sparse_categorical_crossentropy’, this should resolve your issue.

Regards
DP

you haven’t respond for this question, I can only guide you if your responses are more clear arpit.

Regards
DP

Leaky relu I used because when I was seeing and learning about GAN it explicitly mentioned to use leakyrelu to avoid dying relu problem and all GAN codes I studied used this cause even when x is negative, the Leaky ReLU function returns a small negative value proportional to the input x.

so did you check if in your data you x is going negative??

Also did you try once relu activation and check how the model is doing?

Also I hope you know adding too many convolution layers can also have an effect on your model training output.

Based on your model sharing,

here is what I would make changes and see if I see any improvement.

  1. use maximum of 2-3 convolution 2d layer with rela activation with subsequent Maxpooling2D layer
  2. I would use flatten() layer but not dropout layer
  3. use 2 dense layer(then increase number of dense layer if want to improve the training) with last dense layer of softmax activation.
  4. You have used large units for your model, start with 32 as your input shape also use 32 by 32 dimension, then go 64 and 64 again for subsequent convolution 2D layer units.
  5. For first dense layer unit use 128 or 512 with rela activation.
  6. for last dense layer unit of 1 as you already have used. I noticed you used activation of sigmoid which is again incorrect as you don’t have a binary classification. I hope you know how last dense layer activation are selected? for more than 2 classes, softmax activation is used. Probably that’s why you also used loss of binary crossentropy which would be incorrect in your case as you are classifying diabetic retinopathy into 5 classes.

Also I forgot to ask another important question, can I know how many number of images do you have for your model training??

Regards
DP

This is not true. You’re assuming that the weight value for this unit is positive.

Weights can be positive or negative.

If you have a negative weight, then the product with negative x will give a positive result.

Another reason for x probably cannot be negative if assuming as per images you are looking for hemorrhage or white patches which surely won’t be negative.

For that reason, I would not go with leakyRelu

Thanks this worked out one of the other major reasons was that I was working with python 3.12.2 so changing code a bit and using python 3.10.10 made it work fine now I guess the remapper fail error was due to some different interpretation of code in tensorflow 2.16.1 which was resolved when I downgraded it to 2.15.0

1 Like

Hello Buddy!

I want to know where did you collected the data from. Actually I am working on a similar project of Diabetes Retinopathy detection and I am concerned how will I collect the data.