How to do transfer learning on a pre-trained ResNet50 with different image size












0












$begingroup$


I have a pretrained ResNet model which is trained on 64x64 images. I would like to do transfer learning with new dataset that contains 200x200 images.



I am loading the model like:



model = ResNet50(include_top=False, weights=None, input_shape=(64,64,3))
model.load_weights("a trained model weights on 64x64")

model.layers.pop()
for layer in model.layers:
layer.trainable = False

x = model.output
x = MaxPooling2D((2,2), strides=(2,2), padding='same')(x)
x = Flatten(name='flatten')(x)
x = Dropout(0.2)(x)
x = Dense(512, activation='relu')(x)
predictions = Dense(101, activation='softmax', name='predictions')(x)

top_model = Model(inputs=model.input, outputs=predictions)

top_model.compile(loss='categorical_crossentropy',
optimizer=adam,
metrics=[accuracy])

EPOCHS = 100
BATCH_SIZE = 32
STEPS_PER_EPOCH = 4424 // BATCH_SIZE
VALIDATION_STEPS = 466 // BATCH_SIZE

callbacks = [LearningRateScheduler(schedule=Schedule(EPOCHS, initial_lr=lr_rate)),
ModelCheckpoint(str(output_dir) + "/weights.{epoch:03d}-{val_loss:.3f}-{val_age_mae:.3f}.hdf5",
monitor="val_age_mae",
verbose=1,
save_best_only=False,
mode="min")
]

hist = top_model.fit_generator(generator=train_set,
epochs=EPOCHS,
steps_per_epoch = STEPS_PER_EPOCH,
validation_data=val_set,
validation_steps = VALIDATION_STEPS,
verbose=1,
callbacks=callbacks)




I would like to do transfer learning based with images of 200x200 pixels. I am very new to this, how can I modify?



is there a way to modify the model input shape? and do I. need to do something with spatial size?



And which optimizer is recommended? Adam or SGD?




__________________________________________________________________________________________________
res5c_branch2a (Conv2D) (None, 2, 2, 512) 1049088 activation_46[0][0]
__________________________________________________________________________________________________
bn5c_branch2a (BatchNormalizati (None, 2, 2, 512) 2048 res5c_branch2a[0][0]
__________________________________________________________________________________________________
activation_47 (Activation) (None, 2, 2, 512) 0 bn5c_branch2a[0][0]
__________________________________________________________________________________________________
res5c_branch2b (Conv2D) (None, 2, 2, 512) 2359808 activation_47[0][0]
__________________________________________________________________________________________________
bn5c_branch2b (BatchNormalizati (None, 2, 2, 512) 2048 res5c_branch2b[0][0]
__________________________________________________________________________________________________
activation_48 (Activation) (None, 2, 2, 512) 0 bn5c_branch2b[0][0]
__________________________________________________________________________________________________
res5c_branch2c (Conv2D) (None, 2, 2, 2048) 1050624 activation_48[0][0]
__________________________________________________________________________________________________
bn5c_branch2c (BatchNormalizati (None, 2, 2, 2048) 8192 res5c_branch2c[0][0]
__________________________________________________________________________________________________
add_16 (Add) (None, 2, 2, 2048) 0 bn5c_branch2c[0][0]
activation_46[0][0]
__________________________________________________________________________________________________
activation_49 (Activation) (None, 2, 2, 2048) 0 add_16[0][0]
__________________________________________________________________________________________________
pred_age (Dense) (None, 2, 2, 101) 206848 activation_49[0][0]
==================================================================================================
Total params: 23,794,560
Trainable params: 23,741,440
Non-trainable params: 53,120
__________________________________________________________________________________________________



Getting the following error



ValueError: Error when checking input: expected input_1 to have shape (64, 64, 3) but got array with shape (128, 128, 3)









share|improve this question







New contributor




TheJokerAEZ is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$

















    0












    $begingroup$


    I have a pretrained ResNet model which is trained on 64x64 images. I would like to do transfer learning with new dataset that contains 200x200 images.



    I am loading the model like:



    model = ResNet50(include_top=False, weights=None, input_shape=(64,64,3))
    model.load_weights("a trained model weights on 64x64")

    model.layers.pop()
    for layer in model.layers:
    layer.trainable = False

    x = model.output
    x = MaxPooling2D((2,2), strides=(2,2), padding='same')(x)
    x = Flatten(name='flatten')(x)
    x = Dropout(0.2)(x)
    x = Dense(512, activation='relu')(x)
    predictions = Dense(101, activation='softmax', name='predictions')(x)

    top_model = Model(inputs=model.input, outputs=predictions)

    top_model.compile(loss='categorical_crossentropy',
    optimizer=adam,
    metrics=[accuracy])

    EPOCHS = 100
    BATCH_SIZE = 32
    STEPS_PER_EPOCH = 4424 // BATCH_SIZE
    VALIDATION_STEPS = 466 // BATCH_SIZE

    callbacks = [LearningRateScheduler(schedule=Schedule(EPOCHS, initial_lr=lr_rate)),
    ModelCheckpoint(str(output_dir) + "/weights.{epoch:03d}-{val_loss:.3f}-{val_age_mae:.3f}.hdf5",
    monitor="val_age_mae",
    verbose=1,
    save_best_only=False,
    mode="min")
    ]

    hist = top_model.fit_generator(generator=train_set,
    epochs=EPOCHS,
    steps_per_epoch = STEPS_PER_EPOCH,
    validation_data=val_set,
    validation_steps = VALIDATION_STEPS,
    verbose=1,
    callbacks=callbacks)




    I would like to do transfer learning based with images of 200x200 pixels. I am very new to this, how can I modify?



    is there a way to modify the model input shape? and do I. need to do something with spatial size?



    And which optimizer is recommended? Adam or SGD?




    __________________________________________________________________________________________________
    res5c_branch2a (Conv2D) (None, 2, 2, 512) 1049088 activation_46[0][0]
    __________________________________________________________________________________________________
    bn5c_branch2a (BatchNormalizati (None, 2, 2, 512) 2048 res5c_branch2a[0][0]
    __________________________________________________________________________________________________
    activation_47 (Activation) (None, 2, 2, 512) 0 bn5c_branch2a[0][0]
    __________________________________________________________________________________________________
    res5c_branch2b (Conv2D) (None, 2, 2, 512) 2359808 activation_47[0][0]
    __________________________________________________________________________________________________
    bn5c_branch2b (BatchNormalizati (None, 2, 2, 512) 2048 res5c_branch2b[0][0]
    __________________________________________________________________________________________________
    activation_48 (Activation) (None, 2, 2, 512) 0 bn5c_branch2b[0][0]
    __________________________________________________________________________________________________
    res5c_branch2c (Conv2D) (None, 2, 2, 2048) 1050624 activation_48[0][0]
    __________________________________________________________________________________________________
    bn5c_branch2c (BatchNormalizati (None, 2, 2, 2048) 8192 res5c_branch2c[0][0]
    __________________________________________________________________________________________________
    add_16 (Add) (None, 2, 2, 2048) 0 bn5c_branch2c[0][0]
    activation_46[0][0]
    __________________________________________________________________________________________________
    activation_49 (Activation) (None, 2, 2, 2048) 0 add_16[0][0]
    __________________________________________________________________________________________________
    pred_age (Dense) (None, 2, 2, 101) 206848 activation_49[0][0]
    ==================================================================================================
    Total params: 23,794,560
    Trainable params: 23,741,440
    Non-trainable params: 53,120
    __________________________________________________________________________________________________



    Getting the following error



    ValueError: Error when checking input: expected input_1 to have shape (64, 64, 3) but got array with shape (128, 128, 3)









    share|improve this question







    New contributor




    TheJokerAEZ is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.







    $endgroup$















      0












      0








      0





      $begingroup$


      I have a pretrained ResNet model which is trained on 64x64 images. I would like to do transfer learning with new dataset that contains 200x200 images.



      I am loading the model like:



      model = ResNet50(include_top=False, weights=None, input_shape=(64,64,3))
      model.load_weights("a trained model weights on 64x64")

      model.layers.pop()
      for layer in model.layers:
      layer.trainable = False

      x = model.output
      x = MaxPooling2D((2,2), strides=(2,2), padding='same')(x)
      x = Flatten(name='flatten')(x)
      x = Dropout(0.2)(x)
      x = Dense(512, activation='relu')(x)
      predictions = Dense(101, activation='softmax', name='predictions')(x)

      top_model = Model(inputs=model.input, outputs=predictions)

      top_model.compile(loss='categorical_crossentropy',
      optimizer=adam,
      metrics=[accuracy])

      EPOCHS = 100
      BATCH_SIZE = 32
      STEPS_PER_EPOCH = 4424 // BATCH_SIZE
      VALIDATION_STEPS = 466 // BATCH_SIZE

      callbacks = [LearningRateScheduler(schedule=Schedule(EPOCHS, initial_lr=lr_rate)),
      ModelCheckpoint(str(output_dir) + "/weights.{epoch:03d}-{val_loss:.3f}-{val_age_mae:.3f}.hdf5",
      monitor="val_age_mae",
      verbose=1,
      save_best_only=False,
      mode="min")
      ]

      hist = top_model.fit_generator(generator=train_set,
      epochs=EPOCHS,
      steps_per_epoch = STEPS_PER_EPOCH,
      validation_data=val_set,
      validation_steps = VALIDATION_STEPS,
      verbose=1,
      callbacks=callbacks)




      I would like to do transfer learning based with images of 200x200 pixels. I am very new to this, how can I modify?



      is there a way to modify the model input shape? and do I. need to do something with spatial size?



      And which optimizer is recommended? Adam or SGD?




      __________________________________________________________________________________________________
      res5c_branch2a (Conv2D) (None, 2, 2, 512) 1049088 activation_46[0][0]
      __________________________________________________________________________________________________
      bn5c_branch2a (BatchNormalizati (None, 2, 2, 512) 2048 res5c_branch2a[0][0]
      __________________________________________________________________________________________________
      activation_47 (Activation) (None, 2, 2, 512) 0 bn5c_branch2a[0][0]
      __________________________________________________________________________________________________
      res5c_branch2b (Conv2D) (None, 2, 2, 512) 2359808 activation_47[0][0]
      __________________________________________________________________________________________________
      bn5c_branch2b (BatchNormalizati (None, 2, 2, 512) 2048 res5c_branch2b[0][0]
      __________________________________________________________________________________________________
      activation_48 (Activation) (None, 2, 2, 512) 0 bn5c_branch2b[0][0]
      __________________________________________________________________________________________________
      res5c_branch2c (Conv2D) (None, 2, 2, 2048) 1050624 activation_48[0][0]
      __________________________________________________________________________________________________
      bn5c_branch2c (BatchNormalizati (None, 2, 2, 2048) 8192 res5c_branch2c[0][0]
      __________________________________________________________________________________________________
      add_16 (Add) (None, 2, 2, 2048) 0 bn5c_branch2c[0][0]
      activation_46[0][0]
      __________________________________________________________________________________________________
      activation_49 (Activation) (None, 2, 2, 2048) 0 add_16[0][0]
      __________________________________________________________________________________________________
      pred_age (Dense) (None, 2, 2, 101) 206848 activation_49[0][0]
      ==================================================================================================
      Total params: 23,794,560
      Trainable params: 23,741,440
      Non-trainable params: 53,120
      __________________________________________________________________________________________________



      Getting the following error



      ValueError: Error when checking input: expected input_1 to have shape (64, 64, 3) but got array with shape (128, 128, 3)









      share|improve this question







      New contributor




      TheJokerAEZ is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.







      $endgroup$




      I have a pretrained ResNet model which is trained on 64x64 images. I would like to do transfer learning with new dataset that contains 200x200 images.



      I am loading the model like:



      model = ResNet50(include_top=False, weights=None, input_shape=(64,64,3))
      model.load_weights("a trained model weights on 64x64")

      model.layers.pop()
      for layer in model.layers:
      layer.trainable = False

      x = model.output
      x = MaxPooling2D((2,2), strides=(2,2), padding='same')(x)
      x = Flatten(name='flatten')(x)
      x = Dropout(0.2)(x)
      x = Dense(512, activation='relu')(x)
      predictions = Dense(101, activation='softmax', name='predictions')(x)

      top_model = Model(inputs=model.input, outputs=predictions)

      top_model.compile(loss='categorical_crossentropy',
      optimizer=adam,
      metrics=[accuracy])

      EPOCHS = 100
      BATCH_SIZE = 32
      STEPS_PER_EPOCH = 4424 // BATCH_SIZE
      VALIDATION_STEPS = 466 // BATCH_SIZE

      callbacks = [LearningRateScheduler(schedule=Schedule(EPOCHS, initial_lr=lr_rate)),
      ModelCheckpoint(str(output_dir) + "/weights.{epoch:03d}-{val_loss:.3f}-{val_age_mae:.3f}.hdf5",
      monitor="val_age_mae",
      verbose=1,
      save_best_only=False,
      mode="min")
      ]

      hist = top_model.fit_generator(generator=train_set,
      epochs=EPOCHS,
      steps_per_epoch = STEPS_PER_EPOCH,
      validation_data=val_set,
      validation_steps = VALIDATION_STEPS,
      verbose=1,
      callbacks=callbacks)




      I would like to do transfer learning based with images of 200x200 pixels. I am very new to this, how can I modify?



      is there a way to modify the model input shape? and do I. need to do something with spatial size?



      And which optimizer is recommended? Adam or SGD?




      __________________________________________________________________________________________________
      res5c_branch2a (Conv2D) (None, 2, 2, 512) 1049088 activation_46[0][0]
      __________________________________________________________________________________________________
      bn5c_branch2a (BatchNormalizati (None, 2, 2, 512) 2048 res5c_branch2a[0][0]
      __________________________________________________________________________________________________
      activation_47 (Activation) (None, 2, 2, 512) 0 bn5c_branch2a[0][0]
      __________________________________________________________________________________________________
      res5c_branch2b (Conv2D) (None, 2, 2, 512) 2359808 activation_47[0][0]
      __________________________________________________________________________________________________
      bn5c_branch2b (BatchNormalizati (None, 2, 2, 512) 2048 res5c_branch2b[0][0]
      __________________________________________________________________________________________________
      activation_48 (Activation) (None, 2, 2, 512) 0 bn5c_branch2b[0][0]
      __________________________________________________________________________________________________
      res5c_branch2c (Conv2D) (None, 2, 2, 2048) 1050624 activation_48[0][0]
      __________________________________________________________________________________________________
      bn5c_branch2c (BatchNormalizati (None, 2, 2, 2048) 8192 res5c_branch2c[0][0]
      __________________________________________________________________________________________________
      add_16 (Add) (None, 2, 2, 2048) 0 bn5c_branch2c[0][0]
      activation_46[0][0]
      __________________________________________________________________________________________________
      activation_49 (Activation) (None, 2, 2, 2048) 0 add_16[0][0]
      __________________________________________________________________________________________________
      pred_age (Dense) (None, 2, 2, 101) 206848 activation_49[0][0]
      ==================================================================================================
      Total params: 23,794,560
      Trainable params: 23,741,440
      Non-trainable params: 53,120
      __________________________________________________________________________________________________



      Getting the following error



      ValueError: Error when checking input: expected input_1 to have shape (64, 64, 3) but got array with shape (128, 128, 3)






      python deep-learning keras tensorflow cnn






      share|improve this question







      New contributor




      TheJokerAEZ is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      TheJokerAEZ is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      TheJokerAEZ is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 59 mins ago









      TheJokerAEZTheJokerAEZ

      1




      1




      New contributor




      TheJokerAEZ is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      TheJokerAEZ is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      TheJokerAEZ is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          1 Answer
          1






          active

          oldest

          votes


















          0












          $begingroup$

          you did not mention your generator.



          Just add target_size to your train_set generator. it can be as follows.



          and your dataset should be in "data_generator" folder, with classes as subfolders.



          train_set =ImageDataGenerator(preprocessing_function=preprocess_input)
          train_generator= train_set.flow_from_directory('data_generator',
          target_size=(64, 64),
          color_mode='rgb',
          batch_size=32,
          class_mode='categorical',
          shuffle=True)


          vote up, if this helps ;)






          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
            });


            }
            });






            TheJokerAEZ is a new contributor. Be nice, and check out our Code of Conduct.










            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdatascience.stackexchange.com%2fquestions%2f48385%2fhow-to-do-transfer-learning-on-a-pre-trained-resnet50-with-different-image-size%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












            $begingroup$

            you did not mention your generator.



            Just add target_size to your train_set generator. it can be as follows.



            and your dataset should be in "data_generator" folder, with classes as subfolders.



            train_set =ImageDataGenerator(preprocessing_function=preprocess_input)
            train_generator= train_set.flow_from_directory('data_generator',
            target_size=(64, 64),
            color_mode='rgb',
            batch_size=32,
            class_mode='categorical',
            shuffle=True)


            vote up, if this helps ;)






            share|improve this answer









            $endgroup$


















              0












              $begingroup$

              you did not mention your generator.



              Just add target_size to your train_set generator. it can be as follows.



              and your dataset should be in "data_generator" folder, with classes as subfolders.



              train_set =ImageDataGenerator(preprocessing_function=preprocess_input)
              train_generator= train_set.flow_from_directory('data_generator',
              target_size=(64, 64),
              color_mode='rgb',
              batch_size=32,
              class_mode='categorical',
              shuffle=True)


              vote up, if this helps ;)






              share|improve this answer









              $endgroup$
















                0












                0








                0





                $begingroup$

                you did not mention your generator.



                Just add target_size to your train_set generator. it can be as follows.



                and your dataset should be in "data_generator" folder, with classes as subfolders.



                train_set =ImageDataGenerator(preprocessing_function=preprocess_input)
                train_generator= train_set.flow_from_directory('data_generator',
                target_size=(64, 64),
                color_mode='rgb',
                batch_size=32,
                class_mode='categorical',
                shuffle=True)


                vote up, if this helps ;)






                share|improve this answer









                $endgroup$



                you did not mention your generator.



                Just add target_size to your train_set generator. it can be as follows.



                and your dataset should be in "data_generator" folder, with classes as subfolders.



                train_set =ImageDataGenerator(preprocessing_function=preprocess_input)
                train_generator= train_set.flow_from_directory('data_generator',
                target_size=(64, 64),
                color_mode='rgb',
                batch_size=32,
                class_mode='categorical',
                shuffle=True)


                vote up, if this helps ;)







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 42 mins ago









                William ScottWilliam Scott

                1063




                1063






















                    TheJokerAEZ is a new contributor. Be nice, and check out our Code of Conduct.










                    draft saved

                    draft discarded


















                    TheJokerAEZ is a new contributor. Be nice, and check out our Code of Conduct.













                    TheJokerAEZ is a new contributor. Be nice, and check out our Code of Conduct.












                    TheJokerAEZ is a new contributor. Be nice, and check out our Code of Conduct.
















                    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%2f48385%2fhow-to-do-transfer-learning-on-a-pre-trained-resnet50-with-different-image-size%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

                    Callistus I

                    Tabula Rosettana

                    How to label and detect the document text images