How to do Ensemble of Resnet50 and Resnet152?

Hello Everyone,

I trained two models Resnet50 and Resnet152 and I want to use both of them as Average Ensemble!

I know I can do it in Keras but is it possible in fastai? I tried to search for it but I couldn’t find any.

Any help would be appreciated :slight_smile:

2 Likes

I build a tabular learner using the validation prediction from both model as input.
The result is so much better than a single model.

3 Likes

Wow! Can u share the code, please?

The csv files contain the probability for classifying it as 0 and 1.

Both of the models are trained on the same set of data with the same set of validation with the same random seed for random splitting.

from fastai.tabular import *
from fastai.callbacks import ReduceLROnPlateauCallback, EarlyStoppingCallback, SaveModelCallback
from sklearn.metrics import roc_auc_score
import gc

dense161 = pd.read_csv("../input/cancer-densenet161-v2-for-ensemble/validation_0.976066529750824.csv") dense161_test = pd.read_csv("../input/cancer-densenet161-v2-for-ensemble/submission_0.976066529750824.csv")
dense201 = pd.read_csv("../input/cancer-densenet201-v2-for-ensemble/validation_0.9749373197555542.csv") dense201_test = pd.read_csv("../input/cancer-densenet201-v2-for-ensemble/submission_0.9749373197555542.csv")
res50 = pd.read_csv("../input/cancer-resnet50-v2-for-ensemble/validation_0.9727705717086792.csv") res50_test = pd.read_csv("../input/cancer-resnet50-v2-for-ensemble/submission_0.9727705717086792.csv")

train = pd.DataFrame({'dense161_0':dense161.val_0, 'dense161_1':dense161.val_1, 
                      'dense201_0':dense201.val_0, 'dense201_1':dense201.val_1,
                      'res50_0':res50.val_0, 'res50_1':res50.val_1,
                      "y":dense161.ground_truth_label})
test = pd.DataFrame({'dense161_0':dense161_test.pred_0, 'dense161_1':dense161_test.pred_1, 
                      'dense201_0':dense201_test.pred_0, 'dense201_1':dense201_test.pred_1,
                      'res50_0':res50_test.pred_0, 'res50_1':res50_test.pred_1})
test.y=0

dep_var = 'y'
cont_names = ['dense161_0', 'dense161_1', 'dense201_0', 'dense201_1', 'res50_0','res50_1']

data = (TabularList.from_df(train, cont_names=cont_names)
            .split_by_rand_pct(seed=47)
            .label_from_df(cols=dep_var)
            .add_test(TabularList.from_df(test, cont_names=cont_names))
            .databunch())

def roc_score(inp, target):
    _, indices = inp.max(1)
    return torch.Tensor([roc_auc_score(target, indices)])[0]

from fastai.callbacks import ReduceLROnPlateauCallback, EarlyStoppingCallback, SaveModelCallback
ES = EarlyStoppingCallback(learn, monitor='roc_score',patience = 5)
RLR = ReduceLROnPlateauCallback(learn, monitor='roc_score',patience = 2)
SAVEML = SaveModelCallback(learn, every='improvement', monitor='roc_score', name='best')
learn.fit_one_cycle(20, 1e-3, callbacks = [ES, RLR, SAVEML])

learn.load('best')

preds, _ = learn.get_preds(DatasetType.Test)
preds = torch.softmax(preds, dim=1)[:, 1].numpy()
7 Likes

Thanks for sharing.

I don’t have CSV files for my model I have only the weight files. How can I apply it directly to them??

How much does this model improve the score?

Hi Ben, I am facing a similar problem. Thanks for your code. But I didn’t find the part that creates the learner. Would you share it with us?
Thank you very much.

1 Like

what is val_0 and val_1 should’nt there be just 1 column…and why is test.y set to zero?

Can you please explain this code. Why are you reading csv files and putting them in a df?

2 Likes

yeah, the csv files are the image val set predictions and test set predictions from each model.
so I use the image val set as the tabular training set and try to use that tabular model to make an ensemble model.