Hello there everyone!
for the exercise Classifying Galaxies I’m getting a terrible accuracy score, I don’t know why! On the exercise, it’s mentioned that, following the steps, we were supposed to have a pretty good score:
Your accuracy tells you that your model assigns the highest probability to the correct class more than 60% of the time. For a classification task with over four classes, this is no small feat: a random baseline model would achieve only ~25% accuracy on the dataset. Your AUC tells you that for a random galaxy, there is more than an 80% chance your model would assign a higher probability to a true class than to a false one.
But it’s definitely not the case. The training data has a 0.32 and validation data 0.3. Yickes. .summary() prints the following:
categorical_accuracy: 0.3214 - val_loss: 1.3868 - val_categorical_accuracy: 0.3000
Model: “sequential”
Layer (type) Output Shape Param #
=================================================================
conv2d (Conv2D) (None, 63, 63, 8) 224
max_pooling2d (MaxPooling2D) (None, 31, 31, 8) 0
conv2d_1 (Conv2D) (None, 15, 15, 8) 584
max_pooling2d_1 (MaxPooling2 (None, 7, 7, 8) 0
flatten (Flatten) (None, 392) 0
dense (Dense) (None, 16) 6288
dense_1 (Dense) (None, 4) 68
Total params: 7,164
Trainable params: 7,164
Non-trainable params: 0
I’ll try to adjust things, but I’m wondering if I’ve done something wrong. Here’s the code!
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import numpy as np
from sklearn.model_selection import train_test_split
from utils import load_galaxy_data
import app
input_data, labels = load_galaxy_data()
batch_size = 5
print(input_data.shape)
print(labels.shape)
generator = ImageDataGenerator(rescale=1./128)
x_train, x_test, y_train, y_test = train_test_split(input_data, labels, test_size = 0.2, shuffle=True, random_state=222, stratify=labels)
training_iterator = generator.flow(x_train, y_train, batch_size=5)
validation_iterator = generator.flow(x_test, y_test, batch_size=batch_size)
model = tf.keras.Sequential([
tf.keras.layers.Input(shape=(128,128,3),),
tf.keras.layers.Conv2D(8, 3, strides=2),
tf.keras.layers.MaxPooling2D(pool_size=(2,2), strides=(2,2)),
tf.keras.layers.Conv2D(8, 3, strides=2),
tf.keras.layers.MaxPooling2D(pool_size=(2,2), strides=(2,2)),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(16, activation='relu'),
tf.keras.layers.Dense(4, activation='softmax')
])
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001), loss='CategoricalCrossentropy', metrics = ['CategoricalAccuracy'])
model.fit(x_train, y_train, steps_per_epoch = len(x_train)/batch_size, epochs = 8, validation_data = validation_iterator, validation_steps = len(validation_iterator)/batch_size)
print(model.summary())