@@ -77,7 +77,6 @@ def _accuracy_function(self, accuracyname, input, target):
77
77
return calc_accuracy (input , target )
78
78
else :
79
79
input = torch .argmax (input , 1 )
80
- target = torch .argmax (target , 1 )
81
80
return calc_accuracy (input , target )
82
81
83
82
def trainprocess (self , trainimage , trainmask , validationimage , validationmask , model_dir , epochs = 50 , lr = 1e-3 ):
@@ -88,7 +87,8 @@ def trainprocess(self, trainimage, trainmask, validationimage, validationmask, m
88
87
print (self .model )
89
88
# 1、initialize loss function and optimizer
90
89
lossFunc = self ._loss_function (self .loss_name )
91
- opt = optim .Adam (self .model .parameters (), lr = lr )
90
+ opt = optim .Adam (self .model .parameters (), lr = lr , weight_decay = 1e-4 )
91
+ lr_scheduler = optim .lr_scheduler .ReduceLROnPlateau (opt , 'min' , patience = 2 , verbose = True )
92
92
# 2、load data train and validation dataset
93
93
train_loader = self ._dataloder (trainimage , trainmask , True )
94
94
val_loader = self ._dataloder (validationimage , validationmask )
@@ -154,6 +154,7 @@ def trainprocess(self, trainimage, trainmask, validationimage, validationmask, m
154
154
avgValidationLoss = torch .mean (torch .stack (totalValidationLoss ))
155
155
avgTrainAccu = torch .mean (torch .stack (totalTrainAccu ))
156
156
avgValidationAccu = torch .mean (torch .stack (totalValiadtionAccu ))
157
+ lr_scheduler .step (avgValidationLoss )
157
158
# 4.6、update our training history
158
159
H ["train_loss" ].append (avgTrainLoss .cpu ().detach ().numpy ())
159
160
H ["valdation_loss" ].append (avgValidationLoss .cpu ().detach ().numpy ())
@@ -286,7 +287,6 @@ def _accuracy_function(self, accuracyname, input, target):
286
287
return calc_accuracy (input , target )
287
288
else :
288
289
input = torch .argmax (input , 1 )
289
- target = torch .argmax (target , 1 )
290
290
return calc_accuracy (input , target )
291
291
292
292
def trainprocess (self , trainimage , trainmask , validationimage , validationmask , model_dir , epochs = 50 , lr = 1e-3 ):
@@ -297,7 +297,8 @@ def trainprocess(self, trainimage, trainmask, validationimage, validationmask, m
297
297
print (self .model )
298
298
# 1、initialize loss function and optimizer
299
299
lossFunc = self ._loss_function (self .loss_name )
300
- opt = optim .Adam (self .model .parameters (), lr = lr )
300
+ opt = optim .Adam (self .model .parameters (), lr = lr , weight_decay = 1e-4 )
301
+ lr_scheduler = optim .lr_scheduler .ReduceLROnPlateau (opt , 'min' , patience = 2 , verbose = True )
301
302
# 2、load data train and validation dataset
302
303
train_loader = self ._dataloder (trainimage , trainmask , True )
303
304
val_loader = self ._dataloder (validationimage , validationmask )
@@ -323,8 +324,6 @@ def trainprocess(self, trainimage, trainmask, validationimage, validationmask, m
323
324
# y should tensor with shape (N,C,W,H),
324
325
# if have mutil label y should one-hot,if only one label,the C is one
325
326
y = batch ['label' ]
326
- # one-hot encoding
327
- y = F .one_hot (y , self .numclass )
328
327
# send the input to the device
329
328
x , y = x .to (self .device ), y .to (self .device )
330
329
# perform a forward pass and calculate the training loss and accu
@@ -351,8 +350,6 @@ def trainprocess(self, trainimage, trainmask, validationimage, validationmask, m
351
350
x = batch ['image' ]
352
351
# y should tensor with shape (N,)
353
352
y = batch ['label' ]
354
- # one-hot encoding
355
- y = F .one_hot (y , self .numclass )
356
353
# send the input to the device
357
354
(x , y ) = (x .to (self .device ), y .to (self .device ))
358
355
# make the predictions and calculate the validation loss
@@ -368,6 +365,7 @@ def trainprocess(self, trainimage, trainmask, validationimage, validationmask, m
368
365
avgValidationLoss = torch .mean (torch .stack (totalValidationLoss ))
369
366
avgTrainAccu = torch .mean (torch .stack (totalTrainAccu ))
370
367
avgValidationAccu = torch .mean (torch .stack (totalValiadtionAccu ))
368
+ lr_scheduler .step (avgValidationLoss )
371
369
# 4.6、update our training history
372
370
H ["train_loss" ].append (avgTrainLoss .cpu ().detach ().numpy ())
373
371
H ["valdation_loss" ].append (avgValidationLoss .cpu ().detach ().numpy ())
@@ -502,7 +500,6 @@ def _accuracy_function(self, accuracyname, input, target):
502
500
return calc_accuracy (input , target )
503
501
else :
504
502
input = torch .argmax (input , 1 )
505
- target = torch .argmax (target , 1 )
506
503
return calc_accuracy (input , target )
507
504
508
505
def trainprocess (self , trainimage , trainmask , validationimage , validationmask , model_dir , epochs = 50 , lr = 1e-3 ):
@@ -513,7 +510,8 @@ def trainprocess(self, trainimage, trainmask, validationimage, validationmask, m
513
510
print (self .model )
514
511
# 1、initialize loss function and optimizer
515
512
lossFunc = self ._loss_function (self .loss_name )
516
- opt = optim .Adam (self .model .parameters (), lr = lr )
513
+ opt = optim .Adam (self .model .parameters (), lr = lr , weight_decay = 1e-4 )
514
+ lr_scheduler = optim .lr_scheduler .ReduceLROnPlateau (opt , 'min' , patience = 2 , verbose = True )
517
515
# 2、load data train and validation dataset
518
516
train_loader = self ._dataloder (trainimage , trainmask , True )
519
517
val_loader = self ._dataloder (validationimage , validationmask )
@@ -579,6 +577,7 @@ def trainprocess(self, trainimage, trainmask, validationimage, validationmask, m
579
577
avgValidationLoss = torch .mean (torch .stack (totalValidationLoss ))
580
578
avgTrainAccu = torch .mean (torch .stack (totalTrainAccu ))
581
579
avgValidationAccu = torch .mean (torch .stack (totalValiadtionAccu ))
580
+ lr_scheduler .step (avgValidationLoss )
582
581
# 4.6、update our training history
583
582
H ["train_loss" ].append (avgTrainLoss .cpu ().detach ().numpy ())
584
583
H ["valdation_loss" ].append (avgValidationLoss .cpu ().detach ().numpy ())
@@ -711,7 +710,6 @@ def _accuracy_function(self, accuracyname, input, target):
711
710
return calc_accuracy (input , target )
712
711
else :
713
712
input = torch .argmax (input , 1 )
714
- target = torch .argmax (target , 1 )
715
713
return calc_accuracy (input , target )
716
714
717
715
def trainprocess (self , trainimage , trainmask , validationimage , validationmask , model_dir , epochs = 50 , lr = 1e-3 ):
@@ -722,7 +720,8 @@ def trainprocess(self, trainimage, trainmask, validationimage, validationmask, m
722
720
print (self .model )
723
721
# 1、initialize loss function and optimizer
724
722
lossFunc = self ._loss_function (self .loss_name )
725
- opt = optim .Adam (self .model .parameters (), lr = lr )
723
+ opt = optim .Adam (self .model .parameters (), lr = lr , weight_decay = 1e-4 )
724
+ lr_scheduler = optim .lr_scheduler .ReduceLROnPlateau (opt , 'min' , patience = 2 , verbose = True )
726
725
# 2、load data train and validation dataset
727
726
train_loader = self ._dataloder (trainimage , trainmask , True )
728
727
val_loader = self ._dataloder (validationimage , validationmask )
@@ -748,7 +747,6 @@ def trainprocess(self, trainimage, trainmask, validationimage, validationmask, m
748
747
# y should tensor with shape (N,C,W,H),
749
748
# if have mutil label y should one-hot,if only one label,the C is one
750
749
y = batch ['label' ]
751
- y = F .one_hot (y , self .numclass )
752
750
# send the input to the device
753
751
x , y = x .to (self .device ), y .to (self .device )
754
752
# perform a forward pass and calculate the training loss and accu
@@ -775,7 +773,6 @@ def trainprocess(self, trainimage, trainmask, validationimage, validationmask, m
775
773
x = batch ['image' ]
776
774
# y should tensor with shape (N,C,W,H)
777
775
y = batch ['label' ]
778
- y = F .one_hot (y , self .numclass )
779
776
# send the input to the device
780
777
(x , y ) = (x .to (self .device ), y .to (self .device ))
781
778
# make the predictions and calculate the validation loss
@@ -790,6 +787,7 @@ def trainprocess(self, trainimage, trainmask, validationimage, validationmask, m
790
787
avgValidationLoss = torch .mean (torch .stack (totalValidationLoss ))
791
788
avgTrainAccu = torch .mean (torch .stack (totalTrainAccu ))
792
789
avgValidationAccu = torch .mean (torch .stack (totalValiadtionAccu ))
790
+ lr_scheduler .step (avgValidationLoss )
793
791
# 4.6、update our training history
794
792
H ["train_loss" ].append (avgTrainLoss .cpu ().detach ().numpy ())
795
793
H ["valdation_loss" ].append (avgValidationLoss .cpu ().detach ().numpy ())
0 commit comments