Skip to content
Advertisement

Why is my model giving poor accuracy when the data is loaded using tf.data?

I am new to the tf.data API and trying to use it to load images from disk in the Dogs vs. Cats Redux: Kernels Edition Kaggle competition. To do this, I first created a pandas DataFrame named train_df with two columns – file_path containing the relative path of images and target containing the target labels 0 (for cat) and 1(for dog). Here’s how the first 10 rows of the DataFrame looks like:

enter image description here

Then, I tried loading the images with the following code:

JavaScript

After this, I tried training my model using the following code

JavaScript

but got a training accuracy of only 50% whereas with ImageDataGenerator, I am getting an accuracy of 99%. Thus, the problem lies somewhere in the data loading part which I am not able find out.

I have used EfficientNetB0 with weights trained from imagenet as feature extractor and single neuron layer at the end as classifier.

Pretrained EfficientNetB0 model:

JavaScript

Dense layer with one neuron at the end of the EfficientNetB0:

JavaScript

Compiling the model:

JavaScript

Advertisement

Answer

In the above notebook, change the input reading function read_images as follows:

JavaScript

Also note that, tf.keras.applications.EfficientNet-Bx has in-built normalization layer. So, it’s better not to normalize the data in the above function (i.e. /255.0).

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement