Skip to content
This repository has been archived by the owner on Jul 1, 2024. It is now read-only.

add option test_phase_period to ClassificationTask #395

Closed
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
16 changes: 15 additions & 1 deletion classy_vision/tasks/classification_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ def __init__(self):
self.datasets = {}
self.meters = []
self.num_epochs = 1
self.test_phase_period = 1
self.test_only = False
self.base_model = None
self.optimizer = None
Expand Down Expand Up @@ -146,6 +147,15 @@ def set_num_epochs(self, num_epochs: Union[int, float]):
self.num_epochs = num_epochs
return self

def set_test_phase_period(self, test_phase_period: int):
"""Set the period of test phase.

Args:
test_phase_period: The period of test phase
"""
self.test_phase_period = test_phase_period
return self

def set_dataset(self, dataset: ClassyDataset, phase_type: str):
"""Set dataset for phase type on task

Expand Down Expand Up @@ -295,6 +305,7 @@ def from_config(cls, config: Dict[str, Any]) -> "ClassificationTask":
task = (
cls()
.set_num_epochs(config["num_epochs"])
.set_test_phase_period(config.get("test_phase_period", 1))
.set_loss(loss)
.set_test_only(test_only)
.set_model(model)
Expand Down Expand Up @@ -378,8 +389,11 @@ def _build_phases(self):
phases = [{"train": True} for _ in range(self.num_epochs)]

final_phases = []
for phase in phases:
for i, phase in enumerate(phases):
final_phases.append(phase)
if (i + 1) % self.test_phase_period == 0:
final_phases.append({"train": False})
if final_phases[-1]["train"]:
final_phases.append({"train": False})
return final_phases

Expand Down