โครงสร้างที่เรียบง่ายและได้รับการออกแบบมาอย่างดีเป็นสิ่งจำเป็นสำหรับโครงการการเรียนรู้เชิงลึกดังนั้นหลังจากฝึกฝนและมีส่วนร่วมในโครงการ Pytorch นี่คือเทมเพลตโครงการ Pytorch ที่รวม ความเรียบง่ายแนวปฏิบัติที่ดีที่สุดสำหรับโครงสร้างโฟลเดอร์ และ การออกแบบ OOP ที่ดี แนวคิดหลักคือมีสิ่งเดียวกันกับที่คุณทำทุกครั้งที่คุณเริ่มต้นโครงการ Pytorch ดังนั้นการห่อสิ่งที่ใช้ร่วมกันทั้งหมดนี้จะช่วยให้คุณเปลี่ยนแนวคิดหลักทุกครั้งที่คุณเริ่มโครงการ Pytorch ใหม่
ดังนั้นนี่คือเทมเพลต Pytorch ที่เรียบง่ายที่ช่วยให้คุณเข้าสู่โครงการหลักได้เร็วขึ้นและมุ่งเน้นไปที่แกนกลางของคุณ (สถาปัตยกรรมแบบจำลองการฝึกอบรม ฯลฯ )
เพื่อลดสิ่งที่ซ้ำ ๆ เราขอแนะนำให้ใช้ห้องสมุดระดับสูง คุณสามารถเขียนห้องสมุดระดับสูงของคุณเองหรือคุณสามารถใช้ห้องสมุดส่วนที่สามบางส่วนเช่น Ignite, Fastai, MMCV … ฯลฯ ซึ่งจะช่วยให้คุณเขียนลูปการฝึกอบรมขนาดกะทัดรัด แต่เต็มรูปแบบในรหัสไม่กี่บรรทัด ที่นี่เราใช้ Ignite เพื่อฝึก MNIST เป็นตัวอย่าง
สรุปนี่คือวิธีการใช้เทมเพลตนี้ดังนั้น ตัวอย่างเช่น สมมติว่าคุณต้องการใช้ RESNET-18 เพื่อฝึกอบรม MNIST ดังนั้นคุณควรทำสิ่งต่อไปนี้:
modeling สร้างไฟล์ Python ชื่อสิ่งที่คุณต้องการที่นี่เราตั้งชื่อมัน example_model.py ใน modeling/__init__.py ไฟล์คุณสามารถสร้างฟังก์ชั่นชื่อ build_model เพื่อเรียกโมเดลของคุณ from . example_model import ResNet18
def build_model ( cfg ):
model = ResNet18 ( cfg . MODEL . NUM_CLASSES )
return modelengine สร้างฟังก์ชั่นเทรนเนอร์รุ่นและฟังก์ชั่นการอนุมาน ในฟังก์ชั่นเทรนเนอร์คุณต้องเขียนตรรกะของกระบวนการฝึกอบรมคุณสามารถใช้ห้องสมุดบุคคลที่สามเพื่อลดสิ่งที่ทำซ้ำ # trainer
def do_train ( cfg , model , train_loader , val_loader , optimizer , scheduler , loss_fn ):
"""
implement the logic of epoch:
-loop on the number of iterations in the config and call the train step
-add any summaries you want using the summary
"""
pass
# inference
def inference ( cfg , model , val_loader ):
"""
implement the logic of the train step
- run the tensorflow session
- return any metrics you need to summarize
"""
passtools คุณสร้าง train.py ในไฟล์นี้คุณจะต้องได้รับอินสแตนซ์ของวัตถุ "โมเดล" ต่อไปนี้ "Dataloader", "Optimizer" และ config # create instance of the model you want
model = build_model ( cfg )
# create your data generator
train_loader = make_data_loader ( cfg , is_train = True )
val_loader = make_data_loader ( cfg , is_train = False )
# create your model optimizer
optimizer = make_optimizer ( cfg , model )do_train และเริ่มการฝึกอบรมของคุณ # here you train your model
do_train ( cfg , model , train_loader , val_loader , optimizer , None , F . cross_entropy )คุณจะพบไฟล์เทมเพลตและตัวอย่างง่ายๆในโฟลเดอร์โมเดลและเทรนเนอร์ที่แสดงวิธีลองรุ่นแรกของคุณ
├── config
│ └── defaults.py - here's the default config file.
│
│
├── configs
│ └── train_mnist_softmax.yml - here's the specific config file for specific model or dataset.
│
│
├── data
│ └── datasets - here's the datasets folder that is responsible for all data handling.
│ └── transforms - here's the data preprocess folder that is responsible for all data augmentation.
│ └── build.py - here's the file to make dataloader.
│ └── collate_batch.py - here's the file that is responsible for merges a list of samples to form a mini-batch.
│
│
├── engine
│ ├── trainer.py - this file contains the train loops.
│ └── inference.py - this file contains the inference process.
│
│
├── layers - this folder contains any customed layers of your project.
│ └── conv_layer.py
│
│
├── modeling - this folder contains any model of your project.
│ └── example_model.py
│
│
├── solver - this folder contains optimizer of your project.
│ └── build.py
│ └── lr_scheduler.py
│
│
├── tools - here's the train/test model of your project.
│ └── train_net.py - here's an example of train model that is responsible for the whole pipeline.
│
│
└── utils
│ ├── logger.py
│ └── any_other_utils_you_need
│
│
└── tests - this foler contains unit test of your project.
├── test_data_sampler.py
ยินดีต้อนรับการเพิ่มประสิทธิภาพหรือการมีส่วนร่วมใด ๆ