Unfreeze layers of models on GluonCV

76 Views Asked by At

I'm using Faster-RCNN, Yolo, and SSD models on GluonCV (mxnet) to predict on some medical images. However, the training result isn't ideal because the number of images in the dataset is small. As a result, I decide to use transfer learning and unfreeze the output layer with the 'reset_class' method to train my models. But the result is still below expectation. Thus, I'm trying to unfreeze more layers to improve the accuracy of the training result, but I couldn't find any build-in function to achieve this.

Basically, I have two questions as follow: First, is it possible to unfreeze more layers on GluonCV? Second, if not, is there any other way that I could use to further improve the accuracy of my result?

1

There are 1 best solutions below

0
On

To be honest, I'm not sure why you believe that model parameters are frozen by default and reset_class will unfreeze an output layer. Still, if you want to unfreeze specific parameters, you need to choose them with collect_params method and set their grad_req attribute to 'write'. For example, having the following convolutional network

import mxnet as mx

class ConvNet(mx.gluon.nn.HybridSequential):
    def __init__(self, n_classes, params=None, prefix=None):
        super().__init__(params=params, prefix=prefix)

        self.features = mx.gluon.nn.HybridSequential()
        self.features.add(mx.gluon.nn.Conv2D(channels=6, kernel_size=5, padding=2,
                          activation='relu'))
        self.add(mx.gluon.nn.MaxPool2D(pool_size=2, strides=2))
        self.add(mx.gluon.nn.Flatten())

        self.output = mx.gluon.nn.Dense(units=n_classes)

    def hybrid_forward(self, F, x):
        x = self.features(x)
        return self.output(x)

net = ConvNet(10)

you can unfreeze convolution (features) block with

net.features.collect_params().setattr('grad_req', 'write')

Also, if your model is not composed of features, output, and/or other separate blocks (e.g. it's a single sequential block):

net = mx.gluon.nn.HybridSequential()
net.add(mx.gluon.nn.Conv2D(channels=6, kernel_size=5, padding=2,
                           activation='relu'))
net.add(mx.gluon.nn.MaxPool2D(pool_size=2, strides=2))
net.add(mx.gluon.nn.Flatten())
net.add(mx.gluon.nn.Dense(units=10))

to unfreeze the convolution block try this:

net.collect_params('conv*').setattr('grad_req', 'write')