Home > Back-end >  Training tensorflow to classify dogs/cats using VGG16, but getting very low accuracy
Training tensorflow to classify dogs/cats using VGG16, but getting very low accuracy

Time:03-05

Here is my complete code:

from tensorflow.keras.preprocessing.image import load_img
from tensorflow.keras.preprocessing.image import img_to_array
from tensorflow.keras.applications.vgg16 import preprocess_input
from tensorflow.keras.applications.vgg16 import decode_predictions
from tensorflow.keras.applications.vgg16 import VGG16
from tensorflow.keras.models import load_model
from tensorflow.keras.layers import Dense, Flatten, Dropout, Input
from tensorflow.keras.models import Model
import numpy as np
from os import listdir
from os.path import isfile, join
import matplotlib.pyplot as plt

train_cats_path = r"C:\Users\jrand\Downloads\dogscats\train\cats"
train_cats = [train_cats_path "\\" f for f in listdir(train_cats_path) if isfile(join(train_cats_path, f))]
train_cat_array = []
for tmp_cat in train_cats:
    img = load_img(tmp_cat, target_size=(224, 224)) # Load image in with desired dimensions
    img = img_to_array(img) # Convert the image to a numpy array
    img = img.reshape(224, 224, 3)
    img = preprocess_input(img)
    train_cat_array.append(img)
x_train_cat_labels = np.array([1.0]*len(train_cat_array))
x_train_cat_images = np.array(train_cat_array)
train_dogs_path = r"C:\Users\jrand\Downloads\dogscats\train\cats"
train_dogs = [train_dogs_path "\\" f for f in listdir(train_dogs_path) if isfile(join(train_dogs_path, f))]
train_dog_array = []
for tmp_dog in train_dogs:
    img = load_img(tmp_dog, target_size=(224, 224)) # Load image in with desired dimensions
    img = img_to_array(img) # Convert the image to a numpy array
    img = img.reshape(224, 224, 3)
    img = preprocess_input(img)
    train_dog_array.append(img)
x_train_dog_labels = np.array([0.0]*len(train_dog_array))
x_train_dog_images = np.array(train_dog_array)

print("len of dog images", len(x_train_dog_images), "len of cat images", len(x_train_cat_images))
print("len of dog labels", len(x_train_dog_labels), "len of cat labels", len(x_train_cat_labels))

x_train_images = np.concatenate([x_train_dog_images, x_train_cat_images])[0:1000]
x_train_labels =  np.concatenate([x_train_dog_labels, x_train_cat_labels])[0:1000]

model = VGG16(weights = 'imagenet', include_top = False, input_shape = (224, 224, 3))
for layer in model.layers:
    layer.trainable = False
x = Flatten()(model.output)
predictions = Dense(1, activation="sigmoid")(x)
new_model = Model(model.input, predictions)



# compile model
model.compile(optimizer="Adam", loss="binary_crossentropy", metrics=["accuracy"])
# train model
history = model.fit(x_train_images, x_train_labels, batch_size=1, epochs=100)

So, as you can see, I take the images of cats and dogs, generate labels for them, then I concatenate the two arrays together using numpy so that I can then train on them.

I guess my problems are as follows:

  1. I have to reduce the size of my training set by splicing the arrays to the first 1000 images, which doesn't help things
  2. I have to use a batch_size of 1, also doesn't help things

Can anyone give me some tips for improving this code and the NN performance?

Here is sample output as things currently stand:

len of dog images 11500 len of cat images 11500
len of dog labels 11500 len of cat labels 11500
2022-03-04 11:46:54.410085: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2022-03-04 11:46:55.170021: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1510] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 6613 MB memory:  -> device: 0, name: NVIDIA GeForce GTX 1080, pci bus id: 0000:02:00.0, compute capability: 6.1
2022-03-04 11:46:55.170948: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1510] Created device /job:localhost/replica:0/task:0/device:GPU:1 with 6613 MB memory:  -> device: 1, name: NVIDIA GeForce GTX 1080, pci bus id: 0000:04:00.0, compute capability: 6.1
2022-03-04 11:46:56.198632: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:185] None of the MLIR Optimization Passes are enabled (registered 2)
Epoch 1/100
2022-03-04 11:46:56.725619: I tensorflow/stream_executor/cuda/cuda_dnn.cc:369] Loaded cuDNN version 8201
1000/1000 [==============================] - 9s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 2/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 3/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 4/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 5/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 6/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 7/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 8/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 9/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 10/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 11/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 12/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 13/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 14/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 15/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 16/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 17/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 18/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 19/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 20/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 21/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 22/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 23/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 24/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 25/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 26/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 27/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 28/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 29/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 30/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 31/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 32/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 33/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 34/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 35/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 36/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 37/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 38/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 39/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 40/100

The reason I wrote this program is to get some experience with transfer learning. There may be better ways to accomplish this but this is the way I chose.

I updated the code and removed a loop I was messing with. Sorry about that.

CodePudding user response:

I don't think the issue is the small dataset, since transfer learning is used to deal with smaller datasets.

The issue is that you are freezing all the layers of the pre-trained model (VGG), without adding any new Dense Layer. Then you call model.fit, but none of the layers are trainable. Therefore, nothing is allowed to change. In fact, your problem is not that you are getting very low accuracy, but that the accuracy doesn't change at all among epochs. This should be a red flag meaning something in your code is broken!

Try to add at least another Dense layer before the last.

EDIT:

You are also compiling and calling fit() on model instead of new_model.

I hope I've been helpful

  • Related