generated from Borda/kaggle_SandBox
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add CLI train * update req. * kaggle link Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
9b203e0
commit 03727f3
Showing
4 changed files
with
63 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import os | ||
|
||
import fire | ||
import flash | ||
import torch | ||
from flash.image import ObjectDetectionData, ObjectDetector | ||
|
||
|
||
def main( | ||
path_dataset: str, | ||
image_size: int = 512, | ||
head: str = "efficientdet", | ||
backbone: str = "tf_d0", | ||
learn_rate: float = 1.5e-5, | ||
batch_size: int = 12, | ||
num_epochs: int = 30 | ||
) -> None: | ||
# 1. Create the DataModule | ||
dm = ObjectDetectionData.from_coco( | ||
train_folder=os.path.join(path_dataset, "images", 'train'), | ||
train_ann_file=os.path.join(path_dataset, "covid_train.json"), | ||
val_split=0.1, | ||
batch_size=batch_size, | ||
image_size=image_size, | ||
) | ||
|
||
# 2. Build the task | ||
model = ObjectDetector( | ||
head=head, | ||
backbone=backbone, | ||
optimizer=torch.optim.AdamW, | ||
learning_rate=learn_rate, | ||
num_classes=dm.num_classes, | ||
image_size=image_size, | ||
) | ||
|
||
# 3. Create the trainer and finetune the model | ||
trainer = flash.Trainer( | ||
max_epochs=num_epochs, | ||
gpus=torch.cuda.device_count(), | ||
precision=16, | ||
accumulate_grad_batches=24, | ||
val_check_interval=0.5, | ||
) | ||
trainer.finetune(model, datamodule=dm, strategy="freeze_unfreeze") | ||
|
||
# 3. Save the model! | ||
trainer.save_checkpoint("object_detection_model.pt") | ||
|
||
|
||
if __name__ == '__main__': | ||
fire.Fire(main) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
torch>=1.6 | ||
lightning-flash>=0.4 | ||
# lightning-flash[image]>=0.5 # install master till v0.5 is released | ||
git+https://github.com/PyTorchLightning/lightning-flash.git#egg=lightning-flash[audio,image] | ||
python-gdcm | ||
pydicom | ||
opencv-python-headless | ||
pycocotools | ||
fire |