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}