选项 1 - 双方都是同一个模型,只是使用不同的输入
假设您有一个模型可以升级到“谓词”,称为predModel。
创建两个输入张量:
input1 = Input(shape)
input2 = Input(shape)
获取每个输入的输出:
pred1 = predModel(input1)
pred2 = predModel(input2)
平均输出:
output = Average()([pred1,pred2])
创建最终模型:
model = Model([input1,input2], output)
Option2 - 两边是相似的模型,但使用不同的权重
与上述基本相同,但为每一面单独创建图层。
def createCommonPart(inputTensor):
out = ZeroPadding2D(...)(inputTensor)
out = Conv2D(...)(out)
...
out = Flatten()(out)
return Dense(...)(out)
进行两个输入:
input1 = Input(shape)
input2 = Input(shape)
得到两个输出:
pred1 = createCommonPart(input1)
pred2 = createCommonPart(input2)
平均输出:
output = Average()([pred1,pred2])
创建最终模型:
model = Model([input1,input2], output)
生成器
任何产生[xTrain1,xTrain2], y 的东西。
你可以像这样创建一个:
def generator(files1,files2, batch_size):
while True: #must be infinite
for i in range(len(files1)//batch_size)):
bStart = i*batch_size
bEnd = bStart+batch_size
x1 = loadImagesSomehow(files1[bStart:bEnd])
x2 = loadImagesSomehow(files2[bStart:bEnd])
y = loadPredictionsSomeHow(forSamples[bStart:bEnd])
yield [x1,x2], y
您也可以用类似的方式实现keras.utils.Sequence。
class gen(Sequence):
def __init__(self, files1, files2, batchSize):
self.files1 = files1
self.files2 = files2
self.batchSize = batchSize
def __len__(self):
return self.len(files1) // self.batchSize
def __getitem__(self,i):
bStart = i*self.batchSize
bEnd = bStart+self.batchSize
x1 = loadImagesSomehow(files1[bStart:bEnd])
x2 = loadImagesSomehow(files2[bStart:bEnd])
y = loadPredictionsSomeHow(forSamples[bStart:bEnd])
return [x1,x2], y