Running into an issue with training my image segmentation model

Hey all, first time poster, and have not been active due to my projects, I am very new to deep learning and ML, I have trained some semantic segmentation models in the past, but not a whole lot. I am just asking for some help with an error that I keep getting, it is a no gradient provided for any variable error, I am doing some semantic segmentation of brain data, its taking brain data and categorizing (onehot) into 4 specific categories, this data was filtered for the specific categories needed. Below is my main code, I believe it is due to the one hot encoding method from keras but if anyone has ever dealt with something similar, some tips would be really helpful.

Thanks all, and have a great day, here is the code:

imgs_list_train = glob.glob(“D:/BMEN/Data/Test/BrainSet2/*.npy”)
masks_list_train = glob.glob(“D:/BMEN/Data/Test/BrainSet2/*.npy”)

batch_size = 32
gen_train = DataEncoder(imgs_list_train, masks_list_train, batch_size = batch_size)

model_name = “C:/Users/Keilan Pieper/Desktop/BMEN/ImageSegmentation/Models/BrainStructure”
early_stop = tf.keras.callbacks.EarlyStopping(monitor= ‘val_loss’, patience = 10)

model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate = 1e-4))

history =, epochs=1, verbose = 1,
callbacks= [early_stop, monitor, lr_schedule])

Data encoder class

class DataEncoder(keras.utils.Sequence):
‘Generates data for Keras’
def __init__(self, imgs_list, masks_list, patch_size=(128, 128), batch_size=32, shuffle=True):

self.imgs_list = imgs_list
self.masks_list = masks_list
self.patch_size = patch_size
self.batch_size = batch_size
self.nsamples = len(imgs_list)
self.shuffle = True

def __len__(self):
‘Denotes the number of batches per epoch’
return len(self.imgs_list) // self.batch_size

def __getitem__(self, index):
‘Generate one batch of data’
# Generate indexes of the batch
batch_indexes = self.indexes[index * self.batch_size:(index + 1) * self.batch_size]

# Generate data
X, Y = self.__data_generation(batch_indexes)

return X, Y

def on_epoch_end(self):
‘Updates indexes after each epoch’
self.indexes = np.arange(self.nsamples)
if self.shuffle == True:

def __data_generation(self, batch_indexes):
‘Generates data containing batch_size samples’
# Initialization
X = np.empty((self.batch_size, self.patch_size[0], self.patch_size[1], 1))
Y = np.empty((self.batch_size, self.patch_size[0], self.patch_size[1], 1))

for (jj, ii) in enumerate(batch_indexes):
aux_img = np.load(self.imgs_list[ii])
aux_mask = np.load(self.masks_list[ii])

# Implement data augmentation function
aux_img_patch, aux_mask_patch = self.__extract_patch(aux_img, aux_mask)

X[jj, :, :, 0] = aux_img_patch
tf.one_hot(Y, 4)
Y[jj, :, :, 0] = aux_mask_patch

return X, Y

def __extract_patch(self, img, mask):
crop_idx = [None] * 2
crop_idx[0] = np.random.randint(0, img.shape[0] – self.patch_size[0])
crop_idx[1] = np.random.randint(0, img.shape[1] – self.patch_size[1])
img_cropped = img[crop_idx[0]:crop_idx[0] + self.patch_size[0],
crop_idx[1]:crop_idx[1] + self.patch_size[1]]
mask_cropped = mask[crop_idx[0]:crop_idx[0] + self.patch_size[0],
crop_idx[1]:crop_idx[1] + self.patch_size[1]]

return img_cropped, mask_cropped

submitted by /u/Independent-Dust7072
[visit reddit] [comments]

Leave a Reply

Your email address will not be published. Required fields are marked *