The accuracy decreased after integrating categorical weighted loss function to my code

I want to increase the accuracy and I have imbalance dataset: akiec: 229, bcc:360, bkl:769,df:81,mel:779,vasc :99. In an effort to address this issue, I opted to integrate a categorical weighted loss mechanism into the model. However, despite this adjustment, I noticed a subsequent decrease in accuracy. This unexpected outcome leads me to suspect an error in the implementation process. Could you please assist me in identifying and resolving any potential mistakes to optimize the performance of my model?

# Define the directories
train_dir = '/content/drive/MyDrive/ikinciasamadataset/Train'
test_dir = '/content/drive/MyDrive/ikinciasamadataset/Test'
validation_dir = '/content/drive/MyDrive/ikinciasamadataset/Validation'

# Determine number of classes
numClasses = len(os.listdir(train_dir))

# Define grid of hyperparameters
param_grid = {
    'learning_rate': [0.001],
    'batch_size': [16],
}

best_accuracy = 0
best_params = None

# Perform grid search
for params in ParameterGrid(param_grid):
    # Load pre-trained VGG19 model for each grid search iteration
    base_model = VGG19(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
    for layer in base_model.layers:
        layer.trainable = False

    # Define function to extract features from the last convolutional layer
    def extract_features(generator, model):
        features = model.predict(generator)
        return features.reshape((len(generator.filenames), -1))

    # Create data generators
    train_datagen = ImageDataGenerator(
        rescale=1./255,
        rotation_range=20,
        width_shift_range=0.2,
        height_shift_range=0.2,
        shear_range=0.2,
        zoom_range=0.2,
        horizontal_flip=True,
        fill_mode='nearest')

    validation_datagen = ImageDataGenerator(rescale=1./255)

    train_generator = train_datagen.flow_from_directory(
        train_dir,
        target_size=(224, 224),
        batch_size=params['batch_size'],
        class_mode='categorical'
    )

    validation_generator = validation_datagen.flow_from_directory(
        validation_dir,
        target_size=(224, 224),
        batch_size=params['batch_size'],
        class_mode='categorical'
    )
'''

Probably there is a mistake here

'''

    # Define class indices
    class_indices = {
        'akiec': 0,
        'bcc': 1,
        'bkl': 2,
        'df': 3,
        'mel': 4,
        'vasc': 5
    }

    ## Calculate class counts
    class_counts = {}
    for class_name in os.listdir(train_dir):
        class_counts[class_name] = len(os.listdir(os.path.join(train_dir, class_name)))

    # Compute class weights
    class_weights = {}
    total_samples = sum(class_counts.values())
    for class_name, class_count in class_counts.items():
        class_weights[class_indices[class_name]] = total_samples / (class_count * len(class_counts))



    # Define the model architecture to accept extracted features as input
    inputs = Input(shape=(combined_data_train.shape[1],))
    x = Dense(256, activation='relu')(inputs)
    predictions = Dense(numClasses, activation='softmax')(x)
    model = Model(inputs=inputs, outputs=predictions)

    # Compile the model with current hyperparameters and class weights
    model.compile(optimizer=SGD(learning_rate=params['learning_rate']), loss='sparse_categorical_crossentropy', metrics=['accuracy'], sample_weight_mode='temporal')

    # Define early stopping
    early_stopping = EarlyStopping(monitor='val_loss', patience=5, restore_best_weights=True)

    # Train the model with early stopping
    num_epochs = 50  # You can adjust the number of epochs here
    history = model.fit(
        x=combined_data_train,
        y=train_generator.labels,
        epochs=num_epochs,
        batch_size=params['batch_size'],
        validation_data=(combined_data_validation, validation_generator.labels),
        callbacks=[early_stopping],
        class_weight=class_weights,
        verbose=1
    )

    model.save('best_vgg19_model_with_age.h5')

    # Evaluate the model on validation data
    _, val_accuracy = model.evaluate(combined_data_validation, validation_generator.labels, verbose=0)

    # Update best accuracy and best parameters if necessary
    if val_accuracy > best_accuracy:
        best_accuracy = val_accuracy
        best_params = params

# Print best parameters and accuracy
print('Best parameters:', best_params)
print('Best validation accuracy:', best_accuracy)


# Load the best model
best_model = load_model('best_vgg19_model_with_age.h5')

New contributor

Müberra Kurt is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật