Splitting a neural network in 2 microservices The Next CEO of Stack Overflow2019 Community Moderator ElectionWhen is something a Deep Neural Network (DNN) and not NN?combining trained neural nets in tensorflowAdding hand-crafted features to a convolutional neural network (CNN) in TensorFlowAmount of multiplications in a neural network modelText Localization using Convolutional NNReusing a portion of a neural network (with shared weights) in KerasNeural Network unable to track training dataTraining a convoltion neural network for localizationWhat is exactly meant by neural network that can take different types of input?How to train two neural networks together

Is it correct to say moon starry nights?

What happened in Rome, when the western empire "fell"?

Is French Guiana a (hard) EU border?

Does destroying a Lich's phylactery destroy the soul within it?

Touchpad not working on Debian 9

In the "Harry Potter and the Order of the Phoenix" video game, what potion is used to sabotage Umbridge's speakers?

Is there an equivalent of cd - for cp or mv

Is it okay to majorly distort historical facts while writing a fiction story?

Why don't programming languages automatically manage the synchronous/asynchronous problem?

What CSS properties can the br tag have?

Is a distribution that is normal, but highly skewed, considered Gaussian?

TikZ: How to fill area with a special pattern?

How to use ReplaceAll on an expression that contains a rule

Yu-Gi-Oh cards in Python 3

Can this note be analyzed as a non-chord tone?

Reshaping json / reparing json inside shell script (remove trailing comma)

Is there a difference between "Fahrstuhl" and "Aufzug"?

My ex-girlfriend uses my Apple ID to login to her iPad, do I have to give her my Apple ID password to reset it?

Help understanding this unsettling image of Titan, Epimetheus, and Saturn's rings?

Can I calculate next year's exemptions based on this year's refund/amount owed?

Small nick on power cord from an electric alarm clock, and copper wiring exposed but intact

Does higher Oxidation/ reduction potential translate to higher energy storage in battery?

Is it ever safe to open a suspicious HTML file (e.g. email attachment)?

How to get the last not-null value in an ordered column of a huge table?



Splitting a neural network in 2 microservices



The Next CEO of Stack Overflow
2019 Community Moderator ElectionWhen is something a Deep Neural Network (DNN) and not NN?combining trained neural nets in tensorflowAdding hand-crafted features to a convolutional neural network (CNN) in TensorFlowAmount of multiplications in a neural network modelText Localization using Convolutional NNReusing a portion of a neural network (with shared weights) in KerasNeural Network unable to track training dataTraining a convoltion neural network for localizationWhat is exactly meant by neural network that can take different types of input?How to train two neural networks together










1












$begingroup$


I have a neural network, that is already trained locally that can detect objects in the scene.



But I have to split the neural network into 2 parts, let's say it has 16 layers, and I want to have one microservice handle the first 8 layers and give the output of the 8th layer to the next microservice and it takes the data from there and proceeds with the 9th layer (1st layer in the 2nd microservice).



Sending an image to the 1st microservice will give the result from the 2nd microservice, is this feasible using TensorFlow?










share|improve this question









$endgroup$
















    1












    $begingroup$


    I have a neural network, that is already trained locally that can detect objects in the scene.



    But I have to split the neural network into 2 parts, let's say it has 16 layers, and I want to have one microservice handle the first 8 layers and give the output of the 8th layer to the next microservice and it takes the data from there and proceeds with the 9th layer (1st layer in the 2nd microservice).



    Sending an image to the 1st microservice will give the result from the 2nd microservice, is this feasible using TensorFlow?










    share|improve this question









    $endgroup$














      1












      1








      1





      $begingroup$


      I have a neural network, that is already trained locally that can detect objects in the scene.



      But I have to split the neural network into 2 parts, let's say it has 16 layers, and I want to have one microservice handle the first 8 layers and give the output of the 8th layer to the next microservice and it takes the data from there and proceeds with the 9th layer (1st layer in the 2nd microservice).



      Sending an image to the 1st microservice will give the result from the 2nd microservice, is this feasible using TensorFlow?










      share|improve this question









      $endgroup$




      I have a neural network, that is already trained locally that can detect objects in the scene.



      But I have to split the neural network into 2 parts, let's say it has 16 layers, and I want to have one microservice handle the first 8 layers and give the output of the 8th layer to the next microservice and it takes the data from there and proceeds with the 9th layer (1st layer in the 2nd microservice).



      Sending an image to the 1st microservice will give the result from the 2nd microservice, is this feasible using TensorFlow?







      deep-learning tensorflow cnn






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Feb 22 at 11:39









      AnilAnil

      1062




      1062




















          1 Answer
          1






          active

          oldest

          votes


















          1












          $begingroup$

          Yes, you can split the model into two parts after training. Not sure, what would be the advantage, but it is possible. High level steps :



          1. Define and train the model

          2. Save trained model

          3. Create two copies of model (Layers 1 to 8 and new input layer + 9 to 16)

          4. Transfer weights to these copies of model

          After this, service 1 can run first few layers and provide the output to service 2.



          Example code to split the model (Code uses Keras as wrapper over TensorFlow):



          import keras
          from keras.models import Model, load_model
          from keras.layers import Input, Dense
          from keras.optimizers import RMSprop
          import numpy as np


          # Create original model and save it
          inputs = Input((1,))
          dense_1 = Dense(10, activation='relu')(inputs)
          dense_2 = Dense(10, activation='relu')(dense_1)
          dense_3 = Dense(10, activation='relu')(dense_2)
          outputs = Dense(10)(dense_3)

          model = Model(inputs=inputs, outputs=outputs)
          model.compile(optimizer=RMSprop(), loss='mse')
          model.save('test.h5')


          # Load the model and make modifications to it
          loaded_model = load_model('test.h5')
          loaded_model.layers.pop()
          loaded_model.layers.pop()


          # Create your new model with the two layers removed and transfer weights
          new_model = Model(inputs=inputs, outputs=dense_1)
          new_model.compile(optimizer=RMSprop(), loss='mse')
          new_model.set_weights(loaded_model.get_weights())

          new_model.summary()
          new_model.save('test_complete.h5')


          Source : https://github.com/keras-team/keras/issues/8772






          share|improve this answer









          $endgroup$













            Your Answer





            StackExchange.ifUsing("editor", function ()
            return StackExchange.using("mathjaxEditing", function ()
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
            );
            );
            , "mathjax-editing");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "557"
            ;
            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: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            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%2fdatascience.stackexchange.com%2fquestions%2f46017%2fsplitting-a-neural-network-in-2-microservices%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









            1












            $begingroup$

            Yes, you can split the model into two parts after training. Not sure, what would be the advantage, but it is possible. High level steps :



            1. Define and train the model

            2. Save trained model

            3. Create two copies of model (Layers 1 to 8 and new input layer + 9 to 16)

            4. Transfer weights to these copies of model

            After this, service 1 can run first few layers and provide the output to service 2.



            Example code to split the model (Code uses Keras as wrapper over TensorFlow):



            import keras
            from keras.models import Model, load_model
            from keras.layers import Input, Dense
            from keras.optimizers import RMSprop
            import numpy as np


            # Create original model and save it
            inputs = Input((1,))
            dense_1 = Dense(10, activation='relu')(inputs)
            dense_2 = Dense(10, activation='relu')(dense_1)
            dense_3 = Dense(10, activation='relu')(dense_2)
            outputs = Dense(10)(dense_3)

            model = Model(inputs=inputs, outputs=outputs)
            model.compile(optimizer=RMSprop(), loss='mse')
            model.save('test.h5')


            # Load the model and make modifications to it
            loaded_model = load_model('test.h5')
            loaded_model.layers.pop()
            loaded_model.layers.pop()


            # Create your new model with the two layers removed and transfer weights
            new_model = Model(inputs=inputs, outputs=dense_1)
            new_model.compile(optimizer=RMSprop(), loss='mse')
            new_model.set_weights(loaded_model.get_weights())

            new_model.summary()
            new_model.save('test_complete.h5')


            Source : https://github.com/keras-team/keras/issues/8772






            share|improve this answer









            $endgroup$

















              1












              $begingroup$

              Yes, you can split the model into two parts after training. Not sure, what would be the advantage, but it is possible. High level steps :



              1. Define and train the model

              2. Save trained model

              3. Create two copies of model (Layers 1 to 8 and new input layer + 9 to 16)

              4. Transfer weights to these copies of model

              After this, service 1 can run first few layers and provide the output to service 2.



              Example code to split the model (Code uses Keras as wrapper over TensorFlow):



              import keras
              from keras.models import Model, load_model
              from keras.layers import Input, Dense
              from keras.optimizers import RMSprop
              import numpy as np


              # Create original model and save it
              inputs = Input((1,))
              dense_1 = Dense(10, activation='relu')(inputs)
              dense_2 = Dense(10, activation='relu')(dense_1)
              dense_3 = Dense(10, activation='relu')(dense_2)
              outputs = Dense(10)(dense_3)

              model = Model(inputs=inputs, outputs=outputs)
              model.compile(optimizer=RMSprop(), loss='mse')
              model.save('test.h5')


              # Load the model and make modifications to it
              loaded_model = load_model('test.h5')
              loaded_model.layers.pop()
              loaded_model.layers.pop()


              # Create your new model with the two layers removed and transfer weights
              new_model = Model(inputs=inputs, outputs=dense_1)
              new_model.compile(optimizer=RMSprop(), loss='mse')
              new_model.set_weights(loaded_model.get_weights())

              new_model.summary()
              new_model.save('test_complete.h5')


              Source : https://github.com/keras-team/keras/issues/8772






              share|improve this answer









              $endgroup$















                1












                1








                1





                $begingroup$

                Yes, you can split the model into two parts after training. Not sure, what would be the advantage, but it is possible. High level steps :



                1. Define and train the model

                2. Save trained model

                3. Create two copies of model (Layers 1 to 8 and new input layer + 9 to 16)

                4. Transfer weights to these copies of model

                After this, service 1 can run first few layers and provide the output to service 2.



                Example code to split the model (Code uses Keras as wrapper over TensorFlow):



                import keras
                from keras.models import Model, load_model
                from keras.layers import Input, Dense
                from keras.optimizers import RMSprop
                import numpy as np


                # Create original model and save it
                inputs = Input((1,))
                dense_1 = Dense(10, activation='relu')(inputs)
                dense_2 = Dense(10, activation='relu')(dense_1)
                dense_3 = Dense(10, activation='relu')(dense_2)
                outputs = Dense(10)(dense_3)

                model = Model(inputs=inputs, outputs=outputs)
                model.compile(optimizer=RMSprop(), loss='mse')
                model.save('test.h5')


                # Load the model and make modifications to it
                loaded_model = load_model('test.h5')
                loaded_model.layers.pop()
                loaded_model.layers.pop()


                # Create your new model with the two layers removed and transfer weights
                new_model = Model(inputs=inputs, outputs=dense_1)
                new_model.compile(optimizer=RMSprop(), loss='mse')
                new_model.set_weights(loaded_model.get_weights())

                new_model.summary()
                new_model.save('test_complete.h5')


                Source : https://github.com/keras-team/keras/issues/8772






                share|improve this answer









                $endgroup$



                Yes, you can split the model into two parts after training. Not sure, what would be the advantage, but it is possible. High level steps :



                1. Define and train the model

                2. Save trained model

                3. Create two copies of model (Layers 1 to 8 and new input layer + 9 to 16)

                4. Transfer weights to these copies of model

                After this, service 1 can run first few layers and provide the output to service 2.



                Example code to split the model (Code uses Keras as wrapper over TensorFlow):



                import keras
                from keras.models import Model, load_model
                from keras.layers import Input, Dense
                from keras.optimizers import RMSprop
                import numpy as np


                # Create original model and save it
                inputs = Input((1,))
                dense_1 = Dense(10, activation='relu')(inputs)
                dense_2 = Dense(10, activation='relu')(dense_1)
                dense_3 = Dense(10, activation='relu')(dense_2)
                outputs = Dense(10)(dense_3)

                model = Model(inputs=inputs, outputs=outputs)
                model.compile(optimizer=RMSprop(), loss='mse')
                model.save('test.h5')


                # Load the model and make modifications to it
                loaded_model = load_model('test.h5')
                loaded_model.layers.pop()
                loaded_model.layers.pop()


                # Create your new model with the two layers removed and transfer weights
                new_model = Model(inputs=inputs, outputs=dense_1)
                new_model.compile(optimizer=RMSprop(), loss='mse')
                new_model.set_weights(loaded_model.get_weights())

                new_model.summary()
                new_model.save('test_complete.h5')


                Source : https://github.com/keras-team/keras/issues/8772







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Feb 22 at 11:56









                Shamit VermaShamit Verma

                1,1191211




                1,1191211



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Data Science Stack Exchange!


                    • 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.

                    Use MathJax to format equations. MathJax reference.


                    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%2fdatascience.stackexchange.com%2fquestions%2f46017%2fsplitting-a-neural-network-in-2-microservices%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

                    Marja Vauras Lähteet | Aiheesta muualla | NavigointivalikkoMarja Vauras Turun yliopiston tutkimusportaalissaInfobox OKSuomalaisen Tiedeakatemian varsinaiset jäsenetKasvatustieteiden tiedekunnan dekaanit ja muu johtoMarja VaurasKoulutusvienti on kestävyys- ja ketteryyslaji (2.5.2017)laajentamallaWorldCat Identities0000 0001 0855 9405n86069603utb201588738523620927

                    Which is better: GPT or RelGAN for text generation?2019 Community Moderator ElectionWhat is the difference between TextGAN and LM for text generation?GANs (generative adversarial networks) possible for text as well?Generator loss not decreasing- text to image synthesisChoosing a right algorithm for template-based text generationHow should I format input and output for text generation with LSTMsGumbel Softmax vs Vanilla Softmax for GAN trainingWhich neural network to choose for classification from text/speech?NLP text autoencoder that generates text in poetic meterWhat is the interpretation of the expectation notation in the GAN formulation?What is the difference between TextGAN and LM for text generation?How to prepare the data for text generation task

                    Is flight data recorder erased after every flight?When are black boxes used?What protects the location beacon (pinger) of a flight data recorder?Is there anywhere I can pick up raw flight data recorder information?Who legally owns the Flight Data Recorder?Constructing flight recorder dataWhy are FDRs and CVRs still two separate physical devices?What are the data elements shown on the GE235 flight data recorder (FDR) plot?Are CVR and FDR reset after every flight?What is the format of data stored by a Flight Data Recorder?How much data is stored in the flight data recorder per hour in a typical flight of an A380?Is a smart flight data recorder possible?