Skip to content

Commit

Permalink
Merge pull request #91 from ShunsukeOnoo/fix-feature-extractor
Browse files Browse the repository at this point in the history
Fix issue #72 Forward Hooks Persist After Destroying FeatureExtractor
  • Loading branch information
ShuntaroAoki authored Jul 29, 2024
2 parents a9ec4dd + 3560c80 commit 6075f53
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion bdpy/dl/torch/torch.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'''PyTorch module.'''

from typing import Iterable, List, Dict, Union, Tuple, Any, Callable, Optional

from collections import OrderedDict
import os

import numpy as np
Expand Down Expand Up @@ -93,6 +93,19 @@ def run(self, x: _tensor_t) -> Dict[str, _tensor_t]:
}

return features

def __del__(self):
'''
Remove forward hooks for the FeatureExtractor while keeping
other forward hooks in the model.
'''
for layer in self.__layers:
if self.__layer_map is not None:
layer = self.__layer_map[layer]
layer_object = models._parse_layer_name(self._encoder, layer)
for key, hook in layer_object._forward_hooks.items():
if hook == self._extractor:
del layer_object._forward_hooks[key]


class FeatureExtractorHandle(object):
Expand Down

4 comments on commit 6075f53

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage

Coverage Report
FileStmtsMissCoverMissing
bdpy/bdata
   bdata.py39931920%73–79, 86, 90, 95, 99, 104, 109, 113, 118, 122, 132–134, 155–172, 190, 206–207, 232–248, 252–262, 276–277, 293, 310, 314, 318–356, 388–477, 508, 539, 547–551, 560, 577–584, 589–598, 604–614, 618, 622–625, 628, 632–653, 656–665, 668–677, 683–691, 696–729, 735–744, 750–757, 761–767, 771–799, 803–824, 828–862, 866–868, 872–874, 878–887
   featureselector.py645711%43–47, 52–93, 98–124
   metadata.py674828%21–30, 34, 38, 42, 46, 50, 54, 74–115, 135–144, 149, 154
   utils.py1131039%44–110, 127–173, 201, 217–253, 258, 263
bdpy/dataform
   datastore.py1078521%59–75, 90–93, 97–98, 102–113, 116–119, 122–127, 131–132, 137–158, 190–197, 222–259, 262–265
   features.py29823322%29–32, 41–47, 74–103, 107, 111, 115, 119, 137–163, 168–197, 213–238, 241–269, 272–275, 278–289, 305–319, 323, 327, 331, 335, 339, 343, 347, 351, 355, 359, 364–394, 398–418, 422–462, 465, 470–477, 491–493, 496–499, 502–505, 508–512, 515–516, 536–549
   kvs.py1401400%4–272
   pd.py9544%25–27, 43–44
   sparse.py675124%19–29, 35–46, 52–58, 65–74, 78, 81–87, 90–93, 96–98, 101–126
   utils.py12120%3–18
bdpy/dataset
   utils.py45450%3–98
bdpy/distcomp
   distcomp.py927815%20–29, 32–49, 52–70, 73–93, 96–107, 111, 114–117, 121–127
bdpy/dl
   caffe.py60600%4–129
bdpy/dl/torch
   base.py432444%31–41, 48, 54, 60, 63, 73–83, 90, 96, 102, 105
   models.py33329811%28–84, 114–140, 148–169, 175–238, 249–253, 259–279, 288–292, 297–316, 327–331, 340–405, 427–431, 442–494, 515–517, 528–587, 611–614, 625–684, 708–711, 722–771, 790–793, 804–853, 872–875
   torch.py1189123%40–57, 60, 77–95, 102–108, 113, 120, 123, 128, 135, 138, 185–215, 218, 221–233, 236–271
bdpy/evals
   metrics.py95878%12–34, 40–73, 82–112, 118–159, 172–179
bdpy/feature
   feature.py30287%33–74
bdpy/fig
   __init__.py440%6–9
   draw_group_image_set.py90900%3–182
   fig.py88880%16–164
   makeplots.py3363360%1–729
   tile_images.py59590%1–193
bdpy/ml
   crossvalidation.py59548%34–61, 104–128, 138, 164–196
   ensemble.py13931%33–46
   learning.py30826414%43–44, 48, 52, 59, 91–104, 109–125, 128, 158–170, 184–209, 260–284, 290–433, 436–461, 465–504, 507–508, 524–536, 541–613
   model.py14012014%29–39, 54–70, 86–144, 156–169, 184–222, 225, 230–250, 254–258, 271–285
   regress.py11827%29–38
   searchlight.py161319%32–51
bdpy/mri
   fmriprep.py4974519%25–34, 38, 44–62, 65–75, 78–89, 92–160, 163–194, 230–360, 367–380, 384, 388–390, 394, 398–400, 410–434, 437–454, 457–464, 471–472, 475–491, 494, 498, 502–815, 819–831, 842–862
   glm.py403610%46–95
   image.py241921%29–54
   load_epi.py281836%36–50, 56–63, 82–88
   load_mri.py191616%16–36
   roi.py2482346%37–100, 122–148, 165–235, 241–314, 320–387, 399–466, 473–499
   spm.py15813912%26–155, 162–166, 170, 174–179, 183–300
bdpy/opendata
   __init__.py110%1
   openneuro.py2102100%1–329
bdpy/pipeline
   config.py362919%15–64
bdpy/preproc
   interface.py524415%31–40, 60–69, 96–105, 111–123, 148–157, 208–217
   preprocessor.py12910717%35, 43–65, 74–78, 85–97, 104–132, 138–189, 196–227, 234–239
   select_top.py231822%35–60
   util.py6267%14, 22
bdpy/recon
   utils.py55550%4–146
bdpy/recon/torch
   __init__.py110%1
   icnn.py1611610%15–478
bdpy/stats
   corr.py433812%29–77, 96–112
bdpy/util
   info.py473623%19–79
   math.py131023%23–38
   utils.py362628%48–66, 93–96, 116–121, 137–145
TOTAL5077446012% 

Tests Skipped Failures Errors Time
1 0 💤 0 ❌ 1 🔥 4.053s ⏱️

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage

Coverage Report
FileStmtsMissCoverMissing
bdpy/bdata
   bdata.py39931920%73–79, 86, 90, 95, 99, 104, 109, 113, 118, 122, 132–134, 155–172, 190, 206–207, 232–248, 252–262, 276–277, 293, 310, 314, 318–356, 388–477, 508, 539, 547–551, 560, 577–584, 589–598, 604–614, 618, 622–625, 628, 632–653, 656–665, 668–677, 683–691, 696–729, 735–744, 750–757, 761–767, 771–799, 803–824, 828–862, 866–868, 872–874, 878–887
   featureselector.py645711%43–47, 52–93, 98–124
   metadata.py674828%21–30, 34, 38, 42, 46, 50, 54, 74–115, 135–144, 149, 154
   utils.py1131039%44–110, 127–173, 201, 217–253, 258, 263
bdpy/dataform
   datastore.py1078521%59–75, 90–93, 97–98, 102–113, 116–119, 122–127, 131–132, 137–158, 190–197, 222–259, 262–265
   features.py29823322%29–32, 41–47, 74–103, 107, 111, 115, 119, 137–163, 168–197, 213–238, 241–269, 272–275, 278–289, 305–319, 323, 327, 331, 335, 339, 343, 347, 351, 355, 359, 364–394, 398–418, 422–462, 465, 470–477, 491–493, 496–499, 502–505, 508–512, 515–516, 536–549
   kvs.py1401400%4–272
   pd.py9544%25–27, 43–44
   sparse.py675124%19–29, 35–46, 52–58, 65–74, 78, 81–87, 90–93, 96–98, 101–126
   utils.py12120%3–18
bdpy/dataset
   utils.py45450%3–98
bdpy/distcomp
   distcomp.py927815%20–29, 32–49, 52–70, 73–93, 96–107, 111, 114–117, 121–127
bdpy/dl
   caffe.py60600%4–129
bdpy/dl/torch
   base.py432444%31–41, 48, 54, 60, 63, 73–83, 90, 96, 102, 105
   models.py33329811%28–84, 114–140, 148–169, 175–238, 249–253, 259–279, 288–292, 297–316, 327–331, 340–405, 427–431, 442–494, 515–517, 528–587, 611–614, 625–684, 708–711, 722–771, 790–793, 804–853, 872–875
   torch.py1189123%40–57, 60, 77–95, 102–108, 113, 120, 123, 128, 135, 138, 185–215, 218, 221–233, 236–271
bdpy/evals
   metrics.py95878%12–34, 40–73, 82–112, 118–159, 172–179
bdpy/feature
   feature.py30287%33–74
bdpy/fig
   __init__.py440%6–9
   draw_group_image_set.py90900%3–182
   fig.py88880%16–164
   makeplots.py3363360%1–729
   tile_images.py59590%1–193
bdpy/ml
   crossvalidation.py59548%34–61, 104–128, 138, 164–196
   ensemble.py13931%33–46
   learning.py30826414%43–44, 48, 52, 59, 91–104, 109–125, 128, 158–170, 184–209, 260–284, 290–433, 436–461, 465–504, 507–508, 524–536, 541–613
   model.py14012014%29–39, 54–70, 86–144, 156–169, 184–222, 225, 230–250, 254–258, 271–285
   regress.py11827%29–38
   searchlight.py161319%32–51
bdpy/mri
   fmriprep.py4974519%25–34, 38, 44–62, 65–75, 78–89, 92–160, 163–194, 230–360, 367–380, 384, 388–390, 394, 398–400, 410–434, 437–454, 457–464, 471–472, 475–491, 494, 498, 502–815, 819–831, 842–862
   glm.py403610%46–95
   image.py241921%29–54
   load_epi.py281836%36–50, 56–63, 82–88
   load_mri.py191616%16–36
   roi.py2482346%37–100, 122–148, 165–235, 241–314, 320–387, 399–466, 473–499
   spm.py15813912%26–155, 162–166, 170, 174–179, 183–300
bdpy/opendata
   __init__.py110%1
   openneuro.py2102100%1–329
bdpy/pipeline
   config.py362919%15–64
bdpy/preproc
   interface.py524415%31–40, 60–69, 96–105, 111–123, 148–157, 208–217
   preprocessor.py12910717%35, 43–65, 74–78, 85–97, 104–132, 138–189, 196–227, 234–239
   select_top.py231822%35–60
   util.py6267%14, 22
bdpy/recon
   utils.py55550%4–146
bdpy/recon/torch
   __init__.py110%1
   icnn.py1611610%15–478
bdpy/stats
   corr.py433812%29–77, 96–112
bdpy/util
   info.py473623%19–79
   math.py131023%23–38
   utils.py362628%48–66, 93–96, 116–121, 137–145
TOTAL5077446012% 

Tests Skipped Failures Errors Time
1 0 💤 0 ❌ 1 🔥 4.517s ⏱️

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage

Coverage Report
FileStmtsMissCoverMissing
bdpy/bdata
   bdata.py40032020%73–79, 86, 90, 95, 99, 104, 109, 113, 118, 122, 132–134, 155–172, 190, 206–207, 232–248, 252–262, 276–277, 293, 310, 314, 318–356, 388–477, 508, 539, 547–551, 560, 577–584, 589–598, 604–614, 618, 622–625, 628, 632–653, 656–665, 668–677, 683–691, 696–729, 735–744, 750–757, 761–767, 771–799, 803–824, 828–862, 866–868, 872–874, 878–887
   featureselector.py645711%43–47, 52–93, 98–124
   metadata.py674828%21–30, 34, 38, 42, 46, 50, 54, 74–115, 135–144, 149, 154
   utils.py1131039%44–110, 127–173, 201, 217–253, 258, 263
bdpy/dataform
   datastore.py1078521%59–75, 90–93, 97–98, 102–113, 116–119, 122–127, 131–132, 137–158, 190–197, 222–259, 262–265
   features.py29823322%29–32, 41–47, 74–103, 107, 111, 115, 119, 137–163, 168–197, 213–238, 241–269, 272–275, 278–289, 305–319, 323, 327, 331, 335, 339, 343, 347, 351, 355, 359, 364–394, 398–418, 422–462, 465, 470–477, 491–493, 496–499, 502–505, 508–512, 515–516, 536–549
   kvs.py1401400%4–272
   pd.py9544%25–27, 43–44
   sparse.py675124%19–29, 35–46, 52–58, 65–74, 78, 81–87, 90–93, 96–98, 101–126
   utils.py12120%3–18
bdpy/dataset
   utils.py45450%3–98
bdpy/distcomp
   distcomp.py927815%20–29, 32–49, 52–70, 73–93, 96–107, 111, 114–117, 121–127
bdpy/dl
   caffe.py60600%4–129
bdpy/dl/torch
   base.py432444%31–41, 48, 54, 60, 63, 73–83, 90, 96, 102, 105
   models.py33329811%28–84, 114–140, 148–169, 175–238, 249–253, 259–279, 288–292, 297–316, 327–331, 340–405, 427–431, 442–494, 515–517, 528–587, 611–614, 625–684, 708–711, 722–771, 790–793, 804–853, 872–875
   torch.py1189123%40–57, 60, 77–95, 102–108, 113, 120, 123, 128, 135, 138, 185–215, 218, 221–233, 236–271
bdpy/evals
   metrics.py95878%12–34, 40–73, 82–112, 118–159, 172–179
bdpy/feature
   feature.py30287%33–74
bdpy/fig
   __init__.py440%6–9
   draw_group_image_set.py88880%3–182
   fig.py88880%16–164
   makeplots.py3363360%1–729
   tile_images.py59590%1–193
bdpy/ml
   crossvalidation.py59548%34–61, 104–128, 138, 164–196
   ensemble.py13931%33–46
   learning.py30926514%43–44, 48, 52, 59, 91–104, 109–125, 128, 158–170, 184–209, 260–284, 290–433, 436–461, 465–504, 507–508, 524–536, 541–613
   model.py14012014%29–39, 54–70, 86–144, 156–169, 184–222, 225, 230–250, 254–258, 271–285
   regress.py11827%29–38
   searchlight.py161319%32–51
bdpy/mri
   fmriprep.py4974529%25–34, 38, 44–62, 65–75, 78–89, 92–160, 163–194, 230–360, 367–380, 384, 388–390, 394, 398–400, 410–434, 437–454, 457–464, 471–472, 475–491, 494, 498, 502–815, 819–831, 842–862, 866
   glm.py403610%46–95
   image.py241921%29–54
   load_epi.py281836%36–50, 56–63, 82–88
   load_mri.py191616%16–36
   roi.py2482346%37–100, 122–148, 165–235, 241–314, 320–387, 399–466, 473–499
   spm.py15813912%26–155, 162–166, 170, 174–179, 183–300
bdpy/opendata
   __init__.py110%1
   openneuro.py2102100%1–329
bdpy/pipeline
   config.py362919%15–64
bdpy/preproc
   interface.py524415%31–40, 60–69, 96–105, 111–123, 148–157, 208–217
   preprocessor.py12910717%35, 43–65, 74–78, 85–97, 104–132, 138–189, 196–227, 234–239
   select_top.py231822%35–60
   util.py6267%14, 22
bdpy/recon
   utils.py55550%4–146
bdpy/recon/torch
   __init__.py110%1
   icnn.py1611610%15–478
bdpy/stats
   corr.py433812%29–77, 96–112
bdpy/util
   info.py473623%19–79
   math.py131023%23–38
   utils.py362628%48–66, 93–96, 116–121, 137–145
TOTAL5077446112% 

Tests Skipped Failures Errors Time
1 0 💤 0 ❌ 1 🔥 4.029s ⏱️

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage

Coverage Report
FileStmtsMissCoverMissing
bdpy/bdata
   bdata.py40032020%73–79, 86, 90, 95, 99, 104, 109, 113, 118, 122, 132–134, 155–172, 190, 206–207, 232–248, 252–262, 276–277, 293, 310, 314, 318–356, 388–477, 508, 539, 547–551, 560, 577–584, 589–598, 604–614, 618, 622–625, 628, 632–653, 656–665, 668–677, 683–691, 696–729, 735–744, 750–757, 761–767, 771–799, 803–824, 828–862, 866–868, 872–874, 878–887
   featureselector.py645711%43–47, 52–93, 98–124
   metadata.py674828%21–30, 34, 38, 42, 46, 50, 54, 74–115, 135–144, 149, 154
   utils.py1131039%44–110, 127–173, 201, 217–253, 258, 263
bdpy/dataform
   datastore.py1078521%59–75, 90–93, 97–98, 102–113, 116–119, 122–127, 131–132, 137–158, 190–197, 222–259, 262–265
   features.py29823322%29–32, 41–47, 74–103, 107, 111, 115, 119, 137–163, 168–197, 213–238, 241–269, 272–275, 278–289, 305–319, 323, 327, 331, 335, 339, 343, 347, 351, 355, 359, 364–394, 398–418, 422–462, 465, 470–477, 491–493, 496–499, 502–505, 508–512, 515–516, 536–549
   kvs.py1401400%4–272
   pd.py9544%25–27, 43–44
   sparse.py675124%19–29, 35–46, 52–58, 65–74, 78, 81–87, 90–93, 96–98, 101–126
   utils.py12120%3–18
bdpy/dataset
   utils.py45450%3–98
bdpy/distcomp
   distcomp.py927815%20–29, 32–49, 52–70, 73–93, 96–107, 111, 114–117, 121–127
bdpy/dl
   caffe.py60600%4–129
bdpy/dl/torch
   base.py432444%31–41, 48, 54, 60, 63, 73–83, 90, 96, 102, 105
   models.py33329811%28–84, 114–140, 148–169, 175–238, 249–253, 259–279, 288–292, 297–316, 327–331, 340–405, 427–431, 442–494, 515–517, 528–587, 611–614, 625–684, 708–711, 722–771, 790–793, 804–853, 872–875
   torch.py1189123%40–57, 60, 77–95, 102–108, 113, 120, 123, 128, 135, 138, 185–215, 218, 221–233, 236–271
bdpy/evals
   metrics.py95878%12–34, 40–73, 82–112, 118–159, 172–179
bdpy/feature
   feature.py30287%33–74
bdpy/fig
   __init__.py440%6–9
   draw_group_image_set.py88880%3–182
   fig.py88880%16–164
   makeplots.py3363360%1–729
   tile_images.py59590%1–193
bdpy/ml
   crossvalidation.py59548%34–61, 104–128, 138, 164–196
   ensemble.py13931%33–46
   learning.py30926514%43–44, 48, 52, 59, 91–104, 109–125, 128, 158–170, 184–209, 260–284, 290–433, 436–461, 465–504, 507–508, 524–536, 541–613
   model.py14012014%29–39, 54–70, 86–144, 156–169, 184–222, 225, 230–250, 254–258, 271–285
   regress.py11827%29–38
   searchlight.py161319%32–51
bdpy/mri
   fmriprep.py4974529%25–34, 38, 44–62, 65–75, 78–89, 92–160, 163–194, 230–360, 367–380, 384, 388–390, 394, 398–400, 410–434, 437–454, 457–464, 471–472, 475–491, 494, 498, 502–815, 819–831, 842–862, 866
   glm.py403610%46–95
   image.py241921%29–54
   load_epi.py281836%36–50, 56–63, 82–88
   load_mri.py191616%16–36
   roi.py2482346%37–100, 122–148, 165–235, 241–314, 320–387, 399–466, 473–499
   spm.py15813912%26–155, 162–166, 170, 174–179, 183–300
bdpy/opendata
   __init__.py110%1
   openneuro.py2102100%1–329
bdpy/pipeline
   config.py362919%15–64
bdpy/preproc
   interface.py524415%31–40, 60–69, 96–105, 111–123, 148–157, 208–217
   preprocessor.py12910717%35, 43–65, 74–78, 85–97, 104–132, 138–189, 196–227, 234–239
   select_top.py231822%35–60
   util.py6267%14, 22
bdpy/recon
   utils.py55550%4–146
bdpy/recon/torch
   __init__.py110%1
   icnn.py1611610%15–478
bdpy/stats
   corr.py433812%29–77, 96–112
bdpy/util
   info.py473623%19–79
   math.py131023%23–38
   utils.py362628%48–66, 93–96, 116–121, 137–145
TOTAL5077446112% 

Tests Skipped Failures Errors Time
1 0 💤 0 ❌ 1 🔥 4.971s ⏱️

Please sign in to comment.