我正在处理由3个子网组成的多CNN,CNN接受6张图像作为输入,每3张图像代表一个人。 我将数据标记为:2000对为正数,2000对为负数,但是我想确保批次也保持平衡,例如:64个批次表示32个正数和32个负数。
模型采用以下形状的6个输入:(3024,1,192,192) y_train shape =(1008,1)
但是,当我尝试平衡批次时,fit_gen期望输出以下(x,y) 并且我需要以下输出(x [:0] .. X [:5],y)使其适合模型
发生以下错误
Output of generator should be a tuple `(x, y, sample_weight)` or `(x, y)`. Found: (array([[[[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.],
...,
[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.]]],
我使用以下公式来平衡批次:
img_1 = x_train[:, 0]
img_2 = x_train[:, 1]
img_3 = x_train[:, 2]
img_4 = x_train[:, 3]
img_5 = x_train[:, 4]
img_6 = x_train[:, 5]
gen = balanced_generator(x_train,img_1, img_2, img_3, img_4, img_5, img_6, y_train, 64)
modelMerged.fit_generator(gen, steps_per_epoch=1, epochs=epochs)
def balanced_generator(x,x1,x2,x3,x4,x5,x6, y, batch_size):
batch_x_shape = (batch_size, x.shape[1], x.shape[2], x.shape[3],x.shape[4])
batch_y_shape = (batch_size, )
batch_size1 = int(batch_size/2)
batch_x = np.ndarray(shape=batch_x_shape, dtype=x.dtype)
batch_y = np.zeros(shape=batch_y_shape, dtype=y.dtype)
for i in range(batch_size1):
ind1 = np.random.randint(0,y.shape[0])
while y[ind1] == 0:
ind1 = np.random.randint(0,y.shape[0])
batch_x[i,0,:,:,:] = x1[ind1,:,:,:]
batch_x[i,1,:,:,:] = x2[ind1,:,:,:]
batch_x[i,2,:,:,:] = x3[ind1,:,:,:]
batch_x[i,3,:,:,:] = x4[ind1,:,:,:]
batch_x[i,4,:,:,:] = x5[ind1,:,:,:]
batch_x[i,5,:,:,:] = x6[ind1,:,:,:]
batch_y[i] = y[ind1]
for i in range(batch_size):
i = int(batch_size/2)
ind2 = np.random.randint(0,y.shape[0])
while y[ind2] == 0:
ind2 = np.random.randint(0,y.shape[0])
batch_x[i,0,:,:,:] = x1[ind2,:,:,:]
batch_x[i,1,:,:,:] = x2[ind2,:,:,:]
batch_x[i,2,:,:,:] = x3[ind2,:,:,:]
batch_x[i,3,:,:,:] = x4[ind2,:,:,:]
batch_x[i,4,:,:,:] = x5[ind2,:,:,:]
batch_x[i,5,:,:,:] = x6[ind2,:,:,:]
batch_y[i] = y[ind2]
print("batch #")
yield(batch_x[:, 0],batch_x[:, 1],batch_x[:, 2],batch_x[:, 3],batch_x[:, 4],batch_x[:, 5], batch_y)
答案 0 :(得分:1)
fit_generator
中的keras
仅以(X,Y)
格式输入,因此您应将yield
部分中的X输出连接到自定义的list
中生成器功能:
yield( [batch_x[:, 0],batch_x[:, 1],batch_x[:, 2],batch_x[:, 3],batch_x[:, 4],batch_x[:, 5]], batch_y) # <- Add []