prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Can't save a full model using torch.save (at least with faster-RCNN)
  It is not possible to save a full model using default settings of `torch.save` (see stack trace below). This is because of the implementation of `remove_internal_model_transforms`, which uses inner functions in its implementation. The default pickle module does not support inner functions.
  
  Workaround: use the `dill` module instead, which does support inner functions.
  
  Suggested fix: It does not look as if the internal functions are necessary. If there were moved to standard functions, then the default pickle module should work.
  `torch.save(model, 'mod.pth', pickle_module=pickle)` causes an error.
  
  `torch.save(model, 'mod.pth', pickle_module=dill)` is a workaround.
  
  **To Reproduce**
  
  `torch.save(model, 'mod1-full.pth', pickle_module=pickle)`
  results in:
  
  ```python
  ---------------------------------------------------------------------------
  AttributeError                            Traceback (most recent call last)
  <ipython-input-12-50f3761f4f3c> in <module>
  ----> 1 torch.save(model, 'mod1-full.pth', pickle_module=pickle)
  
  ~/anaconda3/envs/dlm/lib/python3.8/site-packages/torch/serialization.py in save(obj, f, pickle_module, pickle_protocol, _use_new_zipfile_serialization)
      370         if _use_new_zipfile_serialization:
      371             with _open_zipfile_writer(opened_file) as opened_zipfile:
  --> 372                 _save(obj, opened_zipfile, pickle_module, pickle_protocol)
      373                 return
      374         _legacy_save(obj, opened_file, pickle_module, pickle_protocol)
  
  ~/anaconda3/envs/dlm/lib/python3.8/site-packages/torch/serialization.py in _save(obj, zip_file, pickle_module, pickle_protocol)
      474     pickler = pickle_module.Pickler(data_buf, protocol=pickle_protocol)
      475     pickler.persistent_id = persistent_id
  --> 476     pickler.dump(obj)
      477     data_value = data_buf.getvalue()
      478     zip_file.write_record('data.pkl', data_value, len(data_value))
  
  AttributeError: Can't pickle local object 'remove_internal_model_transforms.<locals>.noop_normalize'
  ```
  
  Relevant definition:
  ```
  def remove_internal_model_transforms(model: GeneralizedRCNN):
      def noop_normalize(image: Tensor) -> Tensor:
          return image
  
      def noop_resize(
          image: Tensor, target: Optional[Dict[str, Tensor]]
      ) -> Tuple[Tensor, Optional[Dict[str, Tensor]]]:
          return image, target
  
      model.transform.normalize = noop_normalize
      model.transform.resize = noop_resize
  ```
  
  
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `icevision/models/torchvision/utils.py`
  Content:
  ```
  1 __all__ = [
  2     "remove_internal_model_transforms",
  3     "patch_rcnn_param_groups",
  4     "patch_retinanet_param_groups",
  5 ]
  6 
  7 from icevision.imports import *
  8 from icevision.utils import *
  9 from torchvision.models.detection.generalized_rcnn import GeneralizedRCNN
  10 
  11 
  12 def remove_internal_model_transforms(model: GeneralizedRCNN):
  13     def noop_normalize(image: Tensor) -> Tensor:
  14         return image
  15 
  16     def noop_resize(
  17         image: Tensor, target: Optional[Dict[str, Tensor]]
  18     ) -> Tuple[Tensor, Optional[Dict[str, Tensor]]]:
  19         return image, target
  20 
  21     model.transform.normalize = noop_normalize
  22     model.transform.resize = noop_resize
  23 
  24 
  25 def patch_param_groups(
  26     model: nn.Module,
  27     head_layers: List[nn.Module],
  28     backbone_param_groups: List[List[nn.Parameter]],
  29 ):
  30     def param_groups(model: nn.Module) -> List[List[nn.Parameter]]:
  31         head_param_groups = [list(layer.parameters()) for layer in head_layers]
  32 
  33         _param_groups = backbone_param_groups + head_param_groups
  34         check_all_model_params_in_groups2(model, _param_groups)
  35 
  36         return _param_groups
  37 
  38     model.param_groups = MethodType(param_groups, model)
  39 
  40 
  41 def patch_rcnn_param_groups(model: nn.Module):
  42     return patch_param_groups(
  43         model=model,
  44         head_layers=[model.rpn, model.roi_heads],
  45         backbone_param_groups=model.backbone.param_groups(),
  46     )
  47 
  48 
  49 def patch_retinanet_param_groups(model: nn.Module):
  50     return patch_param_groups(
  51         model=model,
  52         head_layers=[model.head],
  53         backbone_param_groups=model.backbone.param_groups(),
  54     )
  55
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/icevision/models/torchvision/utils.py b/icevision/models/torchvision/utils.py
  --- a/icevision/models/torchvision/utils.py
  +++ b/icevision/models/torchvision/utils.py
  @@ -9,17 +9,19 @@
   from torchvision.models.detection.generalized_rcnn import GeneralizedRCNN
   
   
  -def remove_internal_model_transforms(model: GeneralizedRCNN):
  -    def noop_normalize(image: Tensor) -> Tensor:
  -        return image
  +def _noop_normalize(image: Tensor) -> Tensor:
  +    return image
  +
   
  -    def noop_resize(
  -        image: Tensor, target: Optional[Dict[str, Tensor]]
  -    ) -> Tuple[Tensor, Optional[Dict[str, Tensor]]]:
  -        return image, target
  +def _noop_resize(
  +    image: Tensor, target: Optional[Dict[str, Tensor]]
  +) -> Tuple[Tensor, Optional[Dict[str, Tensor]]]:
  +    return image, target
   
  -    model.transform.normalize = noop_normalize
  -    model.transform.resize = noop_resize
  +
  +def remove_internal_model_transforms(model: GeneralizedRCNN):
  +    model.transform.normalize = _noop_normalize
  +    model.transform.resize = _noop_resize
   
   
   def patch_param_groups(
  
verification_info: {"golden_diff": "diff --git a/icevision/models/torchvision/utils.py b/icevision/models/torchvision/utils.py\n--- a/icevision/models/torchvision/utils.py\n+++ b/icevision/models/torchvision/utils.py\n@@ -9,17 +9,19 @@\n from torchvision.models.detection.generalized_rcnn import GeneralizedRCNN\n \n \n-def remove_internal_model_transforms(model: GeneralizedRCNN):\n-    def noop_normalize(image: Tensor) -> Tensor:\n-        return image\n+def _noop_normalize(image: Tensor) -> Tensor:\n+    return image\n+\n \n-    def noop_resize(\n-        image: Tensor, target: Optional[Dict[str, Tensor]]\n-    ) -> Tuple[Tensor, Optional[Dict[str, Tensor]]]:\n-        return image, target\n+def _noop_resize(\n+    image: Tensor, target: Optional[Dict[str, Tensor]]\n+) -> Tuple[Tensor, Optional[Dict[str, Tensor]]]:\n+    return image, target\n \n-    model.transform.normalize = noop_normalize\n-    model.transform.resize = noop_resize\n+\n+def remove_internal_model_transforms(model: GeneralizedRCNN):\n+    model.transform.normalize = _noop_normalize\n+    model.transform.resize = _noop_resize\n \n \n def patch_param_groups(\n", "issue": "Can't save a full model using torch.save (at least with faster-RCNN)\nIt is not possible to save a full model using default settings of `torch.save` (see stack trace below). This is because of the implementation of `remove_internal_model_transforms`, which uses inner functions in its implementation. The default pickle module does not support inner functions.\r\n\r\nWorkaround: use the `dill` module instead, which does support inner functions.\r\n\r\nSuggested fix: It does not look as if the internal functions are necessary. If there were moved to standard functions, then the default pickle module should work.\r\n`torch.save(model, 'mod.pth', pickle_module=pickle)` causes an error.\r\n\r\n`torch.save(model, 'mod.pth', pickle_module=dill)` is a workaround.\r\n\r\n**To Reproduce**\r\n\r\n`torch.save(model, 'mod1-full.pth', pickle_module=pickle)`\r\nresults in:\r\n\r\n```python\r\n---------------------------------------------------------------------------\r\nAttributeError                            Traceback (most recent call last)\r\n<ipython-input-12-50f3761f4f3c> in <module>\r\n----> 1 torch.save(model, 'mod1-full.pth', pickle_module=pickle)\r\n\r\n~/anaconda3/envs/dlm/lib/python3.8/site-packages/torch/serialization.py in save(obj, f, pickle_module, pickle_protocol, _use_new_zipfile_serialization)\r\n    370         if _use_new_zipfile_serialization:\r\n    371             with _open_zipfile_writer(opened_file) as opened_zipfile:\r\n--> 372                 _save(obj, opened_zipfile, pickle_module, pickle_protocol)\r\n    373                 return\r\n    374         _legacy_save(obj, opened_file, pickle_module, pickle_protocol)\r\n\r\n~/anaconda3/envs/dlm/lib/python3.8/site-packages/torch/serialization.py in _save(obj, zip_file, pickle_module, pickle_protocol)\r\n    474     pickler = pickle_module.Pickler(data_buf, protocol=pickle_protocol)\r\n    475     pickler.persistent_id = persistent_id\r\n--> 476     pickler.dump(obj)\r\n    477     data_value = data_buf.getvalue()\r\n    478     zip_file.write_record('data.pkl', data_value, len(data_value))\r\n\r\nAttributeError: Can't pickle local object 'remove_internal_model_transforms.<locals>.noop_normalize'\r\n```\r\n\r\nRelevant definition:\r\n```\r\ndef remove_internal_model_transforms(model: GeneralizedRCNN):\r\n    def noop_normalize(image: Tensor) -> Tensor:\r\n        return image\r\n\r\n    def noop_resize(\r\n        image: Tensor, target: Optional[Dict[str, Tensor]]\r\n    ) -> Tuple[Tensor, Optional[Dict[str, Tensor]]]:\r\n        return image, target\r\n\r\n    model.transform.normalize = noop_normalize\r\n    model.transform.resize = noop_resize\r\n```\r\n\r\n\n", "before_files": [{"content": "__all__ = [\n    \"remove_internal_model_transforms\",\n    \"patch_rcnn_param_groups\",\n    \"patch_retinanet_param_groups\",\n]\n\nfrom icevision.imports import *\nfrom icevision.utils import *\nfrom torchvision.models.detection.generalized_rcnn import GeneralizedRCNN\n\n\ndef remove_internal_model_transforms(model: GeneralizedRCNN):\n    def noop_normalize(image: Tensor) -> Tensor:\n        return image\n\n    def noop_resize(\n        image: Tensor, target: Optional[Dict[str, Tensor]]\n    ) -> Tuple[Tensor, Optional[Dict[str, Tensor]]]:\n        return image, target\n\n    model.transform.normalize = noop_normalize\n    model.transform.resize = noop_resize\n\n\ndef patch_param_groups(\n    model: nn.Module,\n    head_layers: List[nn.Module],\n    backbone_param_groups: List[List[nn.Parameter]],\n):\n    def param_groups(model: nn.Module) -> List[List[nn.Parameter]]:\n        head_param_groups = [list(layer.parameters()) for layer in head_layers]\n\n        _param_groups = backbone_param_groups + head_param_groups\n        check_all_model_params_in_groups2(model, _param_groups)\n\n        return _param_groups\n\n    model.param_groups = MethodType(param_groups, model)\n\n\ndef patch_rcnn_param_groups(model: nn.Module):\n    return patch_param_groups(\n        model=model,\n        head_layers=[model.rpn, model.roi_heads],\n        backbone_param_groups=model.backbone.param_groups(),\n    )\n\n\ndef patch_retinanet_param_groups(model: nn.Module):\n    return patch_param_groups(\n        model=model,\n        head_layers=[model.head],\n        backbone_param_groups=model.backbone.param_groups(),\n    )\n", "path": "icevision/models/torchvision/utils.py"}], "after_files": [{"content": "__all__ = [\n    \"remove_internal_model_transforms\",\n    \"patch_rcnn_param_groups\",\n    \"patch_retinanet_param_groups\",\n]\n\nfrom icevision.imports import *\nfrom icevision.utils import *\nfrom torchvision.models.detection.generalized_rcnn import GeneralizedRCNN\n\n\ndef _noop_normalize(image: Tensor) -> Tensor:\n    return image\n\n\ndef _noop_resize(\n    image: Tensor, target: Optional[Dict[str, Tensor]]\n) -> Tuple[Tensor, Optional[Dict[str, Tensor]]]:\n    return image, target\n\n\ndef remove_internal_model_transforms(model: GeneralizedRCNN):\n    model.transform.normalize = _noop_normalize\n    model.transform.resize = _noop_resize\n\n\ndef patch_param_groups(\n    model: nn.Module,\n    head_layers: List[nn.Module],\n    backbone_param_groups: List[List[nn.Parameter]],\n):\n    def param_groups(model: nn.Module) -> List[List[nn.Parameter]]:\n        head_param_groups = [list(layer.parameters()) for layer in head_layers]\n\n        _param_groups = backbone_param_groups + head_param_groups\n        check_all_model_params_in_groups2(model, _param_groups)\n\n        return _param_groups\n\n    model.param_groups = MethodType(param_groups, model)\n\n\ndef patch_rcnn_param_groups(model: nn.Module):\n    return patch_param_groups(\n        model=model,\n        head_layers=[model.rpn, model.roi_heads],\n        backbone_param_groups=model.backbone.param_groups(),\n    )\n\n\ndef patch_retinanet_param_groups(model: nn.Module):\n    return patch_param_groups(\n        model=model,\n        head_layers=[model.head],\n        backbone_param_groups=model.backbone.param_groups(),\n    )\n", "path": "icevision/models/torchvision/utils.py"}]}