Achmad Setiawan
Achmad Setiawan

Reputation: 179

How to combine number data and image data for training in tensorflow?

so, for example, I have this code:

import numpy as np
import cv2 as cv
import tensorflow as tf

data = [2.78,1.78]
y = [1]
img = cv.imread("path/to/image")
data.append(img)
data_array = np.array(data)
model = tf.keras.models.Sequential([
    tf.keras.layers.Dense(128, input_shape=(3,), activation='relu'),
    tf.keras.layers.Dense(64, activation='relu'),
    tf.keras.layers.Dense(32, activation='relu'),
    tf.keras.layers.Dense(3, activation='softmax')
    ])

model.compile(loss='sparse_categoricalcrossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(data_array, y, epochs=100)

When I try to run this code, it raise this error

ValueError: Failed to find data adapter that can handle input: <class 'numpy.ndarray'>, (<class 'list'> containing values of types {"<class 'int'>"})

Is there anyway to combine those two data (data and img) for training?

Upvotes: 1

Views: 549

Answers (1)

Albert
Albert

Reputation: 550

The data going into the neural network must have a homogeneous format; you can't feed it an integer sometimes, and then an image. The shape of the data going into the network must remain the same. Here you put the shape as (3,), which is incorrect―that's simply the length of the array you're passing. The shape argument refers instead to the dimension of an individual data point, e.g., a 100x100 RGB image could have shape (100,100,3).

So, the simplest way to combine image and integer data would be to append an integer to a flat array representing an image. This would probably not work very well, but would at least compile.

A better approach is to architect this in such a way that the integer doesn't get drowned out by raw image data. Perhaps this means running the images through a convnet to get a feature vector, appending the corresponding integer to it, then feeding that to another net. But this would depend on the specifics of what you're trying to do.

Upvotes: 1

Related Questions