简体   繁体   English

自定义解码预测 keras model

[英]Decode prediction of custom keras model

some days ago I started with ML as I wanted to do a hcaptcha solver.几天前,我开始使用 ML,因为我想做一个 hcaptcha 求解器。 I have everything ready, I just need to train a model that will classify the captcha images so I can send a request with the good answer and get the captcha token.我已经准备好了一切,我只需要训练一个 model 来对验证码图像进行分类,这样我就可以发送一个带有正确答案的请求并获得验证码令牌。

I've looked into some tutorials on how to train my own model with several classes.我研究了一些关于如何用几个课程训练我自己的 model 的教程。 I have it the following way:我有以下方式:

1 trainer folder, 1 validation folder and 1 testing folder. 1 个培训文件夹、1 个验证文件夹和 1 个测试文件夹。 On the trainer and validation folder there is more subfolders named airplane, truck, boat, train,... each one containing aprox 20 images.在培训师和验证文件夹中,还有更多子文件夹,名为飞机、卡车、船、火车……每个子文件夹包含大约 20 张图像。 On the testing folder, some random images related with the classes I have.在测试文件夹中,一些与我所拥有的课程相关的随机图像。

I have trained the model and it seems like I'm getting a 1 accuracy.我已经训练了 model,看起来我的准确度是 1。 Then I get some of the random testing images and try to predict them using this saved model. It does it's job and predicts them, returning an array of numbers.然后我得到一些随机测试图像并尝试使用这个保存的 model 来预测它们。它完成了它的工作并预测它们,返回一个数字数组。 The thing is I don't know how to decode those predictions nor how to see the classes list with his representative integer before predicting.问题是我不知道如何解码这些预测,也不知道如何在预测之前查看他的代表 integer 的类列表。

I'm super new on this so I'm sure anything will help:)我对此非常陌生,所以我相信任何事情都会有所帮助:)

My code below:我的代码如下:

import os

from keras.preprocessing import image
from keras.models import Sequential
from keras import layers
from keras.models import load_model

import numpy as np

trainer_path = "./img/trainer"
validator_path = "./img/validator"
testing_path = "./img/tester"

WIDTH = 128
HEIGHT = 128
BATCH = 30
EPOCHS = 15

train_dataset = image.image_dataset_from_directory(
    trainer_path,
    label_mode="int",
    batch_size=BATCH,
    image_size=(WIDTH, HEIGHT)
)

validator_dataset = image.image_dataset_from_directory(
    validator_path,
    label_mode="int",
    batch_size=BATCH,
    image_size=(WIDTH, HEIGHT)
)


model = Sequential([
    layers.Input((WIDTH, HEIGHT, 3)),
    layers.Conv2D(16, 3, padding="same"),
    layers.Conv2D(32, 3, padding="same"),
    layers.MaxPooling2D(),
    layers.Flatten(),
    layers.Dense(10)
])

model.compile(
    optimizer="adam",
    loss=[
        "sparse_categorical_crossentropy"
    ],
    metrics=["accuracy"]
)

model_fit = model.fit(
    train_dataset,
    epochs=EPOCHS,
    validation_data=validator_dataset,
    verbose=2
)

#loading the saved model
model = load_model("./model")
 
for i in os.listdir(testing_path):
    
    img = image.load_img(testing_path + "/" + i, target_size=(WIDTH, HEIGHT, 3))
    
    img_array = image.img_to_array(img)
    
    img_batch = np.expand_dims(img_array, axis=0)
            
    prediction = model.predict(img_batch)
    
    print(prediction)
    print()

Output example: Output 示例:

[[  875.5614    3123.8257    1521.7046      90.056526   335.5274
   -785.3671    1075.9199    1105.3068     -14.917503 -3745.6494  ]]

You have to apply activation function on last Dense layer, if you want to classify the image it should be softmax (you will get probabilities for all classes), here is the link:你必须在最后一个密集层应用激活 function,如果你想对图像进行分类,它应该是 softmax(你将获得所有类别的概率),这是链接:

https://keras.io/api/layers/activations/ https://keras.io/api/layers/activations/

When it comes to class names it should be sorted by alphanumerical values, you can also pass class_names argument, here is the link to arguments of this function:当涉及到 class 名称时,它应该按字母数字值排序,你也可以传递class_names参数,这里是这个 function 的 arguments 的链接:

https://www.tensorflow.org/api_docs/python/tf/keras/utils/image_dataset_from_directory https://www.tensorflow.org/api_docs/python/tf/keras/utils/image_dataset_from_directory

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

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