def initModel (self): h, w, d = self.img_shape input_1 = Input(shape=(h, w, d), name='ip_scale1')
##def Input(shape=None, batch_shape=None, name=None, dtype=K.floatx(), sparse=False, tensor=None): vgg_layer_output = self.VGG16(input_1) shared_model = Model(inputs=input_1, outputs=vgg_layer_output, name='shared_model')
##class Model(Container): #Model类继承于Container没有__init__函数,
##Container的__init__函数为def __init__(self, inputs, outputs, name=None): shared_model.load_weights(self.vgg_weights_path, by_name=True)
input_2 = Input(shape=(int(h/2), int(w/2), d), name='ip_scale2') x2 = shared_model(input_2)#下面解释了为什么采用该形式能将input_2输入计算图模型,并返回计算结果。
##Model继承于Container,Container继承于Layer,但Mode和Container均没有__call__,而Layer类的__call__函数如下
##def __call__(self, inputs, **kwargs):
##return output
#############################################
python对象通过提供__call__(slef, [,*args [,**kwargs]])方法可以模拟函数的行为,如果一个对象x提供了该方法,就可以像
函数一样使用它,也就是说x(arg1, arg2...) 等同于调用x.__call__(self, arg1, arg2)。模拟函数的对象可以用于创建
仿函数(functor) 或代理(proxy).
class DistanceForm(object):
def __init__(self, origin):
self.origin = origin
print "origin :"+str(origin)
def __call__(self, x):
print "x :"+str(x)
p = DistanceForm(100)
p(2000)
输出:
origin :100
x :2000