How would I increase my accuracy in the cifar-100 dataset? I have a 10% accuracy at the moment

Viewed 5484

I am doing a small project for fun with the cifar-100 dataset. I'm not sure why I have a low 10% accuracy. Here is my code, could anyone help? thanks all (fyi, cifar-100 dataset is a keras dataset with 100 types of images.)

import tensorflow as tf
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt

#print(train_images[0])
#print("Network Accuracy: " + str(test_acc))
# plt.imshow(train_images[0], cmap=plt.cm.binary) #greyscale
# plt.imshow(train_images[0]) #neon
# plt.show()

cifar100_mnist = keras.datasets.cifar100

(train_images, train_labels), (test_images, test_labels) = cifar100_mnist.load_data()
print(train_images)
print("-")
print(train_labels)
train_images = train_images/255
test_images = test_images/255
classes = [
    'apple', 'aquarium_fish', 'baby', 'bear', 'beaver', 'bed', 'bee', 'beetle',
    'bicycle', 'bottle', 'bowl', 'boy', 'bridge', 'bus', 'butterfly', 'camel',
    'can', 'castle', 'caterpillar', 'cattle', 'chair', 'chimpanzee', 'clock',
    'cloud', 'cockroach', 'couch', 'crab', 'crocodile', 'cup', 'dinosaur',
    'dolphin', 'elephant', 'flatfish', 'forest', 'fox', 'girl', 'hamster',
    'house', 'kangaroo', 'keyboard', 'lamp', 'lawn_mower', 'leopard', 'lion',
    'lizard', 'lobster', 'man', 'maple_tree', 'motorcycle', 'mountain', 'mouse',
    'mushroom', 'oak_tree', 'orange', 'orchid', 'otter', 'palm_tree', 'pear',
    'pickup_truck', 'pine_tree', 'plain', 'plate', 'poppy', 'porcupine',
    'possum', 'rabbit', 'raccoon', 'ray', 'road', 'rocket', 'rose',
    'sea', 'seal', 'shark', 'shrew', 'skunk', 'skyscraper', 'snail', 'snake',
    'spider', 'squirrel', 'streetcar', 'sunflower', 'sweet_pepper', 'table',
    'tank', 'telephone', 'television', 'tiger', 'tractor', 'train', 'trout',
    'tulip', 'turtle', 'wardrobe', 'whale', 'willow_tree', 'wolf', 'woman',
    'worm'
]
model = keras.Sequential([
    keras.layers.Flatten(input_shape=(32, 32, 3)),
    keras.layers.Dense(500, activation="relu"),
    keras.layers.Dense(100, activation="softmax")
])

model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"])
model.fit(train_images, train_labels, epochs=5)

test_loss, test_acc = model.evaluate(test_images, test_labels)
print(test_acc)
# print(test_images)
prediction = model.predict(test_images)
answer = np.argmax(prediction[0])
print(classes[answer])
# print(train_images[0])
plt.imshow(train_images[0])
plt.show()

Hope that I can get an answer, thanks guys, I really appreciate the help.

3 Answers

An example of Implementing Convolutions regarding your task:

model = tf.keras.models.Sequential([
    tf.keras.layers.Conv2D(64, (3,3), activation='relu', input_shape=(32, 32, 3)),
    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(100, activation='softmax')
])

model.summary()

Try more epochs (e.g. 25):

model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])

history = model.fit(train_images, train_labels, epochs=25)

You gain high Accuracy on the Trainingset:

Epoch 25/25 50000/50000 [==============================] - 12s 248us/sample - loss: 0.2362 - acc: 0.9243

Let's have a look on the Test Accuracy:

test_loss, test_acc = model.evaluate(test_images, test_labels)
print(test_acc)
prediction = model.predict(test_images)

10000/10000 [==============================] - 1s 139us/sample - loss: 7.7701 - acc: 0.3272 0.3272

Which means your model overfits the trainingset.

Try it yourself:

image_number = 5
answer = np.argmax(prediction[image_number])
print(classes[answer])
plt.imshow(train_images[image_number])

lizard

obviously no lizard :)

obviously no lizard :)

Here begins the Task of Machine Learning. To improve your model to reduce bias and variance and gain high accuracy. I am still learning too, so i hope this points you in the right direction.

But be aware of this dataset. You can read on the CIFAR-100 Page:

There are 100 classes containing 600 images each. There are 500 training images and 100 testing images per class.

500 images is way too less to train a CNN and this will lead to overfitting. I think the CIFAR 10 is more likely for beginners? They mention on the same Page:

[...] 18% test error without data augmentation [...]

Which would be much more fun to experiment with :)

You mentioned in the comments that you want to see an example, here is one in Github.

What needs to be declared is that I did not personally test this code repository.

I think it is your model that is wrong. Try doing conv2D, then maxpooling several times. As for the second last dense layer, its presence in your code only depends on the number of epochs you are doing. Analyze your model with tensorboard. Also, when you are dividing the train_images by 255, best use 255.0. I do not think there are any other issues with your code. However, I would recommend using the same order of classes as given on the cifar100 website

Related