Reputation: 73
I'm pretty much new to Machine learning, i have this CNN Model to classify 7 instruments: {'bassoon': 0, 'erhu': 1, 'flute': 2, 'frenchhorn': 3, 'guitar': 4, 'saxophone': 5, 'violin': 6}
there is 1214 image for training and 1206 images for testing
this is my model:
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
normalizedTrainingSet = ImageDataGenerator(rescale=1 / 255)
normalizedTestingSet = ImageDataGenerator(rescale=1 / 255)
trainingClass = normalizedTrainingSet.flow_from_directory("DataSet/Training",
target_size=(100, 100),
batch_size=32,
class_mode="categorical",
shuffle=True)
testingClass = normalizedTrainingSet.flow_from_directory("DataSet/Testing",
target_size=(100, 100),
batch_size=32,
class_mode="categorical",
shuffle=True)
print(trainingClass.class_indices)
print(testingClass.class_indices)
model = tf.keras.models.Sequential \
([
tf.keras.layers.Conv2D(200, (3, 3), activation="softmax", input_shape=(100, 100, 3)),
tf.keras.layers.MaxPool2D(2, 2),
tf.keras.layers.Conv2D(200, (3, 3), activation="softmax", input_shape=(100, 100, 3)),
tf.keras.layers.MaxPool2D(2, 2),
tf.keras.layers.Conv2D(200, (3, 3), activation="softmax", input_shape=(100, 100, 3)),
tf.keras.layers.MaxPool2D(2, 2),
tf.keras.layers.Conv2D(200, (3, 3), activation="softmax", input_shape=(100, 100, 3)),
tf.keras.layers.MaxPool2D(2, 2),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(142, activation="softmax"),
tf.keras.layers.Dense(1, activation="sigmoid")
])
model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=['accuracy'])
fittedModel = model.fit(trainingClass, epochs=1, batch_size=100, validation_data=testingClass, shuffle=True)
model.save('newModel')
I want to add epochs later on, as the model will take too much time to train!
Thanks for helping!!
Update: I removed the batch size and I got a whooping 85%, but the predictions are wrong, if I tell the model to predict on an image that is in the training dataset, it gets it super wrong...
Update2: I change softmax, to relu, my accuracy is 15% now...
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
normalizedTrainingSet = ImageDataGenerator(rescale=1 / 255)
normalizedTestingSet = ImageDataGenerator(rescale=1 / 255)
trainingClass = normalizedTrainingSet.flow_from_directory("DataSet/Training",
target_size=(100, 100),
class_mode="categorical",
shuffle=True)
testingClass = normalizedTrainingSet.flow_from_directory("DataSet/Testing",
target_size=(100, 100),
class_mode="categorical",
shuffle=True)
print(trainingClass.class_indices)
print(testingClass.class_indices)
model = tf.keras.models.Sequential \
([
tf.keras.layers.Conv2D(200, (3, 3), activation="relu", input_shape=(100, 100, 3)),
tf.keras.layers.MaxPool2D(2, 2),
tf.keras.layers.Conv2D(200, (3, 3), activation="relu", input_shape=(100, 100, 3)),
tf.keras.layers.MaxPool2D(2, 2),
tf.keras.layers.Conv2D(200, (3, 3), activation="relu", input_shape=(100, 100, 3)),
tf.keras.layers.MaxPool2D(2, 2),
tf.keras.layers.Conv2D(200, (3, 3), activation="relu", input_shape=(100, 100, 3)),
tf.keras.layers.MaxPool2D(2, 2),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(142, activation="softmax"),
tf.keras.layers.Dense(7, activation="sigmoid")
])
model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=['accuracy'])
fittedModel = model.fit(trainingClass, epochs=1, validation_data=testingClass, shuffle=True)
model.save('newModel')
Upvotes: 1
Views: 246
Reputation: 8092
Your final classification layer should have 7 neurons. The activation for the layer should be softmax not sigmoid. Remove input_shape=(100, 100, 3) from all layers except the first layer. Change activation to relu in the dense layer with 142 neurons. For the testing_Class set shuffle=False in flow_from_directory. You have a small number of training samples. I would recommend you use image augmentation in the ImageDataGenerator for the training_Class. For example set horizontal_flip=True. Documentation on adding augmentation is here.
Upvotes: 1