Home>

I would like to classify 3 types of images using Python.
200 images were prepared and padded (18 images from one image data) to create a total data set of 10800 images.
This is ("./photo-min.npz").

We prepared 200 images for each image and created a total of 600 datasets without padding.
This is ("./photo-test.npz").

The results of machine learning are shown in the graph below.

I would like to know why the accuracy and loss in test are not ideal graphs like train.

Applicable source code
from keras.models import Sequential
from keras.layers import Activation, Dense, Flatten
import numpy as np
from keras.preprocessing.image import ImageDataGenerator
classes = 3
data_size = 75 * 75 * 3
def main ():
    data = np.load ("./ photo-min.npz") # 10800 data sets
    x = data ["x"]
    y = data ["y"]
    data = np.load ("./ photo-test.npz") # 600 data sets
    x_test = data ["x"]
    y_test = data ["y"]
    x = np.reshape (x, (-1, data_size))
    x_test = np.reshape (x_test, (-1, data_size))
    model = train (x, y)
    model_eval (model, x_test, y_test)
def train (x, y):
    model = Sequential ()
    model.add (Dense (units = 128, input_dim = (data_size)))
    model.add (Activation ('relu'))
    model.add (Dense (units = classes))
    model.add (Activation ('softmax'))
    model.compile (loss = 'sparse_categorical_crossentropy',
        optimizer = 'sgd', metrics = ['accuracy'])
    model.fit (x, y, epochs = 30)
    json_string = model.model.to_json ()
    open ('./ men_predict.json', 'w'). write (json_string)
    hdf5_file = "./men_predict.hdf5"
    model.model.save_weights (hdf5_file)
    return model
def model_eval (model, x, y):
    score = model.evaluate (x, y)
    print ('loss =', score [0])
    print ('accuracy =', score [1])
if __name__ == "__main__":
    main ()

The number of units was trial and error, but the accuracy of the test did not increase.

  • Answer # 1

    If you simply flatten an image of

    75 * 75 * 3and identify it with a simple multilayer perceptron that is not a CNN, I think that's the case. I wonder if the model does not capture the features well.

Related articles