fastai learner requirements and batch prediction









up vote
1
down vote

favorite
1












I previously trained a resnet34 model using the fastai library, and have the weights.h5 file saved. With the latest version of fastai, do I still need to have non-empty train and valid folders in order to import my learner and predict on the test set?



Also, I’m currently looping through every test image and using learn.predict_array, but is there a way to predict in batches on a test folder?



Example of what I’m currently doing just to load/predict:



PATH = '/path/to/model/'
sz = 224
arch=resnet34
tfms = tfms_from_model(resnet34, sz, aug_tfms=transforms_side_on, max_zoom=1.1)
data = ImageClassifierData.from_paths(PATH, tfms=tfms, bs=64)
learn = ConvLearner.pretrained(arch, data, precompute=False)
learn.unfreeze()
learn.load('224_all')

imgs = sorted(glob(os.path.join(test_path, '*.jpg')))
preds =
_,val_tfms = tfms_from_model(resnet34, 224)
for n, i in enumerate(imgs):
im = val_tfms(open_image(i))[None]
preds.append(1-np.argmax(learn.predict_array(im)[0]))


There must be a cleaner way to do this by now, no?










share|improve this question

















This question has an open bounty worth +50
reputation from Austin ending ending at 2018-12-19 22:44:37Z">in 5 days.


This question has not received enough attention.


PyTorch/Fastai version 1.0 answers encouraged!



















    up vote
    1
    down vote

    favorite
    1












    I previously trained a resnet34 model using the fastai library, and have the weights.h5 file saved. With the latest version of fastai, do I still need to have non-empty train and valid folders in order to import my learner and predict on the test set?



    Also, I’m currently looping through every test image and using learn.predict_array, but is there a way to predict in batches on a test folder?



    Example of what I’m currently doing just to load/predict:



    PATH = '/path/to/model/'
    sz = 224
    arch=resnet34
    tfms = tfms_from_model(resnet34, sz, aug_tfms=transforms_side_on, max_zoom=1.1)
    data = ImageClassifierData.from_paths(PATH, tfms=tfms, bs=64)
    learn = ConvLearner.pretrained(arch, data, precompute=False)
    learn.unfreeze()
    learn.load('224_all')

    imgs = sorted(glob(os.path.join(test_path, '*.jpg')))
    preds =
    _,val_tfms = tfms_from_model(resnet34, 224)
    for n, i in enumerate(imgs):
    im = val_tfms(open_image(i))[None]
    preds.append(1-np.argmax(learn.predict_array(im)[0]))


    There must be a cleaner way to do this by now, no?










    share|improve this question

















    This question has an open bounty worth +50
    reputation from Austin ending ending at 2018-12-19 22:44:37Z">in 5 days.


    This question has not received enough attention.


    PyTorch/Fastai version 1.0 answers encouraged!

















      up vote
      1
      down vote

      favorite
      1









      up vote
      1
      down vote

      favorite
      1






      1





      I previously trained a resnet34 model using the fastai library, and have the weights.h5 file saved. With the latest version of fastai, do I still need to have non-empty train and valid folders in order to import my learner and predict on the test set?



      Also, I’m currently looping through every test image and using learn.predict_array, but is there a way to predict in batches on a test folder?



      Example of what I’m currently doing just to load/predict:



      PATH = '/path/to/model/'
      sz = 224
      arch=resnet34
      tfms = tfms_from_model(resnet34, sz, aug_tfms=transforms_side_on, max_zoom=1.1)
      data = ImageClassifierData.from_paths(PATH, tfms=tfms, bs=64)
      learn = ConvLearner.pretrained(arch, data, precompute=False)
      learn.unfreeze()
      learn.load('224_all')

      imgs = sorted(glob(os.path.join(test_path, '*.jpg')))
      preds =
      _,val_tfms = tfms_from_model(resnet34, 224)
      for n, i in enumerate(imgs):
      im = val_tfms(open_image(i))[None]
      preds.append(1-np.argmax(learn.predict_array(im)[0]))


      There must be a cleaner way to do this by now, no?










      share|improve this question















      I previously trained a resnet34 model using the fastai library, and have the weights.h5 file saved. With the latest version of fastai, do I still need to have non-empty train and valid folders in order to import my learner and predict on the test set?



      Also, I’m currently looping through every test image and using learn.predict_array, but is there a way to predict in batches on a test folder?



      Example of what I’m currently doing just to load/predict:



      PATH = '/path/to/model/'
      sz = 224
      arch=resnet34
      tfms = tfms_from_model(resnet34, sz, aug_tfms=transforms_side_on, max_zoom=1.1)
      data = ImageClassifierData.from_paths(PATH, tfms=tfms, bs=64)
      learn = ConvLearner.pretrained(arch, data, precompute=False)
      learn.unfreeze()
      learn.load('224_all')

      imgs = sorted(glob(os.path.join(test_path, '*.jpg')))
      preds =
      _,val_tfms = tfms_from_model(resnet34, 224)
      for n, i in enumerate(imgs):
      im = val_tfms(open_image(i))[None]
      preds.append(1-np.argmax(learn.predict_array(im)[0]))


      There must be a cleaner way to do this by now, no?







      python-3.x machine-learning batch-processing predict fast-ai






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 2 days ago

























      asked Nov 11 at 21:02









      Austin

      1,30121036




      1,30121036






      This question has an open bounty worth +50
      reputation from Austin ending ending at 2018-12-19 22:44:37Z">in 5 days.


      This question has not received enough attention.


      PyTorch/Fastai version 1.0 answers encouraged!








      This question has an open bounty worth +50
      reputation from Austin ending ending at 2018-12-19 22:44:37Z">in 5 days.


      This question has not received enough attention.


      PyTorch/Fastai version 1.0 answers encouraged!





























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53253205%2ffastai-learner-requirements-and-batch-prediction%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f53253205%2ffastai-learner-requirements-and-batch-prediction%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







          這個網誌中的熱門文章

          How to read a connectionString WITH PROVIDER in .NET Core?

          In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

          Museum of Modern and Contemporary Art of Trento and Rovereto