简体   繁体   English

ValueError: 层“sequential_2”的输入 0 与层不兼容:预期形状=(None, 256, 256, 3),发现形状=(None, 1, 256, 256, 3)

[英]ValueError: Input 0 of layer "sequential_2" is incompatible with the layer: expected shape=(None, 256, 256, 3), found shape=(None, 1, 256, 256, 3)

I have the following code, and I want to train the U-Net model with 1000 images, and the corresponding masks.我有以下代码,我想用 1000 张图像和相应的掩码训练 U-Net 模型。 When I try to train the model with just one image everything goes smooth, but when I try to train it with the whole data, I receive the following error:当我尝试只用一张图像训练模型时,一切都很顺利,但是当我尝试用整个数据训练它时,我收到以下错误:

ValueError: Input 0 of layer "sequential_2" is incompatible with the layer: expected shape=(None, 256, 256, 3), found shape=(None, 1, 256, 256, 3)
dataset_dir='/content/drive/MyDrive/training'
# Path to mhd images (not gt)
image_ids = []
mascaras=[]
imagens=[]
# r=root, d=directories, f = files
for r, d, f in os.walk(dataset_dir):
    for file in f:    
        if ('ED.mhd' in file) or ('ES.mhd' in file):
            image_ids.append(os.path.join(r, file))
            #print(image_ids)
            image_path=os.path.join(r, file)
            image1 = sitk.GetArrayFromImage(sitk.ReadImage(image_path,sitk.sitkFloat32))
            image1 = image1[0,:,:] #2D grayscale image
            image2=cv2.resize(image1,(256,256))
            image3=image2/255
            
            #mask0
            mask0_path=os.path.join(r,file)[:-4]+"_gt0.mhd"
            mask0_1=sitk.GetArrayFromImage(sitk.ReadImage(mask0_path,sitk.sitkFloat32))
            mask0_2=cv2.resize(mask0_1,(32,32))
            mask0_2= np.expand_dims(mask0_2, axis=0)
            #mask1
            mask1_path = os.path.join(r,file)[:-4]+"_gt1.mhd"
            mask1_1=sitk.GetArrayFromImage(sitk.ReadImage(mask1_path,sitk.sitkFloat32))
            mask1_2=cv2.resize(mask1_1,(32,32))
            mask1_2= np.expand_dims(mask1_2, axis=0)
            #mask2
            mask2_path = os.path.join(r,file)[:-4]+"_gt2.mhd"
            mask2_1=sitk.GetArrayFromImage(sitk.ReadImage(mask2_path,sitk.sitkFloat32))
            mask2_2=cv2.resize(mask2_1,(32,32))
            mask2_2= np.expand_dims(mask2_2, axis=0)
            #mask3
            mask3_path = os.path.join(r,file)[:-4]+"_gt3.mhd"
            mask3_1=sitk.GetArrayFromImage(sitk.ReadImage(mask3_path,sitk.sitkFloat32))
            mask3_2=cv2.resize(mask3_1,(32,32))
            mask3_2= np.expand_dims(mask3_2, axis=0)

            #mask total
            mask_f=np.stack((mask0_2, mask1_2,mask2_2,mask3_2),axis=3)
            mascaras.append(mask_f)
            mascaras=np.array(mascaras)
            #imagem total
            image_f=np.stack((image3,image3,image3),axis=2)
            image_f=np.expand_dims(image_f, axis=0)
            imagens.append(image_f)
            imagens=np.array(imagens)
history=model1.fit(imagens,mascaras,epochs=20)

您可以通过发出来reshape与模型输入形状匹配的输入数据;

imagengs = imagens.reshape((256, 256, 3))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ValueError:“顺序”层的输入 0 与该层不兼容:预期形状=(无,256、256、3),找到的形状=(无,33) - ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 256, 256, 3), found shape=(None, 33) ValueError:layersequential_1 的输入 0 与 layer 不兼容::预期 min_ndim=4,发现 ndim=3。 收到的完整形状:[无、256、256] - ValueError: Input 0 of layer sequential_1 is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: [None, 256, 256] ValueError:layersequential_32 的输入 0 与 layer 不兼容::预期 min_ndim=3,发现 ndim=2。 收到的完整形状:[无,256] - ValueError: Input 0 of layer sequential_32 is incompatible with the layer: : expected min_ndim=3, found ndim=2. Full shape received: [None, 256] ValueError:层 sequential_3 的输入 0 与层不兼容::预期 min_ndim=4,发现 ndim=3。 已收到完整形状:(无、224、256) - ValueError: Input 0 of layer sequential_3 is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: (None, 224, 256) 层“sequential_14”的输入 0 与层不兼容:预期形状=(None, 256, 256, 3),找到的形状=(None, 32, 32, 3) - nput 0 of layer "sequential_14" is incompatible with the layer: expected shape=(None, 256, 256, 3), found shape=(None, 32, 32, 3) 层 lstm_9 的输入 0 与层不兼容:预期 ndim=3,发现 ndim=4。 收到的完整形状:[None, 2, 4000, 256] - Input 0 of layer lstm_9 is incompatible with the layer: expected ndim=3, found ndim=4. Full shape received: [None, 2, 4000, 256] 层 lstm_35 的输入 0 与层不兼容:预期 ndim=3,发现 ndim=4。 收到的完整形状:[None, 1966, 7059, 256] - Input 0 of layer lstm_35 is incompatible with the layer: expected ndim=3, found ndim=4. Full shape received: [None, 1966, 7059, 256] sequential_2 层的输入 0 与该层不兼容:预期 ndim=3,发现 ndim=2。 收到完整形状:(无,1) - Input 0 of layer sequential_2 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (None, 1) 如何摆脱“sequential_2”层的错误输入 0 与该层不兼容:预期形状 =(None, 32, 1200),找到形状 =(None, 1148) - How to get rid Error Input 0 of layer "sequential_2" is incompatible with the layer: expected shape=(None, 32, 1200), found shape=(None, 1148) ValueError:“顺序”层的输入 0 与层不兼容:预期形状 =(无,223461,5),找到形状 =(无,5) - ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 223461, 5), found shape=(None, 5)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM