Package: pytorch-hrvvi-ext
Overviewpytorch-hrvvi-extis my extension to PyTorch, which contains many "out of the box" tools to facilitate my everyday study. It is very easy to use them and integrate them to your projects.
I will call ithutilbelow because ofimport hutil.Installpip3install-U--no-cache-dir--index-urlhttps://test.pypi.org/simple/--extra-index-urlhttps://pypi.org/simplepytorch-hrvvi-extHightlightsTrainerTraineris written onignite, providing the following features:Train your network in few lines without writing loops explicitly.Automatic gpu support like KerasMetric for both CV and NLP (Loss, Accuracy, Top-K Accuracy, mAP, BLEU)Checkpoints of the whole trainer by epochs or metricsSend metric history to WeChatDatasetshutilcontains many datasets wrapped by me providingtorchvison.datasetsstyle API. Some of them is much easier to train than VOC or COCO and more suitable forBEGINNERSin object detection. Now it contains the following datasets:CaptchaDetectionOnline: generate captcha image and bounding boxes of chars onlineSVHNDetection:SVHNdataset for object detectionCocoDetection: unreleased dataset of torchvison withhutil's transformsVOCDetection: unreleased dataset of torchvison withhutil's transformsTransformsTransoforms inhutiltransform inputs and targets of datasets simultaneously, which is more flexible thantorchvison.transformsand makes it easier to do data augmentation for object detection withtorchvision.transformsstyle API. The following transoforms is provided now:ResizeCenterCropToPercentCoordsComposeInputTransformTargetTransformOtherstrain_test_split: Split a dataset to a train set and a test set with different (or same) transformsFullset: Transform your dataset tohutil' style datasetExamplesCIFAR10# Data Preparationtrain_transforms=InputTransform(Compose([RandomCrop(32,padding=4),RandomHorizontalFlip(),ToTensor(),Normalize((0.4914,0.4822,0.4465),(0.247,0.243,0.261)),]))test_transform=InputTransform(Compose([ToTensor(),Normalize((0.4914,0.4822,0.4465),(0.247,0.243,0.261)),]))data_home=gpath("datasets/CIFAR10")ds=CIFAR10(data_home,train=True,download=True)ds_train,ds_val=train_test_split(ds,test_ratio=0.04,transform=train_transforms,test_transform=test_transform,)ds_test=CIFAR10(data_home,train=False,download=True)# Define network, loss and optimizernet=ResNet(WideSEBasicBlock,[4,4,4],k=2)net.apply(init_weights(nonlinearity='relu'))criterion=nn.CrossEntropyLoss()optimizer=SGD(net.parameters(),lr=1e-1,momentum=0.9,dampening=0,weight_decay=5e-4,nesterov=True)lr_scheduler=MultiStepLR(optimizer,[40,80,110],gamma=0.2)# Define metricsmetrics={'loss':Loss(),'acc':Accuracy(),}# Put it together with Trainertrainer=Trainer(net,criterion,optimizer,lr_scheduler,metrics=metrics,save_path=gpath("models"),name="CIFAR10-SE-WRN28-2")# Show number of parameterssummary(net,(3,32,32))# Define batch sizetrain_loader=DataLoader(ds_train,batch_size=32,shuffle=True,num_workers=1,pin_memory=True)test_loader=DataLoader(ds_test,batch_size=128)val_loader=DataLoader(ds_val,batch_size=128)# Train and save good models by val loss (lower is better) after first 40 epochstrainer.fit(train_loader,100,val_loader=val_loader,save_by_metric='-val_loss',patience=40)CaptchaDetectionOnlineletters="0123456789abcdefghijkmnopqrstuvwxyzABDEFGHJKMNRT"NUM_CLASSES=len(letters)+1WIDTH=128HEIGHT=48LOCATIONS=[(8,3),(4,2),]ASPECT_RATIOS=[(1,2,1/2),(1,2,1/2),]ASPECT_RATIOS=[torch.tensor(ars)forarsinASPECT_RATIOS]NUM_FEATURE_MAPS=len(ASPECT_RATIOS)SCALES=compute_scales(NUM_FEATURE_MAPS,0.2,0.9)DEFAULT_BOXES=[compute_default_boxes(lx,ly,scale,ars)for(lx,ly),scale,arsinzip(LOCATIONS,SCALES,ASPECT_RATIOS)]# Define captcha datasetfonts=[gpath("fonts/msyh.ttf"),gpath("fonts/sfsl0800.pfb.ttf"),gpath("fonts/SimHei.ttf"),gpath("fonts/Times New Roman.ttf"),]font_sizes=(28,32,36,40,44,48)image=ImageCaptcha(WIDTH,HEIGHT,fonts=fonts,font_sizes=font_sizes)transform=Compose([ToPercentCoords(),ToTensor(),SSDTransform(SCALES,DEFAULT_BOXES,NUM_CLASSES),])test_transform=Compose([ToTensor(),])ds_train=CaptchaDetectionOnline(image,size=50000,letters=letters,rotate=20,transform=transform)ds_val=CaptchaDetectionOnline(image,size=1000,letters=letters,rotate=20,transform=test_transform,online=False)# Define network, loss and optimizerout_channels=[(NUM_CLASSES+4)*len(ars)forarsinASPECT_RATIOS]net=DSOD([3,4,4,4],36,out_channels=out_channels,reduction=1)net.apply(init_weights(nonlinearity='relu'))criterion=SSDLoss(NUM_CLASSES)optimizer=Adam(net.parameters(),lr=3e-4)lr_scheduler=MultiStepLR(optimizer,[40,70,100],gamma=0.1)# Define metrics for training and testingmetrics={'loss':TrainLoss(),}test_metrics={'mAP':MeanAveragePrecision(SSDInference(width=WIDTH,height=HEIGHT,f_default_boxes=[cuda(d)fordinDEFAULT_BOXES],num_classes=NUM_CLASSES,))}# Put it together with Trainertrainer=Trainer(net,criterion,optimizer,lr_scheduler,metrics=metrics,evaluate_metrics=test_metrics,save_path=gpath("models"),name="DSOD-CAPTCHA-48")# Show numbers of parameterssummary(net,(3,HEIGHT,WIDTH))# Define batch sizetrain_loader=DataLoader(ds_train,batch_size=32,shuffle=True,num_workers=1,pin_memory=True)val_loader=DataLoader(ds_val,batch_size=32,collate_fn=box_collate_fn)# Train and save by val mAP (higher is better) after first 10 epochstrainer.fit(train_loader,15,val_loader=val_loader,save_by_metric='val_mAP',patience=10)