Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to predict the class of a new image without sending the label #27

Open
almoghitelman opened this issue Mar 14, 2021 · 4 comments
Open

Comments

@almoghitelman
Copy link

i want to use the trained model to predict the class of a test image without sending the label, is it possible?

currently, the implementation required sending the Keras "predict" function a tuble of (X_test, y_test)
Is their a way to use "predict" only with the test image as input and gets the label as output?

@farhantandia
Copy link

farhantandia commented Apr 27, 2021

how to predict the class probability? when I set the output to arcface output (softmax) w.r.t number of class, I got an error when doing prediction
model = Model(inputs=model.input[0], outputs=model.layers[-1].output) ,-1 instead of -3
[-1].output :
<tf.Tensor 'class_output/Softmax_3:0' shape=(None, 10) dtype=float32>
while [-3] return feature vectors which suitable for visualization.
thanks.

@ozora-ogino
Copy link

ozora-ogino commented Jun 2, 2021

Hi @almoghitelman ,
Here is my solution,

class DNN(tf.keras.models.Model):
    def __init__(self, num_classes=10):
        super(DNN, self).__init__()
        weight_decay = 1e-4
        self.layer_1 = tf.keras.layers.Dense(32, activation='relu')
        self.layer_2 = tf.keras.layers.Dense(10)
        self.out = ArcFace(n_classes=num_classes, regularizer=regularizers.l2(weight_decay))

    def call(self, x, training=False):
        if training:
            x, y = x[0], x[1]
        x = self.layer_1(x)
        x = self.layer_2(x)
        if training:
            out = self.out([x, y])
        else:
            # Prediction 
            # Thanks to this, you don't need to pass labels to model when you predict
            out = tf.nn.softmax(x @ self.out.W)
        return out

Sample code is following;

odel = DNN()
optimizer = tf.keras.optimizers.Adam()
loss = tf.keras.losses.categorical_crossentropy

model.compile(loss=loss, optimizer=optimizer, metrics=['acc'])
model.fit([x_train, tf.keras.utils.to_categorical(y_train, 10)], tf.keras.utils.to_categorical(y_train, 10), batch_size=512, epochs=10)

pred = model.predict(x_test)

I hope it works in your situation.

@ozora-ogino
Copy link

I implemented my own ArcFace.
https://github.com/ozora-ogino/asoftmax-tf

This works correctly and I believe this can solve your problem.

@scyzz5
Copy link

scyzz5 commented Apr 17, 2022

Why not use [0, 0, 0......0] as laber

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants