简体   繁体   English

如何在单个输入上应用张量流 model 并获得实际预测以及如何在单独的脚本中实现 model

[英]How do you apply a Tensor Flow model on a single input and obtain the actual prediction and how to implement the model in a separate script

Recently I have been learning Tensor Flow, and I have written a few machine learning programs, however, I am wondering in what way can I test the model on a single input and receive the prediction, and not just evaluate the accuracy of the model on a lot of data as you would do using the model.fit() function.最近我一直在学习 Tensor Flow,并编写了一些机器学习程序,但是,我想知道如何在单个输入上测试 model 并接收预测,而不仅仅是评估 model 在使用model.fit() function 可以获得大量数据。 I am also wondering how can I then implement the model in a script, that for example gathers data and feeds it into the model automatically to obtain the predictions and then for example plots the results on a graph.我还想知道如何在脚本中实现 model,例如收集数据并将其自动输入 model 以获得预测,然后例如在图表上绘制结果。

Thanks in advance.提前致谢。

To use your trained model for a single input lets call it y, you must process y to have the same data format your model was trained on.要将经过训练的 model 用于单个输入,我们将其称为 y,您必须处理 y 以获得与 model 训练时相同的数据格式。 For example lets assume that you trained on model on images of cats and dog.例如,假设您在猫和狗的图像上训练了 model。 If you model trained properly you should be able to submit a picture of a cat or a dog to it and have it tell you which it is.如果你的 model 训练有素,你应该能够向它提交一张猫或狗的照片,并让它告诉你它是什么。 Now if images were the input used to train the model they had a certain image shape (height,width) and a certain channel format for example RGB or Grayscale etc. So for the image y you want to predict you must ensure its size is the same height and width the model was trained on.现在,如果图像是用于训练 model 的输入,它们具有特定的图像形状(高度、宽度)和特定的通道格式,例如 RGB 或灰度等。因此,对于要预测的图像 y,您必须确保其大小为model 的高度和宽度相同。 If the model was trained on rgb images then y must be an rgb image.如果 model 是在 rgb 图像上训练的,那么 y 必须是 rgb 图像。 one more thing.还有一件事。 When using model.predict say for predicting the single image y you will have to account for the fact that model.predict requires that you have the first dimension of y to be the batch_size.当使用 model.predict 说预测单个图像 y 时,您必须考虑到 model.predict 要求您将 y 的第一个维度作为 batch_size 的事实。 For the case of a single image the batch size is 1. So you need to expand the dimensions of y to include the batch size.对于单个图像,批量大小为 1。因此,您需要扩展 y 的维度以包含批量大小。 For an immage the shape of y is (height, width,channels).对于图像,y 的形状是(高度、宽度、通道)。 It doesn't have a batch dimension so you need to add it.它没有批次维度,因此您需要添加它。 You can do that with the y=np.expand_dims(y,axis=0) which will now give y the shape (1, height,width,channels).您可以使用 y=np.expand_dims(y,axis=0) 来做到这一点,它现在将为 y 提供形状(1、高度、宽度、通道)。 For example lets assume you trained you model on images of shape (224,224,3) in rgb format.例如,假设您在 rgb 格式的形状 (224,224,3) 图像上训练了 model。 You have an image y you want to classify and say it is a directory my_pics.你有一个你想要分类的图像,并说它是一个目录 my_pics。 The code below shows how to handle doing a prediction on image y.下面的代码显示了如何处理对图像 y 的预测。 Somewhere in your training code you need to have an ordered list called classes.在您的训练代码中的某处,您需要有一个称为类的有序列表。 For the dog example the index code for cat might be 0 and the index code for dog then will be 1. So classes would be classes=['cat', 'dog']对于狗示例,cat 的索引代码可能为 0,而 dog 的索引代码则为 1。因此 classes 将是 classes=['cat', 'dog']

model=tf.keras.models.load_model(path where model is stored) # load the trained model
image_path=r'my_pics' # path to image y
y=cv2.imread(image_path) #Note cv2 reads in images as bgr
y=cv2.resize(y, (224,224) # gives y the same shape as the training images
y=cv2.cvtColor(y, cv2.COLOR_BGR2RGB)# convert from bgr to rgb
y=np.expand_dims(y, axis=0) # y has shape (1,224,224,3)
prediction = model.predict(y) # make a prediction on y
print (prediction) # is a list with a probability value for each class 
class_index=np.argmax(prediction # gives index of entry in prediction with highest probability
klass=classes[class_index} #selects the class name from the ordered list of classes
print (class)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用张量流迁移学习模型预测单个图像文件 - Prediction for single image file using tensor flow transfer learning model 如何在张量流中测试模型? - How to test a model in tensor flow? 从Tensor Flow Model获得预测 - Get a prediction from Tensor Flow Model 如何将单个输入的 model output 转换回预测类之一? - How to convert back the model output for single input to one of the prediction classes? 如何测试我训练有素的Tensor Flow模型 - How to test my trained Tensor Flow model 如何在 Google Colab 中存储张量流 model? - How to store a tensor flow model in Google Colab? 如何在 stream 中应用 MLFlow 预测 model? - How to apply MLFlow prediction model in a stream? 如何在 Keras 中编译自定义损失 function ,将预测与来自输入张量的信息连接起来? - How do you compile a custom loss function in Keras that concatenates the prediction with information from the input tensor? 如何从 bagging model 进行单一预测 - How to make single prediction from bagging model 如何根据output张量去掉pytorch model的一个预测头? - How to remove a prediction head from pytorch model based on the output tensor?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM