btwardow / tf-face-recognition

Ral-Time Tensor Flow Face Detection and Recognition
MIT License
68 stars 20 forks source link

saving models and load #5

Closed yacaeh closed 6 years ago

yacaeh commented 6 years ago

Hi, Thanks for the great project! I'm now struggling with saving the trained model. It seems it can't update the model. I tried saving model from classifier.py predict using tf.train.write_graph(tf.get_default_graph(), '.', 'faceWeight.pb',as_text=False) And it seems doesn't work when loaded from embedded.py Any idea how to save trained data and load?

btwardow commented 6 years ago

Hi @yacaeh,

can you explain what exactly you are trying to achieve? Is it saving the classification model - for person detection? If so, this is simple kNN algorithm. You can save the examples in file and when application started load them - the most straightforward way. Different approach would be to save the whole classifier model. Here use joblib.dump & joblib.load.

Just let me know if this was it, or you trying to do something diffrent.

yacaeh commented 6 years ago

@btwardow thanks for the quick reply. What confused me was pretrained model that loads from embedded.py I thought I should update that file to save those trained data. This is pre-trained MTCNN for detecting faces only right? So classfier should be saved in model? I'm eventually trying to add new faces and load it from DB. So I changed SAVE_DETECT_FILES = True & SAVE_TRAIN_FILES = True . But it only saves json files and image and can't load again after restart the server. So I guess I should just dump the whole model and load? using joblib? I tried in classifier after calling model.update_training like this

model.update_training(X, y)
 joblib.dump(model,'./knn_test.model')

And it shows errors like this _pickle.PicklingError: Can't pickle <class 'module'>: it's not found as builtins.module

If I saved it then where should I load the model? When init model from init in classifier? It seems when detecting in server.py, load recognizefrom recognition_sklearn and load model from there

yacaeh commented 6 years ago

I decided to load images when the server start and train them before running it. Works fine I guess

def initTrainingSet():
    train_files = [f for f in os.listdir(TRAIN_EXAMPLES_DIR) if f.endswith('.png')]
    print(train_files)
    for f in  train_files:
        name, size, num = f.split(".")[0].split("_")[1:]
        img = Image.open(os.path.join(TRAIN_EXAMPLES_DIR, f))
        learn_from_examples(name, img, int(num), int(size))
    print("train complete!")

initTrainingSet()