Hey I am traying to make a GAN model for DR prediction but getting error while training it

Hey I am trying to train a GAN model but keep getting many errors one of which are :-1:

> Traceback (most recent call last):
>   File "C:\Users\asus\OneDrive\Desktop\project\DR-GAN\TrainModel.py", line 65, in <module>
>     classifier.fit(X, Y, batch_size=32, epochs=50)
>   File "C:\Users\asus\AppData\Roaming\Python\Python312\site-packages\keras\src\utils\traceback_utils.py", line 122, in error_handler
>     raise e.with_traceback(filtered_tb) from None
>   File "C:\Users\asus\AppData\Roaming\Python\Python312\site-packages\keras\src\backend\tensorflow\nn.py", line 553, in categorical_crossentropy
>     raise ValueError(
> ValueError: Arguments `target` and `output` must have the same shape. Received: target.shape=(None, 3), output.shape=(None, 5)

The code for my train model file is :-1:

import numpy as np
import imutils
import sys
import cv2
import os
from tensorflow.keras.utils import to_categorical
from keras.models import model_from_json
from keras.layers import MaxPooling2D
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Convolution2D
from keras.models import Sequential 

images = []
image_labels  = []
directory = 'dataset'
list_of_files = os.listdir(directory)
index = 0
for file in list_of_files:
    subfiles = os.listdir(directory+'/'+file)
    for sub in subfiles:
        path = directory+'/'+file+'/'+sub
        img = cv2.imread(path)
        #img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        if img is None:
          print('Wrong path:', path)
        else:
         img = cv2.resize(img, (32,32))
         im2arr = np.array(img)
         im2arr = im2arr.reshape(32,32,3)
         images.append(im2arr)
         image_labels.append(file)
    print(file)    

X = np.asarray(images)
Y = np.asarray(image_labels)
Y = to_categorical(Y)
img = X[20].reshape(32,32,3)
cv2.imshow('ff',cv2.resize(img,(250,250)))
cv2.waitKey(0)
print("shape == "+str(X.shape))
print("shape == "+str(Y.shape))
print(Y)
X = X.astype('float32')
X = X/255

np.save("model/img_data.txt",X)
np.save("model/img_label.txt",Y)

X = np.load('model/img_data.txt.npy')
Y = np.load('model/img_label.txt.npy')
print(Y)
img = X[20].reshape(32,32,3)
cv2.imshow('ff',cv2.resize(img,(250,250)))
cv2.waitKey(0)

classifier = Sequential() #alexnet transfer learning code here
classifier.add(Convolution2D(32, 3, 3, input_shape = (32, 32, 3), activation = 'relu'))
classifier.add(MaxPooling2D((2, 2) , padding='same'))
classifier.add(Convolution2D(32, 3, 3, activation = 'relu'))
classifier.add(MaxPooling2D((2, 2) , padding='same'))
classifier.add(Flatten())
classifier.add(Dense(units = 128, activation = 'relu'))
classifier.add(Dense(units = 5, activation = 'softmax'))
classifier.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = ['accuracy'])
classifier.fit(X, Y, batch_size=32, epochs=50)
classifier.save_weights('model/train.h5')            
model_json = classifier.to_json()
with open("model/train.json", "w") as json_file:
    json_file.write(model_json)
print(classifier.summary())




for more details message me

Can I know why you are using the unit of 5 for last dense layer? Did you try using 3?

Issue is here

I am not sure why you did the above step yet recalled x and y with totally different variable.

First I thought the append images you used for y variable,but you have used model load path recalling it by image_label and image_labels.

Also you append images with file? Can you explain that step?

im2arr.reshape(32,32,3) reshapes the NumPy array (im2arr) to have dimensions (32, 32, 3). This step ensures that the image data has the correct shape expected by the neural network model (32 pixels height, 32 pixels width, and 3 color channels for RGB images). If the image was successfully read and processed, the resized and reshaped image array (im2arr) is appended to the images list, and the associated label (file) is appended to the image_labelslist. This step collects the preprocessed image data and their corresponding labels for later use in training or testing machine learning models.print(file)` prints the name of the current file (label) being processed. This can be helpful for tracking progress during data processing.

can you share the revised code with what you feel should be the changes I can try that and then share you the output

This part I already understood

I somehow felt this was not required.

you would be requiring this step for the preprocessed image data and correspond labels to use lates in training or testing can be used by recalled name as images and image_labels

You didn’t respond to this query on why are you using unit of 5, why did you try using 3, is your categorical variable is divided into 5 classes?

Really sorry I cannot revise your codes without understand about your data as what your images is about, what you are trying to create or achieve and honestly little busy in my own work to experiment with your codes.

Provide all the necessary details at least about your data, so we can think about where things might have gone wrong.

is 20 the number of images in X

I suppose this has cause the output to have different outcome than the target.

Please explain your data, so correction of codes can be achieved.

Regards
DP

so basically the data I am taking is of left and right fundus images of a patient and the analysing them on basis of clots and nerves formed or possibility and then predicting the type of Diabetic retinopathy whether mild , severe or proliferative
the training data is of about 1024 patients.

1 Like

So you have 3 categories, did you try to change the last dense layer unit to 3 and run the codes?

Do you see any changes?

Please respond to my every query asked.

no there are actually 5 mild severe proliferative moderate nodr

and trying to change is getting no different result still getting same errors

From what I know about types of diabetic retinopathy there are 4

mild, moderate, severe (nonproliferative), and proliferative .

Also are using the 4-2-1 rule to classify the diabetic retinopathy. See the below

that being said, you using 3

on reshape your images using color channels of 3 but using last dense layer of unit 5 defines the number of classes 5 based on your classification of diabetic retinopathy, then your image_labelling needs to be modified as per the classification of the disease.

I am sharing a link of image classification from hugging face, kindly go through, then we will discuss on how to go about it.

Regards
DP

2 Likes

Thank you very much this helped and is working now

2 Likes