classification.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # coding=utf-8
  2. # Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. """Vision-classification finetuning/evaluation."""
  16. from megatron import get_args
  17. from megatron import print_rank_0
  18. from megatron.model.vit_model import VitModel
  19. from megatron.data.vit_dataset import build_train_valid_datasets
  20. from tasks.vision.eval_utils import accuracy_func_provider
  21. from tasks.vision.finetune_utils import finetune
  22. def classification():
  23. def train_valid_datasets_provider():
  24. """Build train and validation dataset."""
  25. args = get_args()
  26. train_ds, valid_ds = build_train_valid_datasets(
  27. data_path=args.data_path,
  28. crop_size=args.img_dim,
  29. )
  30. return train_ds, valid_ds
  31. def model_provider(pre_process=True, post_process=True):
  32. """Build the model."""
  33. args = get_args()
  34. print_rank_0("building classification model for ImageNet ...")
  35. return VitModel(num_classes=args.num_classes, finetune=True,
  36. pre_process=pre_process, post_process=post_process)
  37. """Finetune/evaluate."""
  38. finetune(
  39. train_valid_datasets_provider,
  40. model_provider,
  41. end_of_epoch_callback_provider=accuracy_func_provider,
  42. )
  43. def main():
  44. classification()