所有输入数组和目标数组必须具有相同数量的样本。“ - 训练单个图像以检查模型是否在keras

时间:2016-03-10 13:19:26

标签: machine-learning theano deep-learning keras

def obcandidate(inputvgg,outputmodel):

    graph = Graph()
    graph.add_input(name = 'input1', input_shape = (512, 14, 14))
    graph.add_node(Convolution2D(512, 1, 1), name = 'conv11', input = 'input1')
    graph.add_node(Convolution2D(512, 14, 14), name = 'conv112', input = 'conv11')
    graph.add_node(Flatten(), name = 'flatten11', input = 'conv112')
    graph.add_node(Dense(3136), name = 'dense1', input = 'flatten11')
    graph.add_node((Activation('relu')), name = 'relu', input = 'dense1')
    graph.add_node(Reshape((56,56)), name = 'reshape', input = 'relu')

    sgd = SGD(lr = 0.001, decay = .00005, momentum = 0.9, nesterov = True)

    graph.add_output(name = 'output1', input = 'reshape')
    graph.compile(optimizer = sgd, loss = {
    'output1': 'binary_crossentropy'})

    print 'compile success'

    history = graph.fit({'input1':inputvgg, 'output1':outputmodel}, nb_epoch=1)
    predictions = graph.predict({'input1':inputvgg})

    return graph

""

"main function"

""

if __name__ == "__main__":

    model = VGG_16('vgg16_weights.h5')
    sgdvgg = SGD(lr = 0.1, decay = 1e-6, momentum = 0.9, nesterov = True)
    model.compile(optimizer = sgdvgg, loss = 'categorical_crossentropy')
    finaloutputmodel = outputofconvlayer(model)
    finaloutputmodel.compile(optimizer = sgdvgg, loss = 'categorical_crossentropy')
    img = cv2.resize(cv2.imread('000032.jpg'), (224, 224))
    mean_pixel = [103.939, 116.779, 123.68]
    img = img.astype(np.float32, copy = False)

    for c in range(3):
    img[: , : , c] = img[: , : , c] - mean_pixel[c]
    img = img.transpose((2, 0, 1))
    img = np.expand_dims(img, axis = 0)
    imgout = np.asarray(cv2.resize(cv2.imread('000032seg.png',0), (56, 56)))
    imgout[imgout!=0]=1
    out=imgout
    inputvgg = np.asarray(finaloutputmodel.predict(img))
    obcandidate(inputvgg,out)

嗨,上面是我的代码,我试图通过图形模型来分割候选对象

如果代码有效,我想检查一个输入,所以我给它一个输入图像和输出图像,

但是keras给了我一个错误 - “所有输入数组和目标数组必须具有相同数量的样本。”

任何人都可以告诉我我该怎么做以确定我的模型是否运行。我正在训练一个输入以便我可以验证我的模型是否正确并开始训练,还有其他方法吗?

1 个答案:

答案 0 :(得分:1)

在您执行此操作的部分中,history = graph.fit({'input1':inputvgg, 'output1':outputmodel}, nb_epoch=1) inputvgg和outputmodel应具有相同数量的维度。