Dataset flower_photos.tgz
label |
count |
daisy |
633 |
dandelion |
898 |
roses |
641 |
sunflowers |
699 |
tulips |
799 |
Count the number of images in different categories
import numpy as np
import tensorflow as tf
directory = 'flower_photos'
datagen = tf.keras.preprocessing.image.ImageDataGenerator()
data = datagen.flow_from_directory(directory)
unique = np.unique(data.classes, return_counts=True)
labels_dict = dict(zip(unique[0], unique[1]))
print(labels_dict)
Found 3670 images belonging to 5 classes.
{0: 633, 1: 898, 2: 641, 3: 699, 4: 799}
Calculate the weights of different categories
import math
def get_class_weight(labels_dict):
"""Calculate the weights of different categories
>>> get_class_weight({0: 633, 1: 898, 2: 641, 3: 699, 4: 799})
{0: 1.0, 1: 1.0, 2: 1.0, 3: 1.0, 4: 1.0}
>>> get_class_weight({0: 5, 1: 78, 2: 2814, 3: 7914})
{0: 7.366950709511269, 1: 4.619679795255778, 2: 1.034026384271035, 3: 1.0}
"""
total = sum(labels_dict.values())
max_num = max(labels_dict.values())
mu = 1.0 / (total / max_num)
class_weight = dict()
for key, value in labels_dict.items():
score = math.log(mu * total / float(value))
class_weight[key] = score if score > 1.0 else 1.0
return class_weight
labels_dict = {0: 633, 1: 898, 2: 641, 3: 699, 4: 799}
print(get_class_weight(labels_dict))
{0: 1.0, 1: 1.0, 2: 1.0, 3: 1.0, 4: 1.0}
labels_dict = {0: 5, 1: 78, 2: 2814, 3: 7914}
print(get_class_weight(labels_dict))
{0: 1.0, 1: 3.749820767859636, 2: 1.0, 3: 3.749820767859636, 4: 1.0, 5: 2.5931008483842453, 6: 1.0, 7: 2.5931008483842453}
class_weight.compute_class_weight
produces an array, I need to change it to a dict in order to work with Keras. More specifically, after step 2, useclass_weight_dict = dict(enumerate(class_weight))
– C.Lee Oct 13 '17 at 04:33y_train
is(300096, 3)
numpy array. So theclass_weight=
line gives me TypeError: unhashable type: 'numpy.ndarray' – Simd Dec 14 '17 at 10:25y_ints = [y.argmax() for y in y_train]
. – tkocmathla Apr 12 '18 at 14:19class_weight.compute_class_weight('balanced', classes=np.unique(y_train), weights=y_train)
– fsulser Nov 22 '22 at 13:31class_weights = class_weight.compute_class_weight(class_weight='balanced', classes=np.unique(train["label"]), y=train["label"])
And for passing into keras model.fitclass_weight=dict(zip(np.unique(train["label"]),class_weights))
– Fawaz Ahmed Feb 25 '23 at 01:11