Search code examples
pythontensorflowkerasdeep-learningmulticlass-classification

Is there an architecture for multiclass-classification with large amount of classes?


I'm pretty new to deep learning, doing hobby projects. Right now i'm doing multiclass image classification with 200 classes. Is there a tutorial or an actual architecture i can take a look on?

So far i tried basic Dense and CNN nets, but i could never reach better accuracy than 5%.

So far my very basic CNN looks like this.

model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),
                 activation='relu',
                 input_shape=input_shape,
                 data_format='channels_first'))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(128, (4, 4), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(256, (5, 5), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.5))
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))

model.compile(loss=keras.losses.categorical_crossentropy,
              optimizer=keras.optimizers.Adam(),
              metrics=['accuracy'])

I looked up for solutions but could never find any project with such large amount of classes(besides VGG-19 or other SOTA CNNs, but i would try write my own, since this is for learning purposes). Is anybody had similar projects or have some tutorial or any advise on such problem?

Thanks in advance.


Solution

  • 200 classes is literally few.

    Try

    from keras.applications.resnet50 import ResNet50
    
    model = ResNet50(weights=None, classes=200)
    model.compile(loss=keras.losses.categorical_crossentropy,
                  optimizer=keras.optimizers.Adam(),
                  metrics=['accuracy'])
    

    This model(ResNet50) should be good enough for most task.

    Every models in keras.applications are trained to have 1000 classes, if your task is some real-world image you can use the trained weights by

    from keras.applications.resnet50 import ResNet50
    from keras.layers import Dense
    from keras.models import Model
    
    model = ResNet50(weights='imagenet')
    x = model.get_layer('avg_pool').output
    x = Dense(200, activation='softmax')(x)
    model = Model(model.input, x)