OlafenwaMoses / ImageAI

A python library built to empower developers to build applications and systems with self-contained Computer Vision capabilities
https://www.genxr.co/#products
MIT License
8.5k stars 2.18k forks source link

how to load two prediction model at the same time? #52

Closed LCorleone closed 5 years ago

LCorleone commented 5 years ago

hi, first excellent job you did. I trained two different prediction model, and i want to load them at the same time.

# hat classifier
hat_prediction = CustomImagePrediction()
hat_prediction.setModelTypeAsResNet()
# prediction.setModelTypeAsInceptionV3()
hat_prediction.setModelPath(os.path.join(execution_path, 'classifier', 'models', "20180724VIDEO_ex-036_acc-0.988451.h5"))
hat_prediction.setJsonPath(os.path.join(execution_path, 'classifier', 'json', "model_class.json"))
hat_prediction.loadModel(num_objects=2)

# cloth classifier
cloth_prediction = CustomImagePrediction()
cloth_prediction.setModelTypeAsResNet()
cloth_prediction.setModelPath(os.path.join(execution_path, 'cloth_classifier', 'models', "20180724VIDEO_ex-040_acc-0.994318.h5"))
cloth_prediction.setJsonPath(os.path.join(execution_path, 'cloth_classifier', 'json', "model_class.json"))
cloth_prediction.loadModel(num_objects=2)

But i found that it seems to fail as it can only load one?

LCorleone commented 5 years ago

I solved it. It can load two models at the same time. However, here the global CLASS_INDEX will overwrite two different class names and should be modified.

def decode_predictions(preds, top=5, model_json=""):
    global CLASS_INDEX

    if CLASS_INDEX is None:
        CLASS_INDEX = json.load(open(model_json))
    results = []
    for pred in preds:
        top_indices = pred.argsort()[-top:][::-1]
        for i in top_indices:
            each_result = []
            each_result.append(CLASS_INDEX[str(i)])
            each_result.append(pred[i])
            results.append(each_result)
    return results
OlafenwaMoses commented 5 years ago

@LCorleone Thank you for working on this. It is very useful to have 2 or more custom models active. I will adopt your modifications in the next version of ImageAI.

jgros commented 5 years ago

@LCorleone Can you share what you did to make it work as a workaround?

maxlamenace417 commented 5 years ago

Hello, has it been changed in the code yet?

I think a quick workaround would be: (however the model would be loaded each time)

`def decode_predictions(preds, top=5, model_json=""):

shittyVariable = json.load(open(model_json))
results = []
for pred in preds:
    top_indices = pred.argsort()[-top:][::-1]
    for i in top_indices:
        each_result = []
        each_result.append(shittyVariable [str(i)])
        each_result.append(pred[i])
        results.append(each_result)
return results`

But the main question is why is this method not directly in the CustomImagePrediction class???