The accuracy on the test set corresponds to the class balance. What could be the problem?

I’m trying to build a neural network for classifying images into cats and dogs, I tried different architectures like Resnet50, AlexNet, MobileNetV2 and made my own architecture, as a result there were no problems with accuracy on training data and on validation data, accuracy was around 80-91% on training data data and 75-85% on the validation set, but when I check the accuracy on the test set, it is always at the level of 49-51%. And one day I decided to add another class and the accuracy turned out to be 32-34% on the test set, if I add 5 classes then the accuracy is in the region of 19-21% and the class balance starting from the binary classification of 0.5 then 0.33 and 0.2, respectively.

Checks:
Class weights:
{‘cat’: 1.0, ‘dog’: 1.0}

Class Balance:
{‘cat’: 0.5, ‘dog’: 0.5}

train 4800 validation 1200


Code




    from sklearn.model_selection import train_test_split
    train_df, valid_df = train_test_split(data,
                                    test_size = 0.20,
                                    random_state = 41,
                                    stratify = data['labels'])
    print('train', train_df.shape[0], 'validation', valid_df.shape[0])

    datagen = ImageDataGenerator(rescale = 1./255,
       featurewise_center=False,
       samplewise_center=False,
         rotation_range=90,
         width_shift_range=0.1,
         height_shift_range=0.1,
         horizontal_flip=True,
         vertical_flip=True,
         shear_range = 10
    )
    validation_datagen = ImageDataGenerator(rescale = 1./255,
 
                                        )
    train_generator = datagen.flow_from_dataframe(
     dataframe = train_df,
     directory='/content/images/train/',
    x_col='image',
     y_col='labels',
   target_size=(224,224),
    batch_size=32,
    shuffle=True,
    class_mode = 'binary',
    color_mode = 'rgb'
)

    valid_generator = validation_datagen.flow_from_dataframe(
      dataframe = valid_df,
      directory='/content/images/train/',
       x_col='image',
       y_col='labels',
      target_size=(224,224),
      batch_size=32,
       shuffle=False,
       color_mode = 'rgb',
       class_mode = 'binary'
      )

    backbone = MobileNetV2(input_shape=(224,224, 3),
                     weights='imagenet',
                      include_top=False)
    model = Sequential()
    model.add(backbone)
    model.add(GlobalAveragePooling2D())
    model.add(Dense(100, activation='relu'))
    model.add(Dropout(0.4))
    model.add(Dense(20 activation='relu'))
    model.add(Dropout(0.3))
    model.add(Dense(2, activation='sigmoid'))

    model.compile(optimizer=Adam(lr=0.0001), loss='binary_crossentropy', metrics=['accuracy'])
    model.summary()

    model.fit(train_generator,
           steps_per_epoch=train_generator.samples // 32,
           validation_steps=valid_generator.samples // 32,
          validation_data=valid_generator, epochs=5)
  

I’ve already tried everything and increased lr and Adam changed it to sgd and made 2 neurons in the output layer and removed class_mode or changed it to sparse and changed it to sparse_categorical_crossentropy and different architectures changed and increased the dataset (there were 20,000 images), downloaded and changed different image datasets, everything Equally, the accuracy corresponds to the balance of classes. Maybe I’m processing the images incorrectly or need to be processed somehow. Maybe there is a problem with google colab itself? Because I write the code there

New contributor

Genkoder 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