您可以創建將多個圖像解串器組合成一個源的複合閱讀器。首先,您需要創建兩個地圖文件(帶有虛擬標籤)。一個將包含所有輸入圖像,另一個將包含相應的目標圖像。以下代碼是一個最小的實施方式中,假設文件稱爲map1.txt
和map2.txt
import numpy as np
import cntk as C
import cntk.io.transforms as xforms
import sys
def create_reader(map_file1, map_file2):
transforms = [xforms.scale(width=224, height=224, channels=3, interpolations='linear')]
source1 = C.io.ImageDeserializer(map_file1, C.io.StreamDefs(
source_image = C.io.StreamDef(field='image', transforms=transforms)))
source2 = C.io.ImageDeserializer(map_file2, C.io.StreamDefs(
target_image = C.io.StreamDef(field='image', transforms=transforms)))
return C.io.MinibatchSource([source1, source2], max_samples=sys.maxsize, randomize=True)
x = C.input_variable((3,224,224))
y = C.input_variable((3,224,224))
# world's simplest model
model = C.layers.Convolution((3,3),3, pad=True)
z = model(x)
loss = C.squared_error(z, y)
reader = create_reader("map1.txt", "map2.txt")
trainer = C.Trainer(z, loss, C.sgd(z.parameters, C.learning_rate_schedule(.00001, C.UnitType.minibatch)))
minibatch_size = 2
input_map={
x: reader.streams.source_image,
y: reader.streams.target_image
}
for i in range(30):
data=reader.next_minibatch(minibatch_size, input_map=input_map)
print(data)
trainer.train_minibatch(data)
聽起來[ImageDeserializer](https://cntk.ai/pythondocs/cntk.io.html?highlight=imagedeserializer#cntk。 io.ImageDeserializer)用於圖像,但它要求目標變量是標量(如類標籤)。但是,我的目標變量具有圖像形狀。 –