Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove manual torch.no_grad and train eval mode in LightningModule #1100

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions icevision/models/mmdet/lightning/model_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,10 @@ def training_step(self, batch, batch_idx):
def validation_step(self, batch, batch_idx):
data, records = batch

self.model.eval()
with torch.no_grad():
outputs = self.model.train_step(data=data, optimizer=None)
raw_preds = self.model.forward_test(
imgs=[data["img"]], img_metas=[data["img_metas"]]
)
outputs = self.model.train_step(data=data, optimizer=None)
raw_preds = self.model.forward_test(
imgs=[data["img"]], img_metas=[data["img_metas"]]
)

preds = self.convert_raw_predictions(
batch=data, raw_preds=raw_preds, records=records
Expand All @@ -67,8 +65,5 @@ def validation_step(self, batch, batch_idx):
for k, v in outputs["log_vars"].items():
self.log(f"valid/{k}", v)

# TODO: is train and eval model automatically set by lighnting?
self.model.train()

def validation_epoch_end(self, outs):
self.finalize_metrics()
17 changes: 8 additions & 9 deletions icevision/models/ross/efficientdet/lightning/model_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,14 @@ def training_step(self, batch, batch_idx):
def validation_step(self, batch, batch_idx):
(xb, yb), records = batch

with torch.no_grad():
raw_preds = self(xb, yb)
preds = efficientdet.convert_raw_predictions(
batch=(xb, yb),
raw_preds=raw_preds["detections"],
records=records,
detection_threshold=0.0,
)
loss = efficientdet.loss_fn(raw_preds, yb)
raw_preds = self(xb, yb)
preds = efficientdet.convert_raw_predictions(
batch=(xb, yb),
raw_preds=raw_preds["detections"],
records=records,
detection_threshold=0.0,
)
loss = efficientdet.loss_fn(raw_preds, yb)

self.accumulate_metrics(preds)

Expand Down
21 changes: 10 additions & 11 deletions icevision/models/torchvision/lightning_model_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,16 @@ def training_step(self, batch, batch_idx):

def validation_step(self, batch, batch_idx):
(xb, yb), records = batch
with torch.no_grad():
self.train()
train_preds = self(xb, yb)
loss = loss_fn(train_preds, yb)

self.eval()
raw_preds = self(xb)
preds = self.convert_raw_predictions(
batch=batch, raw_preds=raw_preds, records=records
)
self.accumulate_metrics(preds=preds)
self.train()
train_preds = self(xb, yb)
loss = loss_fn(train_preds, yb)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't able to understand why do we calculate training prediction here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe the name is misleading , these are in fact validation preds. These are used to calculate validation loss and metrics


self.eval()
raw_preds = self(xb)
preds = self.convert_raw_predictions(
batch=batch, raw_preds=raw_preds, records=records
)
self.accumulate_metrics(preds=preds)

self.log("val_loss", loss)

Expand Down
19 changes: 9 additions & 10 deletions icevision/models/ultralytics/yolov5/lightning/model_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,15 @@ def training_step(self, batch, batch_idx):
def validation_step(self, batch, batch_idx):
(xb, yb), records = batch

with torch.no_grad():
inference_out, training_out = self(xb)
preds = yolov5.convert_raw_predictions(
batch=xb,
raw_preds=inference_out,
records=records,
detection_threshold=0.001,
nms_iou_threshold=0.6,
)
loss = self.compute_loss(training_out, yb)[0]
inference_out, training_out = self(xb)
preds = yolov5.convert_raw_predictions(
batch=xb,
raw_preds=inference_out,
records=records,
detection_threshold=0.001,
nms_iou_threshold=0.6,
)
loss = self.compute_loss(training_out, yb)[0]

self.accumulate_metrics(preds)

Expand Down