简体   繁体   中英

"ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type numpy.ndarray). In TensorFlow CNN for image classification

I've been working on a CNN for image classification and I keep getting the same error, my data is being loaded into a dataframe and I can't convert it to a tensor to feed it into the CNN. As you can see I used this code to load the pictures into a dataframe:


for i in range(len(merged)):
    full_path = merged.iloc[i]['Image Path Rel']
    filename = full_path[-22:-1] + 'G'
    try:
        img = img_to_array(load_img('D:/Serengeti_Data/Compressed/Compressed/' + filename, target_size=(32,32,3)))
    except:
        img = np.zeros((32,32,3), dtype=np.float32)
        images = images.append({'Capture Id' : merged.iloc[i]['Capture Id'],'Image' : img}, ignore_index = True)
    else:
        images = images.append({'Capture Id' : merged.iloc[i]['Capture Id'],'Image' : img}, ignore_index = True)

Then once I had the images loaded by using load_img() and img_to_array() I did a reshape to get the desired shape of (32,32,3). Also normalized the values by dividing the Image column by 255.

Then I'm doing this to try to get it into a tensor:

train_tf = tf.data.Dataset.from_tensor_slices(images['Image'])

# Also tried this, but didn't got the same results:
# train_tf = tf.convert_to_tensor(train_df['Image'])

But keep getting the error:

ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type numpy.ndarray)

I also tried skipping that and tried to fit to our model right away, but got the same exact error:

trying_df = pd.DataFrame(images['Image'])
target_df = pd.DataFrame(targets)

animal_model = models.Sequential()

animal_model.add(layers.Conv2D(30, kernel_size = (3,3), padding = 'valid', activation = 'relu', input_shape =(32,32,3)))
animal_model.add(layers.MaxPooling2D(pool_size=(1,1)))

animal_model.add(layers.Conv2D(60,kernel_size=(1,1),activation = 'relu'))

animal_model.add(layers.Flatten())

animal_model.add(layers.Dense(100, activation = 'relu'))
animal_model.add(layers.Dense(10, activation = 'softmax'))

## compiler to model
animal_model.compile(loss = 'categorical_crossentropy', metrics = ['accuracy'], optimizer ='adam')

## training the model
animal_model.fit(trying_df,target_df, batch_size = 128, epochs = 15)

animal_model.summary() 

TensorFlow Version: 2.4.1

Numpy Version: 1.19.5

Pandas Version: 1.0.1

In order to load an image, you can use the following code:

image = cv2.imread(filename)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

In order to resize and scale the image, it's better to let the model "inglobe" the pre processing functionality.

IMG_SIZE = 180

resize_and_rescale = tf.keras.Sequential([
  layers.experimental.preprocessing.Resizing(IMG_SIZE, IMG_SIZE),
  layers.experimental.preprocessing.Rescaling(1./255)
])
model = tf.keras.Sequential(
    [
        resize_and_rescale,
        layers.Conv2D(32, 3, activation="relu"),
        layers.MaxPooling2D(),
        layers.Conv2D(64, 3, activation="relu"),
        layers.MaxPooling2D(),
        layers.Conv2D(128, 3, activation="relu"),
        layers.MaxPooling2D(),
        layers.Flatten(),
        layers.Dense(128, activation="relu"),
        layers.Dense(len(class_names), activation="softmax"),
    ]
)
model.compile(
    optimizer="adam",
    loss=tf.losses.SparseCategoricalCrossentropy(from_logits=True),
    metrics=["accuracy"],
)

NOTE:
Use tf.Data instead of numpy array when dealing with image. You can use the following code as an example:
https://github.com/alessiosavi/tensorflow-face-recognition/blob/90d4acbea8f79539826b50c82a63a7c151441a1a/dense_embedding.py#L155

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