ValueError:检查目标时出错:预期crfrnn具有4个维,但数组具有形状

时间:2019-06-26 18:43:37

标签: python machine-learning deep-learning

Traceback (most recent call last):

  File "run_demo.py", line 185, in <module>
    main()

  File "run_demo.py", line 153, in main

   m.fit_generator(G, 100, epochs=1)

ValueError: Error when checking target: expected crfrnn to have 4 dimensions, but got array with shape (8, 250000, 6)

这是执行我的主文件时出现的确切问题。

我不知道我是否应该尝试更改模型,或者问题出在发电机上。任何人都可以帮助我吗..如果有人希望我可以发送我的github链接来运行整个文件。

我的主文件:

def getImageArr(path, width, height, imgNorm="sub_mean", odering='channels_first'):
    try:
        img = cv2.imread(path, 1)

        if imgNorm == "sub_and_divide":
            img = np.float32(cv2.resize(img, (width, height))) / 127.5 - 1
        elif imgNorm == "sub_mean":
            img = cv2.resize(img, (width, height))
            img = img.astype(np.float32)
            img[:, :, 0] -= 103.939
            img[:, :, 1] -= 116.779
            img[:, :, 2] -= 123.68
        elif imgNorm == "divide":
            img = cv2.resize(img, (width, height))
            img = img.astype(np.float32)
            img = img / 255.0
        if odering == 'channels_first':
            img = np.rollaxis(img, 2, 0)
        img=img.T
        return img
    except Exception as e:
        print(path, e)
        img = np.zeros((height, width, 3))
        if odering == 'channels_first':
            img = np.rollaxis(img, 2, 0)
        img=img.T
        return img
def getSegmentationArr(path, nClasses, width, height):
    seg_labels = np.zeros((height, width, nClasses))
    try:
        img = cv2.imread(path, 1)
        img = cv2.resize(img, (width, height))
        img = img[:, :, 0]
        for c in range(nClasses):
            seg_labels[:, :, c] = (img == c).astype(int)
    except Exception as e:
        print(e)
    seg_labels = np.reshape(seg_labels, (width * height, nClasses))
    return seg_labels
def imageSegmentationGenerator(images_path, segs_path, batch_size, n_classes, input_height, input_width, output_height,output_width):

    images = glob.glob(images_path + "*.jpg") 
    images.sort()
    segmentations = glob.glob(segs_path + "*.jpg") + glob.glob(segs_path + "*.png") + glob.glob(segs_path + "*.jpeg")
    segmentations.sort()
    zipped =itertools.cycle(zip(images, segmentations))
    while True:
        X = []
        Y = []
        for _ in range(batch_size):
            im,seg = next(zipped,(None,None))
            X.append(getImageArr(im, input_width, input_height))
            Y.append(getSegmentationArr(seg, n_classes, output_width, output_height))
        yield np.array(X), np.array(Y)
def main():
    input_file = 'image.jpg'
    output_file = 'labels.png'
    m= get_crfrnn_model_def()            
    m.compile(loss='categorical_crossentropy',optimizer='adadelta',metrics=['accuracy'])
    G = imageSegmentationGenerator('xtrain/', 'ytrain/', 8, 6,500,500,500,500)
    for ep in range(1):
        print('pahuch gya')
        m.fit_generator(G, 100, epochs=1)

0 个答案:

没有答案