Getting the filters values from CNN layersHow to get the current time in PythonHow do I sort a dictionary by value?Cannot make this autoencoder network function properly (with convolutional and maxpool layers)Convolutional Autoencoders: Black Feature MapsIssue with tf.nn.max_poolOutput of conv2d in kerasHow to get stride value in Conv2D layer Tensorflow?Collapsing consecutive linear layersCNN padding and stridingtensorflow: filters vs kernels and strides

Why didn't Nick Fury expose the villain's identity and plans?

Where should I connect my modem in this ethernet junction box?

How can I effectively communicate to recruiters that a phone call is not possible?

Find The One Element In An Array That is Different From The Others

Would dual wielding daggers be a viable choice for a covert bodyguard?

Print the last, middle and first character of your code

Why can a destructor change the state of a constant object?

Civil War story: man hanged from a bridge

Why are Hobbits so fond of mushrooms?

Machine learning and operations research projects

Do you know your 'KVZ's?

How to loop for 3 times in bash script when docker push fails?

How is angular momentum conserved for the orbiting body if the centripetal force disappears?

Can the Mage Hand cantrip be used to trip an enemy who is running away?

Has anyone in space seen or photographed a simple laser pointer from Earth?

Fivenum and a little bit

How to md5 a list of filepaths contained in a file?

definition of "percentile"

When did Tsar Nicholas II become Tsar of Russia?

Misspelling my name on my mathematical publications

Is a 10th-level Transmutation wizard considered a shapechanger for the purpose of effects such as Moonbeam?

Optimization terminology: "Exact" v. "Approximate"

How to find the object of reference of a latin relative pronoun?

What's the minimum number of sensors for a hobby GPS waypoint-following UAV?



Getting the filters values from CNN layers


How to get the current time in PythonHow do I sort a dictionary by value?Cannot make this autoencoder network function properly (with convolutional and maxpool layers)Convolutional Autoencoders: Black Feature MapsIssue with tf.nn.max_poolOutput of conv2d in kerasHow to get stride value in Conv2D layer Tensorflow?Collapsing consecutive linear layersCNN padding and stridingtensorflow: filters vs kernels and strides






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















I have the following model (for example)



input_img = Input(shape=(224,224,1)) # size of the input image
x = Conv2D(64, (3, 3), strides=(1, 1), activation='relu', padding='same')(input_img)


I have several layers of such in my autoencoder model. I am particularly interested in the filters of the first layer. There are 64 filters each of size 3x3.



To get the filters, I tried using the following code:



x.layers[0].get_weights()[0]


but I am getting the error as follows:



AttributeError Traceback (most recent call last)
<ipython-input-166-96506292d6d7> in <module>()
4 x = Conv2D(64, (3, 3), strides=(1, 1), activation='relu', padding='same')(input_img)
5
----> 6 x.layers[0].get_weights()[0]

AttributeError: 'Tensor' object has no attribute 'layers'


I am not using the sequential model. My model will be formed using the following command after several such layers.



 model = Model()


I am new to CNN and I don't even know if the get_weights function can help me get filters value. How do I get value of filters?










share|improve this question






























    1















    I have the following model (for example)



    input_img = Input(shape=(224,224,1)) # size of the input image
    x = Conv2D(64, (3, 3), strides=(1, 1), activation='relu', padding='same')(input_img)


    I have several layers of such in my autoencoder model. I am particularly interested in the filters of the first layer. There are 64 filters each of size 3x3.



    To get the filters, I tried using the following code:



    x.layers[0].get_weights()[0]


    but I am getting the error as follows:



    AttributeError Traceback (most recent call last)
    <ipython-input-166-96506292d6d7> in <module>()
    4 x = Conv2D(64, (3, 3), strides=(1, 1), activation='relu', padding='same')(input_img)
    5
    ----> 6 x.layers[0].get_weights()[0]

    AttributeError: 'Tensor' object has no attribute 'layers'


    I am not using the sequential model. My model will be formed using the following command after several such layers.



     model = Model()


    I am new to CNN and I don't even know if the get_weights function can help me get filters value. How do I get value of filters?










    share|improve this question


























      1












      1








      1








      I have the following model (for example)



      input_img = Input(shape=(224,224,1)) # size of the input image
      x = Conv2D(64, (3, 3), strides=(1, 1), activation='relu', padding='same')(input_img)


      I have several layers of such in my autoencoder model. I am particularly interested in the filters of the first layer. There are 64 filters each of size 3x3.



      To get the filters, I tried using the following code:



      x.layers[0].get_weights()[0]


      but I am getting the error as follows:



      AttributeError Traceback (most recent call last)
      <ipython-input-166-96506292d6d7> in <module>()
      4 x = Conv2D(64, (3, 3), strides=(1, 1), activation='relu', padding='same')(input_img)
      5
      ----> 6 x.layers[0].get_weights()[0]

      AttributeError: 'Tensor' object has no attribute 'layers'


      I am not using the sequential model. My model will be formed using the following command after several such layers.



       model = Model()


      I am new to CNN and I don't even know if the get_weights function can help me get filters value. How do I get value of filters?










      share|improve this question
















      I have the following model (for example)



      input_img = Input(shape=(224,224,1)) # size of the input image
      x = Conv2D(64, (3, 3), strides=(1, 1), activation='relu', padding='same')(input_img)


      I have several layers of such in my autoencoder model. I am particularly interested in the filters of the first layer. There are 64 filters each of size 3x3.



      To get the filters, I tried using the following code:



      x.layers[0].get_weights()[0]


      but I am getting the error as follows:



      AttributeError Traceback (most recent call last)
      <ipython-input-166-96506292d6d7> in <module>()
      4 x = Conv2D(64, (3, 3), strides=(1, 1), activation='relu', padding='same')(input_img)
      5
      ----> 6 x.layers[0].get_weights()[0]

      AttributeError: 'Tensor' object has no attribute 'layers'


      I am not using the sequential model. My model will be formed using the following command after several such layers.



       model = Model()


      I am new to CNN and I don't even know if the get_weights function can help me get filters value. How do I get value of filters?







      python tensorflow conv-neural-network keras-layer autoencoder






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 26 at 2:30







      enjal

















      asked Mar 26 at 2:18









      enjalenjal

      3851 gold badge8 silver badges21 bronze badges




      3851 gold badge8 silver badges21 bronze badges






















          1 Answer
          1






          active

          oldest

          votes


















          0














          At the moment your code is calling the layers function on a layer definition itself.



          The model first needs to be compiled and then you can use the layers function on the model to retrieve the weights of the specific layer.



          In your case:



          weights = model.layers[1].get_weights()


          will give you the set of weights of the 1st convolutional layer



          Which you can use after compiling the model:



          model = Model(inputs=input_img, output=b)


          Where b refers to the last layer in your model.






          share|improve this answer

























          • The convolutional layer can be accessed through the index [1] in the call to get_weights, since the first element will now be your input layer

            – JimmyOnThePage
            Mar 26 at 2:45











          • It gives me an error IndexError: list index out of range. Also, I want to get the filters on the first layer to apply geometric translations on those filters. I don't think this is possible after I compile it. What do you think about it?

            – enjal
            Mar 26 at 4:11












          • As mentioned in the comment, calling the function on the input layer will give you that error, since the input layer does not have weights.

            – JimmyOnThePage
            Mar 26 at 4:18











          • Not sure I understand the second part of your comment. Are you attempting to create a layer in the model that is a 'geometric translation' (you will have to clarify on this) of the first convolutional layer? You do realise the model is initially compiled with random weights, and first needs to be trained? Because I see no reason to apply transformations to filters containing random weights

            – JimmyOnThePage
            Mar 26 at 4:21











          • I did it after I compiled my model the same way you showed in the answer but still got the error.

            – enjal
            Mar 26 at 4:47










          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%2f55348940%2fgetting-the-filters-values-from-cnn-layers%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









          0














          At the moment your code is calling the layers function on a layer definition itself.



          The model first needs to be compiled and then you can use the layers function on the model to retrieve the weights of the specific layer.



          In your case:



          weights = model.layers[1].get_weights()


          will give you the set of weights of the 1st convolutional layer



          Which you can use after compiling the model:



          model = Model(inputs=input_img, output=b)


          Where b refers to the last layer in your model.






          share|improve this answer

























          • The convolutional layer can be accessed through the index [1] in the call to get_weights, since the first element will now be your input layer

            – JimmyOnThePage
            Mar 26 at 2:45











          • It gives me an error IndexError: list index out of range. Also, I want to get the filters on the first layer to apply geometric translations on those filters. I don't think this is possible after I compile it. What do you think about it?

            – enjal
            Mar 26 at 4:11












          • As mentioned in the comment, calling the function on the input layer will give you that error, since the input layer does not have weights.

            – JimmyOnThePage
            Mar 26 at 4:18











          • Not sure I understand the second part of your comment. Are you attempting to create a layer in the model that is a 'geometric translation' (you will have to clarify on this) of the first convolutional layer? You do realise the model is initially compiled with random weights, and first needs to be trained? Because I see no reason to apply transformations to filters containing random weights

            – JimmyOnThePage
            Mar 26 at 4:21











          • I did it after I compiled my model the same way you showed in the answer but still got the error.

            – enjal
            Mar 26 at 4:47















          0














          At the moment your code is calling the layers function on a layer definition itself.



          The model first needs to be compiled and then you can use the layers function on the model to retrieve the weights of the specific layer.



          In your case:



          weights = model.layers[1].get_weights()


          will give you the set of weights of the 1st convolutional layer



          Which you can use after compiling the model:



          model = Model(inputs=input_img, output=b)


          Where b refers to the last layer in your model.






          share|improve this answer

























          • The convolutional layer can be accessed through the index [1] in the call to get_weights, since the first element will now be your input layer

            – JimmyOnThePage
            Mar 26 at 2:45











          • It gives me an error IndexError: list index out of range. Also, I want to get the filters on the first layer to apply geometric translations on those filters. I don't think this is possible after I compile it. What do you think about it?

            – enjal
            Mar 26 at 4:11












          • As mentioned in the comment, calling the function on the input layer will give you that error, since the input layer does not have weights.

            – JimmyOnThePage
            Mar 26 at 4:18











          • Not sure I understand the second part of your comment. Are you attempting to create a layer in the model that is a 'geometric translation' (you will have to clarify on this) of the first convolutional layer? You do realise the model is initially compiled with random weights, and first needs to be trained? Because I see no reason to apply transformations to filters containing random weights

            – JimmyOnThePage
            Mar 26 at 4:21











          • I did it after I compiled my model the same way you showed in the answer but still got the error.

            – enjal
            Mar 26 at 4:47













          0












          0








          0







          At the moment your code is calling the layers function on a layer definition itself.



          The model first needs to be compiled and then you can use the layers function on the model to retrieve the weights of the specific layer.



          In your case:



          weights = model.layers[1].get_weights()


          will give you the set of weights of the 1st convolutional layer



          Which you can use after compiling the model:



          model = Model(inputs=input_img, output=b)


          Where b refers to the last layer in your model.






          share|improve this answer















          At the moment your code is calling the layers function on a layer definition itself.



          The model first needs to be compiled and then you can use the layers function on the model to retrieve the weights of the specific layer.



          In your case:



          weights = model.layers[1].get_weights()


          will give you the set of weights of the 1st convolutional layer



          Which you can use after compiling the model:



          model = Model(inputs=input_img, output=b)


          Where b refers to the last layer in your model.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 26 at 4:26

























          answered Mar 26 at 2:39









          JimmyOnThePageJimmyOnThePage

          5483 silver badges15 bronze badges




          5483 silver badges15 bronze badges












          • The convolutional layer can be accessed through the index [1] in the call to get_weights, since the first element will now be your input layer

            – JimmyOnThePage
            Mar 26 at 2:45











          • It gives me an error IndexError: list index out of range. Also, I want to get the filters on the first layer to apply geometric translations on those filters. I don't think this is possible after I compile it. What do you think about it?

            – enjal
            Mar 26 at 4:11












          • As mentioned in the comment, calling the function on the input layer will give you that error, since the input layer does not have weights.

            – JimmyOnThePage
            Mar 26 at 4:18











          • Not sure I understand the second part of your comment. Are you attempting to create a layer in the model that is a 'geometric translation' (you will have to clarify on this) of the first convolutional layer? You do realise the model is initially compiled with random weights, and first needs to be trained? Because I see no reason to apply transformations to filters containing random weights

            – JimmyOnThePage
            Mar 26 at 4:21











          • I did it after I compiled my model the same way you showed in the answer but still got the error.

            – enjal
            Mar 26 at 4:47

















          • The convolutional layer can be accessed through the index [1] in the call to get_weights, since the first element will now be your input layer

            – JimmyOnThePage
            Mar 26 at 2:45











          • It gives me an error IndexError: list index out of range. Also, I want to get the filters on the first layer to apply geometric translations on those filters. I don't think this is possible after I compile it. What do you think about it?

            – enjal
            Mar 26 at 4:11












          • As mentioned in the comment, calling the function on the input layer will give you that error, since the input layer does not have weights.

            – JimmyOnThePage
            Mar 26 at 4:18











          • Not sure I understand the second part of your comment. Are you attempting to create a layer in the model that is a 'geometric translation' (you will have to clarify on this) of the first convolutional layer? You do realise the model is initially compiled with random weights, and first needs to be trained? Because I see no reason to apply transformations to filters containing random weights

            – JimmyOnThePage
            Mar 26 at 4:21











          • I did it after I compiled my model the same way you showed in the answer but still got the error.

            – enjal
            Mar 26 at 4:47
















          The convolutional layer can be accessed through the index [1] in the call to get_weights, since the first element will now be your input layer

          – JimmyOnThePage
          Mar 26 at 2:45





          The convolutional layer can be accessed through the index [1] in the call to get_weights, since the first element will now be your input layer

          – JimmyOnThePage
          Mar 26 at 2:45













          It gives me an error IndexError: list index out of range. Also, I want to get the filters on the first layer to apply geometric translations on those filters. I don't think this is possible after I compile it. What do you think about it?

          – enjal
          Mar 26 at 4:11






          It gives me an error IndexError: list index out of range. Also, I want to get the filters on the first layer to apply geometric translations on those filters. I don't think this is possible after I compile it. What do you think about it?

          – enjal
          Mar 26 at 4:11














          As mentioned in the comment, calling the function on the input layer will give you that error, since the input layer does not have weights.

          – JimmyOnThePage
          Mar 26 at 4:18





          As mentioned in the comment, calling the function on the input layer will give you that error, since the input layer does not have weights.

          – JimmyOnThePage
          Mar 26 at 4:18













          Not sure I understand the second part of your comment. Are you attempting to create a layer in the model that is a 'geometric translation' (you will have to clarify on this) of the first convolutional layer? You do realise the model is initially compiled with random weights, and first needs to be trained? Because I see no reason to apply transformations to filters containing random weights

          – JimmyOnThePage
          Mar 26 at 4:21





          Not sure I understand the second part of your comment. Are you attempting to create a layer in the model that is a 'geometric translation' (you will have to clarify on this) of the first convolutional layer? You do realise the model is initially compiled with random weights, and first needs to be trained? Because I see no reason to apply transformations to filters containing random weights

          – JimmyOnThePage
          Mar 26 at 4:21













          I did it after I compiled my model the same way you showed in the answer but still got the error.

          – enjal
          Mar 26 at 4:47





          I did it after I compiled my model the same way you showed in the answer but still got the error.

          – enjal
          Mar 26 at 4:47








          Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







          Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















          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.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55348940%2fgetting-the-filters-values-from-cnn-layers%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

          SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

          용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

          155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해