test_class_acc_list = []
for i in range(5):
print(f"\nTrain {i+1}:\n")
model = build_model(input_shape, num_classes)
# Train model
batch_size = 16
epochs = 50
history = train_model(model, X_train, y_train_class, y_train_seg, X_val, y_val_class, y_val_seg, batch_size, epochs)
# Evaluate model on test data
test_class_acc = evaluate_model(model, X_test, y_test_class, y_test_seg)
plot_performance(history)
test_class_acc_list.append(test_class_acc)
The above is the Python code with Keras to do multi-task learning with binary segmentation and classification using one mult-task learning model. I have already loaded and separated the dataset (contain both images and ground truth segmentation masks) into train:val:test ratio of 10:20:60. Add the code to check the number of images of each class in the train,val,test set respectively, e.g.For train: bridge: 12, excess: 27, good:42,... Show full code.
You want to add code to inspect the distribution of classes in the train, validation, and test sets. To achieve this, you can use Python dictionaries to count the occurrences of each class label in y_train_class, y_val_class, and y_test_class. Below is how you can add this feature right after finishing the data splitting process:
def count_class_distributions(y_labels, label_encoder):
# Decode class labels to original form
y_labels_decoded = label_encoder.inverse_transform(np.argmax(y_labels, axis=1))
# Count occurrences of each class
class_counts = {}
for label in y_labels_decoded:
if label in class_counts:
class_counts[label] += 1
else:
class_counts[label] = 1
return class_counts
# Label encoder for decoding purposes later
label_encoder = LabelEncoder()
# Count and print the distribution of classes in the datasets
train_class_distribution = count_class_distributions(y_train_class, label_encoder)
val_class_distribution = count_class_distributions(y_val_class, label_encoder)
test_class_distribution = count_class_distributions(y_test_class, label_encoder)
# Print the distributions
print("Class distribution in the Train set:", train_class_distribution)
print("Class distribution in the Validation set:", val_class_distribution)
print("Class distribution in the Test set:", test_class_distribution)
Here's what each part does in the added code:
count_class_distributions function: This takes the categorical (one-hot encoded) class labels and the label encoder as input. It first decodes the class labels back to their original form using the label encoder. Then, it counts the occurrences of each class in the provided dataset using a dictionary, where the class names are keys, and their counts are values.
Count and print distributions: It computes the distributions for train, validation, and test sets by calling the count_class_distributions function for each dataset's labels. Finally, it prints these counts to give you insights into the number of images of each class present in each subset.
Note: To ensure that the label_encoder is available for decoding purposes in the count_class_distributions function, it needs to be created and fitted before calling the function. This implementation assumes that label_encoder is already fitted as part of the split_data function or separately before calling these functions. If not, you'll need to fit the label encoder to your combined dataset labels before using it for decoding in the function.
The error indicates that the LabelEncoder instance passed to the count_class_distributions function hasn't been fitted with any data yet. This is because in your code, you likely created a LabelEncoder instance without fitting it to your labels. The fitting process allows LabelEncoder to understand the entire set of classes it should work with.
To correct this, you should fit the LabelEncoder to your dataset's labels before you attempt to use it for transforming or inverse transforming labels. Here's a revised approach, making sure we fit label_encoder correctly:
We'll adjust our approach by performing the encoding process for the training, validation, and test labels again within the main body of the script and fitting the LabelEncoder before we attempt to count class distributions:
Fit the Label Encoder on All Labels: Make sure label_encoder is fitted on all unique classes from y_class_labels, which is the array containing all the class labels for our dataset before splitting.
Transform Labels using LabelEncoder: After fitting, transform (encode) your class labels (y_train_class, y_val_class, y_test_class) directly in the main part of your script, so they're ready for training and also for counting class distributions.
Update the function to directly work with encoded labels: Since our labels are encoded and we don't actually need to inverse transform them for counting (we had used inverse transform thinking they were one-hot encoded, which was a misunderstanding based on the initial suggestion), we can count the distributions directly.
First, ensure label_encoder is fitted properly before splitting the dataset:
# Encode class labels
label_encoder.fit(y_class_labels) # Ensure all possible labels are here
# Transform class labels to integers
y_train_class_encoded = label_encoder.transform(y_train_class)
y_val_class_encoded = label_encoder.transform(y_val_class)
y_test_class_encoded = label_encoder.transform(y_test_class)
# Function updated to directly work with encoded labels
def count_class_distributions(y_labels_encoded):
# Count occurrences of each class
class_counts = {}
for label in y_labels_encoded:
if label in class_counts:
class_counts[label] += 1
else:
class_counts[label] = 1
return class_counts
# Count and print the distribution of classes in the datasets
train_class_distribution = count_class_distributions(y_train_class_encoded)
val_class_distribution = count_class_distributions(y_val_class_encoded)
test_class_distribution = count_class_distributions(y_test_class_encoded)
# Print the distributions
print("Class distribution in the Train set:", train_class_distribution)
print("Class distribution in the Validation set:", val_class_distribution)
print("Class distribution in the Test set:", test_class_distribution)
This approach:
Fits the LabelEncoder once with all unique class labels.
Uses the fitted LabelEncoder to transform class labels for training, validation, and test sets.
Adapts the counting function to work with already encoded labels rather than needing to inverse transform them.
By directly working with encoded labels, we avoid the NotFittedError and keep the encoding process streamlined.