简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM