David
David

Reputation: 69

Incorrect data input using Tensorflow 2.0 TFDS dataset

I am trying to use the 'cats_vs_dogs' dataset from tensorflow_datasets for binary classification but unfortunately, I am not able to use the dataset in my model because the input feeded by the dataset iterator doesn't seem to match what my model expects.

import tensorflow_datasets as tfds
import tensorflow as tf
from keras_preprocessing import image
from keras_preprocessing.image import ImageDataGenerator
import matplotlib.pyplot as plt
import numpy as np

dataset_name = 'cats_vs_dogs'
dataset, info = tfds.load(name=dataset_name, split=tfds.Split.TRAIN, with_info=True)

def preprocess(features):
    image, label = features['image'], features['label']
    image = tf.image.resize(image, [224, 224])
    features['image']= image
    label = tf.one_hot(label,2,axis=-1)
    features['label']=label
    return features

#pre-processing the dataset to fit a specific image size and 2D labelling
train_dataset = dataset.map(preprocess).batch(32)

model = tf.keras.models.Sequential([
        tf.keras.layers.Conv2D(16, (5, 5),input_shape=(224, 224, 3),activation='relu'),
        tf.keras.layers.MaxPooling2D(2, 2),
        tf.keras.layers.Conv2D(32, (3, 3), activation='relu'),
        tf.keras.layers.MaxPooling2D(2, 2),
        tf.keras.layers.Conv2D(64, (3, 3), activation='relu'),
        tf.keras.layers.MaxPooling2D(2, 2),
        tf.keras.layers.Conv2D(64, (3, 3), activation='relu'),
        tf.keras.layers.MaxPooling2D(2, 2),
        tf.keras.layers.Flatten(),
        tf.keras.layers.Dense(512, activation='relu'),
        tf.keras.layers.Dense(2, activation='softmax')
    ])

model.compile(optimizer='rmsprop',
                  loss=tf.keras.losses.categorical_crossentropy,
                  metrics=['accuracy'])

model.fit(dataset, epochs=15,verbose=1)

When I run the code I get the following error :

    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/input_spec.py:158 assert_input_compatibility
        ' input tensors. Inputs received: ' + str(inputs))

    ValueError: Layer sequential_4 expects 1 inputs, but it received 3 input tensors. Inputs received: [<tf.Tensor 'IteratorGetNext:0' shape=(None, None, 3) dtype=uint8>, <tf.Tensor 'IteratorGetNext:1' shape=() dtype=string>, <tf.Tensor 'IteratorGetNext:2' shape=() dtype=int64>]

I don't understand why the model receives 3 input tensors (the two last ones seem to be empty??) Any help would be very much appreciated !

Upvotes: 0

Views: 366

Answers (1)

ashraful16
ashraful16

Reputation: 2782

That is your data generator problem. You can try with this.

import tensorflow_datasets as tfds
import tensorflow as tf
from keras_preprocessing import image
from keras_preprocessing.image import ImageDataGenerator
import matplotlib.pyplot as plt
import numpy as np

dataset_name = 'cats_vs_dogs'
dataset, info = tfds.load(name=dataset_name, split=tfds.Split.TRAIN, with_info=True)


def preprocess(features):
    print(features['image'], features['label'])
    image = tf.image.resize(features['image'], [224,224])
    image = tf.divide(image, 255)
    print(image)
    label = features['label']
    label = tf.one_hot(label,2,axis=-1)
    print(label)
    return image, tf.cast(label, tf.float32)

#pre-processing the dataset to fit a specific image size and 2D labelling
train_dataset = dataset.map(preprocess).batch(32)

model = tf.keras.models.Sequential([
        tf.keras.layers.Conv2D(16, (5, 5),input_shape=(224, 224,3),activation='relu'),
        tf.keras.layers.MaxPooling2D(2, 2),
        tf.keras.layers.Conv2D(32, (3, 3), activation='relu'),
        tf.keras.layers.MaxPooling2D(2, 2),
        tf.keras.layers.Conv2D(64, (3, 3), activation='relu'),
        tf.keras.layers.MaxPooling2D(2, 2),
        tf.keras.layers.Conv2D(64, (3, 3), activation='relu'),
        tf.keras.layers.MaxPooling2D(2, 2),
        tf.keras.layers.Flatten(),
        tf.keras.layers.Dense(512, activation='relu'),
        tf.keras.layers.Dense(2, activation='softmax')
    ])

model.compile(optimizer='rmsprop',
                  loss=tf.keras.losses.categorical_crossentropy,
                  metrics=['accuracy'])

model.fit(train_dataset, epochs=15,verbose=1)

Upvotes: 1

Related Questions