{
  "id": 71179,
  "title": "How do I use higher resolution images in Resnet50 based model?",
  "url": "/competitions/human-protein-atlas-image-classification/discussion/71179",
  "author_name": "Varun Prabhu",
  "post_date": "2018-11-11T04:30:22.927000",
  "votes": 0,
  "comment_count": 7,
  "views": 0,
  "content": "<p>I've trained a model with Resnet50 as a base and used 224x224 resolution (Keras with Tensorflow backend). I saved the weights following that training. I want to use the 512x512 sized one and possibly the full sized ones after that. When I try to train with the 512x512 sized images using these weights, i get errors, definitely due to the difference in image size. I want to know what I need to change in the model below to allow higher res models to be used as training inputs. Inputs and explanation appreciated.</p>\n\n<p>`  </p>\n\n<pre><code>def protein_model(input_shape, num_classes):    \n\ninput_tensor = Input(shape = input_shape)\n\nbase_model = ResNet50(include_top = False, weights = 'imagenet', input_shape = input_shape)\n\nbatch_norm = BatchNormalization()(input_tensor)\nX = base_model(batch_norm)\nX = Conv2D(32, kernel_size = (1,1), activation = 'relu')(X)\nX = Flatten()(X)\nX = Dense(256, activation='relu')(X)\nX = Dropout(0.7)(X)\nX = Dense(256, activation='relu')(X)\nX = Dropout(0.7)(X)\noutput = Dense(num_classes, activation = 'sigmoid')(X)\nmodel = Model(input_tensor, output)\nreturn model`\n</code></pre>",
  "messages": [
    {
      "id": 419462,
      "postDate": "2018-11-12T02:54:42.660Z",
      "content": "<p>In general, convolutional models can simply use any input dimension. So you need <code>input_shape = (None,None,channels)</code>.   </p>\n\n<p>But the <code>Flatten</code> layer cannot handle this (the number of parameters for the following <code>Dense</code> layer would change). \nThus, instead of a flatten layer, you need a <code>GlobalAveragePooling2D</code> or a <code>GlobalMaxPooling2D</code>. </p>\n\n<p>Unfortunately, this model that was trained with <code>Flatten</code> cannot be reused, unless you create some extra layers to make the compatibility with the dimensions (the Dense layer after it is expecting a specific number of inputs). But these extra layers would not retain the logic and the <code>Dense</code> layers would need entire retraining.    </p>\n\n<p>If you create these extra layers, you will need to load weights with <code>by_name=True</code>. This will ignore the different number of layers and load every weight matrix if the layer names in the new model match the layer names in the old model. </p>",
      "rawMarkdown": "In general, convolutional models can simply use any input dimension. So you need `input_shape = (None,None,channels)`.   \n\nBut the `Flatten` layer cannot handle this (the number of parameters for the following `Dense` layer would change). \nThus, instead of a flatten layer, you need a `GlobalAveragePooling2D` or a `GlobalMaxPooling2D`. \n\nUnfortunately, this model that was trained with `Flatten` cannot be reused, unless you create some extra layers to make the compatibility with the dimensions (the Dense layer after it is expecting a specific number of inputs). But these extra layers would not retain the logic and the `Dense` layers would need entire retraining.    \n\nIf you create these extra layers, you will need to load weights with `by_name=True`. This will ignore the different number of layers and load every weight matrix if the layer names in the new model match the layer names in the old model. ",
      "votes": 3,
      "replies": [
        {
          "id": 419633,
          "postDate": "2018-11-12T10:32:27.633Z",
          "content": "<p>I initially didn't understand what you said so I did a model.summary with the different input sizes and that helped me figure out what you said about the dense layer. This is where I was facing these issues. Thanks!</p>",
          "rawMarkdown": "I initially didn't understand what you said so I did a model.summary with the different input sizes and that helped me figure out what you said about the dense layer. This is where I was facing these issues. Thanks!",
          "votes": 1
        }
      ]
    },
    {
      "id": 419005,
      "postDate": "2018-11-11T04:30:22.927Z",
      "content": "<p>I've trained a model with Resnet50 as a base and used 224x224 resolution (Keras with Tensorflow backend). I saved the weights following that training. I want to use the 512x512 sized one and possibly the full sized ones after that. When I try to train with the 512x512 sized images using these weights, i get errors, definitely due to the difference in image size. I want to know what I need to change in the model below to allow higher res models to be used as training inputs. Inputs and explanation appreciated.</p>\n\n<p>`  </p>\n\n<pre><code>def protein_model(input_shape, num_classes):    \n\ninput_tensor = Input(shape = input_shape)\n\nbase_model = ResNet50(include_top = False, weights = 'imagenet', input_shape = input_shape)\n\nbatch_norm = BatchNormalization()(input_tensor)\nX = base_model(batch_norm)\nX = Conv2D(32, kernel_size = (1,1), activation = 'relu')(X)\nX = Flatten()(X)\nX = Dense(256, activation='relu')(X)\nX = Dropout(0.7)(X)\nX = Dense(256, activation='relu')(X)\nX = Dropout(0.7)(X)\noutput = Dense(num_classes, activation = 'sigmoid')(X)\nmodel = Model(input_tensor, output)\nreturn model`\n</code></pre>",
      "rawMarkdown": "I've trained a model with Resnet50 as a base and used 224x224 resolution (Keras with Tensorflow backend). I saved the weights following that training. I want to use the 512x512 sized one and possibly the full sized ones after that. When I try to train with the 512x512 sized images using these weights, i get errors, definitely due to the difference in image size. I want to know what I need to change in the model below to allow higher res models to be used as training inputs. Inputs and explanation appreciated.\n\n`  \n\n\n    def protein_model(input_shape, num_classes):    \n\n    input_tensor = Input(shape = input_shape)\n\n    base_model = ResNet50(include_top = False, weights = 'imagenet', input_shape = input_shape)\n\n    batch_norm = BatchNormalization()(input_tensor)\n    X = base_model(batch_norm)\n    X = Conv2D(32, kernel_size = (1,1), activation = 'relu')(X)\n    X = Flatten()(X)\n    X = Dense(256, activation='relu')(X)\n    X = Dropout(0.7)(X)\n    X = Dense(256, activation='relu')(X)\n    X = Dropout(0.7)(X)\n    output = Dense(num_classes, activation = 'sigmoid')(X)\n    model = Model(input_tensor, output)\n    return model`",
      "votes": 1
    },
    {
      "id": 419102,
      "postDate": "2018-11-11T09:16:35.897Z",
      "content": "<p>a simple trick is to replace the default average pooling layer with adaptive average layer. plz note that the function name might be different in keras.</p>",
      "rawMarkdown": "a simple trick is to replace the default average pooling layer with adaptive average layer. plz note that the function name might be different in keras.",
      "votes": 2
    },
    {
      "id": 621550,
      "postDate": "2019-09-08T16:43:25.583Z",
      "content": "<p>This blog gives a good explanation on how to resize your images. Although they have implemented it using VGG16\narchitecture, it is similar to what you would be doing with ResNet.\nI haven't tried it myself but hope it helps.\n<a href=\"https://www.pyimagesearch.com/2019/06/24/change-input-shape-dimensions-for-fine-tuning-with-keras/\">https://www.pyimagesearch.com/2019/06/24/change-input-shape-dimensions-for-fine-tuning-with-keras/</a></p>",
      "rawMarkdown": "This blog gives a good explanation on how to resize your images. Although they have implemented it using VGG16\narchitecture, it is similar to what you would be doing with ResNet.\nI haven't tried it myself but hope it helps.\nhttps://www.pyimagesearch.com/2019/06/24/change-input-shape-dimensions-for-fine-tuning-with-keras/"
    },
    {
      "id": 419074,
      "postDate": "2018-11-11T07:40:14.110Z",
      "rawMarkdown": "",
      "isDeleted": true
    },
    {
      "id": 419047,
      "postDate": "2018-11-11T06:21:00.103Z",
      "content": "<p>if you want to use pretrained models for weight initilization,(for resnet50) you should use (224x224) input size.  or train the model from scratch, then freeze layers[except classification layers(last layers)] and train again (<a href=\"https://www.kaggle.com/rejpalcz/gapnet-pl-lb-0-385\">https://www.kaggle.com/rejpalcz/gapnet-pl-lb-0-385</a>)</p>\n\n<p></p><hr><p></p>\n\n<h2>Train From Scratch Code</h2>\n\n<p><code>input_tensor = Input(shape = input_shape) ;\nbase_model = ResNet50(include_top = True, weights = None, input_shape = input_shape, input_tensor=input_tensor) ;\nbatch_norm = BatchNormalization()(base_model.output)\n</code></p>",
      "rawMarkdown": "if you want to use pretrained models for weight initilization,(for resnet50) you should use (224x224) input size.  or train the model from scratch, then freeze layers[except classification layers(last layers)] and train again (https://www.kaggle.com/rejpalcz/gapnet-pl-lb-0-385)\n\n<hr>\n\n<h2>Train From Scratch Code</h2>\n<code>input_tensor = Input(shape = input_shape) ;\nbase_model = ResNet50(include_top = True, weights = None, input_shape = input_shape, input_tensor=input_tensor) ;\nbatch_norm = BatchNormalization()(base_model.output)\n</code>\n",
      "isDeleted": true
    },
    {
      "id": 419632,
      "postDate": "2018-11-12T10:29:37.193Z",
      "content": "<p>Thank you for your inputs, everyone.</p>",
      "rawMarkdown": "Thank you for your inputs, everyone."
    }
  ],
  "comments": [
    {
      "id": 419462,
      "author_name": "Daniel Möller",
      "author_url": "",
      "post_date": "2018-11-12T02:54:42.660000",
      "content": "<p>In general, convolutional models can simply use any input dimension. So you need <code>input_shape = (None,None,channels)</code>.   </p>\n\n<p>But the <code>Flatten</code> layer cannot handle this (the number of parameters for the following <code>Dense</code> layer would change). \nThus, instead of a flatten layer, you need a <code>GlobalAveragePooling2D</code> or a <code>GlobalMaxPooling2D</code>. </p>\n\n<p>Unfortunately, this model that was trained with <code>Flatten</code> cannot be reused, unless you create some extra layers to make the compatibility with the dimensions (the Dense layer after it is expecting a specific number of inputs). But these extra layers would not retain the logic and the <code>Dense</code> layers would need entire retraining.    </p>\n\n<p>If you create these extra layers, you will need to load weights with <code>by_name=True</code>. This will ignore the different number of layers and load every weight matrix if the layer names in the new model match the layer names in the old model. </p>",
      "votes": 3,
      "replies": [
        {
          "id": 419633,
          "author_name": "Varun Prabhu",
          "author_url": "",
          "post_date": "2018-11-12T10:32:27.633000",
          "content": "<p>I initially didn't understand what you said so I did a model.summary with the different input sizes and that helped me figure out what you said about the dense layer. This is where I was facing these issues. Thanks!</p>",
          "votes": 1,
          "replies": []
        }
      ]
    },
    {
      "id": 419102,
      "author_name": "Paul Chen",
      "author_url": "",
      "post_date": "2018-11-11T09:16:35.897000",
      "content": "<p>a simple trick is to replace the default average pooling layer with adaptive average layer. plz note that the function name might be different in keras.</p>",
      "votes": 2,
      "replies": []
    },
    {
      "id": 621550,
      "author_name": "Rohit Bohra",
      "author_url": "",
      "post_date": "2019-09-08T16:43:25.583000",
      "content": "<p>This blog gives a good explanation on how to resize your images. Although they have implemented it using VGG16\narchitecture, it is similar to what you would be doing with ResNet.\nI haven't tried it myself but hope it helps.\n<a href=\"https://www.pyimagesearch.com/2019/06/24/change-input-shape-dimensions-for-fine-tuning-with-keras/\">https://www.pyimagesearch.com/2019/06/24/change-input-shape-dimensions-for-fine-tuning-with-keras/</a></p>",
      "votes": 0,
      "replies": []
    },
    {
      "id": 419074,
      "author_name": "",
      "author_url": "",
      "post_date": "2018-11-11T07:40:14.110000",
      "content": "",
      "votes": 0,
      "replies": []
    },
    {
      "id": 419047,
      "author_name": "",
      "author_url": "",
      "post_date": "2018-11-11T06:21:00.103000",
      "content": "<p>if you want to use pretrained models for weight initilization,(for resnet50) you should use (224x224) input size.  or train the model from scratch, then freeze layers[except classification layers(last layers)] and train again (<a href=\"https://www.kaggle.com/rejpalcz/gapnet-pl-lb-0-385\">https://www.kaggle.com/rejpalcz/gapnet-pl-lb-0-385</a>)</p>\n\n<p></p><hr><p></p>\n\n<h2>Train From Scratch Code</h2>\n\n<p><code>input_tensor = Input(shape = input_shape) ;\nbase_model = ResNet50(include_top = True, weights = None, input_shape = input_shape, input_tensor=input_tensor) ;\nbatch_norm = BatchNormalization()(base_model.output)\n</code></p>",
      "votes": 0,
      "replies": []
    },
    {
      "id": 419632,
      "author_name": "Varun Prabhu",
      "author_url": "",
      "post_date": "2018-11-12T10:29:37.193000",
      "content": "<p>Thank you for your inputs, everyone.</p>",
      "votes": 0,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "419462": "In general, convolutional models can simply use any input dimension. So you need `input_shape = (None,None,channels)`.   \n\nBut the `Flatten` layer cannot handle this (the number of parameters for the following `Dense` layer would change). \nThus, instead of a flatten layer, you need a `GlobalAveragePooling2D` or a `GlobalMaxPooling2D`. \n\nUnfortunately, this model that was trained with `Flatten` cannot be reused, unless you create some extra layers to make the compatibility with the dimensions (the Dense layer after it is expecting a specific number of inputs). But these extra layers would not retain the logic and the `Dense` layers would need entire retraining.    \n\nIf you create these extra layers, you will need to load weights with `by_name=True`. This will ignore the different number of layers and load every weight matrix if the layer names in the new model match the layer names in the old model. ",
    "419005": "I've trained a model with Resnet50 as a base and used 224x224 resolution (Keras with Tensorflow backend). I saved the weights following that training. I want to use the 512x512 sized one and possibly the full sized ones after that. When I try to train with the 512x512 sized images using these weights, i get errors, definitely due to the difference in image size. I want to know what I need to change in the model below to allow higher res models to be used as training inputs. Inputs and explanation appreciated.\n\n`  \n\n\n    def protein_model(input_shape, num_classes):    \n\n    input_tensor = Input(shape = input_shape)\n\n    base_model = ResNet50(include_top = False, weights = 'imagenet', input_shape = input_shape)\n\n    batch_norm = BatchNormalization()(input_tensor)\n    X = base_model(batch_norm)\n    X = Conv2D(32, kernel_size = (1,1), activation = 'relu')(X)\n    X = Flatten()(X)\n    X = Dense(256, activation='relu')(X)\n    X = Dropout(0.7)(X)\n    X = Dense(256, activation='relu')(X)\n    X = Dropout(0.7)(X)\n    output = Dense(num_classes, activation = 'sigmoid')(X)\n    model = Model(input_tensor, output)\n    return model`",
    "419102": "a simple trick is to replace the default average pooling layer with adaptive average layer. plz note that the function name might be different in keras.",
    "621550": "This blog gives a good explanation on how to resize your images. Although they have implemented it using VGG16\narchitecture, it is similar to what you would be doing with ResNet.\nI haven't tried it myself but hope it helps.\nhttps://www.pyimagesearch.com/2019/06/24/change-input-shape-dimensions-for-fine-tuning-with-keras/",
    "419074": "",
    "419047": "if you want to use pretrained models for weight initilization,(for resnet50) you should use (224x224) input size.  or train the model from scratch, then freeze layers[except classification layers(last layers)] and train again (https://www.kaggle.com/rejpalcz/gapnet-pl-lb-0-385)\n\n<hr>\n\n<h2>Train From Scratch Code</h2>\n<code>input_tensor = Input(shape = input_shape) ;\nbase_model = ResNet50(include_top = True, weights = None, input_shape = input_shape, input_tensor=input_tensor) ;\nbatch_norm = BatchNormalization()(base_model.output)\n</code>\n",
    "419632": "Thank you for your inputs, everyone."
  }
}