internlm/SWE-Fixer-Train-110K huggingface dataset:
Repository: pytorch/vision

Issue:
VOCDetection year=‘2007’ Problem 
## 🐛 Bug
There seems to be a bug where:
torchvision.datasets.VOCDetection(’/content’, year=‘2007’, image_set=‘train’, download=True)
results in download and extraction of VOCtest_06-Nov-2007.tar instead of VOCtrainval_06-Nov-2007.tar. My guess is that this is due to recent changes made to voc.py [here](https://github.com/pytorch/vision/blob/c808d163f6c65ca851db59e9966807a9220fc493/torchvision/datasets/voc.py#L88) . Lines 88-94.

## To Reproduce

train_dataset = torchvision.datasets.VOCDetection(’/content’, year=‘2007’, image_set=‘train’, download=True)

## Expected behavior

I would expect to download and extract VOCtrainval_06-Nov-2007.tar and get the training data from this dataset.

## Environment
Collecting environment information...
PyTorch version: 1.8.0+cu101
Is debug build: False
CUDA used to build PyTorch: 10.1
ROCM used to build PyTorch: N/A

OS: Ubuntu 18.04.5 LTS (x86_64)
GCC version: (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
Clang version: 6.0.0-1ubuntu2 (tags/RELEASE_600/final)
CMake version: version 3.12.0

Python version: 3.7 (64-bit runtime)
Is CUDA available: True
CUDA runtime version: 11.0.221
GPU models and configuration: GPU 0: Tesla T4
Nvidia driver version: 460.32.03
cuDNN version: Probably one of the following:
/usr/lib/x86_64-linux-gnu/libcudnn.so.7.6.5
/usr/lib/x86_64-linux-gnu/libcudnn.so.8.0.4
/usr/lib/x86_64-linux-gnu/libcudnn_adv_infer.so.8.0.4
/usr/lib/x86_64-linux-gnu/libcudnn_adv_train.so.8.0.4
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_infer.so.8.0.4
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_train.so.8.0.4
/usr/lib/x86_64-linux-gnu/libcudnn_ops_infer.so.8.0.4
/usr/lib/x86_64-linux-gnu/libcudnn_ops_train.so.8.0.4
HIP runtime version: N/A
MIOpen runtime version: N/A

Versions of relevant libraries:
[pip3] numpy==1.19.5
[pip3] torch==1.8.0+cu101
[pip3] torchsummary==1.5.1
[pip3] torchtext==0.9.0
[pip3] torchvision==0.9.0+cu101
[conda] Could not collect

cc @pmeier @fmassa @vfdev-5


Discussion:
Hey @maheshsrinivas25 , thanks for reporting this! I encourage you to file an issue specifically for torchvision at https://github.com/pytorch/vision/issues

Resolving patch:
diff --git a/torchvision/datasets/voc.py b/torchvision/datasets/voc.py
--- a/torchvision/datasets/voc.py
+++ b/torchvision/datasets/voc.py
@@ -87,10 +87,9 @@ def __init__(
         valid_image_sets = ["train", "trainval", "val"]
         if year == "2007":
             valid_image_sets.append("test")
-            key = "2007-test"
-        else:
-            key = year
         self.image_set = verify_str_arg(image_set, "image_set", valid_image_sets)
+
+        key = "2007-test" if year == "2007" and image_set == "test" else year
         dataset_year_dict = DATASET_YEAR_DICT[key]
 
         self.url = dataset_year_dict["url"]


Test patch:
Repository: pytorch/vision

Issue:
This may be  has extra "as"
https://github.com/pytorch/vision/blob/631ff9123edeedf044c4355ef99dc42a396feb5b/torchvision/models/detection/transform.py#L87


Discussion:
Thanks @Absolute-Sword for flagging!

Resolving patch:
diff --git a/torchvision/models/detection/transform.py b/torchvision/models/detection/transform.py
--- a/torchvision/models/detection/transform.py
+++ b/torchvision/models/detection/transform.py
@@ -84,7 +84,7 @@ def forward(self,
         if targets is not None:
             # make a copy of targets to avoid modifying it in-place
             # once torchscript supports dict comprehension
-            # this can be simplified as as follows
+            # this can be simplified as follows
             # targets = [{k: v for k,v in t.items()} for t in targets]
             targets_copy: List[Dict[str, Tensor]] = []
             for t in targets:


Test patch:
Repository: pytorch/vision

Issue:
[docs] Interpolation modes are not rendered
Members are not listed/rendered, they are however present in the sources: https://pytorch.org/vision/master/_modules/torchvision/transforms/functional.html#InterpolationMode

![image](https://user-images.githubusercontent.com/1041752/115265337-6a158300-a137-11eb-9d30-c4e171119233.png)



Discussion:
Hey @vadimkantorov can you send a PR for this fix?

It should probably be a line stating

```
Available interpolation methods are "nearest", "bilinear" "bicubic" "box" "hamming" and "lanczos".
```

It should probably be a simple edit over [here](https://github.com/pytorch/vision/blob/master/torchvision/transforms/functional.py#L23)


Resolving patch:
diff --git a/torchvision/transforms/functional.py b/torchvision/transforms/functional.py
--- a/torchvision/transforms/functional.py
+++ b/torchvision/transforms/functional.py
@@ -21,6 +21,7 @@
 
 class InterpolationMode(Enum):
     """Interpolation modes
+    Available interpolation methods are ``nearest``, ``bilinear``, ``bicubic``, ``box``, ``hamming``, and ``lanczos``.
     """
     NEAREST = "nearest"
     BILINEAR = "bilinear"


Test patch:
Repository: pytorch/vision

Issue:
Replace  torch.lstsq with torch.linalg.lstsq in `perspective` transform
## 🚀 Feature

We recently started getting the following types of warnings:

```
/root/project/torchvision/transforms/functional.py:597: UserWarning: torch.lstsq is deprecated in favor of torch.linalg.lstsq and will be removed in a future PyTorch release.
torch.linalg.lstsq has reversed arguments and does not return the QR decomposition in the returned tuple (although it returns other information about the problem).
To get the qr decomposition consider using torch.linalg.qr.
The returned solution in torch.lstsq stored the residuals of the solution in the last m - n columns of the returned value whenever m > n. In torch.linalg.lstsq, the residuals in the field 'residuals' of the returned named tuple.
The unpacking of the solution, as in
X, _ = torch.lstsq(B, A).solution[:A.size(1)]
should be replaced with
X = torch.linalg.lstsq(A, B).solution (Triggered internally at  /opt/conda/conda-bld/pytorch_1620975976061/work/aten/src/ATen/LegacyTHFunctionsCPU.cpp:389.)
  res = torch.lstsq(b_matrix, a_matrix)[0]
```

which gets even more visible in [the newly added galleries](https://579199-73328905-gh.circle-artifacts.com/0/docs/auto_examples/plot_transforms.html#sphx-glr-auto-examples-plot-transforms-py)

We should replace
https://github.com/pytorch/vision/blob/b56f17ae1ae8a5d08067c7f7444af21fb3b59ca6/torchvision/transforms/functional.py#L597
with
```python
res = torch.linalg.lstsq(a_matrix, b_matrix).solution
```

So that we stop using a deprecated function.

One caveat is that maybe some tests might start failing (due to its inherent flakiness), and we should fix the tests that break on the way

It would be good to fix this before the next release, so that users don't get those warnings.

cc @vfdev-5


Discussion:

Resolving patch:
diff --git a/torchvision/transforms/functional.py b/torchvision/transforms/functional.py
--- a/torchvision/transforms/functional.py
+++ b/torchvision/transforms/functional.py
@@ -591,9 +591,9 @@ def _get_perspective_coeffs(
         a_matrix[2 * i + 1, :] = torch.tensor([0, 0, 0, p1[0], p1[1], 1, -p2[1] * p1[0], -p2[1] * p1[1]])
 
     b_matrix = torch.tensor(startpoints, dtype=torch.float).view(8)
-    res = torch.lstsq(b_matrix, a_matrix)[0]
+    res = torch.linalg.lstsq(a_matrix, b_matrix, driver='gels').solution
 
-    output: List[float] = res.squeeze(1).tolist()
+    output: List[float] = res.tolist()
     return output
 
 


Test patch:
Repository: pytorch/vision

Issue:
Add verbose mode for dataset constructors to print directory scan stats + add a warning when video loading fails
I'm trying to create VideoClips object with my custom folder with a video. It returns zero results, because it can't `read_video_timestamps` (under PyAV backend) with an error `av.error.InvalidDataError: [Errno 1094995529] Invalid data found when processing input: 'data/pseudo-kinetics/train_256/class0/P01_01.MP4'; last error log: [mov,mp4,m4a,3gp,3g2,mj2] moov atom not found`

Maybe it's well an invalid file, but it's indeed better to print a warning https://github.com/pytorch/vision/blob/7536e2987b13f02d40d064f57a5f417a7b560909/torchvision/io/video.py#L408 (at least when some verbose flag equals True - maybe worth introducing verbose flag in dataset constructors) and maybe print the stats over all files (how many loaded, how many skipped because of extensions, how many had errors while loading) - this would save a lot of time when creating a new dataset and that has some problems

cc @bjuncek


Discussion:
Thanks for reporting.

I think it would be good to add a warning there instead of silently catching the exception. If you are interested, please send a PR that introduces a warning.
File lists became available: https://github.com/cvdfoundation/kinetics-dataset

So extensions could now be set properly

Resolving patch:
diff --git a/torchvision/io/video.py b/torchvision/io/video.py
--- a/torchvision/io/video.py
+++ b/torchvision/io/video.py
@@ -391,9 +391,9 @@ def read_video_timestamps(filename: str, pts_unit: str = "pts") -> Tuple[List[in
                 except av.AVError:
                     warnings.warn(f"Failed decoding frames for file {filename}")
                 video_fps = float(video_stream.average_rate)
-    except av.AVError:
-        # TODO add a warning
-        pass
+    except av.AVError as e:
+        msg = f"Failed to open container for {filename}; Caught error: {e}"
+        warnings.warn(msg, RuntimeWarning)
 
     pts.sort()
 


Test patch:
Repository: pytorch/vision

Issue:
python_type_check CI is broken: missing stubs for requests module
## 🐛 Bug

The `python_type_check` CI is failing since mypy can't find library stubs for requests module used [here](https://github.com/pytorch/vision/blob/befbe1b372e7c028a7b4cb2155110fbae708038e/torchvision/datasets/utils.py#L204) .
This can be seen in the [failed tests](https://app.circleci.com/pipelines/github/pytorch/vision/8800/workflows/971b988a-2245-4aa7-acdf-97af0a38afe2/jobs/639205) for the recent [commit](https://github.com/pytorch/vision/commit/e4c56081ded40403bc3b53ff73ed819d6a46f33e) by @prabhat00155 (although those changes are completely unrelated to this) and on some other PRs like #4008 (edit: and #4010) 


## Pitch
A simple fix would be to install them with `python3 -m pip install types-requests` or to avoid something like this happening later we should just run `mypy --install-types` to install all the missing stubs.



Discussion:
After digging up a bit more, I found that this sudden appearance is probably due to the [mypy 0.900](https://github.com/python/mypy/releases/tag/v0.900) release today.

According to the docs:
> Starting in mypy 0.900, most third-party package stubs must be installed explicitly. This decouples mypy and stub versioning, allowing stubs to updated without updating mypy. This also allows stubs not originally included with mypy to be installed. Earlier mypy versions included a fixed set of stubs for third-party packages.

cc @NicolasHug @pmeier 

Resolving patch:
diff --git a/torchvision/datasets/utils.py b/torchvision/datasets/utils.py
--- a/torchvision/datasets/utils.py
+++ b/torchvision/datasets/utils.py
@@ -201,7 +201,7 @@ def download_file_from_google_drive(file_id: str, root: str, filename: Optional[
         md5 (str, optional): MD5 checksum of the download. If None, do not check
     """
     # Based on https://stackoverflow.com/questions/38511444/python-download-files-from-google-drive-using-url
-    import requests
+    import requests  # type: ignore
     url = "https://docs.google.com/uc?export=download"
 
     root = os.path.expanduser(root)


Test patch:
Repository: pytorch/vision

Issue:
Use `torch.frombuffer` instead of `np.frombuffer`
PyTorch recently introduced a `torch.frombuffer` method https://github.com/pytorch/pytorch/pull/59077 
We should use it to simplify our code whenever possible, instead of relying on alternatives like `torch.from_numpy(np.frombuffer))`.


Discussion:
@datumbox I can take a look at this issue. Code search does not show too many of occurrences of  `np.frombuffer`

Resolving patch:
diff --git a/torchvision/datasets/mnist.py b/torchvision/datasets/mnist.py
--- a/torchvision/datasets/mnist.py
+++ b/torchvision/datasets/mnist.py
@@ -3,6 +3,7 @@
 import os.path
 import shutil
 import string
+import sys
 import warnings
 from typing import Any, Callable, Dict, List, Optional, Tuple
 from urllib.error import URLError
@@ -489,12 +490,12 @@ def get_int(b: bytes) -> int:
 
 
 SN3_PASCALVINCENT_TYPEMAP = {
-    8: (torch.uint8, np.uint8, np.uint8),
-    9: (torch.int8, np.int8, np.int8),
-    11: (torch.int16, np.dtype(">i2"), "i2"),
-    12: (torch.int32, np.dtype(">i4"), "i4"),
-    13: (torch.float32, np.dtype(">f4"), "f4"),
-    14: (torch.float64, np.dtype(">f8"), "f8"),
+    8: torch.uint8,
+    9: torch.int8,
+    11: torch.int16,
+    12: torch.int32,
+    13: torch.float32,
+    14: torch.float64,
 }
 
 
@@ -511,11 +512,19 @@ def read_sn3_pascalvincent_tensor(path: str, strict: bool = True) -> torch.Tenso
     ty = magic // 256
     assert 1 <= nd <= 3
     assert 8 <= ty <= 14
-    m = SN3_PASCALVINCENT_TYPEMAP[ty]
+    torch_type = SN3_PASCALVINCENT_TYPEMAP[ty]
     s = [get_int(data[4 * (i + 1) : 4 * (i + 2)]) for i in range(nd)]
-    parsed = np.frombuffer(data, dtype=m[1], offset=(4 * (nd + 1)))
+
+    num_bytes_per_value = torch.iinfo(torch_type).bits // 8
+    # The MNIST format uses the big endian byte order. If the system uses little endian byte order by default,
+    # we need to reverse the bytes before we can read them with torch.frombuffer().
+    needs_byte_reversal = sys.byteorder == "little" and num_bytes_per_value > 1
+    parsed = torch.frombuffer(bytearray(data), dtype=torch_type, offset=(4 * (nd + 1)))
+    if needs_byte_reversal:
+        parsed = parsed.flip(0)
+
     assert parsed.shape[0] == np.prod(s) or not strict
-    return torch.from_numpy(parsed.astype(m[2])).view(*s)
+    return parsed.view(*s)
 
 
 def read_label_file(path: str) -> torch.Tensor:


Test patch:
Repository: pytorch/vision

Issue:
Add API usage calls to utils
As discussed in API usage logging policy(#5052), we would like to track usage of utils as well. We need to call _log_api_usage_once in all public methods in [utils.py](https://github.com/pytorch/vision/blob/main/torchvision/utils.py)


Discussion:

Resolving patch:
diff --git a/torchvision/utils.py b/torchvision/utils.py
--- a/torchvision/utils.py
+++ b/torchvision/utils.py
@@ -42,6 +42,7 @@ def make_grid(
     Returns:
         grid (Tensor): the tensor containing grid of images.
     """
+    _log_api_usage_once("utils", "make_grid")
     if not (torch.is_tensor(tensor) or (isinstance(tensor, list) and all(torch.is_tensor(t) for t in tensor))):
         raise TypeError(f"tensor or list of tensors expected, got {type(tensor)}")
 
@@ -130,6 +131,7 @@ def save_image(
         **kwargs: Other arguments are documented in ``make_grid``.
     """
 
+    _log_api_usage_once("utils", "save_image")
     grid = make_grid(tensor, **kwargs)
     # Add 0.5 after unnormalizing to [0, 255] to round to nearest integer
     ndarr = grid.mul(255).add_(0.5).clamp_(0, 255).permute(1, 2, 0).to("cpu", torch.uint8).numpy()
@@ -174,6 +176,7 @@ def draw_bounding_boxes(
         img (Tensor[C, H, W]): Image Tensor of dtype uint8 with bounding boxes plotted.
     """
 
+    _log_api_usage_once("utils", "draw_bounding_boxes")
     if not isinstance(image, torch.Tensor):
         raise TypeError(f"Tensor expected, got {type(image)}")
     elif image.dtype != torch.uint8:
@@ -252,6 +255,7 @@ def draw_segmentation_masks(
         img (Tensor[C, H, W]): Image Tensor, with segmentation masks drawn on top.
     """
 
+    _log_api_usage_once("utils", "draw_segmentation_masks")
     if not isinstance(image, torch.Tensor):
         raise TypeError(f"The image must be a tensor, got {type(image)}")
     elif image.dtype != torch.uint8:
@@ -329,6 +333,7 @@ def draw_keypoints(
         img (Tensor[C, H, W]): Image Tensor of dtype uint8 with keypoints drawn.
     """
 
+    _log_api_usage_once("utils", "draw_keypoints")
     if not isinstance(image, torch.Tensor):
         raise TypeError(f"The image must be a tensor, got {type(image)}")
     elif image.dtype != torch.uint8:


Test patch:
Repository: pytorch/vision

Issue:
Example doesnt work
### 📚 The doc issue

In this example for fasterrcnn https://github.com/pytorch/vision/blob/e65a857b5487a8493bc8a80a95d64d9f049de347/torchvision/models/detection/faster_rcnn.py#L354
in the # For training part
the output is giving an error
"ValueError: All bounding boxes should have positive height and width. Found invalid box [0.45278316736221313, 0.678650438785553, 1.0717594623565674, 0.416100412607193] for target at index 0."

### Suggest a potential alternative/fix

Maybe change the way bbox are defined, here it is being initialized randomly


Discussion:
@mrinath123 Thanks, you are right. Executing the following:

```python
import torch
import torchvision

model = torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained=True)
images, boxes = torch.rand(4, 3, 600, 1200), torch.rand(4, 11, 4)
labels = torch.randint(1, 91, (4, 11))
images = list(image for image in images)
targets = []
for i in range(len(images)):
    d = {}
    d['boxes'] = boxes[i]
    d['labels'] = labels[i]
    targets.append(d)
output = model(images, targets)
 # For inference
model.eval()
x = [torch.rand(3, 300, 400), torch.rand(3, 500, 400)]
predictions = model(x)
```

Leads to the above exception because some of the bottom right coordinates of the boxes have larger values than the ones of top left. As you correctly pointed out, to fix this we need to ensure that `boxes[:, :, 2:4] > boxes[:, :, 0:2]`.

We could add something like the following right after the initialization of `boxes` to fix it but other patches exist:
`boxes[:, :, 2:4] = boxes[:, :, 0:2] + boxes[:, :, 2:4]`

Let me know if you would like to send a PR to fix the problem.
I will love to send you a PR, would be my 1st contribution to this awesome framework!
Sounds great! Feel free to add me as a reviewer once you send the PR. Looking forward to your contribution. :)

Resolving patch:
diff --git a/torchvision/models/detection/faster_rcnn.py b/torchvision/models/detection/faster_rcnn.py
--- a/torchvision/models/detection/faster_rcnn.py
+++ b/torchvision/models/detection/faster_rcnn.py
@@ -356,6 +356,7 @@ def fasterrcnn_resnet50_fpn(
         >>> model = torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained=True)
         >>> # For training
         >>> images, boxes = torch.rand(4, 3, 600, 1200), torch.rand(4, 11, 4)
+        >>> boxes[:, :, 2:4] = boxes[:, :, 0:2] + boxes[:, :, 2:4]
         >>> labels = torch.randint(1, 91, (4, 11))
         >>> images = list(image for image in images)
         >>> targets = []


Test patch:
Repository: pytorch/vision

Issue:
Bug in keypoint_rcnn initializer
### 🐛 Describe the bug

Line 1890 in keypoint_rcnn
"
        if num_classes is not None:
            if keypoint_predictor is not None:
                raise ValueError("num_classes should be None when keypoint_predictor is specified")
"
Shouldn't we change num_classes to num_keypoints in this case?

### Versions

Newest

cc @datumbox


Discussion:
@JunzheJosephZhu You are right, this is a bug. Instead of `num_classes` we should be checking the `num_keypoints`.

The problem now is that the default value of `num_keypoints=17`, so checking for `None` makes no sense. One way to address this is to change the default value to `None` and for maintaining backwards compatibility overwrite the None value to 17 after the highlighted-if. Let me know if you would like to send a PR to fix this.

Resolving patch:
diff --git a/torchvision/models/detection/keypoint_rcnn.py b/torchvision/models/detection/keypoint_rcnn.py
--- a/torchvision/models/detection/keypoint_rcnn.py
+++ b/torchvision/models/detection/keypoint_rcnn.py
@@ -188,16 +188,18 @@ def __init__(
         keypoint_roi_pool=None,
         keypoint_head=None,
         keypoint_predictor=None,
-        num_keypoints=17,
+        num_keypoints=None,
     ):
 
         assert isinstance(keypoint_roi_pool, (MultiScaleRoIAlign, type(None)))
         if min_size is None:
             min_size = (640, 672, 704, 736, 768, 800)
 
-        if num_classes is not None:
+        if num_keypoints is not None:
             if keypoint_predictor is not None:
-                raise ValueError("num_classes should be None when keypoint_predictor is specified")
+                raise ValueError("num_keypoints should be None when keypoint_predictor is specified")
+        else:
+            num_keypoints = 17
 
         out_channels = backbone.out_channels
 


Test patch:
Repository: pytorch/vision

Issue:
Add tests for prototype utilities
Perhaps we should add tests for `sequence_to_str`, so as to ensure that our error message are properly formatted ?

Also nit but if we're going to have such a helper, we could handle the case `a, and b` -> `a and b` (i.e. remove the comma if len == 2

_Originally posted by @NicolasHug in https://github.com/pytorch/vision/pull/5187#discussion_r784852697_

cc @bjuncek


Discussion:

Resolving patch:
diff --git a/torchvision/prototype/utils/_internal.py b/torchvision/prototype/utils/_internal.py
--- a/torchvision/prototype/utils/_internal.py
+++ b/torchvision/prototype/utils/_internal.py
@@ -30,10 +30,15 @@ class StrEnum(enum.Enum, metaclass=StrEnumMeta):
 
 
 def sequence_to_str(seq: Sequence, separate_last: str = "") -> str:
+    if not seq:
+        return ""
     if len(seq) == 1:
         return f"'{seq[0]}'"
 
-    return f"""'{"', '".join([str(item) for item in seq[:-1]])}', {separate_last}'{seq[-1]}'"""
+    head = "'" + "', '".join([str(item) for item in seq[:-1]]) + "'"
+    tail = f"{'' if separate_last and len(seq) == 2 else ','} {separate_last}'{seq[-1]}'"
+
+    return head + tail
 
 
 def add_suggestion(


Test patch:
diff --git a/test/test_prototype_utils.py b/test/test_prototype_utils.py
new file mode 100644
--- /dev/null
+++ b/test/test_prototype_utils.py
@@ -0,0 +1,17 @@
+import pytest
+from torchvision.prototype.utils._internal import sequence_to_str
+
+
+@pytest.mark.parametrize(
+    ("seq", "separate_last", "expected"),
+    [
+        ([], "", ""),
+        (["foo"], "", "'foo'"),
+        (["foo", "bar"], "", "'foo', 'bar'"),
+        (["foo", "bar"], "and ", "'foo' and 'bar'"),
+        (["foo", "bar", "baz"], "", "'foo', 'bar', 'baz'"),
+        (["foo", "bar", "baz"], "and ", "'foo', 'bar', and 'baz'"),
+    ],
+)
+def test_sequence_to_str(seq, separate_last, expected):
+    assert sequence_to_str(seq, separate_last=separate_last) == expected