@@ -61,15 +61,14 @@ import os
61
61
from torch import nn, optim
62
62
from torch.utils.data import DataLoader
63
63
from catalyst import dl, utils
64
- from catalyst.data import ToTensor
65
- from catalyst.contrib.datasets import MNIST
64
+ from catalyst.contrib import ImageToTensor, MNIST
66
65
67
66
model = nn.Sequential(nn.Flatten(), nn.Linear(28 * 28 , 10 ))
68
67
criterion = nn.CrossEntropyLoss()
69
68
optimizer = optim.Adam(model.parameters(), lr = 0.02 )
70
69
71
- train_data = MNIST(os.getcwd(), train = True , download = True , transform = ToTensor ())
72
- valid_data = MNIST(os.getcwd(), train = False , download = True , transform = ToTensor ())
70
+ train_data = MNIST(os.getcwd(), train = True , download = True , transform = ImageToTensor ())
71
+ valid_data = MNIST(os.getcwd(), train = False , download = True , transform = ImageToTensor ())
73
72
loaders = {
74
73
" train" : DataLoader(train_data, batch_size = 32 ),
75
74
" valid" : DataLoader(valid_data, batch_size = 32 ),
@@ -105,7 +104,7 @@ metrics = runner.evaluate_loader(
105
104
loader = loaders[" valid" ],
106
105
callbacks = [dl.AccuracyCallback(input_key = " logits" , target_key = " targets" , topk_args = (1 , 3 , 5 ))],
107
106
)
108
- assert " accuracy " in metrics.keys()
107
+ assert " accuracy01 " in metrics.keys()
109
108
110
109
# model inference
111
110
for prediction in runner.predict_loader(loader = loaders[" valid" ]):
@@ -115,13 +114,13 @@ features_batch = next(iter(loaders["valid"]))[0]
115
114
# model stochastic weight averaging
116
115
model.load_state_dict(utils.get_averaged_weights_by_path_mask(logdir = " ./logs" , path_mask = " *.pth" ))
117
116
# model tracing
118
- utils.trace_model(model = runner.model, batch = features_batch)
117
+ utils.trace_model(model = runner.model.cpu() , batch = features_batch)
119
118
# model quantization
120
119
utils.quantize_model(model = runner.model)
121
120
# model pruning
122
121
utils.prune_model(model = runner.model, pruning_fn = " l1_unstructured" , amount = 0.8 )
123
122
# onnx export
124
- utils.onnx_export(model = runner.model, batch = features_batch, file = " ./logs/mnist.onnx" , verbose = True )
123
+ utils.onnx_export(model = runner.model.cpu() , batch = features_batch, file = " ./logs/mnist.onnx" , verbose = True )
125
124
```
126
125
127
126
### Step-by-step Guide
@@ -248,14 +247,13 @@ from torch import nn, optim
248
247
from torch.nn import functional as F
249
248
from torch.utils.data import DataLoader
250
249
from catalyst import dl, metrics
251
- from catalyst.data import ToTensor
252
- from catalyst.contrib.datasets import MNIST
250
+ from catalyst.contrib import ImageToTensor, MNIST
253
251
254
252
model = nn.Sequential(nn.Flatten(), nn.Linear(28 * 28 , 10 ))
255
253
optimizer = optim.Adam(model.parameters(), lr = 0.02 )
256
254
257
- train_data = MNIST(os.getcwd(), train = True , download = True , transform = ToTensor ())
258
- valid_data = MNIST(os.getcwd(), train = False , download = True , transform = ToTensor ())
255
+ train_data = MNIST(os.getcwd(), train = True , download = True , transform = ImageToTensor ())
256
+ valid_data = MNIST(os.getcwd(), train = False , download = True , transform = ImageToTensor ())
259
257
loaders = {
260
258
" train" : DataLoader(train_data, batch_size = 32 ),
261
259
" valid" : DataLoader(valid_data, batch_size = 32 ),
@@ -325,7 +323,7 @@ for logits in runner.predict_loader(loader=loaders["valid"]):
325
323
<p >
326
324
327
325
``` python
328
- import torch
326
+ import torchx
329
327
from torch.utils.data import DataLoader, TensorDataset
330
328
from catalyst import dl
331
329
@@ -651,15 +649,14 @@ import os
651
649
from torch import nn, optim
652
650
from torch.utils.data import DataLoader
653
651
from catalyst import dl
654
- from catalyst.data import ToTensor
655
- from catalyst.contrib.datasets import MNIST
652
+ from catalyst.contrib import ImageToTensor, MNIST
656
653
657
654
model = nn.Sequential(nn.Flatten(), nn.Linear(28 * 28 , 10 ))
658
655
criterion = nn.CrossEntropyLoss()
659
656
optimizer = optim.Adam(model.parameters(), lr = 0.02 )
660
657
661
- train_data = MNIST(os.getcwd(), train = True , download = True , transform = ToTensor ())
662
- valid_data = MNIST(os.getcwd(), train = False , download = True , transform = ToTensor ())
658
+ train_data = MNIST(os.getcwd(), train = True , download = True , transform = ImageToTensor ())
659
+ valid_data = MNIST(os.getcwd(), train = False , download = True , transform = ImageToTensor ())
663
660
loaders = {
664
661
" train" : DataLoader(train_data, batch_size = 32 ),
665
662
" valid" : DataLoader(valid_data, batch_size = 32 ),
@@ -706,9 +703,7 @@ import torch
706
703
from torch import nn
707
704
from torch.utils.data import DataLoader
708
705
from catalyst import dl
709
- from catalyst.data import ToTensor
710
- from catalyst.contrib.datasets import MNIST
711
- from catalyst.contrib.nn import IoULoss
706
+ from catalyst.contrib import ImageToTensor, IoULoss, MNIST
712
707
713
708
714
709
model = nn.Sequential(
@@ -718,8 +713,8 @@ model = nn.Sequential(
718
713
criterion = IoULoss()
719
714
optimizer = torch.optim.Adam(model.parameters(), lr = 0.02 )
720
715
721
- train_data = MNIST(os.getcwd(), train = True , download = True , transform = ToTensor ())
722
- valid_data = MNIST(os.getcwd(), train = False , download = True , transform = ToTensor ())
716
+ train_data = MNIST(os.getcwd(), train = True , download = True , transform = ImageToTensor ())
717
+ valid_data = MNIST(os.getcwd(), train = False , download = True , transform = ImageToTensor ())
723
718
loaders = {
724
719
" train" : DataLoader(train_data, batch_size = 32 ),
725
720
" valid" : DataLoader(valid_data, batch_size = 32 ),
@@ -769,17 +764,16 @@ from torch import nn, optim
769
764
from torch.nn import functional as F
770
765
from torch.utils.data import DataLoader
771
766
from catalyst import dl
772
- from catalyst.data import ToTensor
773
- from catalyst.contrib.datasets import MNIST
767
+ from catalyst.contrib import ImageToTensor, MNIST
774
768
775
769
# [!] teacher model should be already pretrained
776
770
teacher = nn.Sequential(nn.Flatten(), nn.Linear(28 * 28 , 10 ))
777
771
student = nn.Sequential(nn.Flatten(), nn.Linear(28 * 28 , 10 ))
778
772
criterion = {" cls" : nn.CrossEntropyLoss(), " kl" : nn.KLDivLoss(reduction = " batchmean" )}
779
773
optimizer = optim.Adam(student.parameters(), lr = 0.02 )
780
774
781
- train_data = MNIST(os.getcwd(), train = True , download = True , transform = ToTensor ())
782
- valid_data = MNIST(os.getcwd(), train = False , download = True , transform = ToTensor ())
775
+ train_data = MNIST(os.getcwd(), train = True , download = True , transform = ImageToTensor ())
776
+ valid_data = MNIST(os.getcwd(), train = False , download = True , transform = ImageToTensor ())
783
777
loaders = {
784
778
" train" : DataLoader(train_data, batch_size = 32 ),
785
779
" valid" : DataLoader(valid_data, batch_size = 32 ),
@@ -801,23 +795,13 @@ class DistilRunner(dl.Runner):
801
795
802
796
runner = DistilRunner()
803
797
callbacks = [
804
- dl.AccuracyCallback(
805
- input_key = " t_logits" , target_key = " targets" , num_classes = 2 , prefix = " teacher_"
806
- ),
807
- dl.AccuracyCallback(
808
- input_key = " s_logits" , target_key = " targets" , num_classes = 2 , prefix = " student_"
809
- ),
810
- dl.CriterionCallback(
811
- input_key = " s_logits" , target_key = " targets" , metric_key = " cls_loss" , criterion_key = " cls"
812
- ),
813
- dl.CriterionCallback(
814
- input_key = " s_logprobs" , target_key = " t_probs" , metric_key = " kl_div_loss" , criterion_key = " kl"
815
- ),
798
+ dl.AccuracyCallback(input_key = " t_logits" , target_key = " targets" , num_classes = 2 , prefix = " teacher_" ),
799
+ dl.AccuracyCallback(input_key = " s_logits" , target_key = " targets" , num_classes = 2 , prefix = " student_" ),
800
+ dl.CriterionCallback(input_key = " s_logits" , target_key = " targets" , metric_key = " cls_loss" , criterion_key = " cls" ),
801
+ dl.CriterionCallback(input_key = " s_logprobs" , target_key = " t_probs" , metric_key = " kl_div_loss" , criterion_key = " kl" ),
816
802
dl.MetricAggregationCallback(metric_key = " loss" , metrics = [" kl_div_loss" , " cls_loss" ], mode = " mean" ),
817
803
dl.OptimizerCallback(metric_key = " loss" , model_key = " student" ),
818
- dl.CheckpointCallback(
819
- logdir = " ./logs" , loader_key = " valid" , metric_key = " loss" , minimize = True , save_n_best = 3
820
- ),
804
+ dl.CheckpointCallback(logdir = " ./logs" , loader_key = " valid" , metric_key = " loss" , minimize = True , save_n_best = 3 ),
821
805
]
822
806
# model training
823
807
runner.train(
@@ -843,16 +827,19 @@ runner.train(
843
827
import os
844
828
from torch.optim import Adam
845
829
from torch.utils.data import DataLoader
846
- from catalyst import data, dl
847
- from catalyst.contrib import datasets, models, nn
848
- from catalyst.data.transforms import Compose, Normalize, ToTensor
830
+ from catalyst import dl
831
+ from catalyst.data import BatchBalanceClassSampler
832
+ from catalyst.contrib import data, datasets, models, nn
849
833
850
834
851
835
# 1. train and valid loaders
852
- transforms = Compose([ToTensor(), Normalize((0.1307 ,), (0.3081 ,))])
836
+ transforms = data.Compose([
837
+ data.ImageToTensor(),
838
+ data.NormalizeImage((0.1307 ,), (0.3081 ,))
839
+ ])
853
840
854
841
train_dataset = datasets.MnistMLDataset(root = os.getcwd(), download = True , transform = transforms)
855
- sampler = data. BatchBalanceClassSampler(
842
+ sampler = BatchBalanceClassSampler(
856
843
labels = train_dataset.get_labels(), num_classes = 5 , num_samples = 10 , num_batches = 10
857
844
)
858
845
train_loader = DataLoader(dataset = train_dataset, batch_sampler = sampler)
@@ -929,9 +916,7 @@ import torch
929
916
from torch import nn
930
917
from torch.utils.data import DataLoader
931
918
from catalyst import dl
932
- from catalyst.contrib.datasets import MNIST
933
- from catalyst.contrib.nn.modules import Flatten, GlobalMaxPool2d, Lambda
934
- from catalyst.data import ToTensor
919
+ from catalyst.contrib import Flatten, GlobalMaxPool2d, Lambda, MNIST , ImageToTensor
935
920
936
921
latent_dim = 128
937
922
generator = nn.Sequential(
@@ -962,7 +947,7 @@ optimizer = {
962
947
" generator" : torch.optim.Adam(generator.parameters(), lr = 0.0003 , betas = (0.5 , 0.999 )),
963
948
" discriminator" : torch.optim.Adam(discriminator.parameters(), lr = 0.0003 , betas = (0.5 , 0.999 )),
964
949
}
965
- train_data = MNIST(os.getcwd(), train = False , download = True , transform = ToTensor ())
950
+ train_data = MNIST(os.getcwd(), train = False , download = True , transform = ImageToTensor ())
966
951
loaders = {" train" : DataLoader(train_data, batch_size = 32 )}
967
952
968
953
class CustomRunner (dl .Runner ):
@@ -1070,8 +1055,7 @@ from torch import nn, optim
1070
1055
from torch.nn import functional as F
1071
1056
from torch.utils.data import DataLoader
1072
1057
from catalyst import dl, metrics
1073
- from catalyst.contrib.datasets import MNIST
1074
- from catalyst.data import ToTensor
1058
+ from catalyst.contrib import ImageToTensor, MNIST
1075
1059
1076
1060
LOG_SCALE_MAX = 2
1077
1061
LOG_SCALE_MIN = - 10
@@ -1124,8 +1108,8 @@ class CustomRunner(dl.IRunner):
1124
1108
return 3
1125
1109
1126
1110
def get_loaders (self , stage : str ):
1127
- train_data = MNIST(os.getcwd(), train = True , download = True , transform = ToTensor ())
1128
- valid_data = MNIST(os.getcwd(), train = False , download = True , transform = ToTensor ())
1111
+ train_data = MNIST(os.getcwd(), train = True , download = True , transform = ImageToTensor ())
1112
+ valid_data = MNIST(os.getcwd(), train = False , download = True , transform = ImageToTensor ())
1129
1113
loaders = {
1130
1114
" train" : DataLoader(train_data, batch_size = 32 ),
1131
1115
" valid" : DataLoader(valid_data, batch_size = 32 ),
@@ -1197,8 +1181,7 @@ import os
1197
1181
from torch import nn, optim
1198
1182
from torch.utils.data import DataLoader
1199
1183
from catalyst import dl, utils
1200
- from catalyst.contrib.datasets import MNIST
1201
- from catalyst.data import ToTensor
1184
+ from catalyst.contrib import ImageToTensor, MNIST
1202
1185
1203
1186
1204
1187
class CustomRunner (dl .IRunner ):
@@ -1225,8 +1208,8 @@ class CustomRunner(dl.IRunner):
1225
1208
return 3
1226
1209
1227
1210
def get_loaders (self , stage : str ):
1228
- train_data = MNIST(os.getcwd(), train = True , download = True , transform = ToTensor ())
1229
- valid_data = MNIST(os.getcwd(), train = False , download = True , transform = ToTensor ())
1211
+ train_data = MNIST(os.getcwd(), train = True , download = True , transform = ImageToTensor ())
1212
+ valid_data = MNIST(os.getcwd(), train = False , download = True , transform = ImageToTensor ())
1230
1213
loaders = {
1231
1214
" train" : DataLoader(train_data, batch_size = 32 ),
1232
1215
" valid" : DataLoader(valid_data, batch_size = 32 ),
@@ -1305,8 +1288,7 @@ import os
1305
1288
from torch import nn, optim
1306
1289
from torch.utils.data import DataLoader
1307
1290
from catalyst import dl, utils
1308
- from catalyst.contrib.datasets import MNIST
1309
- from catalyst.data import ToTensor
1291
+ from catalyst.contrib import ImageToTensor, MNIST
1310
1292
1311
1293
1312
1294
class CustomRunner (dl .IRunner ):
@@ -1332,8 +1314,8 @@ class CustomRunner(dl.IRunner):
1332
1314
return 3
1333
1315
1334
1316
def get_loaders (self , stage : str ):
1335
- train_data = MNIST(os.getcwd(), train = True , download = True , transform = ToTensor ())
1336
- valid_data = MNIST(os.getcwd(), train = False , download = True , transform = ToTensor ())
1317
+ train_data = MNIST(os.getcwd(), train = True , download = True , transform = ImageToTensor ())
1318
+ valid_data = MNIST(os.getcwd(), train = False , download = True , transform = ImageToTensor ())
1337
1319
loaders = {
1338
1320
" train" : DataLoader(train_data, batch_size = 32 ),
1339
1321
" valid" : DataLoader(valid_data, batch_size = 32 ),
@@ -1420,16 +1402,15 @@ import torch
1420
1402
from torch import nn
1421
1403
from torch.utils.data import DataLoader
1422
1404
from catalyst import dl
1423
- from catalyst.data import ToTensor
1424
- from catalyst.contrib.datasets import MNIST
1405
+ from catalyst.contrib import ImageToTensor, MNIST
1425
1406
1426
1407
1427
1408
def objective (trial ):
1428
1409
lr = trial.suggest_loguniform(" lr" , 1e-3 , 1e-1 )
1429
1410
num_hidden = int (trial.suggest_loguniform(" num_hidden" , 32 , 128 ))
1430
1411
1431
- train_data = MNIST(os.getcwd(), train = True , download = True , transform = ToTensor ())
1432
- valid_data = MNIST(os.getcwd(), train = False , download = True , transform = ToTensor ())
1412
+ train_data = MNIST(os.getcwd(), train = True , download = True , transform = ImageToTensor ())
1413
+ valid_data = MNIST(os.getcwd(), train = False , download = True , transform = ImageToTensor ())
1433
1414
loaders = {
1434
1415
" train" : DataLoader(train_data, batch_size = 32 ),
1435
1416
" valid" : DataLoader(valid_data, batch_size = 32 ),
0 commit comments