Browse Source

refactored super() calls

Dimitri Korsch 4 years ago
parent
commit
2d102c018e

+ 1 - 1
cvmodelz/classifiers/base.py

@@ -15,7 +15,7 @@ class Classifier(chainer.Chain):
 		loss_func: Callable = F.softmax_cross_entropy,
 		only_head: bool = False,
 		):
-		super(Classifier, self).__init__()
+		super().__init__()
 		self.layer_name = layer_name or model.clf_layer_name
 		self.loss_func = loss_func
 

+ 5 - 5
cvmodelz/classifiers/separate_model_classifier.py

@@ -19,18 +19,18 @@ class SeparateModelClassifier(Classifier):
 
 	@abc.abstractmethod
 	def forward(self, *args, **kwargs) -> chainer.Variable:
-		super(SeparateModelClassifier, self).forward(*args, **kwargs)
+		super().forward(*args, **kwargs)
 
 	def setup(self, model: BaseModel) -> None:
-		super(SeparateModelClassifier, self).setup(model)
+		super().setup(model)
 
 		self.separate_model = self.model.copy(mode="copy")
 
 	def load_for_finetune(self, *args, **kwargs) -> None:
-		super(SeparateModelClassifier, self).load_for_finetune(*args, **kwargs)
+		super().load_for_finetune(*args, **kwargs)
 
 	def load_for_inference(self, *args, **kwargs) -> None:
-		super(SeparateModelClassifier, self).load_for_inference(*args, **kwargs)
+		super().load_for_inference(*args, **kwargs)
 
 
 
@@ -60,7 +60,7 @@ class SeparateModelClassifier(Classifier):
 		return inner_loader
 
 	def enable_only_head(self) -> None:
-		super(SeparateModelClassifier, self).enable_only_head()
+		super().enable_only_head()
 		self.separate_model.disable_update()
 		self.separate_model.fc.enable_update()
 

+ 3 - 3
cvmodelz/models/base.py

@@ -17,7 +17,7 @@ class BaseModel(abc.ABC, chainer.Chain):
 
 	def __init__(self, pooling: Callable = PoolingType.G_AVG.value(),
 		input_size=None, *args, **kwargs):
-		super(BaseModel, self).__init__(*args, **kwargs)
+		super().__init__(*args, **kwargs)
 		self.init_model_info()
 
 		with self.init_scope():
@@ -32,11 +32,11 @@ class BaseModel(abc.ABC, chainer.Chain):
 
 	@abc.abstractmethod
 	def forward(self, *args, **kwargs):
-		return super(BaseModel, self).forward(*args, **kwargs)
+		return super().forward(*args, **kwargs)
 
 	@abc.abstractproperty
 	def functions(self) -> OrderedDict:
-		return super(BaseModel, self).functions
+		return super().functions
 
 	@abc.abstractproperty
 	def model_instance(self) -> chainer.Chain:

+ 2 - 2
cvmodelz/models/pretrained/base.py

@@ -17,7 +17,7 @@ class PretrainedModelMixin(BaseModel):
 	"""
 
 	def __init__(self, *args, n_classes: int = 1000, pretrained_model: str = None, **kwargs):
-		super(PretrainedModelMixin, self).__init__(*args, pretrained_model=pretrained_model, **kwargs)
+		super().__init__(*args, pretrained_model=pretrained_model, **kwargs)
 
 		with self.init_scope():
 			self.init_extra_layers(n_classes)
@@ -28,7 +28,7 @@ class PretrainedModelMixin(BaseModel):
 		assert hasattr(self, "meta"), "Did you forgot to initialize the meta attribute?"
 
 		layer_name = layer_name or self.meta.classifier_layers[-1]
-		caller = super(PretrainedModelMixin, self).forward
+		caller = super().forward
 		activations = caller(X, layers=[layer_name])
 
 		if isinstance(activations, dict):

+ 7 - 7
cvmodelz/models/pretrained/inception/blocks.py

@@ -8,7 +8,7 @@ from chainer_addons.links.pooling import PoolingType
 
 class InceptionHead(chainer.Chain):
 	def __init__(self):
-		super(InceptionHead, self).__init__()
+		super().__init__()
 		with self.init_scope():
 			# input 3 x 299 x 299
 			self.conv1 = Conv2D_BN(3,   32, ksize=3, stride=2)
@@ -39,7 +39,7 @@ class InceptionHead(chainer.Chain):
 
 class Inception1(chainer.Chain):
 	def __init__(self, insize, sizes, outputs, **pool_args):
-		super(Inception1, self).__init__()
+		super().__init__()
 
 		out1x1, out5x5, out3x3, out_pool = outputs
 		s5x5, s3x3_1, s3x3_2 = sizes
@@ -67,7 +67,7 @@ class Inception1(chainer.Chain):
 
 class Inception2(chainer.Chain):
 	def __init__(self, insize, sizes, outputs, **pool_args):
-		super(Inception2, self).__init__()
+		super().__init__()
 
 		out1, out2 = outputs
 		size1, size2 = sizes
@@ -88,7 +88,7 @@ class Inception2(chainer.Chain):
 
 class Inception3(chainer.Chain):
 	def __init__(self, insize, sizes, outputs, **pool_args):
-		super(Inception3, self).__init__()
+		super().__init__()
 
 		out1x1, out7x7, out7x7x2, out_pool = outputs
 		s7x7_1, s7x7_2, s7x7x2_1, s7x7x2_2, s7x7x2_3, s7x7x2_4 = sizes
@@ -120,7 +120,7 @@ class Inception3(chainer.Chain):
 
 class Inception4(chainer.Chain):
 	def __init__(self, insize, sizes, outputs, **pool_args):
-		super(Inception4, self).__init__()
+		super().__init__()
 
 		out3x3, out7x7 = outputs
 		s3x3, s7x7_1, s7x7_2, s7x7_3 = sizes
@@ -146,7 +146,7 @@ class Inception4(chainer.Chain):
 
 class Inception5(chainer.Chain):
 	def __init__(self, insize, sizes, outputs, **pool_args):
-		super(Inception5, self).__init__()
+		super().__init__()
 
 		out1x1, out3x3, out3x3x2, out_pool = outputs
 		s3x3, s3x3x2_1, s3x3x2_2 = sizes
@@ -186,7 +186,7 @@ class Inception5(chainer.Chain):
 
 class AuxilaryClassifier(chainer.Chain):
 	def __init__(self, n_classes):
-		super(AuxilaryClassifier, self).__init__()
+		super().__init__()
 		with self.init_scope():
 			self.conv1 = Conv2D_BN(768, 128, ksize=1, pad=1)
 			self.conv2 = Conv2D_BN(128, 768, ksize=7)

+ 4 - 4
cvmodelz/models/pretrained/inception/inception_v3.py

@@ -43,7 +43,7 @@ class InceptionV3Layers(chainer.Chain):
 	def __init__(self, pretrained_model=None, aux_logits=False, *args, **kwargs):
 		self.aux_logits = aux_logits
 		pooling = PoolingType.G_AVG.value()
-		super(InceptionV3Layers, self).__init__(*args, pooling=pooling, **kwargs)
+		super().__init__(*args, pooling=pooling, **kwargs)
 
 
 class InceptionV3(PretrainedModelMixin, InceptionV3Layers):
@@ -63,7 +63,7 @@ class InceptionV3(PretrainedModelMixin, InceptionV3Layers):
 
 	@property
 	def functions(self):
-		super(InceptionV3, self).functions
+		super().functions
 
 
 
@@ -116,7 +116,7 @@ class InceptionV3(PretrainedModelMixin, InceptionV3Layers):
 		elif isinstance(weights, str) and weights.endswith(".ckpt.npz"):
 			return self._load_from_ckpt_weights(weights)
 
-		return super(InceptionV3, self).load(weights, *args, **kwargs)
+		return super().load(weights, *args, **kwargs)
 
 	def init_extra_layers(self, n_classes) -> None:
 		# input 3 x 299 x 299
@@ -166,7 +166,7 @@ class InceptionV3(PretrainedModelMixin, InceptionV3Layers):
 		# output 2048 x 1 x 1
 
 		# the final fc layer is initilized by PretrainedModelMixin
-		super(InceptionV3, self).init_extra_layers(n_classes)
+		super().init_extra_layers(n_classes)
 
 	def loss(self, pred, gt, loss_func=F.softmax_cross_entropy, alpha=0.4):
 		if isinstance(pred, tuple):

+ 3 - 3
cvmodelz/models/pretrained/resnet.py

@@ -28,7 +28,7 @@ class BaseResNet(PretrainedModelMixin):
 
 	@property
 	def functions(self):
-		return super(BaseResNet, self).functions
+		return super().functions
 
 """
 We need this to "extract" pretrained_model argument,
@@ -38,7 +38,7 @@ chainer.Chain class, where it raises an error
 class ResNet35Layers(chainer.Chain):
 
 	def __init__(self, *args, pretrained_model=None, **kwargs):
-		super(ResNet35Layers, self).__init__(*args, **kwargs)
+		super().__init__(*args, **kwargs)
 
 
 class ResNet35(BaseResNet, ResNet35Layers):
@@ -53,7 +53,7 @@ class ResNet35(BaseResNet, ResNet35Layers):
 		self.res5 = BuildingBlock(3, 1024, 512, 2048, 2, **kwargs)
 
 		# the final fc layer is initilized by PretrainedModelMixin
-		super(ResNet35, self).init_extra_layers(*args, **kwargs)
+		super().init_extra_layers(*args, **kwargs)
 
 	@property
 	def functions(self):

+ 4 - 4
cvmodelz/models/pretrained/vgg.py

@@ -22,24 +22,24 @@ def _vgg_meta(final_conv_layer):
 class VGG19(PretrainedModelMixin, L.VGG19Layers):
 
 	def __init__(self, *args, **kwargs):
-		super(VGG19, self).__init__(*args, pooling=_max_pooling_2d, **kwargs)
+		super().__init__(*args, pooling=_max_pooling_2d, **kwargs)
 
 	def init_model_info(self):
 		self.meta = _vgg_meta("conv5_3")
 
 	@property
 	def functions(self):
-		return super(VGG19, self).functions
+		return super().functions
 
 class VGG16(PretrainedModelMixin, L.VGG16Layers):
 
 	def __init__(self, *args, **kwargs):
-		super(VGG16, self).__init__(*args, pooling=_max_pooling_2d, **kwargs)
+		super().__init__(*args, pooling=_max_pooling_2d, **kwargs)
 
 	def init_model_info(self):
 		self.meta = _vgg_meta("conv5_4")
 
 	@property
 	def functions(self):
-		return super(VGG16, self).functions
+		return super().functions
 

+ 2 - 2
cvmodelz/models/wrapper.py

@@ -16,7 +16,7 @@ class ModelWrapper(BaseModel):
 	"""
 
 	def __init__(self, model: chainer.Chain, *args, **kwargs):
-		super(ModelWrapper, self).__init__(*args, **kwargs)
+		super().__init__(*args, **kwargs)
 
 		name = model.__class__.__name__
 		self.__class__.__name__ = name
@@ -57,7 +57,7 @@ class ModelWrapper(BaseModel):
 		paths = [path, f"{path}wrapped/"]
 		for _path in paths:
 			try:
-				return super(ModelWrapper, self).load(*args, path=_path, **kwargs)
+				return super().load(*args, path=_path, **kwargs)
 			except KeyError as e:
 				pass