Clear Filters
Clear Filters

Info

This question is closed. Reopen it to edit or answer.

ValueError: Error when checking input: expected input_1 to have 4 dimensions, but got array with shape (2048, 256, 3)

1 view (last 30 days)
I got error when running my U-net architecture network and I'm not sure where is the error and to fix that. Here's my codes input image codes: and I have tried so many times to fix this problem, but at the end still error. I am using U-Net architecture network to segment my image data. Since my input image data is M x N x 3, but the dimension is not the same as 256 x 256 x 3. My dimension input image is 251 x 257 x 3 uint8.
I have tried to change the codes into:
imgs = np.asarray((total, image_rows, image_cols, 3), dtype=np.uint8)
imgs_mask = np.asarray((total, image_rows, image_cols), dtype=np.uint8)
from this:
imgs = np.ndarray((total, image_rows, image_cols, 3), dtype=np.uint8)
imgs_mask = np.ndarray((total, image_rows, image_cols), dtype=np.uint8)
Here the full codes for my testing input data:
def create_test_data():
for i in range(len(image)):
print('-'*9)
print("Creating testing images...")
print('-'*9)
print(image[i])
test_data_path = '/home/motmot/Documents/unet/data/test/img/'
test_mask_path = '/home/motmot/Documents/unet/data/test/msk/'
image = os.listdir(test_data_path)
mask = os.listdir(test_mask_path)
total = len(image)
imgs = np.ndarray((total, image_rows, image_cols, 3), dtype=np.uint8)
#imgs = np.asarray((total, image_rows, image_cols, 3), dtype=np.uint8)
imgs_mask = np.ndarray((total, image_rows, image_cols), dtype=np.uint8)
#imgs_mask = np.asarray((total, image_rows, image_cols), dtype=np.uint8)
create_test_data()
And here my U-net architecture network codes:
def get_unet():
inputs = Input((img_rows, img_cols, 3))
conv1 = Conv2D(32, (3, 3), activation='relu', padding='same')(inputs)
conv1 = BatchNormalization()(conv1)
conv1 = Conv2D(32, (3, 3), activation='relu', padding='same')(conv1)
conv1 = BatchNormalization()(conv1)
pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)
conv2 = Conv2D(64, (3, 3), activation='relu', padding='same')(pool1)
conv2 = BatchNormalization()(conv2)
conv2 = Conv2D(64, (3, 3), activation='relu', padding='same')(conv2)
conv2 = BatchNormalization()(conv2)
pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)
conv3 = Conv2D(128, (3, 3), activation='relu', padding='same')(pool2)
conv3 = BatchNormalization()(conv3)
conv3 = Conv2D(128, (3, 3), activation='relu', padding='same')(conv3)
conv3 = BatchNormalization()(conv3)
pool3 = MaxPooling2D(pool_size=(2, 2))(conv3)
conv4 = Conv2D(256, (3, 3), activation='relu', padding='same')(pool3)
conv4 = BatchNormalization()(conv4)
conv4 = Conv2D(256, (3, 3), activation='relu', padding='same')(conv4)
conv4 = BatchNormalization()(conv4)
pool4 = MaxPooling2D(pool_size=(2, 2))(conv4)
conv5 = Conv2D(512, (3, 3), activation='relu', padding='same')(pool4)
conv5 = BatchNormalization()(conv5)
conv5 = Conv2D(512, (3, 3), activation='relu', padding='same')(conv5)
conv5 = BatchNormalization()(conv5)
up6 = concatenate([Conv2DTranspose(256, (2, 2), strides=(2, 2), padding='same')(conv5), conv4], axis=3)
conv6 = Conv2D(256, (3, 3), activation='relu', padding='same')(up6)
conv6 = BatchNormalization()(conv6)
conv6 = Conv2D(256, (3, 3), activation='relu', padding='same')(conv6)
conv6 = BatchNormalization()(conv6)
up7 = concatenate([Conv2DTranspose(128, (2, 2), strides=(2, 2), padding='same')(conv6), conv3], axis=3)
conv7 = Conv2D(128, (3, 3), activation='relu', padding='same')(up7)
conv7 = BatchNormalization()(conv7)
conv7 = Conv2D(128, (3, 3), activation='relu', padding='same')(conv7)
conv7 = BatchNormalization()(conv7)
up8 = concatenate([Conv2DTranspose(64, (2, 2), strides=(2, 2), padding='same')(conv7), conv2], axis=3)
conv8 = Conv2D(64, (3, 3), activation='relu', padding='same')(up8)
conv8 = BatchNormalization()(conv8)
conv8 = Conv2D(64, (3, 3), activation='relu', padding='same')(conv8)
conv8 = BatchNormalization()(conv8)
up9 = concatenate([Conv2DTranspose(32, (2, 2), strides=(2, 2), padding='same')(conv8), conv1], axis=3)
conv9 = Conv2D(32, (3, 3), activation='relu', padding='same')(up9)
conv9 = BatchNormalization()(conv9)
conv9 = Conv2D(32, (3, 3), activation='relu', padding='same')(conv9)
conv9 = BatchNormalization()(conv9)
conv10 = Conv2D(1, (1, 1), activation='sigmoid')(conv9)
model = Model(inputs=[inputs], outputs=[conv10])
model.compile(optimizer=Adam(), loss=[loss], metrics=[dice_coef, iou_coef])
return model
And the error occured :
ValueError: Error when checking input: expected input_1 to have 4 dimensions, but got array with shape (2048, 256, 3)
Is the ValueError said the error is in this codes?
imgs = np.ndarray((total, image_rows, image_cols, 3), dtype=np.uint8)
imgs_mask = np.ndarray((total, image_rows, image_cols), dtype=np.uint8)
Because I have mentions the imgs:
((total, image_rows, image_cols, 3), dtype=np.uint8)
in 4_dimension, and the ground truth imgs_mask:
((total, image_rows, image_cols), dtype=np.uint8)
which in 3_dimension? Am I correct? How should I fix this?

Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!