Search code examples
kerasdeep-learningcomputer-visiontransfer-learningefficientnet

How can I train a model ( like EfficientNet, Resnet ) from scratch in Keras?


Is there a way, where we can load the architecture of a network and then train it from scratch in Keras?


Solution

  • Yes, Let's say you want to train a classifier for 2 classes and 255x255x3 input using "ResNet50v2" from scratch, All you have to do is import the Architecture without its last softmax layer, add your custom layers and initialize weights with "None".

    from keras.applications.resnet_v2 import ResNet50V2 
    from keras.models import Model
    from keras.layers import Dense, GlobalAveragePooling2D
    
    input_shape = (255,255,3)
    n_class = 2
    base_model = ResNet50V2(weights=None,input_shape=input_shape,include_top=False)
    
    # Add Custom layers
    x = base_model.output
    x = GlobalAveragePooling2D()(x)
    # ADD a fully-connected layer
    x = Dense(1024, activation='relu')(x)
    # Softmax Layer
    predictions = Dense(n_class, activation='softmax')(x)
    model = Model(inputs=base_model.input, outputs=predictions)
    
    # Compile Model
    model.compile(optimizer='adam', loss='categorical_crossentropy',metrics=['accuracy'])
    
    # Train
    model.fit(X_train,y_train,epochs=20,batch_size=50,validation_data=(X_val,y_val))
    

    Similarly, To use other architecture such as EfficienNet, Please refer to Keras Documention. For specifically EfficientNet,you can also follow this link