CoreML2 keeps telling me everything is the same thing












-1














I am trying to start small and have trained my model with 2 items. It recognises both just fine but when I show it something other than the 2 items it knows, it keeps telling me that is is always the same item. For example, I have an apple and banana. If I show it the apple, it correctly gives me apple, if I show it a banana it correctly returns banana. But if I show it a dog, it tells me it is an apple. If I show it a helicopter it tells me it is an apple. I even tried an if statement to say that if it isn't an apple or a banana then just return that it can't be recognised, but that never comes because everything besides the banana is always an apple?!



EDIT
My question was down voted perhaps because of a misunderstanding. I am not asking why it doesn't recognise a dog when I have only trained an apple and banana, I am asking why it doesn't tell me that the image is unrecognised when I show it a dog instead of telling me it's an apple. Obviously if I have only trained it with 2 items it is only going to recognise 2 items.



Here is the code that returns the classifications



func processClassifications(for request: VNRequest, error: Error?) {
DispatchQueue.main.async {
guard let results = request.results else {
self.classificationLabel.text = "Unable to classify image"
return
}

let classifications = results as! [VNClassificationObservation]

if classifications.isEmpty {
self.classificationLabel.text = "Nothing recognized"
} else {
//Display top classifications ranked by confidence in the UI
let topClassifications = classifications.prefix(1)
let descriptions = topClassifications.map { classification in
return String(format: " (%.2f) %@", classification.confidence, classification.identifier)

}


self.classificationLabel.text = descriptions.joined(separator: "n")

}
}
}









share|improve this question




















  • 1




    create one more item called Unknowned
    – canister_exister
    Nov 12 at 19:52










  • That's worth a shot.
    – user8463989
    Nov 12 at 19:54
















-1














I am trying to start small and have trained my model with 2 items. It recognises both just fine but when I show it something other than the 2 items it knows, it keeps telling me that is is always the same item. For example, I have an apple and banana. If I show it the apple, it correctly gives me apple, if I show it a banana it correctly returns banana. But if I show it a dog, it tells me it is an apple. If I show it a helicopter it tells me it is an apple. I even tried an if statement to say that if it isn't an apple or a banana then just return that it can't be recognised, but that never comes because everything besides the banana is always an apple?!



EDIT
My question was down voted perhaps because of a misunderstanding. I am not asking why it doesn't recognise a dog when I have only trained an apple and banana, I am asking why it doesn't tell me that the image is unrecognised when I show it a dog instead of telling me it's an apple. Obviously if I have only trained it with 2 items it is only going to recognise 2 items.



Here is the code that returns the classifications



func processClassifications(for request: VNRequest, error: Error?) {
DispatchQueue.main.async {
guard let results = request.results else {
self.classificationLabel.text = "Unable to classify image"
return
}

let classifications = results as! [VNClassificationObservation]

if classifications.isEmpty {
self.classificationLabel.text = "Nothing recognized"
} else {
//Display top classifications ranked by confidence in the UI
let topClassifications = classifications.prefix(1)
let descriptions = topClassifications.map { classification in
return String(format: " (%.2f) %@", classification.confidence, classification.identifier)

}


self.classificationLabel.text = descriptions.joined(separator: "n")

}
}
}









share|improve this question




















  • 1




    create one more item called Unknowned
    – canister_exister
    Nov 12 at 19:52










  • That's worth a shot.
    – user8463989
    Nov 12 at 19:54














-1












-1








-1







I am trying to start small and have trained my model with 2 items. It recognises both just fine but when I show it something other than the 2 items it knows, it keeps telling me that is is always the same item. For example, I have an apple and banana. If I show it the apple, it correctly gives me apple, if I show it a banana it correctly returns banana. But if I show it a dog, it tells me it is an apple. If I show it a helicopter it tells me it is an apple. I even tried an if statement to say that if it isn't an apple or a banana then just return that it can't be recognised, but that never comes because everything besides the banana is always an apple?!



EDIT
My question was down voted perhaps because of a misunderstanding. I am not asking why it doesn't recognise a dog when I have only trained an apple and banana, I am asking why it doesn't tell me that the image is unrecognised when I show it a dog instead of telling me it's an apple. Obviously if I have only trained it with 2 items it is only going to recognise 2 items.



Here is the code that returns the classifications



func processClassifications(for request: VNRequest, error: Error?) {
DispatchQueue.main.async {
guard let results = request.results else {
self.classificationLabel.text = "Unable to classify image"
return
}

let classifications = results as! [VNClassificationObservation]

if classifications.isEmpty {
self.classificationLabel.text = "Nothing recognized"
} else {
//Display top classifications ranked by confidence in the UI
let topClassifications = classifications.prefix(1)
let descriptions = topClassifications.map { classification in
return String(format: " (%.2f) %@", classification.confidence, classification.identifier)

}


self.classificationLabel.text = descriptions.joined(separator: "n")

}
}
}









share|improve this question















I am trying to start small and have trained my model with 2 items. It recognises both just fine but when I show it something other than the 2 items it knows, it keeps telling me that is is always the same item. For example, I have an apple and banana. If I show it the apple, it correctly gives me apple, if I show it a banana it correctly returns banana. But if I show it a dog, it tells me it is an apple. If I show it a helicopter it tells me it is an apple. I even tried an if statement to say that if it isn't an apple or a banana then just return that it can't be recognised, but that never comes because everything besides the banana is always an apple?!



EDIT
My question was down voted perhaps because of a misunderstanding. I am not asking why it doesn't recognise a dog when I have only trained an apple and banana, I am asking why it doesn't tell me that the image is unrecognised when I show it a dog instead of telling me it's an apple. Obviously if I have only trained it with 2 items it is only going to recognise 2 items.



Here is the code that returns the classifications



func processClassifications(for request: VNRequest, error: Error?) {
DispatchQueue.main.async {
guard let results = request.results else {
self.classificationLabel.text = "Unable to classify image"
return
}

let classifications = results as! [VNClassificationObservation]

if classifications.isEmpty {
self.classificationLabel.text = "Nothing recognized"
} else {
//Display top classifications ranked by confidence in the UI
let topClassifications = classifications.prefix(1)
let descriptions = topClassifications.map { classification in
return String(format: " (%.2f) %@", classification.confidence, classification.identifier)

}


self.classificationLabel.text = descriptions.joined(separator: "n")

}
}
}






swift coreml






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 20:04

























asked Nov 12 at 19:33









user8463989

11810




11810








  • 1




    create one more item called Unknowned
    – canister_exister
    Nov 12 at 19:52










  • That's worth a shot.
    – user8463989
    Nov 12 at 19:54














  • 1




    create one more item called Unknowned
    – canister_exister
    Nov 12 at 19:52










  • That's worth a shot.
    – user8463989
    Nov 12 at 19:54








1




1




create one more item called Unknowned
– canister_exister
Nov 12 at 19:52




create one more item called Unknowned
– canister_exister
Nov 12 at 19:52












That's worth a shot.
– user8463989
Nov 12 at 19:54




That's worth a shot.
– user8463989
Nov 12 at 19:54












2 Answers
2






active

oldest

votes


















2














If you have model trained with 2 items only (in your case Apple and Banana) you can't expect that your ML model recognize something else than these 2 items.
Code you wrote always return item which will have the biggest confidence.



If you want to return item just if topClassification with your testing image match at least for example 75% replace this



if classifications.isEmpty {
self.classificationLabel.text = "Nothing recognized"
} else {
//Display top classifications ranked by confidence in the UI
let topClassifications = classifications.prefix(1)
let descriptions = topClassifications.map { classification in
return String(format: " (%.2f) %@", classification.confidence, classification.identifier)
}


self.classificationLabel.text = descriptions.joined(separator: "n")

}


with



guard let topResult = classifications.first else { return }
if topResult.confidence > 0.75 {
print(topResult.identifier)
} else {
print("Match is less than 75%")
}





share|improve this answer























  • I would have thought though if you show it something that looks nothing like those 2 items it would not recognise it instead of telling you it is always one of the two things? I would rather have a not recognised message than everything being an apple
    – user8463989
    Nov 12 at 19:48












  • I am not expecitng it to give me the correct names for something I haven't trained, but I am not expecting it to tell me everything is an apple but rather that it doesn't recognise something that isn't an apple or a banana
    – user8463989
    Nov 12 at 19:52












  • @user8463989 try edited answer
    – Robert Dresler
    Nov 12 at 20:11










  • thanks for that. It would have been perfect if the unknown images would score that low. I checked the scores and for a picture of a waterfall it seems to know with 100% certainty that it is an apple. So, that code of yours never fires.
    – user8463989
    Nov 13 at 8:05










  • @user8463989 If you print topResult.confidence right above if statement what is printed?
    – Robert Dresler
    Nov 13 at 8:36



















0














In a model that is a classifier, you always get just as many VNClassificationObservation objects as there are classes. No more, no less. The array is only empty if there was a problem running the model.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53268919%2fcoreml2-keeps-telling-me-everything-is-the-same-thing%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    If you have model trained with 2 items only (in your case Apple and Banana) you can't expect that your ML model recognize something else than these 2 items.
    Code you wrote always return item which will have the biggest confidence.



    If you want to return item just if topClassification with your testing image match at least for example 75% replace this



    if classifications.isEmpty {
    self.classificationLabel.text = "Nothing recognized"
    } else {
    //Display top classifications ranked by confidence in the UI
    let topClassifications = classifications.prefix(1)
    let descriptions = topClassifications.map { classification in
    return String(format: " (%.2f) %@", classification.confidence, classification.identifier)
    }


    self.classificationLabel.text = descriptions.joined(separator: "n")

    }


    with



    guard let topResult = classifications.first else { return }
    if topResult.confidence > 0.75 {
    print(topResult.identifier)
    } else {
    print("Match is less than 75%")
    }





    share|improve this answer























    • I would have thought though if you show it something that looks nothing like those 2 items it would not recognise it instead of telling you it is always one of the two things? I would rather have a not recognised message than everything being an apple
      – user8463989
      Nov 12 at 19:48












    • I am not expecitng it to give me the correct names for something I haven't trained, but I am not expecting it to tell me everything is an apple but rather that it doesn't recognise something that isn't an apple or a banana
      – user8463989
      Nov 12 at 19:52












    • @user8463989 try edited answer
      – Robert Dresler
      Nov 12 at 20:11










    • thanks for that. It would have been perfect if the unknown images would score that low. I checked the scores and for a picture of a waterfall it seems to know with 100% certainty that it is an apple. So, that code of yours never fires.
      – user8463989
      Nov 13 at 8:05










    • @user8463989 If you print topResult.confidence right above if statement what is printed?
      – Robert Dresler
      Nov 13 at 8:36
















    2














    If you have model trained with 2 items only (in your case Apple and Banana) you can't expect that your ML model recognize something else than these 2 items.
    Code you wrote always return item which will have the biggest confidence.



    If you want to return item just if topClassification with your testing image match at least for example 75% replace this



    if classifications.isEmpty {
    self.classificationLabel.text = "Nothing recognized"
    } else {
    //Display top classifications ranked by confidence in the UI
    let topClassifications = classifications.prefix(1)
    let descriptions = topClassifications.map { classification in
    return String(format: " (%.2f) %@", classification.confidence, classification.identifier)
    }


    self.classificationLabel.text = descriptions.joined(separator: "n")

    }


    with



    guard let topResult = classifications.first else { return }
    if topResult.confidence > 0.75 {
    print(topResult.identifier)
    } else {
    print("Match is less than 75%")
    }





    share|improve this answer























    • I would have thought though if you show it something that looks nothing like those 2 items it would not recognise it instead of telling you it is always one of the two things? I would rather have a not recognised message than everything being an apple
      – user8463989
      Nov 12 at 19:48












    • I am not expecitng it to give me the correct names for something I haven't trained, but I am not expecting it to tell me everything is an apple but rather that it doesn't recognise something that isn't an apple or a banana
      – user8463989
      Nov 12 at 19:52












    • @user8463989 try edited answer
      – Robert Dresler
      Nov 12 at 20:11










    • thanks for that. It would have been perfect if the unknown images would score that low. I checked the scores and for a picture of a waterfall it seems to know with 100% certainty that it is an apple. So, that code of yours never fires.
      – user8463989
      Nov 13 at 8:05










    • @user8463989 If you print topResult.confidence right above if statement what is printed?
      – Robert Dresler
      Nov 13 at 8:36














    2












    2








    2






    If you have model trained with 2 items only (in your case Apple and Banana) you can't expect that your ML model recognize something else than these 2 items.
    Code you wrote always return item which will have the biggest confidence.



    If you want to return item just if topClassification with your testing image match at least for example 75% replace this



    if classifications.isEmpty {
    self.classificationLabel.text = "Nothing recognized"
    } else {
    //Display top classifications ranked by confidence in the UI
    let topClassifications = classifications.prefix(1)
    let descriptions = topClassifications.map { classification in
    return String(format: " (%.2f) %@", classification.confidence, classification.identifier)
    }


    self.classificationLabel.text = descriptions.joined(separator: "n")

    }


    with



    guard let topResult = classifications.first else { return }
    if topResult.confidence > 0.75 {
    print(topResult.identifier)
    } else {
    print("Match is less than 75%")
    }





    share|improve this answer














    If you have model trained with 2 items only (in your case Apple and Banana) you can't expect that your ML model recognize something else than these 2 items.
    Code you wrote always return item which will have the biggest confidence.



    If you want to return item just if topClassification with your testing image match at least for example 75% replace this



    if classifications.isEmpty {
    self.classificationLabel.text = "Nothing recognized"
    } else {
    //Display top classifications ranked by confidence in the UI
    let topClassifications = classifications.prefix(1)
    let descriptions = topClassifications.map { classification in
    return String(format: " (%.2f) %@", classification.confidence, classification.identifier)
    }


    self.classificationLabel.text = descriptions.joined(separator: "n")

    }


    with



    guard let topResult = classifications.first else { return }
    if topResult.confidence > 0.75 {
    print(topResult.identifier)
    } else {
    print("Match is less than 75%")
    }






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 13 at 8:14

























    answered Nov 12 at 19:45









    Robert Dresler

    3,9651526




    3,9651526












    • I would have thought though if you show it something that looks nothing like those 2 items it would not recognise it instead of telling you it is always one of the two things? I would rather have a not recognised message than everything being an apple
      – user8463989
      Nov 12 at 19:48












    • I am not expecitng it to give me the correct names for something I haven't trained, but I am not expecting it to tell me everything is an apple but rather that it doesn't recognise something that isn't an apple or a banana
      – user8463989
      Nov 12 at 19:52












    • @user8463989 try edited answer
      – Robert Dresler
      Nov 12 at 20:11










    • thanks for that. It would have been perfect if the unknown images would score that low. I checked the scores and for a picture of a waterfall it seems to know with 100% certainty that it is an apple. So, that code of yours never fires.
      – user8463989
      Nov 13 at 8:05










    • @user8463989 If you print topResult.confidence right above if statement what is printed?
      – Robert Dresler
      Nov 13 at 8:36


















    • I would have thought though if you show it something that looks nothing like those 2 items it would not recognise it instead of telling you it is always one of the two things? I would rather have a not recognised message than everything being an apple
      – user8463989
      Nov 12 at 19:48












    • I am not expecitng it to give me the correct names for something I haven't trained, but I am not expecting it to tell me everything is an apple but rather that it doesn't recognise something that isn't an apple or a banana
      – user8463989
      Nov 12 at 19:52












    • @user8463989 try edited answer
      – Robert Dresler
      Nov 12 at 20:11










    • thanks for that. It would have been perfect if the unknown images would score that low. I checked the scores and for a picture of a waterfall it seems to know with 100% certainty that it is an apple. So, that code of yours never fires.
      – user8463989
      Nov 13 at 8:05










    • @user8463989 If you print topResult.confidence right above if statement what is printed?
      – Robert Dresler
      Nov 13 at 8:36
















    I would have thought though if you show it something that looks nothing like those 2 items it would not recognise it instead of telling you it is always one of the two things? I would rather have a not recognised message than everything being an apple
    – user8463989
    Nov 12 at 19:48






    I would have thought though if you show it something that looks nothing like those 2 items it would not recognise it instead of telling you it is always one of the two things? I would rather have a not recognised message than everything being an apple
    – user8463989
    Nov 12 at 19:48














    I am not expecitng it to give me the correct names for something I haven't trained, but I am not expecting it to tell me everything is an apple but rather that it doesn't recognise something that isn't an apple or a banana
    – user8463989
    Nov 12 at 19:52






    I am not expecitng it to give me the correct names for something I haven't trained, but I am not expecting it to tell me everything is an apple but rather that it doesn't recognise something that isn't an apple or a banana
    – user8463989
    Nov 12 at 19:52














    @user8463989 try edited answer
    – Robert Dresler
    Nov 12 at 20:11




    @user8463989 try edited answer
    – Robert Dresler
    Nov 12 at 20:11












    thanks for that. It would have been perfect if the unknown images would score that low. I checked the scores and for a picture of a waterfall it seems to know with 100% certainty that it is an apple. So, that code of yours never fires.
    – user8463989
    Nov 13 at 8:05




    thanks for that. It would have been perfect if the unknown images would score that low. I checked the scores and for a picture of a waterfall it seems to know with 100% certainty that it is an apple. So, that code of yours never fires.
    – user8463989
    Nov 13 at 8:05












    @user8463989 If you print topResult.confidence right above if statement what is printed?
    – Robert Dresler
    Nov 13 at 8:36




    @user8463989 If you print topResult.confidence right above if statement what is printed?
    – Robert Dresler
    Nov 13 at 8:36













    0














    In a model that is a classifier, you always get just as many VNClassificationObservation objects as there are classes. No more, no less. The array is only empty if there was a problem running the model.






    share|improve this answer


























      0














      In a model that is a classifier, you always get just as many VNClassificationObservation objects as there are classes. No more, no less. The array is only empty if there was a problem running the model.






      share|improve this answer
























        0












        0








        0






        In a model that is a classifier, you always get just as many VNClassificationObservation objects as there are classes. No more, no less. The array is only empty if there was a problem running the model.






        share|improve this answer












        In a model that is a classifier, you always get just as many VNClassificationObservation objects as there are classes. No more, no less. The array is only empty if there was a problem running the model.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 at 9:44









        Matthijs Hollemans

        2,6981210




        2,6981210






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53268919%2fcoreml2-keeps-telling-me-everything-is-the-same-thing%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Xamarin.iOS Cant Deploy on Iphone

            Glorious Revolution

            Dulmage-Mendelsohn matrix decomposition in Python