Search code examples
pythonmachine-learningdeep-learningktrain

How to load a model on local system for testing it on data


I have trained a model on google colab and saved it using the save function. How do I load this model on my local system for testing this model on the data I have?

.....
# Trained a model and ran it for some epochs
predictor=ktrain.get_predictor(learner.model, txt)
predictor.save('Final_Model')

My question is, now I have downloaded the Final_Model folder which has a .preproc file, a config.json file and a .h5 file. How do I load this model on my local system to test it on some data stored in a csv file.(i.e. use the predict() function on this model now). I am stuck here:

import pandas as pd
predictor = # Load the model but HOW?
df = pd.read_csv('Testing_Data.csv')
responses = []
for idx,query in enumerate(df['Query']):
  resp = predictor.predict(query)
  responses.append(resp)
df['predicted'] = responses
df.to_csv('Final_Data.csv', index = False)

Solution

  • As shown in the ktrain text classification tutorial and example notebooks like this:

    import ktrain
    predictor = ktrain.load_predictor('Final_Model')
    

    Also, the predict method accepts a list of inputs. So, you probably don't need that for loop.