Reversing an ML model











up vote
0
down vote

favorite
1












I am currently teaching myself the basics of machine learning by creating a simple image classifier using Keras (with a Tensorflow backend). The model classifies a (greyscaled) image as either a cat or not a cat.



My model is relatively good at this task, so I now want to see if it can generate images that it would classify as a cat.



I have attempted to start this in a simple way, by creating a random array of the same shape as the images, with random numbers in each index:



    from random import randint

json_file = open('model.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
model = model_from_json(loaded_model_json)
model.load_weights("model_weights.h5")

confidence = 0.0
thresholdConfidence = 0.6

while confidence < thresholdConfidence:
img_array = np.array([[[randint(0, 255) for z in range(1)] for y in range(64)] for x in range(64)])
img_array = img_array.reshape((1,) + img_array.shape)
confidence = model.predict(img_array)


This method is obviously not good at all, since it just creates random things and could potentially run eternally. Could the model somehow run in reverse by telling it that an array is 100% cat, and having it predict what the array representation of the image is?



Thank you for reading.



[This is my first post on StackOverflow, so please let me know if I've done something wrong!]










share|improve this question


















  • 3




    The short and simple answer is no, you can't simply invert your classifier. Your classifier does not, as it were, know what a cat is so can't draw one. It's a (admittedly complex) one-way function from the world of images to a binary cat / not-cat classification. The long and complex answer is long and complex and outside the possibilities offered by SO.
    – High Performance Mark
    Nov 19 at 15:30










  • I imagine that the output will probably be non-sensical most of the time. I'm also assuming that the output will be different every time, and it won't just copy one of the images that it already classified as 100% cat. It could help with determining what the model sees as cat-like features.
    – Kay Tukendorf
    Nov 19 at 15:31










  • As being said above. Try looking at Generative Adversarial Networks, or net by google called DeepDream.
    – Josef Korbel
    Nov 19 at 15:35








  • 1




    Oh, I could have added ... yes, you have done something wrong. You've asked a question which is too broad to be on topic here. Read again the material in the help centre which explains what questions are on topic and what are not.
    – High Performance Mark
    Nov 19 at 15:46















up vote
0
down vote

favorite
1












I am currently teaching myself the basics of machine learning by creating a simple image classifier using Keras (with a Tensorflow backend). The model classifies a (greyscaled) image as either a cat or not a cat.



My model is relatively good at this task, so I now want to see if it can generate images that it would classify as a cat.



I have attempted to start this in a simple way, by creating a random array of the same shape as the images, with random numbers in each index:



    from random import randint

json_file = open('model.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
model = model_from_json(loaded_model_json)
model.load_weights("model_weights.h5")

confidence = 0.0
thresholdConfidence = 0.6

while confidence < thresholdConfidence:
img_array = np.array([[[randint(0, 255) for z in range(1)] for y in range(64)] for x in range(64)])
img_array = img_array.reshape((1,) + img_array.shape)
confidence = model.predict(img_array)


This method is obviously not good at all, since it just creates random things and could potentially run eternally. Could the model somehow run in reverse by telling it that an array is 100% cat, and having it predict what the array representation of the image is?



Thank you for reading.



[This is my first post on StackOverflow, so please let me know if I've done something wrong!]










share|improve this question


















  • 3




    The short and simple answer is no, you can't simply invert your classifier. Your classifier does not, as it were, know what a cat is so can't draw one. It's a (admittedly complex) one-way function from the world of images to a binary cat / not-cat classification. The long and complex answer is long and complex and outside the possibilities offered by SO.
    – High Performance Mark
    Nov 19 at 15:30










  • I imagine that the output will probably be non-sensical most of the time. I'm also assuming that the output will be different every time, and it won't just copy one of the images that it already classified as 100% cat. It could help with determining what the model sees as cat-like features.
    – Kay Tukendorf
    Nov 19 at 15:31










  • As being said above. Try looking at Generative Adversarial Networks, or net by google called DeepDream.
    – Josef Korbel
    Nov 19 at 15:35








  • 1




    Oh, I could have added ... yes, you have done something wrong. You've asked a question which is too broad to be on topic here. Read again the material in the help centre which explains what questions are on topic and what are not.
    – High Performance Mark
    Nov 19 at 15:46













up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1





I am currently teaching myself the basics of machine learning by creating a simple image classifier using Keras (with a Tensorflow backend). The model classifies a (greyscaled) image as either a cat or not a cat.



My model is relatively good at this task, so I now want to see if it can generate images that it would classify as a cat.



I have attempted to start this in a simple way, by creating a random array of the same shape as the images, with random numbers in each index:



    from random import randint

json_file = open('model.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
model = model_from_json(loaded_model_json)
model.load_weights("model_weights.h5")

confidence = 0.0
thresholdConfidence = 0.6

while confidence < thresholdConfidence:
img_array = np.array([[[randint(0, 255) for z in range(1)] for y in range(64)] for x in range(64)])
img_array = img_array.reshape((1,) + img_array.shape)
confidence = model.predict(img_array)


This method is obviously not good at all, since it just creates random things and could potentially run eternally. Could the model somehow run in reverse by telling it that an array is 100% cat, and having it predict what the array representation of the image is?



Thank you for reading.



[This is my first post on StackOverflow, so please let me know if I've done something wrong!]










share|improve this question













I am currently teaching myself the basics of machine learning by creating a simple image classifier using Keras (with a Tensorflow backend). The model classifies a (greyscaled) image as either a cat or not a cat.



My model is relatively good at this task, so I now want to see if it can generate images that it would classify as a cat.



I have attempted to start this in a simple way, by creating a random array of the same shape as the images, with random numbers in each index:



    from random import randint

json_file = open('model.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
model = model_from_json(loaded_model_json)
model.load_weights("model_weights.h5")

confidence = 0.0
thresholdConfidence = 0.6

while confidence < thresholdConfidence:
img_array = np.array([[[randint(0, 255) for z in range(1)] for y in range(64)] for x in range(64)])
img_array = img_array.reshape((1,) + img_array.shape)
confidence = model.predict(img_array)


This method is obviously not good at all, since it just creates random things and could potentially run eternally. Could the model somehow run in reverse by telling it that an array is 100% cat, and having it predict what the array representation of the image is?



Thank you for reading.



[This is my first post on StackOverflow, so please let me know if I've done something wrong!]







python tensorflow machine-learning keras






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 at 15:19









Kay Tukendorf

65




65








  • 3




    The short and simple answer is no, you can't simply invert your classifier. Your classifier does not, as it were, know what a cat is so can't draw one. It's a (admittedly complex) one-way function from the world of images to a binary cat / not-cat classification. The long and complex answer is long and complex and outside the possibilities offered by SO.
    – High Performance Mark
    Nov 19 at 15:30










  • I imagine that the output will probably be non-sensical most of the time. I'm also assuming that the output will be different every time, and it won't just copy one of the images that it already classified as 100% cat. It could help with determining what the model sees as cat-like features.
    – Kay Tukendorf
    Nov 19 at 15:31










  • As being said above. Try looking at Generative Adversarial Networks, or net by google called DeepDream.
    – Josef Korbel
    Nov 19 at 15:35








  • 1




    Oh, I could have added ... yes, you have done something wrong. You've asked a question which is too broad to be on topic here. Read again the material in the help centre which explains what questions are on topic and what are not.
    – High Performance Mark
    Nov 19 at 15:46














  • 3




    The short and simple answer is no, you can't simply invert your classifier. Your classifier does not, as it were, know what a cat is so can't draw one. It's a (admittedly complex) one-way function from the world of images to a binary cat / not-cat classification. The long and complex answer is long and complex and outside the possibilities offered by SO.
    – High Performance Mark
    Nov 19 at 15:30










  • I imagine that the output will probably be non-sensical most of the time. I'm also assuming that the output will be different every time, and it won't just copy one of the images that it already classified as 100% cat. It could help with determining what the model sees as cat-like features.
    – Kay Tukendorf
    Nov 19 at 15:31










  • As being said above. Try looking at Generative Adversarial Networks, or net by google called DeepDream.
    – Josef Korbel
    Nov 19 at 15:35








  • 1




    Oh, I could have added ... yes, you have done something wrong. You've asked a question which is too broad to be on topic here. Read again the material in the help centre which explains what questions are on topic and what are not.
    – High Performance Mark
    Nov 19 at 15:46








3




3




The short and simple answer is no, you can't simply invert your classifier. Your classifier does not, as it were, know what a cat is so can't draw one. It's a (admittedly complex) one-way function from the world of images to a binary cat / not-cat classification. The long and complex answer is long and complex and outside the possibilities offered by SO.
– High Performance Mark
Nov 19 at 15:30




The short and simple answer is no, you can't simply invert your classifier. Your classifier does not, as it were, know what a cat is so can't draw one. It's a (admittedly complex) one-way function from the world of images to a binary cat / not-cat classification. The long and complex answer is long and complex and outside the possibilities offered by SO.
– High Performance Mark
Nov 19 at 15:30












I imagine that the output will probably be non-sensical most of the time. I'm also assuming that the output will be different every time, and it won't just copy one of the images that it already classified as 100% cat. It could help with determining what the model sees as cat-like features.
– Kay Tukendorf
Nov 19 at 15:31




I imagine that the output will probably be non-sensical most of the time. I'm also assuming that the output will be different every time, and it won't just copy one of the images that it already classified as 100% cat. It could help with determining what the model sees as cat-like features.
– Kay Tukendorf
Nov 19 at 15:31












As being said above. Try looking at Generative Adversarial Networks, or net by google called DeepDream.
– Josef Korbel
Nov 19 at 15:35






As being said above. Try looking at Generative Adversarial Networks, or net by google called DeepDream.
– Josef Korbel
Nov 19 at 15:35






1




1




Oh, I could have added ... yes, you have done something wrong. You've asked a question which is too broad to be on topic here. Read again the material in the help centre which explains what questions are on topic and what are not.
– High Performance Mark
Nov 19 at 15:46




Oh, I could have added ... yes, you have done something wrong. You've asked a question which is too broad to be on topic here. Read again the material in the help centre which explains what questions are on topic and what are not.
– High Performance Mark
Nov 19 at 15:46












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










If you wish to generate a special type of image, you can use Generative Adversary Networks. This are made into two parts which need to be trained separately. The two parts are





  1. Generator : Creates noise that is random images.


  2. Discriminator : Gives feedback to the generator regarding the images


You can refer here.






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',
    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%2f53377681%2freversing-an-ml-model%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote



    accepted










    If you wish to generate a special type of image, you can use Generative Adversary Networks. This are made into two parts which need to be trained separately. The two parts are





    1. Generator : Creates noise that is random images.


    2. Discriminator : Gives feedback to the generator regarding the images


    You can refer here.






    share|improve this answer

























      up vote
      0
      down vote



      accepted










      If you wish to generate a special type of image, you can use Generative Adversary Networks. This are made into two parts which need to be trained separately. The two parts are





      1. Generator : Creates noise that is random images.


      2. Discriminator : Gives feedback to the generator regarding the images


      You can refer here.






      share|improve this answer























        up vote
        0
        down vote



        accepted







        up vote
        0
        down vote



        accepted






        If you wish to generate a special type of image, you can use Generative Adversary Networks. This are made into two parts which need to be trained separately. The two parts are





        1. Generator : Creates noise that is random images.


        2. Discriminator : Gives feedback to the generator regarding the images


        You can refer here.






        share|improve this answer












        If you wish to generate a special type of image, you can use Generative Adversary Networks. This are made into two parts which need to be trained separately. The two parts are





        1. Generator : Creates noise that is random images.


        2. Discriminator : Gives feedback to the generator regarding the images


        You can refer here.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 at 15:46









        Shubham Panchal

        17418




        17418






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53377681%2freversing-an-ml-model%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

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            TypeError: fit_transform() missing 1 required positional argument: 'X'