0
votes

I'm constructing an image array with numpy and then trying to convert it to a tensor to fit a tensorflow model but then I get an error

Data prep

def prep_data(images):
    count = len(images)
    data = np.ndarray((count, CHANNELS, ROWS, COLS), dtype=np.uint8)
    for i, image_file in enumerate(tqdm(images)):
        image = read_image(image_file)
        data[i] = image.T
    return data

train = prep_data(train_images)
test = prep_data(test_images)

Build model

pretrained_base = hub.KerasLayer("https://tfhub.dev/google/imagenet/inception_v1/classification/5")
pretrained_base.trainable = False

model = keras.Sequential([
    tf.keras.layers.InputLayer(input_shape=(64, 64, 3)),
    pretrained_base,
    Flatten(),
    Dense(6, activation='relu'),
    Dense(1, activation='sigmoid')
])
model.build((None, 64, 64, 3))
model.summary()
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(train, labels, test_size=0.25, random_state=0)

train_dataset = tf.data.Dataset.from_tensor_slices((X_train, y_train))
test_dataset = tf.data.Dataset.from_tensor_slices((X_test, y_test))


def run_catdog():
    history = LossHistory()
    model.fit(train_dataset,
              batch_size=batch_size,
              epochs=nb_epoch,
              verbose=1,
              callbacks=[history, early_stopping])
    

    predictions = model.predict(test, verbose=0)
    return predictions, history
predictions, history = run_catdog()

WARNING:tensorflow:Model was constructed with shape (None, 64, 64, 3) for input KerasTensor(type_spec=TensorSpec(shape=(None, 64, 64, 3), dtype=tf.float32, name='input_63'), name='input_63', description="created by layer 'input_63'"), but it was called on an input with incompatible shape (None, 3, 64, 64).

Can't quite figure out how to change/convert the numpy array to TF

1

1 Answers

1
votes

You don't need to convert the NumPy array to tensor, just change the shape of your input. np.moveaxis can do the trick. It works like this:

np.moveaxis(your_array, source, destination).

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(train, labels, test_size=0.25, random_state=0)

# now reshape the train and test input data 
X_train = np.moveaxis(X_train, 0, -1)
X_test = np.moveaxis(X_test, 0, -1)

train_dataset = tf.data.Dataset.from_tensor_slices((X_train, y_train))
test_dataset = tf.data.Dataset.from_tensor_slices((X_test, y_test))



def run_catdog():
    history = LossHistory()
    model.fit(train_dataset,
              batch_size=batch_size,
              epochs=nb_epoch,
              verbose=1,
              callbacks=[history, early_stopping])
    

    predictions = model.predict(test, verbose=0)
    return predictions, history
predictions, history = run_catdog()