-
-
Notifications
You must be signed in to change notification settings - Fork 16.2k
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
Refactor train function into Trainer class #515
Conversation
Updated to latest commit
Update master branch
Update to v2
* Update datasets.py (ultralytics#494) * update yolo.py TTA flexibility and extensibility (ultralytics#506) * update yolo.py TTA flexibility and extensibility * Update scale_img() * Update utils.py strip_optimizer() (ultralytics#509) * Update utils.py strip_optimizer() (ultralytics#509) Follow-on update that I missed adding into PR 509. * Update utils.py strip_optimizer() (ultralytics#509) Follow-on update that I missed adding into PR 509. Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
@MagicFrogSJTU , could you tell me if I'm going the right path? |
For train.py, I think we need a deep survey and draw a design first. Simpler spliting the code is not what we only want. The most important target is to separate accademic logics and enginner routines, as in pytorch_lightning. |
I see. I followed the example from hugging_face/transformers. I notice that the code was split into smaller functions. The only difference is that transformers 's I will re-look at pytorch lightning's then. |
After reading the code on lightning, I think it's too complex. I will have to spend some time thinking if it's even possible for me to do it or worth it as it does not seem to be an urgent issue. I will close this PR first. |
@NanoCode012 My advice on imitating pytorch_lighting is just a suggestion, cause I myself don't have a clear vision now. So do what you think is right! Don't let me stop you! |
No, you are right on the splitting part. I actually felt like I made things harder to understand due to many functions and helper functions. Right now, I have a feeling to make one Trainer class that will be inherited by a new Train class. The Trainer class is where the “necessary” part goes , for loop in batch, set img.to(device), set sampler. Where as the Train class will implement the “forward” function, “backward”, create_optimiser. This way, the main implementation should only be in the Train class. |
Sounds reasonable! |
Hm, would it be unreasonable to use the Lightning module? Since they have already implemented the hard work and tested, wouldn’t it be better to use them? Or, are there certain reasons we can’t use them(license etc) |
For my own opionion, pytorch_lightning is too heavy. I think quick, easy, smooth transform is more acceptable for Glenn. |
Ok. I see! |
My idea is to break down the parts of
train
function into smaller functions and put them into one class calledTrainer
.Now,
train
function only needs to callIn
Trainer
class, I created functions and helper functions for each section in the originaltrain
code.I believe this will be better because it separates different codes under different functions, so it will be easier to read and understand instead of being solely dependent on comments.
The only files modified are
train.py
andtrainer.py
. The others are most likely due to commit history when I merge to master.I plan to move
trainer.py
into utils folder after I fix the imports issue.The biggest part to do would be the
fit
function where the training happens. I'm creating this draft PR first to see if glenn likes this idea before I proceed further.🛠️ PR Summary
Made with ❤️ by Ultralytics Actions
🌟 Summary
Refactoring YOLOv5 training flow to introduce a trainer class for better code organization and maintainability.
📊 Key Changes
create_pretrained
function in favor ofstrip_optimizer
function.create_pretrained
replaced withstrip_optimizer
indicating a move towards finalizing model training.Trainer
class and updates in how the optimizer and scheduler are managed.Trainer
class responsible for organizing the entire training process.create_pretrained
function and changes tostrip_optimizer
to strip optimizers after training completion.🎯 Purpose & Impact
Trainer
class makes the code more modular and easier to maintain.strip_optimizer
function helps in obtaining slimmer models by removing non-essential information post-training.