What is the value of the chain image recognition Classifier.predictor?

Asked 1 months ago, Updated 1 months ago, 5 views

I saved the model by using a chain to learn 73 hiragana characters from images on a convolutional neural network.I'm writing a code that reads the model I learned in serializers.load_npz, enters a handwritten image, recognizes it, and displays the top five as a result with a percentage.

Target Display Results (Example)

tegaki_na.png Identification Results
 Yes: 60%
 only: 25%
 Yes: 10%
 Ah: 4%
 —1% of 1%

However, we found that the matrix (73 elements) obtained from the predictor data that we were trying to use as a percentage is from positive to negative, so it cannot be used as a percentage (even if we add everything together, it never went to 1 or 100).
When I read the official Chain.Links.Classifier section, there was no description of the output of the predictor, so I asked you a question here.

Here is the source code.Here, the actual value in the predictor is to be printed as it is (I want this to be a percentage)

import numpy as np
import cv2
import chain
import chain.links as L
from chain import Variable
# Convolution model saved as convolution_model.py
import convolution_model as CNN

label=['A', 'I', 'U', 'E', 'O',
         'Ka', 'ki', 'gi', 'ku', 'gu', 'ke', 'ge', 'ko', 'go', 'go', 'go', 'go'
         'Sa', 'the', 'shi', 'ji', 'su', 'zu', 'se', 'ze', 'so', 'so', 'so', 'so', 'so', 'so', 'so', 'so', 'so', 'so', 'so', 'so', 'so', 'so', 'so', 'so', '
         "Ta", "da", "chi", "ji", "tsu", "tsu", "te", "te", "te", "te", "te", "te", "te", "te", "te", "te", "te", "te", "te", "te", "te", "te", "te", "te", "te
         of 'na', 'n', 'n', 'ne', 'ne', 'ne', 'ne', 'ne', 'ne', 'ne', 'ne', 'ne,ne',
         ' is ', 'ba', 'pa', 'hi', 'bi', 'pi', 'fu', 'bu', 'pu', 'he', 'be', 'bae', 'ho', 'bo', 'po', 'po', 'po', 'po', 'po', 'po', 'po', 'po', 'po', 'po', 'po', 'po', 'po', 'po', 'po', 'po', 'po', 'po', 'po', 'po', 'po', 'po',
         "Ma", "Mi", "Mu", "Me", "Momo",
         'Hey', 'Yu', 'Yu', 'Yo',
         'ra', 'ri', 'ru', 're', 'ro', 'wa', 'i', 'e', 'e', 'e', 'e', 'n'n'n'n'n'n'n'n'n'n'n'n'n'n'n'n'n'n'n'n'n'n]

# Assume image size is 48x48
model = L. Classifier (CNN.Model())
chain.serializers.load_npz('nihongo.model',model)

defloadimg (image):
    img_=cv2.imread(image,0)# Load as grayscale

    height,width=img_.shape [:2]
    if height!=48 or width!=48:# If length is not 48px
        resize to img_=cv2.resize(img_, (48,48))#48x48

    img_=img_.astype(np.float32)/255
    temp = [ ]
    temp2 = [ ]
    temp.append(img_)
    temp2.append(temp)
    img_=np.array(temp2)
    return img_

def score(image):
    img=loadimg(image)

    result=model.predictor(Variable(img))
    result=result.data[0]#Probability of each character?

    a_label=np.argsort(result)
    a_ratio=np.sort(result)

    a_label = a_label [-1::-1]
    a_ratio=a_ratio [-1::-1]
    return a_label, a_ratio

Code to execute (assuming you are moving the current to the working directory)

IMGName='test_ga.png'
AL,AR = score (IMGName)

print(IMGName, 'Results')
for i in range (73):
    an_l = AL[i]
    print(str(label[ans_l], ':', str(AR[i]))

Results

tegaki_na.png Identification Results
 Yes: 42.3303
 only: 30.025
 is 26.0882
 Ah: 25.0445
 where: 19.8176

python chainer

2022-09-30 12:01

1 Answers

http://docs.chainer.org/en/stable/reference/generated/chainer.links.Classifier.html#chainer-links-classifier
https://github.com/chainer/chainer/blob/v3.1.0/chainer/links/model/classifier.py#L71

The predictor attribute stores the first argument given to the L.Classifier, so the source of the question is the CNN.Model()) portion of model=L.Classifier(CNN.Model()).
The Model in convolution_model.py just shows the value you defined.

(In other words, L.Classifier is only passing the identification model you defined.)


2022-09-30 12:01

If you have any answers or tips


© 2022 OneMinuteCode. All rights reserved.