class C(B): def method(self, arg): super(C, self).method(arg)子类C重写了父类B中同名方法method,在重写的实现中通过super实例化的代理对象调用父类的同名方法。
举例如下
class BaseSuperResolutionModel(object): def __init__(self, model_name, scale_factor): """ Base model to PRovide a standard interface of adding Super Resolution models """ self.model = None # type: Model self.model_name = model_name self.scale_factor = scale_factor self.weight_path = None self.type_scale_type = "norm" # Default = "norm" = 1. / 255 self.type_requires_divisible_shape = False self.type_true_upscaling = False self.evaluation_func = None self.uses_learning_phase = False def create_model(self, height=32, width=32, channels=3, load_weights=False, batch_size=128) -> Model: """ Subclass dependent implementation. """ if self.type_requires_divisible_shape: assert height * img_utils._image_scale_multiplier % 4 == 0, "Height of the image must be divisible by 4" assert width * img_utils._image_scale_multiplier % 4 == 0, "Width of the image must be divisible by 4" if K.image_dim_ordering() == "th": shape = (channels, width * img_utils._image_scale_multiplier, height * img_utils._image_scale_multiplier) else: shape = (width * img_utils._image_scale_multiplier, height * img_utils._image_scale_multiplier, channels) init = Input(shape=shape) return initclass ImageSuperResolutionModel(BaseSuperResolutionModel): def __init__(self, scale_factor): super(ImageSuperResolutionModel, self).__init__("Image SR", scale_factor) self.f1 = 9 self.f2 = 1 self.f3 = 5 self.n1 = 64 self.n2 = 32 self.weight_path = "weights/SR Weights %dX.h5" % (self.scale_factor) def create_model(self, height=32, width=32, channels=3, load_weights=False, batch_size=128): """ Creates a model to be used to scale images of specific height and width. """ init = super(ImageSuperResolutionModel, self).create_model(height, width, channels, load_weights, batch_size) x = Convolution2D(self.n1, self.f1, self.f1, activation='relu', border_mode='same', name='level1')(init) x = Convolution2D(self.n2, self.f2, self.f2, activation='relu', border_mode='same', name='level2')(x) out = Convolution2D(channels, self.f3, self.f3, border_mode='same', name='output')(x) model = Model(init, out) adam = optimizers.Adam(lr=1e-3) model.compile(optimizer=adam, loss='mse', metrics=[PSNRLoss]) if load_weights: model.load_weights(self.weight_path) self.model = modelreturn modelsuper(ImageSuperResolutionModel, self).__init__("Image SR", scale_factor) 继承了基类中的初始化方法
新闻热点
疑难解答