internlm/SWE-Fixer-Train-110K huggingface dataset:
Repository: ultralytics/yolov5

Issue:
Unexpected behavior of `check_git_status()` utility function
### Search before asking

- [X] I have searched the YOLOv5 [issues](https://github.com/ultralytics/yolov5/issues) and found no similar bug report.


### YOLOv5 Component

Training

### Bug

The following function is called from within `train.py`.

https://github.com/ultralytics/yolov5/blob/4c0982a243aac3969345fc61e10eb7ea4d78e104/utils/general.py#L206-L222

Based on my understanding, the current implementation of `check_git_status()` makes one of two assumptions:

1. `train.py` has been called directly from inside the root YOLOv5 directory: In this case `check_git_status()` works as intended, informing whether the repository is up to date or not.
2. User has called `train.py` from somewhere else, most likely by providing the full path. For instance, `python path/to/train.py ...`. In this case, `check_git_status()` assumes that the directory from where the training script is executed is NOT version controlled.

The assumption made in scenario 2 is invalid if the training command is executed from some other **version-controlled** directory. Since that directory will also have a `.git` folder, `check_git_status()` mistakenly considers that the script has been called from within the Yolo repository.

And we end up with something like this:

![image](https://user-images.githubusercontent.com/19644085/139732946-02353c09-61dd-41b0-93d4-f6e32070e0f1.png)



### Environment

- YOLOv5  v6.0-40-g8c326a1 torch 1.10.0 CUDA:0 (Quadro P1000, 4096.0MB)
- OS: Windows
- Python: 3.8

### Minimal Reproducible Example

_No response_

### Additional

A potential solution could be to change the working directory to the YOLOv5 repository root within the function `check_git_status()`

### Are you willing to submit a PR?

- [X] Yes I'd like to help by submitting a PR!


Discussion:

Resolving patch:
diff --git a/utils/general.py b/utils/general.py
--- a/utils/general.py
+++ b/utils/general.py
@@ -81,6 +81,19 @@ def __exit__(self, exc_type, exc_val, exc_tb):
             return True
 
 
+class WorkingDirectory(contextlib.ContextDecorator):
+    # Usage: @WorkingDirectory(dir) decorator or 'with WorkingDirectory(dir):' context manager
+    def __init__(self, new_dir):
+        self.dir = new_dir  # new dir
+        self.cwd = Path.cwd().resolve()  # current dir
+
+    def __enter__(self):
+        os.chdir(self.dir)
+
+    def __exit__(self, exc_type, exc_val, exc_tb):
+        os.chdir(self.cwd)
+
+
 def try_except(func):
     # try-except function. Usage: @try_except decorator
     def handler(*args, **kwargs):
@@ -203,6 +216,7 @@ def check_online():
 
 
 @try_except
+@WorkingDirectory(ROOT)
 def check_git_status():
     # Recommend 'git pull' if code is out of date
     msg = ', for updates see https://github.com/ultralytics/yolov5'


Test patch:
Repository: ultralytics/yolov5

Issue:
Documentation on aspect ratio threshold 
## 🚀 Feature
Hi @glenn-jocher
Thanks for the amazing work with yolov5
This feature is related to documentation on the aspect ratio threshold in utils/datasets.py that is used for custom training.

## Motivation
I have been working on object detection projects and using YOLOv5 frequently for some of the training tasks at hand. However, most of the time, mAP performance did not match my expectations set seeing the mAP scores on the COCO dataset. My dataset has mostly rectangular objects, and about 10-15% of them being either very wide or very tall. Going through one of the issues on GitHub, I realized that there is an aspect ratio threshold that discards all objects having an aspect ratio > 20 by default.

## Pitch

It would be great to make this clear in the train custom dataset (https://github.com/ultralytics/yolov5/wiki/Train-Custom-Data)
so that we can adjust this parameter from the get go during experimentation. Thanks for the help!




Discussion:
@ml5ah thanks for the feedback! Yes perhaps box candidate thresholds should be included in the hyperparameters used for training to make them easier to modify and easier to spot. Can you submit a PR for this to help others? I will add a TODO here to update the Custom Training Tutorial with this information.

In general you should also be aware that the default architecture is not optimized for high aspect ratio objects, and the models would benefit from changes targeted specifically to this domain, which may include elongation of the convolution kernels in the C3 layers, i.e. perhaps alternate 5x1 and 1x5 convolutions in place of, or in addition to, the current 3x3 and 1x1 sequences.

TODO: Update Train Custom Data Tutorial with label candidate criteria section, as well as NMS settings section.
Very good note 👍👍
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Resolving patch:
diff --git a/utils/augmentations.py b/utils/augmentations.py
--- a/utils/augmentations.py
+++ b/utils/augmentations.py
@@ -269,7 +269,7 @@ def mixup(im, labels, im2, labels2):
     return im, labels
 
 
-def box_candidates(box1, box2, wh_thr=2, ar_thr=20, area_thr=0.1, eps=1e-16):  # box1(4,n), box2(4,n)
+def box_candidates(box1, box2, wh_thr=2, ar_thr=100, area_thr=0.1, eps=1e-16):  # box1(4,n), box2(4,n)
     # Compute candidate boxes: box1 before augment, box2 after augment, wh_thr (pixels), aspect_ratio_thr, area_ratio
     w1, h1 = box1[2] - box1[0], box1[3] - box1[1]
     w2, h2 = box2[2] - box2[0], box2[3] - box2[1]


Test patch:
Repository: ultralytics/yolov5

Issue:
Call to Detections.__init__() missing 1 required positional argument: 'files'
### Search before asking

- [X] I have searched the YOLOv5 [issues](https://github.com/ultralytics/yolov5/issues) and found no similar bug report.


### YOLOv5 Component

Detection

### Bug

Calling `.tolist()` on a `Detections` object crashes with the following message:

```
  File "/****/.cache/torch/hub/ultralytics_yolov5_master/models/common.py", line 615, in <listcomp>
    x = [Detections([self.imgs[i]], [self.pred[i]], names=self.names, shape=self.s) for i in range(self.n)]
TypeError: __init__() missing 1 required positional argument: 'files'
```

There was a recent change (PR #5907), which fixed another bug. Unfortunately, it seems to have introduced this one.

### Environment

_No response_

### Minimal Reproducible Example

_No response_

### Additional

_No response_

### Are you willing to submit a PR?

- [X] Yes I'd like to help by submitting a PR!


Discussion:

Resolving patch:
diff --git a/models/common.py b/models/common.py
--- a/models/common.py
+++ b/models/common.py
@@ -525,7 +525,7 @@ def forward(self, imgs, size=640, augment=False, profile=False):
 
 class Detections:
     # YOLOv5 detections class for inference results
-    def __init__(self, imgs, pred, files, times=None, names=None, shape=None):
+    def __init__(self, imgs, pred, files, times=(0, 0, 0, 0), names=None, shape=None):
         super().__init__()
         d = pred[0].device  # device
         gn = [torch.tensor([*(im.shape[i] for i in [1, 0, 1, 0]), 1, 1], device=d) for im in imgs]  # normalizations
@@ -533,6 +533,7 @@ def __init__(self, imgs, pred, files, times=None, names=None, shape=None):
         self.pred = pred  # list of tensors pred[0] = (xyxy, conf, cls)
         self.names = names  # class names
         self.files = files  # image filenames
+        self.times = times  # profiling times
         self.xyxy = pred  # xyxy pixels
         self.xywh = [xyxy2xywh(x) for x in pred]  # xywh pixels
         self.xyxyn = [x / g for x, g in zip(self.xyxy, gn)]  # xyxy normalized
@@ -612,10 +613,11 @@ def pandas(self):
 
     def tolist(self):
         # return a list of Detections objects, i.e. 'for result in results.tolist():'
-        x = [Detections([self.imgs[i]], [self.pred[i]], names=self.names, shape=self.s) for i in range(self.n)]
-        for d in x:
-            for k in ['imgs', 'pred', 'xyxy', 'xyxyn', 'xywh', 'xywhn']:
-                setattr(d, k, getattr(d, k)[0])  # pop out of list
+        r = range(self.n)  # iterable
+        x = [Detections([self.imgs[i]], [self.pred[i]], [self.files[i]], self.times, self.names, self.s) for i in r]
+        # for d in x:
+        #    for k in ['imgs', 'pred', 'xyxy', 'xyxyn', 'xywh', 'xywhn']:
+        #        setattr(d, k, getattr(d, k)[0])  # pop out of list
         return x
 
     def __len__(self):


Test patch:
Repository: ultralytics/yolov5

Issue:
('int' object is not iterable) running detect from a python script
### Search before asking

- [X] I have searched the YOLOv5 [issues](https://github.com/ultralytics/yolov5/issues) and found no similar bug report.


### YOLOv5 Component

Detection

### Bug

Hello,

`python detect.py --weights x --source y` works well from command line.

But whenever I try to use it from a python script i get the following error :

```
File "...\venv\lib\site-packages\torch\autograd\grad_mode.py", line 28, in decorate_context
    return func(*args, **kwargs)
  File "...\yolov5\detect.py", line 100, in run
    model.warmup(imgsz=(1, 3, *imgsz), half=half)  # warmup
TypeError: 'int' object is not iterable
```

Script example :
```
import yolov5.detect as detect

if __name__ == "__main__":
    torch.cuda.empty_cache()
    detect.run(weights=".../best.pt", source=".../images")
```

I'm using subprocess for now to get around this issue but using the output is not friendly at all 🤣 

Keep up this awesome project !

### Environment

yolo: YOLOv5  v6.0-133-g4fb6dd4 torch 1.10.0+cu102 CUDA:0 (NVIDIA GeForce RTX 2080 Ti, 11264MiB)
OS: win10
py: 3.8.9

### Minimal Reproducible Example

```
import yolov5.detect as detect

if __name__ == "__main__":
    torch.cuda.empty_cache()
    detect.run(weights=".../best.pt", source=".../images")
```

### Additional

_No response_

### Are you willing to submit a PR?

- [ ] Yes I'd like to help by submitting a PR!


Discussion:

Resolving patch:
diff --git a/detect.py b/detect.py
--- a/detect.py
+++ b/detect.py
@@ -38,7 +38,7 @@
 @torch.no_grad()
 def run(weights=ROOT / 'yolov5s.pt',  # model.pt path(s)
         source=ROOT / 'data/images',  # file/dir/URL/glob, 0 for webcam
-        imgsz=640,  # inference size (pixels)
+        imgsz=(640, 640),  # inference size (height, width)
         conf_thres=0.25,  # confidence threshold
         iou_thres=0.45,  # NMS IOU threshold
         max_det=1000,  # maximum detections per image


Test patch:
Repository: ultralytics/yolov5

Issue:
Yolov5 Torchscript Lite Version
### Search before asking

- [X] I have searched the YOLOv5 [issues](https://github.com/ultralytics/yolov5/issues) and [discussions](https://github.com/ultralytics/yolov5/discussions) and found no similar questions.


### Question

Hello @glenn-jocher  et all,

when I run android demo project for yolov5 on android I see error message below. I have follow any insctruction for save model with _save_for_lite_interpreter() method but it has no change. How to fixed it ? thanks



### Additional

java.lang.RuntimeException: Unable to start activity ComponentInfo{org.pytorch.demo.objectdetection/org.pytorch.demo.objectdetection.MainActivity}: com.facebook.jni.CppException: PytorchStreamReader failed locating file bytecode.pkl: file not found ()


Discussion:

Resolving patch:
diff --git a/export.py b/export.py
--- a/export.py
+++ b/export.py
@@ -75,7 +75,10 @@ def export_torchscript(model, im, file, optimize, prefix=colorstr('TorchScript:'
         ts = torch.jit.trace(model, im, strict=False)
         d = {"shape": im.shape, "stride": int(max(model.stride)), "names": model.names}
         extra_files = {'config.txt': json.dumps(d)}  # torch._C.ExtraFilesMap()
-        (optimize_for_mobile(ts) if optimize else ts).save(str(f), _extra_files=extra_files)
+        if optimize:  # https://pytorch.org/tutorials/recipes/mobile_interpreter.html
+            optimize_for_mobile(ts)._save_for_lite_interpreter(str(f), _extra_files=extra_files)
+        else:
+            ts.save(str(f), _extra_files=extra_files)
 
         LOGGER.info(f'{prefix} export success, saved as {f} ({file_size(f):.1f} MB)')
     except Exception as e:


Test patch:
Repository: ultralytics/yolov5

Issue:
tensorrt 7.2.3.4 get some wrong boxes with high score,but tensort 8.2 is correct
### Search before asking

- [X] I have searched the YOLOv5 [issues](https://github.com/ultralytics/yolov5/issues) and [discussions](https://github.com/ultralytics/yolov5/discussions) and found no similar questions.


### Question

tensorrt 7.2.3.4 get some wrong boxes with high score,but tensort 8.2 is correct
ubuntu 16.04  tensorrt 7.2.3.4  cuda 10.2  

![zidane](https://user-images.githubusercontent.com/29919617/146485594-c859bde3-2686-4d8a-b8b8-0b9dbc70c817.jpg)

have you tried the  tensorrt 7 ？

### Additional

_No response_


Discussion:

Resolving patch:
diff --git a/export.py b/export.py
--- a/export.py
+++ b/export.py
@@ -175,7 +175,13 @@ def export_engine(model, im, file, train, half, simplify, workspace=4, verbose=F
         import tensorrt as trt
 
         opset = (12, 13)[trt.__version__[0] == '8']  # test on TensorRT 7.x and 8.x
-        export_onnx(model, im, file, opset, train, False, simplify)
+        if opset == 12:  # TensorRT 7 handling https://github.com/ultralytics/yolov5/issues/6012
+            grid = model.model[-1].anchor_grid
+            model.model[-1].anchor_grid = [a[..., :1, :1, :] for a in grid]
+            export_onnx(model, im, file, opset, train, False, simplify)
+            model.model[-1].anchor_grid = grid
+        else:  # TensorRT >= 8
+            export_onnx(model, im, file, opset, train, False, simplify)
         onnx = file.with_suffix('.onnx')
         assert onnx.exists(), f'failed to export ONNX file: {onnx}'
 
@@ -418,12 +424,12 @@ def run(data=ROOT / 'data/coco128.yaml',  # 'dataset.yaml path'
     # Exports
     if 'torchscript' in include:
         export_torchscript(model, im, file, optimize)
+    if 'engine' in include:  # TensorRT required before ONNX
+        export_engine(model, im, file, train, half, simplify, workspace, verbose)
     if ('onnx' in include) or ('openvino' in include):  # OpenVINO requires ONNX
         export_onnx(model, im, file, opset, train, dynamic, simplify)
     if 'openvino' in include:
         export_openvino(model, im, file)
-    if 'engine' in include:
-        export_engine(model, im, file, train, half, simplify, workspace, verbose)
     if 'coreml' in include:
         export_coreml(model, im, file)
 


Test patch:
Repository: ultralytics/yolov5

Issue:
GPU Memory Leak on Loading Pre-Trained Checkpoint
### Search before asking

- [X] I have searched the YOLOv5 [issues](https://github.com/ultralytics/yolov5/issues) and found no similar bug report.


### YOLOv5 Component

Training

### Bug

Training YOLO from a checkpoint (*.pt) consumes more GPU memory than training from a pre-trained weight (i.e. yolov5l).

### Environment

- YOLO: YOLOv5 (latest; how to check the yolo version?)
- CUDA: 11.6 (Tesla T4, 15360MiB)
- OS: Ubuntu 18.04.6 LTS (Bionic Beaver)
- Python: 3.8.12

### Minimal Reproducible Example

In the below training command, case 2 requires more GPU memory than case 1.
```
# 1. train from pre-trained model
train.py ... --weights yolov5l

# 2. train from pre-trained checkpoint
train.py ... --weights pre_trained_checkpoint.pt
```

### Additional

As reported on the pytorch forum[1], loading state dict on CUDA device causes memory leak. We should load it on CPU memory:

```python
state_dict = torch.load(directory, map_location=lambda storage, loc: storage)
```

- [1] https://discuss.pytorch.org/t/load-state-dict-causes-memory-leak/36189/5?u=bilzrd

### Are you willing to submit a PR?

- [X] Yes I'd like to help by submitting a PR!


Discussion:

Resolving patch:
diff --git a/train.py b/train.py
--- a/train.py
+++ b/train.py
@@ -120,7 +120,7 @@ def train(hyp,  # path/to/hyp.yaml or hyp dictionary
     if pretrained:
         with torch_distributed_zero_first(LOCAL_RANK):
             weights = attempt_download(weights)  # download if not found locally
-        ckpt = torch.load(weights, map_location=device)  # load checkpoint
+        ckpt = torch.load(weights, map_location='cpu')  # load checkpoint to CPU to avoid CUDA memory leak
         model = Model(cfg or ckpt['model'].yaml, ch=3, nc=nc, anchors=hyp.get('anchors')).to(device)  # create
         exclude = ['anchor'] if (cfg or hyp.get('anchors')) and not resume else []  # exclude keys
         csd = ckpt['model'].float().state_dict()  # checkpoint state_dict as FP32


Test patch:
Repository: ultralytics/yolov5

Issue:
Please add transport stream to VIDEO file formats
### Search before asking

- [X] I have searched the YOLOv5 [issues](https://github.com/ultralytics/yolov5/issues) and found no similar feature requests.


### Description


Can transport stream be added , tested and it works fine.

utils/datasets.py (line 37)

FROM
VID_FORMATS = ['mov', 'avi', 'mp4', 'mpg', 'mpeg', 'm4v', 'wmv', 'mkv']  # acceptable video suffixes

TO
VID_FORMATS = ['mov', 'avi', 'mp4', 'mpg', 'mpeg', 'm4v', 'wmv', 'mkv', 'ts']  # acceptable video suffixes

### Use case

Allow more input file formats.


### Additional

Please consider adding a frame skip parameter.

### Are you willing to submit a PR?

- [ ] Yes I'd like to help by submitting a PR!


Discussion:

Resolving patch:
diff --git a/utils/datasets.py b/utils/datasets.py
--- a/utils/datasets.py
+++ b/utils/datasets.py
@@ -33,8 +33,8 @@
 
 # Parameters
 HELP_URL = 'https://github.com/ultralytics/yolov5/wiki/Train-Custom-Data'
-IMG_FORMATS = ['bmp', 'dng', 'jpeg', 'jpg', 'mpo', 'png', 'tif', 'tiff', 'webp']  # include image suffixes
-VID_FORMATS = ['asf', 'avi', 'gif', 'm4v', 'mkv', 'mov', 'mp4', 'mpeg', 'mpg', 'wmv']  # include video suffixes
+IMG_FORMATS = 'bmp', 'dng', 'jpeg', 'jpg', 'mpo', 'png', 'tif', 'tiff', 'webp'  # include image suffixes
+VID_FORMATS = 'asf', 'avi', 'gif', 'm4v', 'mkv', 'mov', 'mp4', 'mpeg', 'mpg', 'ts', 'wmv'  # include video suffixes
 
 # Get orientation exif tag
 for orientation in ExifTags.TAGS.keys():


Test patch:
Repository: ultralytics/yolov5

Issue:
Inconsistent model output format after PR #6292
### Search before asking

- [X] I have searched the YOLOv5 [issues](https://github.com/ultralytics/yolov5/issues) and found no similar bug report.


### YOLOv5 Component

Export

### Bug

Based on this return statement in `TFModel`, the output of a TF saved_model was/should be a tensor. https://github.com/ultralytics/yolov5/blob/master/models/tf.py#L368

However, after the changes introduced in #6292, the TF saved_model **returns the said tensor wrapped in a list**.

1. This breaks compatibility across TF saved_models exported before and after the change.
2. Moreover, the output of a saved model is either a tensor (if `keras=True`) or the tensor wrapped in a list `keras=False` (now default).

### Environment

YOLOv5 Latest
OS: Ubuntu, Windows
Python: 3.9
TensorFlow: 2.7

### Minimal Reproducible Example

_No response_

### Additional

#7032 attempts to solve all these issues.

### Are you willing to submit a PR?

- [X] Yes I'd like to help by submitting a PR!


Discussion:

Resolving patch:
diff --git a/export.py b/export.py
--- a/export.py
+++ b/export.py
@@ -275,7 +275,7 @@ def export_saved_model(model, im, file, dynamic,
             m = m.get_concrete_function(spec)
             frozen_func = convert_variables_to_constants_v2(m)
             tfm = tf.Module()
-            tfm.__call__ = tf.function(lambda x: frozen_func(x), [spec])
+            tfm.__call__ = tf.function(lambda x: frozen_func(x)[0], [spec])
             tfm.__call__(im)
             tf.saved_model.save(
                 tfm,
diff --git a/models/common.py b/models/common.py
--- a/models/common.py
+++ b/models/common.py
@@ -441,7 +441,7 @@ def forward(self, im, augment=False, visualize=False, val=False):
         else:  # TensorFlow (SavedModel, GraphDef, Lite, Edge TPU)
             im = im.permute(0, 2, 3, 1).cpu().numpy()  # torch BCHW to numpy BHWC shape(1,320,192,3)
             if self.saved_model:  # SavedModel
-                y = (self.model(im, training=False) if self.keras else self.model(im)[0]).numpy()
+                y = (self.model(im, training=False) if self.keras else self.model(im)).numpy()
             elif self.pb:  # GraphDef
                 y = self.frozen_func(x=self.tf.constant(im)).numpy()
             else:  # Lite or Edge TPU


Test patch:
Repository: ultralytics/yolov5

Issue:
Celery logger stops working using torch.hub.load
### Search before asking

- [X] I have searched the YOLOv5 [issues](https://github.com/ultralytics/yolov5/issues) and found no similar bug report.


### YOLOv5 Component

PyTorch Hub

### Bug

For some reasons, while using torch.hub.load in celery worker, logger is getting shutdown. By specifying v6.0 in repo_or_dir, issue seems to be resolved

### Environment

_No response_

### Minimal Reproducible Example

_No response_

### Additional

_No response_

### Are you willing to submit a PR?

- [ ] Yes I'd like to help by submitting a PR!


Discussion:

Resolving patch:
diff --git a/utils/general.py b/utils/general.py
--- a/utils/general.py
+++ b/utils/general.py
@@ -82,11 +82,17 @@ def set_logging(name=None, verbose=VERBOSE):
         for h in logging.root.handlers:
             logging.root.removeHandler(h)  # remove all handlers associated with the root logger object
     rank = int(os.getenv('RANK', -1))  # rank in world for Multi-GPU trainings
-    logging.basicConfig(format="%(message)s", level=logging.INFO if (verbose and rank in (-1, 0)) else logging.WARNING)
-    return logging.getLogger(name)
+    level = logging.INFO if (verbose and rank in (-1, 0)) else logging.WARNING
+    log = logging.getLogger(name)
+    log.setLevel(level)
+    handler = logging.StreamHandler()
+    handler.setFormatter(logging.Formatter("%(message)s"))
+    handler.setLevel(level)
+    log.addHandler(handler)
 
 
-LOGGER = set_logging('yolov5')  # define globally (used in train.py, val.py, detect.py, etc.)
+set_logging()  # run before defining LOGGER
+LOGGER = logging.getLogger("yolov5")  # define globally (used in train.py, val.py, detect.py, etc.)
 
 
 def user_config_dir(dir='Ultralytics', env_var='YOLOV5_CONFIG_DIR'):


Test patch: