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

2024-03-28 12:49:33 发布

您现在位置:Python中文网/ 问答频道 /正文

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)

Tags: pathinimginputoutputlabelsdefnp