简体   繁体   中英

Load Tensorflow model with labels

I stored a model using model.save('model') after this tutorial:

https://towardsdatascience.com/keras-transfer-learning-for-beginners-6c9b8b7143e

The labels are taken from the directory itself. Now I would like to load it and do a prediction on an image using the following code:

import numpy as np
import tensorflow as tf
from tensorflow import keras
from keras.preprocessing import image

new_model = keras.models.load_model('./model/')

# Check its architecture
new_model.summary()

with image.load_img('testpics/mypic.jpg') as img: # , target_size=(32,32)) 
    img  = image.img_to_array(img)
    img  = img.reshape((1,) + img.shape)
    # img  = img/255
    # img = img.reshape(-1,784)
    img_class=new_model.predict(img) 
    prediction = img_class[0]
    classname = img_class[0]
    print("Class: ",classname)

Sadly the output is just

Class: [1.3706615e-03 2.9885881e-03 1.6783881e-03 3.0293325e-03 2.9168031e-03 7.2344812e-04 2.0196944e-06 2.0119224e-02 2.2996603e-04 1.1960276e-05 3.0794670e-04 6.0808496e-05 1.4892215e-05 1.5410941e-02 1.2452166e-04 8.2580920e-09 2.4049083e-02 3.1140331e-05 7.4609083e-01 1.5793210e-01 2.4283256e-03 1.5755130e-04 2.4227127e-03 2.2325735e-07 7.2101393e-06 7.6298704e-03 2.0922457e-04 1.2269774e-03 5.5882465e-06 2.4516811e-04 8.5745640e-03]

And I cannot figure out how to reload the labels... could someone help me out here:/?

这是我保存的文件的屏幕截图

The model does not contain the label names. Therefore it cannot be retrieved in this way. You have to save the labels while training and can then load and use them in the prediction phase.

I have used pickle to store the labels in a file as a serialized array. You can then load them and use the argmax of the predictions as the array index.

Here is the training phase:

CLASS_NAMES = ['ClassA', 'ClassB'] # should be dynamic
f = open('labels.pickle', "wb")
f.write(pickle.dumps(CLASS_NAMES))
f.close()

And in the prediction:

CLASS_NAMES = pickle.loads(open('labels.pickle', "rb").read())
predictions = model.predict(predict_image)
result = CLASS_NAMES[predictions.argmax(axis=1)[0]]

So you could just load the classes and map them no?

with open("classes.txt") as f:
  classes = f.readlines()
  correct_classname = classes[np.argmax(classname)] # classname is the variable equal to what you set it in your question

I don't think the labels are saved anywhere in your model, unless you implemented that manually. If you really need to save it in the model you can do something like this (which doesn't require you to retrain your model:):

import tensorflow as tf
import tensorflow_hub as hub

iput = tf.keras.layers.Input(...)
inferred = hub.Keraslayer(path_to_saved_model)(iput)
oput = tf.keras.layers.Lambda(lookup_fn)(inferred)
model = tf.keras.Model(inputs=[iput], outputs=[oput])

You'll then have to figure out the lookup_fn yourself, but a nice starting point is tf.lookup.TextFileInitializer .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM