How to get train/eval flg in side the model?

Hi,

During the training, we use

model.train() 
for i, ( imgs, labels) in enumerate(train_loader):
    prd = model(imgs)

and

model.eval() 
for i, ( imgs, labels) in enumerate(val_loader):
    prd = model(imgs)

to swtich on/off some functions.

How could I get the train/eval flag in side the model? Is there a build-in parameters that I could use? Or I have to pass the flg like

prd = model(imgs, flg_trn=True) 

Inside of your model you can use the self.training attribute.

1 Like

I copy the source code of resnet.py to the folder. And add different codes in forward(self, x) for the training case and eval case.

def forward(self, x)
      x = ......
      if self.training :
            print("...........training.......")

This print doesnt work.

from resent import resnet50
model = resnet50()
model.train() 
for i, ( imgs, labels) in enumerate(train_loader):
    prd = model(imgs)

Am I missing something?

It should work. Could you add a print statement before the if condition to check, if your custom code is really used instead of the original torchvision implementation?

You’re right.

I changed model.train() to model.eval() by mistake.

Thanks for your help.