Kernel dies when I apply a class weights on a CNN with Tensorflow for Imbalanced Data

Viewed 102

I am currently struggling with a problem that occurs when I execute my CNN training algorithm with class weights dictionary. I am following this tutorial. There are no problems with the code but, when I start to train the network, suddenly the inscription appears:

Kernel died, restarting

Has someone had my same problem? Could it be a problem with the loss function?

loss_fun = 'categorical_crossentropy'
Optimizer = 'Adam with lr=0.0001'

model = create_model_fun(choice, H, W, input_size)
model.compile(Adam(lr=.0001), loss=loss_fun, metrics=['accuracy'])

# Class Weights
output_shape = len(PD_Dict)
total = sum(N_PD_Samples)
weight_for_i = [(1 / N_PD_Samples[ii])*(total)/output_shape for ii in range(output_shape)]
class_weight = {}
for ii in range(output_shape):
    class_weight[ii] = weight_for_i[ii]
    
history = model.fit(train_map, np.array(train_lab_Matr), validation_data=(valid_map, np.array(
    valid_lab_Matr)), epochs=N_epochs, shuffle=True, batch_size=Dim_Batch, callbacks=[es, mc], class_weight=class_weight)
1 Answers

To find the class weights first iterate through all your classes and find the class that has the largest number of samples. Lets call that value most_samples. Then iterate through the classes and determine the number of samples in the ith class, call it samples[i]. The weight for each class is most_samples/samples[i]. Use this all the time and have had no problems. For example assume you have 3 classes as shown below

class 0  samples = 500
class 1  samples = 1000
class 2  samples = 2000

the the value of most samples is 2000 so the weights are

class 0 weight=2000/500=4
class 1 weight=2000/1000=2
class 2 weight=2000/2000=1
so the class weight dictionary looks like
(0:4, 1:2, 2:1)
Related