keras-team / tf-keras

The TensorFlow-specific implementation of the Keras API, which was the default Keras from 2019 to 2023.
Apache License 2.0
53 stars 26 forks source link

ValueError: Error when checking input: expected layer_1_input to have shape (128, 128, 1) but got array with shape (128, 128, 3) #547

Closed W2YN closed 2 years ago

W2YN commented 2 years ago

System information. tensorflow==1.15.2 keras==2.2.4 python version 36.6

Describe the feature and the current behavior/state. Traceback (most recent call last): File "C:\ProgramData\Anaconda3\envs\tensorflow\lib\site-packages\IPython\core\interactiveshell.py", line 2878, in run_code exec(code_obj, self.user_global_ns, self.user_ns) File "", line 1, in runfile('F:/Image-segmentaion-Limited-dataset-Tongue-segmentaion-master/xxx.py', wdir='F:/Image-segmentaion-Limited-dataset-Tongue-segmentaion-master') File "E:\PyCharm 2021.2.3\plugins\python\helpers\pydev_pydev_bundle\pydev_umd.py", line 198, in runfile pydev_imports.execfile(filename, global_vars, local_vars) # execute the script File "E:\PyCharm 2021.2.3\plugins\python\helpers\pydev_pydev_imps_pydev_execfile.py", line 18, in execfile exec(compile(contents+"\n", file, 'exec'), glob, loc) File "F:/Image-segmentaion-Limited-dataset-Tongue-segmentaion-master/xxx.py", line 241, in model.fit(X_1, y_1, batch_size=64, epochs=500) File "C:\Users\Administrator\AppData\Roaming\Python\Python36\site-packages\keras\engine\training.py", line 952, in fit batch_size=batch_size) File "C:\Users\Administrator\AppData\Roaming\Python\Python36\site-packages\keras\engine\training.py", line 751, in _standardize_user_data exception_prefix='input') File "C:\Users\Administrator\AppData\Roaming\Python\Python36\site-packages\keras\engine\training_utils.py", line 138, in standardize_input_data str(data_shape)) ValueError: Error when checking input: expected layer_1_input to have shape (128, 128, 1) but got array with shape (128, 128, 3)

Use tf.where in 2.0, which has the same broadcast rule as np.where


Layer (type) Output Shape Param #

layer_1 (Layer) (None, 128, 128, 1) 0


zero_padding2d_1 (ZeroPaddin (None, 130, 130, 1) 0


conv2d_1 (Conv2D) (None, 128, 128, 64) 640


batch_normalization_1 (Batch (None, 128, 128, 64) 256


max_pooling2d_1 (MaxPooling2 (None, 64, 64, 64) 0


zero_padding2d_2 (ZeroPaddin (None, 66, 66, 64) 0


conv2d_2 (Conv2D) (None, 64, 64, 128) 73856


batch_normalization_2 (Batch (None, 64, 64, 128) 512


activation_1 (Activation) (None, 64, 64, 128) 0


max_pooling2d_2 (MaxPooling2 (None, 32, 32, 128) 0


zero_padding2d_3 (ZeroPaddin (None, 34, 34, 128) 0


conv2d_3 (Conv2D) (None, 32, 32, 256) 295168


batch_normalization_3 (Batch (None, 32, 32, 256) 1024


activation_2 (Activation) (None, 32, 32, 256) 0


max_pooling2d_3 (MaxPooling2 (None, 16, 16, 256) 0


zero_padding2d_4 (ZeroPaddin (None, 18, 18, 256) 0


conv2d_4 (Conv2D) (None, 16, 16, 512) 1180160


batch_normalization_4 (Batch (None, 16, 16, 512) 2048


activation_3 (Activation) (None, 16, 16, 512) 0


zero_padding2d_5 (ZeroPaddin (None, 18, 18, 512) 0


conv2d_5 (Conv2D) (None, 16, 16, 512) 2359808


batch_normalization_5 (Batch (None, 16, 16, 512) 2048


up_sampling2d_1 (UpSampling2 (None, 32, 32, 512) 0


zero_padding2d_6 (ZeroPaddin (None, 34, 34, 512) 0


conv2d_6 (Conv2D) (None, 32, 32, 256) 1179904


batch_normalization_6 (Batch (None, 32, 32, 256) 1024


up_sampling2d_2 (UpSampling2 (None, 64, 64, 256) 0


zero_padding2d_7 (ZeroPaddin (None, 66, 66, 256) 0


conv2d_7 (Conv2D) (None, 64, 64, 128) 295040


batch_normalization_7 (Batch (None, 64, 64, 128) 512


up_sampling2d_3 (UpSampling2 (None, 128, 128, 128) 0


zero_padding2d_8 (ZeroPaddin (None, 130, 130, 128) 0


conv2d_8 (Conv2D) (None, 128, 128, 64) 73792


batch_normalization_8 (Batch (None, 128, 128, 64) 256


conv2d_9 (Conv2D) (None, 128, 128, 1) 65


reshape_1 (Reshape) (None, 16384) 0


activation_4 (Activation) (None, 16384) 0


reshape_2 (Reshape) (None, 128, 128, 1) 0

Total params: 5,466,113 Trainable params: 5,462,273 Non-trainable params: 3,840

my code:

img_rows, img_cols = 128, 128 label_rows, label_cols = 128, 128

img_data_gen_args = dict( rescale=1. / 255, fill_mode="constant", cval=0 ) label_data_gen_args = dict( fill_mode="constant", cval=1 ) image_datagen = ImageDataGenerator(img_data_gen_args) mask_datagen = ImageDataGenerator(label_data_gen_args)

seed = 1

image_generator = image_datagen.flow_from_directory( 'E:/xxxx/xxx/', target_size=(img_rows, img_cols), class_mode=None, batch_size=8, shuffle=False, seed=seed)

mask_generator = mask_datagen.flow_from_directory( 'E:/xxxx/xxx/mask', target_size=(label_rows, label_cols), class_mode=None, batch_size=8, shuffle=False, color_mode='grayscale', seed=seed)

train_generator = zip(image_generator, mask_generator)

kernel = 3 filter_size = 64 pad = 1 pool_size = 2

from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img,save_img from keras.models import Model from keras.callbacks import ModelCheckpoint from keras.models import Sequential from keras.layers import Convolution2D, MaxPooling2D from keras.layers import Input, Layer, Conv2D, SeparableConv2D, Dense,Add, Conv2DTranspose,Dropout, Activation, Flatten, Reshape, Permute from keras.layers import Concatenate from keras.layers import ZeroPadding2D, UpSampling2D from keras.layers.normalization import BatchNormalization from keras.optimizers import SGD, Adam import sys import os import numpy as np import matplotlib import matplotlib.pyplot as plt import pathlib import itertools from timeit import timeit import tensorflow as tf import PIL.Image from keras.preprocessing import image import tqdm from keras import layers

model = Sequential() model.add(Layer(input_shape=(img_rows, img_cols, 1)))

model.add(ZeroPadding2D(padding=(pad, pad))) model.add(Convolution2D(filter_size, kernel, kernel, border_mode='valid')) model.add(BatchNormalization()) model.add(MaxPooling2D(pool_size=(pool_size, pool_size)))

model.add(ZeroPadding2D(padding=(pad, pad))) model.add(Convolution2D(128, kernel, kernel, border_mode='valid')) model.add(BatchNormalization()) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(pool_size, pool_size)))

model.add(ZeroPadding2D(padding=(pad, pad))) model.add(Convolution2D(256, kernel, kernel, border_mode='valid')) model.add(BatchNormalization()) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(pool_size, pool_size)))

model.add(ZeroPadding2D(padding=(pad, pad))) model.add(Convolution2D(512, kernel, kernel, border_mode='valid')) model.add(BatchNormalization()) model.add(Activation('relu'))

model.add(ZeroPadding2D(padding=(pad, pad))) model.add(Convolution2D(512, kernel, kernel, border_mode='valid')) model.add(BatchNormalization())

model.add(UpSampling2D(size=(pool_size, pool_size))) model.add(ZeroPadding2D(padding=(pad, pad))) model.add(Convolution2D(256, kernel, kernel, border_mode='valid')) model.add(BatchNormalization())

model.add(UpSampling2D(size=(pool_size, pool_size))) model.add(ZeroPadding2D(padding=(pad, pad))) model.add(Convolution2D(128, kernel, kernel, border_mode='valid')) model.add(BatchNormalization())

model.add(UpSampling2D(size=(pool_size, pool_size))) model.add(ZeroPadding2D(padding=(pad, pad))) model.add(Convolution2D(filter_size, kernel, kernel, border_mode='valid')) model.add(BatchNormalization())

model.add(Convolution2D(1, 1, 1, border_mode='valid', )) print(model.output_shape) model.add(Reshape((label_rows * label_cols,))) model.add(Activation('sigmoid'))

model.add(Reshape((label_rows, label_cols, 1))) model.compile(loss="binary_crossentropy", optimizer='adam', metrics=['binary_accuracy'])

model.summary()

def load_image_mask( image_path='E:/xxxx/xxx/images/', mask_path='E:/xxxx/xxx/mask/', ): import pathlib X = [] y = []

for i in tqdm.tqdm(range(299)): x_1 = image.load_img(image_path + "/" + str(i+1) + ".bmp", target_size=(128, 128)) x_1 = image.img_to_array(x_1) x_1 = np.expand_dims(x_1, axis=0) X.append(x_1) for i in tqdm.tqdm(range(299)): y_1 = image.load_img(mask_path + "/" + str(i+1) + ".bmp", target_size=(128, 128)) y_1 = image.img_to_array(y_1) y_1 = np.expand_dims(y_1, axis=0) y.append(y_1) return X, y

X, y = load_image_mask() X_1 = np.concatenate(X, axis=0) y_1 = np.concatenate(y, axis=0) np.save("E:/xxxx/xxx/Image1_6_21.npy", X_1) np.save("E:/xxxx/xxx/Mask1_6_21.npy", y_1)

model.fit(X_1, y_1, batch_size=64, epochs=500) model.save("E:/xxxx/xxx/model_2022_6_23.model")

from keras.models import load_model import numpy as np

model = load_model('E:/xxxx/xxx/model_2022_6_23.model',compile=False) X = np.load("E:/xxxx/xxx/model_2022_6_23.model/Image1_6_21.npy") y = np.load("E:/xxxx/xxx/model_2022_6_23.model/Mask1_6_21.npy") model.save("E:/xxxx/xxx/model_2022_6_23.model")

Training picture information:

img: 768x576 24 mask: 768x576 1

Hope to get your help, thanks!

tilakrayal commented 2 years ago

@W2YN, We see that you are using tf version 1.15, 1.x is not actively supported, please update to 2.x and let us know if you are facing the same issue. Thank you!

W2YN commented 2 years ago

@tilakrayal Thank you. I'll try again update to 2.x

google-ml-butler[bot] commented 2 years ago

This issue has been automatically marked as stale because it has no recent activity. It will be closed if no further activity occurs. Thank you.

google-ml-butler[bot] commented 2 years ago

Closing as stale. Please reopen if you'd like to work on this further.