ValueError: Dimensions must be equal, but are 508 and 512 - python

I am trying to create an autoencoder for 3d images and here is the model:
def create_encoder(width, height, depth):
x = Input(shape=(height, width, depth))
# Encoder
e_conv1 = Conv2D(64, (3, 3), activation='relu', padding='same')(x)
pool1 = MaxPooling2D((2, 2), padding='same')(e_conv1)
batchnorm_1 = BatchNormalization()(pool1)
e_conv2 = Conv2D(32, (3, 3), activation='relu', padding='same')(batchnorm_1)
pool2 = MaxPooling2D((2, 2), padding='same')(e_conv2)
batchnorm_2 = BatchNormalization()(pool2)
e_conv3 = Conv2D(16, (3, 3), activation='relu', padding='same')(batchnorm_2)
h = MaxPooling2D((2, 2), padding='same')(e_conv3)
# Decoder
d_conv1 = Conv2D(64, (3, 3), activation='relu', padding='same')(h)
up1 = UpSampling2D((2, 2))(d_conv1)
d_conv2 = Conv2D(32, (3, 3), activation='relu', padding='same')(up1)
up2 = UpSampling2D((2, 2))(d_conv2)
d_conv3 = Conv2D(16, (3, 3), activation='relu')(up2)
up3 = UpSampling2D((2, 2))(d_conv3)
r = Conv2D(3, (3, 3), activation='sigmoid', padding='same')(up3)
model = Model(x, r)
model.compile(optimizer='adam', loss='mse')
return model
But whenever I try to run the code:
width = 512
height = 512
depth = 3
EPOCHS = 100
BS = 128
autoencoder = create_encoder(width, height, depth)
earlystop = EarlyStopping(monitor='loss', patience=3)
H = autoencoder.fit(trainXNoisy, trainX, validation_data=(testXNoisy, testX), epochs=EPOCHS, batch_size=BS, callbacks=[earlystop])
I get this error:
Epoch 1/100
/usr/local/lib/python3.7/dist-packages/keras/optimizer_v2/adam.py:105: UserWarning: The `lr` argument is deprecated, use `learning_rate` instead.
super(Adam, self).__init__(name, **kwargs)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-91-48e3c96bc5fd> in <module>()
12 earlystop = EarlyStopping(monitor='loss', patience=3)
13 # H = autoencoder.fit(trainXNoisy, trainX, validation_data=(testXNoisy, testX), epochs=EPOCHS, batch_size=BS, callbacks=[earlystop])
---> 14 H = autoencoder.fit(trainXNoisy, trainX, epochs=EPOCHS, batch_size=BS, callbacks=[earlystop])
1 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in autograph_handler(*args, **kwargs)
1127 except Exception as e: # pylint:disable=broad-except
1128 if hasattr(e, "ag_error_metadata"):
-> 1129 raise e.ag_error_metadata.to_exception(e)
1130 else:
1131 raise
ValueError: in user code:
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 878, in train_function *
return step_function(self, iterator)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 867, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 860, in run_step **
outputs = model.train_step(data)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 810, in train_step
y, y_pred, sample_weight, regularization_losses=self.losses)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/compile_utils.py", line 201, in __call__
loss_value = loss_obj(y_t, y_p, sample_weight=sw)
File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 141, in __call__
losses = call_fn(y_true, y_pred)
File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 245, in call **
return ag_fn(y_true, y_pred, **self._fn_kwargs)
File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 1204, in mean_squared_error
return backend.mean(tf.math.squared_difference(y_pred, y_true), axis=-1)
ValueError: Dimensions must be equal, but are 508 and 512 for '{{node mean_squared_error/SquaredDifference}} = SquaredDifference[T=DT_FLOAT](model_8/conv2d_85/Sigmoid, IteratorGetNext:1)' with input shapes: [?,508,508,3], [?,512,512,3].
I am a newbie to autoencoders (maybe to the whole machine learning world) but I tried to get information from other similar problems but I couldn't. If any one could give a better a version, it'll be way better.
Thanks,

The line
d_conv3 = Conv2D(16, (3, 3), activation='relu')(up2)
miss the padding argument. Therefore instead of a 256 X 256 X 16 output you get a 254 X 254 X 16, which becomes a 508 X 508 X 16 after the upsampling, and finaly a 508 X 508 X 3 after the last Conv2D
MSE Error needs to compare two images of the same size, and the input is a 512 X 512 X 3. Juste add the padding argument as you do in the other Conv2D and it should work just fine
d_conv3 = Conv2D(16, (3, 3), activation='relu', padding='same')(up2)

Related

ValueError: Input 0 of layer "sequential_5" is incompatible with the layer: expected shape=(None, 200, 200, 3), found shape=(None, 200, 3)

I am coding a model that can classify between images of cats and dogs, Everything works up to fitting the model with the array of resized images, however, when I attempt to run the fitting, it gives me the following error
ValueError: in user code:
File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1051, in train_function *
return step_function(self, iterator)
File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1040, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1030, in run_step **
outputs = model.train_step(data)
File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 889, in train_step
y_pred = self(x, training=True)
File "/usr/local/lib/python3.8/dist-packages/keras/utils/traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "/usr/local/lib/python3.8/dist-packages/keras/engine/input_spec.py", line 264, in assert_input_compatibility
raise ValueError(f'Input {input_index} of layer "{layer_name}" is '
ValueError: Input 0 of layer "sequential_5" is incompatible with the layer: expected shape=(None, 200, 200, 3), found shape=(None, 200, 3)
Code:
files_train = '/content/drive/MyDrive/Colab Notebooks/Cats_Dogs/training_set/*.jpg'
files_test = '/content/drive/MyDrive/Colab Notebooks/Cats_Dogs/test_set/*.jpg'
glob.glob(files_train)
glob.glob(files_test)
images_train = [cv2.imread(image) for image in glob.glob(files_train)]
images_test = [cv2.imread(image) for image in glob.glob(files_test)]
x_train = []
x_test = []
for img in images_train:
x_train.append(cv2.resize(img, (200,200), interpolation=cv2.INTER_CUBIC))
for img in images_test:
x_test.append(cv2.resize(img, (200,200), interpolation=cv2.INTER_CUBIC))
for x in x_train_arr:
x_train_arr = x / 255
for x in x_test_arr:
x_test_arr = x / 255
model = Sequential()
model.add( Conv2D(200, (5,5), activation='relu', input_shape=(200, 200, 3)))
model.add(MaxPooling2D(pool_size = (2,2)))
model.add( Conv2D(200, (5,5), activation='relu'))
model.add(MaxPooling2D(pool_size = (2,2)))
model.add(Flatten())
model.add(Dense(250, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(100, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='relu'))
model.add(Dense(2, activation='softmax'))
hist = model.fit(x_train_arr,text_train,
batch_size = 256,
epochs = 10,
validation_split = 0.2)
(Did not paste all code, only which what I thought was relevant)
What I do not understand is that when I run: print(x_train_arr.shape) it gives (3003, 200, 200, 3)
How do I solve this issue?

Keras in google colab shows ValueError after changing number of classes

I am a student working on a school project that needs me to identify agricultural pests from images. I am using Google colab to run the code. This is my code
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
image_size = (50, 50)
batch_size = 300
train_ds = tf.keras.preprocessing.image_dataset_from_directory(
"/content/gdrive/My Drive/pest/train",
seed=1337,
image_size=image_size,
batch_size=batch_size,
)
val_ds = tf.keras.preprocessing.image_dataset_from_directory(
"/content/gdrive/My Drive/pest/validation",
seed=1337,
image_size=image_size,
batch_size=batch_size,
)
data_augmentation = keras.Sequential(
[
layers.RandomFlip("horizontal"),
layers.RandomRotation(0.1),
]
)
def make_model(input_shape, num_classes):
inputs = keras.Input(shape=input_shape)
# Image augmentation block
x = data_augmentation(inputs)
# Entry block
x = layers.Rescaling(1.0 / 255)(x)
x = layers.Conv2D(32, 3, strides=2, padding="same")(x)
x = layers.BatchNormalization()(x)
x = layers.Activation("relu")(x)
x = layers.Conv2D(64, 3, padding="same")(x)
x = layers.BatchNormalization()(x)
x = layers.Activation("relu")(x)
previous_block_activation = x # Set aside residual
for size in [128, 256, 512, 728]:
x = layers.Activation("relu")(x)
x = layers.SeparableConv2D(size, 3, padding="same")(x)
x = layers.BatchNormalization()(x)
x = layers.Activation("relu")(x)
x = layers.SeparableConv2D(size, 3, padding="same")(x)
x = layers.BatchNormalization()(x)
x = layers.MaxPooling2D(3, strides=2, padding="same")(x)
# Project residual
residual = layers.Conv2D(size, 1, strides=2, padding="same")(
previous_block_activation
)
x = layers.add([x, residual]) # Add back residual
previous_block_activation = x # Set aside next residual
x = layers.SeparableConv2D(1024, 3, padding="same")(x)
x = layers.BatchNormalization()(x)
x = layers.Activation("relu")(x)
x = layers.GlobalAveragePooling2D()(x)
if num_classes == 2:
activation = "sigmoid"
units = 1
else:
activation = "softmax"
units = num_classes
x = layers.Dropout(0.5)(x)
outputs = layers.Dense(units, activation=activation)(x)
return keras.Model(inputs, outputs)
model = make_model(input_shape=image_size + (3,), num_classes=8)
keras.utils.plot_model(model, show_shapes=True)
epochs = 50
callbacks = [
keras.callbacks.ModelCheckpoint("save_at_{epoch}.h5"),
]
model.compile(
optimizer=keras.optimizers.Adam(1e-3),
loss="binary_crossentropy",
metrics=["accuracy"],
)
model.fit(
train_ds, epochs=epochs, callbacks=callbacks, validation_data=val_ds,
)
However, when I ran model.fit it gives me an error
Epoch 1/50
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-6-49bbab22d55e> in <module>()
10 )
11 model.fit(
---> 12 train_ds, epochs=epochs, callbacks=callbacks, validation_data=val_ds,
13 )
1 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in autograph_handler(*args, **kwargs)
1145 except Exception as e: # pylint:disable=broad-except
1146 if hasattr(e, "ag_error_metadata"):
-> 1147 raise e.ag_error_metadata.to_exception(e)
1148 else:
1149 raise
ValueError: in user code:
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1021, in train_function *
return step_function(self, iterator)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1010, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1000, in run_step **
outputs = model.train_step(data)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 860, in train_step
loss = self.compute_loss(x, y, y_pred, sample_weight)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 919, in compute_loss
y, y_pred, sample_weight, regularization_losses=self.losses)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/compile_utils.py", line 201, in __call__
loss_value = loss_obj(y_t, y_p, sample_weight=sw)
File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 141, in __call__
losses = call_fn(y_true, y_pred)
File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 245, in call **
return ag_fn(y_true, y_pred, **self._fn_kwargs)
File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 1932, in binary_crossentropy
backend.binary_crossentropy(y_true, y_pred, from_logits=from_logits),
File "/usr/local/lib/python3.7/dist-packages/keras/backend.py", line 5247, in binary_crossentropy
return tf.nn.sigmoid_cross_entropy_with_logits(labels=target, logits=output)
ValueError: `logits` and `labels` must have the same shape, received ((None, 8) vs (None, 1)).
I got the code straight from keras.io (keras documentation). Before changing num_classes=2 to num_classes=8, the code did ran but only had 0.1 accuracy. My teacher said it is a formatting error but I followed a tutorial and it ran previously. The format is training dataset in /content/gdrive/My Drive/pest/train and validation dataset in /content/gdrive/My Drive/pest/validation.
Any solutions?
The problem is that you are using binary_crossentropy loss even when your problem is not binary.
You need to use categorical_crossentropy for non binary cases.
It will also depend on your labels' shape: if they are not one-hot-encoded, you need to use sparse_categorical_crossentropy.
My advise is to use always soft max activation for the output with as much neurons as labels, since it works even for two labels:
activation = "softmax"
units = num_classes
# more code...
model.compile(
optimizer=keras.optimizers.Adam(1e-3),
loss="sparse_categorical_crossentropy",
metrics=["accuracy"],
)

unhashable type: 'numpy.ndarray' in model fit tensorflow

Hello I have TypeError: unhashable type: 'numpy.ndarray'
Main file :
import glob
import os
import random
import string
import skimage.io as io
import numpy as np
import cv2
from PIL import Image
from sklearn.model_selection import train_test_split
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import model
import tensorflow as tf
import tensorflow_addons as tfa
from tensorflow.keras.callbacks import EarlyStopping
from dataLoader import DataGenerator
PATH = './images/'
path_train = 'train/'
path_masks = 'masks/'
path_val = 'test/'
def adjustData(img, mask, flag_multi_class, num_class):
if (flag_multi_class):
img = img / 255
mask = mask[:, :, :, 0] if (len(mask.shape) == 4) else mask[:, :, 0]
new_mask = np.zeros(mask.shape + (num_class,))
for i in range(num_class):
# for one pixel in the image, find the class in mask and convert it into one-hot vector
# index = np.where(mask == i)
# index_mask = (index[0],index[1],index[2],np.zeros(len(index[0]),dtype = np.int64) + i) if (len(mask.shape) == 4) else (index[0],index[1],np.zeros(len(index[0]),dtype = np.int64) + i)
# new_mask[index_mask] = 1
new_mask[mask == i, i] = 1
new_mask = np.reshape(new_mask, (new_mask.shape[0], new_mask.shape[1] * new_mask.shape[2],
new_mask.shape[3])) if flag_multi_class else np.reshape(new_mask, (
new_mask.shape[0] * new_mask.shape[1], new_mask.shape[2]))
mask = new_mask
elif (np.max(img) > 1):
img = img / 255
mask = mask / 255
mask[mask > 0.5] = 1
mask[mask <= 0.5] = 0
return (img, mask)
def geneTrainNpy(image_path, mask_path, flag_multi_class=False, num_class=2, image_prefix="image", mask_prefix="mask",
image_as_gray=True, mask_as_gray=True):
print('-' * 30)
print(' Generation npy file...')
print('-' * 30)
image_name_arr = glob.glob(os.path.join(image_path, "%s*.bmp" % image_prefix))
image_arr = []
mask_arr = []
for index, item in enumerate(image_name_arr):
img = io.imread(item, as_gray=image_as_gray)
img = np.reshape(img, img.shape + (1,)) if image_as_gray else img
item = item[28:]
mask = io.imread(mask_path+item, as_gray=mask_as_gray)
mask = np.reshape(mask, mask.shape + (1,)) if mask_as_gray else mask
img, mask = adjustData(img, mask, flag_multi_class, num_class)
image_arr.append(img)
mask_arr.append(mask)
image_arr = np.array(image_arr)
mask_arr = np.array(mask_arr)
return image_arr, mask_arr
def makedirs(path_dirs):
if not os.path.exists(path_dirs):
os.makedirs(path_dirs)
def CLAHE(img):
clahe = cv2.createCLAHE(clipLimit=1.5, tileGridSize=(8, 8))
cl1 = clahe.apply(img)
return cl1
def eraseFile(repertoire):
files_e = os.listdir(repertoire)
for i in range(0, len(files_e)):
os.remove(repertoire + '/' + files_e[i])
def random_char(y):
return ''.join(random.choice(string.ascii_letters) for x in range(y))
path_image_train = os.path.join(PATH, path_train)
path_image_train_preprocessed = os.path.join(PATH, 'train_preprocessed/')
path_image_masks = os.path.join(PATH, path_masks)
path_image_masks_preprocessed = os.path.join(PATH, 'masks_preprocessed/')
path_image_val = os.path.join(PATH, path_val)
x, y = geneTrainNpy(path_image_train_preprocessed, path_image_masks_preprocessed,image_as_gray=True,mask_as_gray=True, image_prefix="", mask_prefix="")
x_train, x_val, y_train, y_val = train_test_split(x, y, test_size=0.10, random_state=42)
model = model.unet()
tqdm_callback = tfa.callbacks.TQDMProgressBar()
early_callback = EarlyStopping(monitor='val_acc',
verbose=1,
patience=10,
mode='max',
restore_best_weights=True)
batch_size = 10
epochs = 100
target_size = (512, 512)
training_generator = DataGenerator(x_train, y_train, batch_size=batch_size, shuffle=True)
steps_per_epoch = len(training_generator)
#validation_steps = len(val_generator)
history = model.fit(training_generator,
steps_per_epoch=steps_per_epoch,
epochs=epochs,
verbose=0,
callbacks=[tqdm_callback, early_callback])
My function geneTrainNpy() put my image and mask (.bpm) in a numpy array
Image and Mask are in grayscale with shape (512,512,1)
My model :
def unet(pretrained_weights=None, input_size=(512, 512, 1)):
inputs = Input(input_size)
conv1 = Conv2D(32, (3, 3), activation='relu', padding='same')(inputs)
conv1 = Conv2D(32, (3, 3), activation='relu', padding='same')(conv1)
pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)
conv2 = Conv2D(64, (3, 3), activation='relu', padding='same')(pool1)
conv2 = Conv2D(64, (3, 3), activation='relu', padding='same')(conv2)
pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)
conv3 = Conv2D(128, (3, 3), activation='relu', padding='same')(pool2)
conv3 = Conv2D(128, (3, 3), activation='relu', padding='same')(conv3)
pool3 = MaxPooling2D(pool_size=(2, 2))(conv3)
conv4 = Conv2D(256, (3, 3), activation='relu', padding='same')(pool3)
conv4 = Conv2D(256, (3, 3), activation='relu', padding='same')(conv4)
pool4 = MaxPooling2D(pool_size=(2, 2))(conv4)
conv5 = Conv2D(512, (3, 3), activation='relu', padding='same')(pool4)
conv5 = Conv2D(512, (3, 3), activation='relu', padding='same')(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 = Conv2D(256, (3, 3), activation='relu', padding='same')(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 = Conv2D(128, (3, 3), activation='relu', padding='same')(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 = Conv2D(64, (3, 3), activation='relu', padding='same')(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 = Conv2D(32, (3, 3), activation='relu', padding='same')(conv9)
conv10 = Conv2D(1, (1, 1), activation='sigmoid')(conv9)
model = Model(inputs=[inputs], outputs=[conv10])
# plot_model(model, to_file='model.png')
model.compile(optimizer="adam", loss='binary_crossentropy', metrics=['accuracy'])
if (pretrained_weights):
model = tf.keras.models.load_model(pretrained_weights)
model.summary()
return model
And my dataLoader with keras Sequence :
import cv2
import tensorflow as tf
import os
import numpy as np
import math
from skimage.io import imread
class DataGenerator(tf.compat.v2.keras.utils.Sequence):
def __init__(self, X_data, y_data, batch_size, shuffle=True):
self.batch_size = batch_size
self.X_data = X_data
self.y_data = y_data
self.shuffle = shuffle
self.n = 0
def __len__(self):
# Return the number of batches of the dataset
return math.ceil(len(self.X_data) / self.batch_size)
def __getitem__(self, idx):
#print('getitem', idx)
batch_x = self.X_data[idx * self.batch_size:(idx + 1) *
self.batch_size]
batch_y = self.y_data[idx * self.batch_size:(idx + 1) *
self.batch_size]
#pil_img = tf.keras.preprocessing.image.array_to_img(batch_x[0])
#pil_mask = tf.keras.preprocessing.image.array_to_img(batch_y[0])
#pil_img.show()
#pil_mask.show()
print(batch_x.size)
print(type(batch_x))
print(batch_x.dtype)
print(batch_y.size)
print(type(batch_y))
print(batch_y.dtype)
return (batch_x, batch_y)
def on_epoch_end(self):
self.indexes = np.arange(len(self.X_data))
if self.shuffle:
np.random.shuffle(self.indexes)
My output of my print in getitem_
262144
<class 'numpy.ndarray'>
float64
262144
<class 'numpy.ndarray'>
float64
My error :
Training: 0%| 0/100 ETA: ?s, ?epochs/s
ETA: ?s - Epoch 1/100
W tensorflow/core/framework/op_kernel.cc:1733] INVALID_ARGUMENT: TypeError: unhashable type: 'numpy.ndarray'
Traceback (most recent call last):
File "C:\Users\441880\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\ops\script_ops.py", line 269, in __call__
return func(device, token, args)
File "C:\Users\441880\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\ops\script_ops.py", line 147, in __call__
outputs = self._call(device, args)
File "C:\Users\441880\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\ops\script_ops.py", line 154, in _call
ret = self._func(*args)
File "C:\Users\441880\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\autograph\impl\api.py", line 642, in wrapper
return func(*args, **kwargs)
File "C:\Users\441880\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\data\ops\structured_function.py", line 220, in py_function_wrapper
ret = self._func(*nested_args)
File "C:\Users\441880\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\data\ops\dataset_ops.py", line 1053, in generator_next_fn
flat_values = script_ops.numpy_function(generator_py_func,
File "C:\Users\441880\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\util\traceback_utils.py", line 153, in error_handler
raise e.with_traceback(filtered_tb) from None
File "C:\Users\441880\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\data\ops\dataset_ops.py", line 822, in get_iterator
return self._iterators[iterator_id]
TypeError: unhashable type: 'numpy.ndarray'
Traceback (most recent call last):
File "C:/Users/441880/PycharmProjects/ML_python/main.py", line 232, in <module>
history = model.fit(training_generator,
File "C:\Users\441880\AppData\Local\Programs\Python\Python38\lib\site-packages\keras\utils\traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "C:\Users\441880\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\framework\ops.py", line 7186, in raise_from_not_ok_status
raise core._status_to_exception(e) from None # pylint: disable=protected-access
tensorflow.python.framework.errors_impl.InvalidArgumentError: TypeError: unhashable type: 'numpy.ndarray'
Traceback (most recent call last):
File "C:\Users\441880\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\ops\script_ops.py", line 269, in __call__
return func(device, token, args)
File "C:\Users\441880\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\ops\script_ops.py", line 147, in __call__
outputs = self._call(device, args)
File "C:\Users\441880\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\ops\script_ops.py", line 154, in _call
ret = self._func(*args)
File "C:\Users\441880\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\autograph\impl\api.py", line 642, in wrapper
return func(*args, **kwargs)
File "C:\Users\441880\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\data\ops\structured_function.py", line 220, in py_function_wrapper
ret = self._func(*nested_args)
File "C:\Users\441880\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\data\ops\dataset_ops.py", line 1053, in generator_next_fn
flat_values = script_ops.numpy_function(generator_py_func,
File "C:\Users\441880\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\util\traceback_utils.py", line 153, in error_handler
raise e.with_traceback(filtered_tb) from None
File "C:\Users\441880\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\data\ops\dataset_ops.py", line 822, in get_iterator
return self._iterators[iterator_id]
TypeError: unhashable type: 'numpy.ndarray'
[[{{node EagerPyFunc}}]] [Op:IteratorGetNext]
W tensorflow/core/kernels/data/generator_dataset_op.cc:107] Error occurred when finalizing GeneratorDataset iterator: FAILED_PRECONDITION: Python interpreter state is not initialized. The process may be terminated.
[[{{node EagerPyFunc}}]]
I dont have solution, I search on google "unhashable type: 'numpy.ndarray'" but solution doesn't fit with my problem.
I'm expecting to launch my model fit
This might have been caused due to GPU memory. If a TensorFlow operation has both CPU and GPU implementations, by default, the GPU device is prioritized when the operation is assigned.
For example, tf.matmul has both CPU and GPU kernels and on a system with devices CPU:0 and GPU:0, the GPU:0 device is selected to run tf.matmul. By default, Tensorflow allocates the full amount of available GPU memory when it is launched.
Adding the following code before your program, after importing dependencies will allocate only as much GPU memory as needed for the runtime allocations: it starts out allocating very little memory, and as the program gets run and more GPU memory is needed the GPU memory is extended for the TensorFlow process.
physical_devices = tf.config.list_physical_devices('GPU')
try:
tf.config.experimental.set_memory_growth(physical_devices[0], True)
except:
# Invalid device or cannot modify virtual devices once initialized.
pass

logits and labels must have the same first dimension, got logits shape [327680,7] and labels shape [983040]

I am trying to perform semantic segmentation by using the U-Net architecture.
When I fit the model:
history = model.fit(imgs_train,
masks_train,
batch_size= 5,
epochs = 5)
I keep getting the error below.
imgs_train.shape: (1500, 256, 256, 3)
masks_train.shape: (1500, 256, 256, 3)
# Building Unet using encoder and decoder blocks
from keras.models import Model
from keras.layers import Input, Conv2D, MaxPooling2D, concatenate, Conv2DTranspose,
BatchNormalization, Dropout, Lambda
from keras.layers import Activation, MaxPool2D, Concatenate
def conv_block(input, num_filters=64):
# first conv layer
x = Conv2D(num_filters, kernel_size = (3,3), padding='same')(input)
x = BatchNormalization()(x)
x = Activation('relu')(x)
# second conv layer
x = Conv2D(num_filters, kernel_size= (3,3), padding='same')(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
return x
def encoder_block(input, num_filters=64):
# conv block
x = conv_block(input,num_filters)
# maxpooling
p = MaxPool2D(strides = (2,2))(x)
p = Dropout(0.4)(p)
return x,p
def decoder_block(input, skip_features, num_filters=64):
x = Conv2DTranspose(num_filters, (2,2), strides=2, padding='same')(input)
x = Concatenate()([x, skip_features])
x = conv_block(x, num_filters)
return x
num_classes=7
def unet_architect(input_shape=(256,256,3)):
""" Input Layer """
inputs = Input(input_shape)
""" Encoder """
s1,p1 = encoder_block(inputs, 64)
s2,p2 = encoder_block(p1,128)
s3,p3 = encoder_block(p2, 256)
s4,p4 = encoder_block(p3, 512)
""" Bridge """
b1 = conv_block(p4,1024)
""" Decoder """
d1 = decoder_block(b1, s4, 512)
d2 = decoder_block(d1, s3, 256)
d3 = decoder_block(d2, s2, 128)
d4 = decoder_block(d3, s1, 64)
""" Output Layer """
outputs = Conv2D(num_classes, (1,1), padding='same', activation = 'softmax')(d4)
model = Model(inputs, outputs, name='U-Net')
return model
model = unet_architect()
model.compile(optimizer = 'adam' ,
loss = 'sparse_categorical_crossentropy',
metrics=['accuracy'])
I tried to change sparse_categorical_crossentropy to categorical_cross_entropy, another error shows up.
And when I change the batch size while fitting the model, the logits.shape and labels.shape changes accordingly.
ERROR
Epoch 1/5
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-35-4c0704d8f65d> in <module>()
2 masks_train,
3 batch_size= 10,
----> 4 epochs = 5)
1 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in autograph_handler(*args, **kwargs)
1145 except Exception as e: # pylint:disable=broad-except
1146 if hasattr(e, "ag_error_metadata"):
-> 1147 raise e.ag_error_metadata.to_exception(e)
1148 else:
1149 raise
ValueError: in user code:
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1021, in train_function *
return step_function(self, iterator)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1010, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1000, in run_step **
outputs = model.train_step(data)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 860, in train_step
loss = self.compute_loss(x, y, y_pred, sample_weight)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 919, in compute_loss
y, y_pred, sample_weight, regularization_losses=self.losses)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/compile_utils.py", line 201, in __call__
loss_value = loss_obj(y_t, y_p, sample_weight=sw)
File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 141, in __call__
losses = call_fn(y_true, y_pred)
File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 245, in call **
return ag_fn(y_true, y_pred, **self._fn_kwargs)
File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 1863, in sparse_categorical_crossentropy
y_true, y_pred, from_logits=from_logits, axis=axis)
File "/usr/local/lib/python3.7/dist-packages/keras/backend.py", line 5203, in sparse_categorical_crossentropy
labels=target, logits=output)
ValueError: `labels.shape` must equal `logits.shape` except for the last dimension. Received: labels.shape=(1966080,) and logits.shape=(655360, 7)
NOTEBOOK LINK:https://github.com/Tamimi123600/Deep-Learning/blob/main/Image_Segmentation1.ipynb
Thanks in advance
Why are your mask image (GT targets) of shape 1500, 256, 256, 3, and not 1500, 256, 256? You have num_classes=7 so your GT images should have a single channel with values {0...6} representing the class of each pixel.
Please check how you load and process your target images -- the issue is there.

2CNN in Keras: Shape mismatch

I'm trying to construct a 2D CNN neural network, looking at this code: https://kgptalkie.com/human-activity-recognition-using-accelerometer-data/. I have four arrtibutes and nine classes
X_train[0].shape, X_test[0].shape
((200, 4), (200, 4))
X_train = X_train.reshape(3104, 200, 4, 1)
X_test = X_test.reshape(776, 200, 4, 1)
X_train[0].shape, X_test[0].shape
((200, 4, 1), (200, 4, 1))
model = Sequential()
model.add(Conv2D(16, (2, 2), activation = 'relu', input_shape = X_train[0].shape))
model.add(Dropout(0.1))
model.add(Conv2D(32, (2, 2), activation='relu'))
model.add(Dropout(0.2))
model.add(Flatten())
model.add(Dense(64, activation = 'relu'))
model.add(Dropout(0.5))
model.add(Dense(9, activation='softmax'))
model.compile(optimizer=Adam(learning_rate = 0.001), loss = 'sparse_categorical_crossentropy', metrics = ['accuracy'])
history = model.fit(X_train, y_train, epochs = 10, validation_data= (X_test, y_test), verbose=1)
I'm finding this error:
ValueError Traceback (most recent call last)
<ipython-input-42-d7e8ba9ba93b> in <module>()
1 model.compile(optimizer=Adam(learning_rate = 0.001), loss = 'sparse_categorical_crossentropy', metrics = ['accuracy'])
----> 2 history = model.fit(X_train, y_train, epochs = 10, validation_data= (X_test, y_test), verbose=1)
10 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs)
971 except Exception as e: # pylint:disable=broad-except
972 if hasattr(e, "ag_error_metadata"):
--> 973 raise e.ag_error_metadata.to_exception(e)
974 else:
975 raise
ValueError: in user code:
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:806 train_function *
return step_function(self, iterator)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:796 step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:1211 run
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:2585 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:2945 _call_for_each_replica
return fn(*args, **kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:789 run_step **
outputs = model.train_step(data)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:749 train_step
y, y_pred, sample_weight, regularization_losses=self.losses)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/compile_utils.py:204 __call__
loss_value = loss_obj(y_t, y_p, sample_weight=sw)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/losses.py:149 __call__
losses = ag_call(y_true, y_pred)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/losses.py:253 call **
return ag_fn(y_true, y_pred, **self._fn_kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/dispatch.py:201 wrapper
return target(*args, **kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/losses.py:1567 sparse_categorical_crossentropy
y_true, y_pred, from_logits=from_logits, axis=axis)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/dispatch.py:201 wrapper
return target(*args, **kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/backend.py:4783 sparse_categorical_crossentropy
labels=target, logits=output)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/dispatch.py:201 wrapper
return target(*args, **kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/nn_ops.py:4176 sparse_softmax_cross_entropy_with_logits_v2
labels=labels, logits=logits, name=name)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/dispatch.py:201 wrapper
return target(*args, **kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/nn_ops.py:4091 sparse_softmax_cross_entropy_with_logits
logits.get_shape()))
ValueError: Shape mismatch: The shape of labels (received (288,)) should equal the shape of logits except for the last dimension (received (32, 6)).
Could you help me please?
I resolved the problem changing the loss mode from 'sparse_categorical_crossentropy' to loss=tf.keras.losses.KLDivergence()

Categories

Resources