new
Browse files- Dockerfile +1 -1
- app.py +4 -5
- ultralytics/nn/tasks.py +1 -38
Dockerfile
CHANGED
@@ -28,4 +28,4 @@ USER user
|
|
28 |
COPY --chown=user . /app
|
29 |
|
30 |
# Set the command to run the app with Gunicorn
|
31 |
-
CMD ["gunicorn", "-b", "0.0.0.0:7860", "app:app"]
|
|
|
28 |
COPY --chown=user . /app
|
29 |
|
30 |
# Set the command to run the app with Gunicorn
|
31 |
+
CMD ["gunicorn", "-b", "0.0.0.0:7860", "app:app","--timeout", "9600"]
|
app.py
CHANGED
@@ -102,7 +102,10 @@ def process_image():
|
|
102 |
# Check contents in the root directory
|
103 |
print("Current directory contents:", os.listdir('/'))
|
104 |
|
105 |
-
model = YOLOv10(f'./runs/detect/train3/weights/best
|
|
|
|
|
|
|
106 |
dataset = sv.DetectionDataset.from_yolo(
|
107 |
images_directory_path=f"./data/MyNewVersion5.0Dataset/valid/images",
|
108 |
annotations_directory_path=f"./data/MyNewVersion5.0Dataset/valid/labels",
|
@@ -239,7 +242,3 @@ def download_image(filename):
|
|
239 |
@app.route('/uploads/<filename>')
|
240 |
def serve_uploaded_file(filename):
|
241 |
return send_from_directory('./app/data1', filename)
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
|
|
102 |
# Check contents in the root directory
|
103 |
print("Current directory contents:", os.listdir('/'))
|
104 |
|
105 |
+
model = YOLOv10(f'./runs/detect/train3/weights/best.pt')
|
106 |
+
#device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
107 |
+
#model.to(device)
|
108 |
+
|
109 |
dataset = sv.DetectionDataset.from_yolo(
|
110 |
images_directory_path=f"./data/MyNewVersion5.0Dataset/valid/images",
|
111 |
annotations_directory_path=f"./data/MyNewVersion5.0Dataset/valid/labels",
|
|
|
242 |
@app.route('/uploads/<filename>')
|
243 |
def serve_uploaded_file(filename):
|
244 |
return send_from_directory('./app/data1', filename)
|
|
|
|
|
|
|
|
ultralytics/nn/tasks.py
CHANGED
@@ -82,10 +82,8 @@ class BaseModel(nn.Module):
|
|
82 |
def forward(self, x, *args, **kwargs):
|
83 |
"""
|
84 |
Forward pass of the model on a single scale. Wrapper for `_forward_once` method.
|
85 |
-
|
86 |
Args:
|
87 |
x (torch.Tensor | dict): The input image tensor or a dict including image tensor and gt labels.
|
88 |
-
|
89 |
Returns:
|
90 |
(torch.Tensor): The output of the network.
|
91 |
"""
|
@@ -96,14 +94,12 @@ class BaseModel(nn.Module):
|
|
96 |
def predict(self, x, profile=False, visualize=False, augment=False, embed=None):
|
97 |
"""
|
98 |
Perform a forward pass through the network.
|
99 |
-
|
100 |
Args:
|
101 |
x (torch.Tensor): The input tensor to the model.
|
102 |
profile (bool): Print the computation time of each layer if True, defaults to False.
|
103 |
visualize (bool): Save the feature maps of the model if True, defaults to False.
|
104 |
augment (bool): Augment image during prediction, defaults to False.
|
105 |
embed (list, optional): A list of feature vectors/embeddings to return.
|
106 |
-
|
107 |
Returns:
|
108 |
(torch.Tensor): The last output of the model.
|
109 |
"""
|
@@ -114,13 +110,11 @@ class BaseModel(nn.Module):
|
|
114 |
def _predict_once(self, x, profile=False, visualize=False, embed=None):
|
115 |
"""
|
116 |
Perform a forward pass through the network.
|
117 |
-
|
118 |
Args:
|
119 |
x (torch.Tensor): The input tensor to the model.
|
120 |
profile (bool): Print the computation time of each layer if True, defaults to False.
|
121 |
visualize (bool): Save the feature maps of the model if True, defaults to False.
|
122 |
embed (list, optional): A list of feature vectors/embeddings to return.
|
123 |
-
|
124 |
Returns:
|
125 |
(torch.Tensor): The last output of the model.
|
126 |
"""
|
@@ -152,12 +146,10 @@ class BaseModel(nn.Module):
|
|
152 |
"""
|
153 |
Profile the computation time and FLOPs of a single layer of the model on a given input. Appends the results to
|
154 |
the provided list.
|
155 |
-
|
156 |
Args:
|
157 |
m (nn.Module): The layer to be profiled.
|
158 |
x (torch.Tensor): The input data to the layer.
|
159 |
dt (list): A list to store the computation time of the layer.
|
160 |
-
|
161 |
Returns:
|
162 |
None
|
163 |
"""
|
@@ -177,7 +169,6 @@ class BaseModel(nn.Module):
|
|
177 |
"""
|
178 |
Fuse the `Conv2d()` and `BatchNorm2d()` layers of the model into a single layer, in order to improve the
|
179 |
computation efficiency.
|
180 |
-
|
181 |
Returns:
|
182 |
(nn.Module): The fused model is returned.
|
183 |
"""
|
@@ -206,10 +197,8 @@ class BaseModel(nn.Module):
|
|
206 |
def is_fused(self, thresh=10):
|
207 |
"""
|
208 |
Check if the model has less than a certain threshold of BatchNorm layers.
|
209 |
-
|
210 |
Args:
|
211 |
thresh (int, optional): The threshold number of BatchNorm layers. Default is 10.
|
212 |
-
|
213 |
Returns:
|
214 |
(bool): True if the number of BatchNorm layers in the model is less than the threshold, False otherwise.
|
215 |
"""
|
@@ -219,7 +208,6 @@ class BaseModel(nn.Module):
|
|
219 |
def info(self, detailed=False, verbose=True, imgsz=640):
|
220 |
"""
|
221 |
Prints model information.
|
222 |
-
|
223 |
Args:
|
224 |
detailed (bool): if True, prints out detailed information about the model. Defaults to False
|
225 |
verbose (bool): if True, prints out the model information. Defaults to False
|
@@ -230,10 +218,8 @@ class BaseModel(nn.Module):
|
|
230 |
def _apply(self, fn):
|
231 |
"""
|
232 |
Applies a function to all the tensors in the model that are not parameters or registered buffers.
|
233 |
-
|
234 |
Args:
|
235 |
fn (function): the function to apply to the model
|
236 |
-
|
237 |
Returns:
|
238 |
(BaseModel): An updated BaseModel object.
|
239 |
"""
|
@@ -248,7 +234,6 @@ class BaseModel(nn.Module):
|
|
248 |
def load(self, weights, verbose=True):
|
249 |
"""
|
250 |
Load the weights into the model.
|
251 |
-
|
252 |
Args:
|
253 |
weights (dict | torch.nn.Module): The pre-trained weights to be loaded.
|
254 |
verbose (bool, optional): Whether to log the transfer progress. Defaults to True.
|
@@ -263,7 +248,6 @@ class BaseModel(nn.Module):
|
|
263 |
def loss(self, batch, preds=None):
|
264 |
"""
|
265 |
Compute loss.
|
266 |
-
|
267 |
Args:
|
268 |
batch (dict): Batch to compute loss on
|
269 |
preds (torch.Tensor | List[torch.Tensor]): Predictions.
|
@@ -455,17 +439,14 @@ class ClassificationModel(BaseModel):
|
|
455 |
class RTDETRDetectionModel(DetectionModel):
|
456 |
"""
|
457 |
RTDETR (Real-time DEtection and Tracking using Transformers) Detection Model class.
|
458 |
-
|
459 |
This class is responsible for constructing the RTDETR architecture, defining loss functions, and facilitating both
|
460 |
the training and inference processes. RTDETR is an object detection and tracking model that extends from the
|
461 |
DetectionModel base class.
|
462 |
-
|
463 |
Attributes:
|
464 |
cfg (str): The configuration file path or preset string. Default is 'rtdetr-l.yaml'.
|
465 |
ch (int): Number of input channels. Default is 3 (RGB).
|
466 |
nc (int, optional): Number of classes for object detection. Default is None.
|
467 |
verbose (bool): Specifies if summary statistics are shown during initialization. Default is True.
|
468 |
-
|
469 |
Methods:
|
470 |
init_criterion: Initializes the criterion used for loss calculation.
|
471 |
loss: Computes and returns the loss during training.
|
@@ -475,7 +456,6 @@ class RTDETRDetectionModel(DetectionModel):
|
|
475 |
def __init__(self, cfg="rtdetr-l.yaml", ch=3, nc=None, verbose=True):
|
476 |
"""
|
477 |
Initialize the RTDETRDetectionModel.
|
478 |
-
|
479 |
Args:
|
480 |
cfg (str): Configuration file name or path.
|
481 |
ch (int): Number of input channels.
|
@@ -493,11 +473,9 @@ class RTDETRDetectionModel(DetectionModel):
|
|
493 |
def loss(self, batch, preds=None):
|
494 |
"""
|
495 |
Compute the loss for the given batch of data.
|
496 |
-
|
497 |
Args:
|
498 |
batch (dict): Dictionary containing image and label data.
|
499 |
preds (torch.Tensor, optional): Precomputed model predictions. Defaults to None.
|
500 |
-
|
501 |
Returns:
|
502 |
(tuple): A tuple containing the total loss and main three losses in a tensor.
|
503 |
"""
|
@@ -538,7 +516,6 @@ class RTDETRDetectionModel(DetectionModel):
|
|
538 |
def predict(self, x, profile=False, visualize=False, batch=None, augment=False, embed=None):
|
539 |
"""
|
540 |
Perform a forward pass through the model.
|
541 |
-
|
542 |
Args:
|
543 |
x (torch.Tensor): The input tensor.
|
544 |
profile (bool, optional): If True, profile the computation time for each layer. Defaults to False.
|
@@ -546,7 +523,6 @@ class RTDETRDetectionModel(DetectionModel):
|
|
546 |
batch (dict, optional): Ground truth data for evaluation. Defaults to None.
|
547 |
augment (bool, optional): If True, perform data augmentation during inference. Defaults to False.
|
548 |
embed (list, optional): A list of feature vectors/embeddings to return.
|
549 |
-
|
550 |
Returns:
|
551 |
(torch.Tensor): Model's output tensor.
|
552 |
"""
|
@@ -602,14 +578,12 @@ class WorldModel(DetectionModel):
|
|
602 |
def predict(self, x, profile=False, visualize=False, augment=False, embed=None):
|
603 |
"""
|
604 |
Perform a forward pass through the model.
|
605 |
-
|
606 |
Args:
|
607 |
x (torch.Tensor): The input tensor.
|
608 |
profile (bool, optional): If True, profile the computation time for each layer. Defaults to False.
|
609 |
visualize (bool, optional): If True, save feature maps for visualization. Defaults to False.
|
610 |
augment (bool, optional): If True, perform data augmentation during inference. Defaults to False.
|
611 |
embed (list, optional): A list of feature vectors/embeddings to return.
|
612 |
-
|
613 |
Returns:
|
614 |
(torch.Tensor): Model's output tensor.
|
615 |
"""
|
@@ -668,20 +642,16 @@ class Ensemble(nn.ModuleList):
|
|
668 |
def temporary_modules(modules=None):
|
669 |
"""
|
670 |
Context manager for temporarily adding or modifying modules in Python's module cache (`sys.modules`).
|
671 |
-
|
672 |
This function can be used to change the module paths during runtime. It's useful when refactoring code,
|
673 |
where you've moved a module from one location to another, but you still want to support the old import
|
674 |
paths for backwards compatibility.
|
675 |
-
|
676 |
Args:
|
677 |
modules (dict, optional): A dictionary mapping old module paths to new module paths.
|
678 |
-
|
679 |
Example:
|
680 |
```python
|
681 |
with temporary_modules({'old.module.path': 'new.module.path'}):
|
682 |
import old.module.path # this will now import new.module.path
|
683 |
```
|
684 |
-
|
685 |
Note:
|
686 |
The changes are only in effect inside the context manager and are undone once the context manager exits.
|
687 |
Be aware that directly manipulating `sys.modules` can lead to unpredictable results, especially in larger
|
@@ -711,10 +681,8 @@ def torch_safe_load(weight):
|
|
711 |
This function attempts to load a PyTorch model with the torch.load() function. If a ModuleNotFoundError is raised,
|
712 |
it catches the error, logs a warning message, and attempts to install the missing module via the
|
713 |
check_requirements() function. After installation, the function again attempts to load the model using torch.load().
|
714 |
-
|
715 |
Args:
|
716 |
weight (str): The file path of the PyTorch model.
|
717 |
-
|
718 |
Returns:
|
719 |
(dict): The loaded PyTorch model.
|
720 |
"""
|
@@ -730,7 +698,7 @@ def torch_safe_load(weight):
|
|
730 |
"ultralytics.yolo.data": "ultralytics.data",
|
731 |
}
|
732 |
): # for legacy 8.0 Classify and Pose models
|
733 |
-
ckpt = torch.load(file, map_location="cpu")
|
734 |
|
735 |
except ModuleNotFoundError as e: # e.name is missing module name
|
736 |
if e.name == "models":
|
@@ -972,10 +940,8 @@ def guess_model_scale(model_path):
|
|
972 |
Takes a path to a YOLO model's YAML file as input and extracts the size character of the model's scale. The function
|
973 |
uses regular expression matching to find the pattern of the model scale in the YAML file name, which is denoted by
|
974 |
n, s, m, l, or x. The function returns the size character of the model scale as a string.
|
975 |
-
|
976 |
Args:
|
977 |
model_path (str | Path): The path to the YOLO model's YAML file.
|
978 |
-
|
979 |
Returns:
|
980 |
(str): The size character of the model's scale, which can be n, s, m, l, or x.
|
981 |
"""
|
@@ -989,13 +955,10 @@ def guess_model_scale(model_path):
|
|
989 |
def guess_model_task(model):
|
990 |
"""
|
991 |
Guess the task of a PyTorch model from its architecture or configuration.
|
992 |
-
|
993 |
Args:
|
994 |
model (nn.Module | dict): PyTorch model or model configuration in YAML format.
|
995 |
-
|
996 |
Returns:
|
997 |
(str): Task of the model ('detect', 'segment', 'classify', 'pose').
|
998 |
-
|
999 |
Raises:
|
1000 |
SyntaxError: If the task of the model could not be determined.
|
1001 |
"""
|
|
|
82 |
def forward(self, x, *args, **kwargs):
|
83 |
"""
|
84 |
Forward pass of the model on a single scale. Wrapper for `_forward_once` method.
|
|
|
85 |
Args:
|
86 |
x (torch.Tensor | dict): The input image tensor or a dict including image tensor and gt labels.
|
|
|
87 |
Returns:
|
88 |
(torch.Tensor): The output of the network.
|
89 |
"""
|
|
|
94 |
def predict(self, x, profile=False, visualize=False, augment=False, embed=None):
|
95 |
"""
|
96 |
Perform a forward pass through the network.
|
|
|
97 |
Args:
|
98 |
x (torch.Tensor): The input tensor to the model.
|
99 |
profile (bool): Print the computation time of each layer if True, defaults to False.
|
100 |
visualize (bool): Save the feature maps of the model if True, defaults to False.
|
101 |
augment (bool): Augment image during prediction, defaults to False.
|
102 |
embed (list, optional): A list of feature vectors/embeddings to return.
|
|
|
103 |
Returns:
|
104 |
(torch.Tensor): The last output of the model.
|
105 |
"""
|
|
|
110 |
def _predict_once(self, x, profile=False, visualize=False, embed=None):
|
111 |
"""
|
112 |
Perform a forward pass through the network.
|
|
|
113 |
Args:
|
114 |
x (torch.Tensor): The input tensor to the model.
|
115 |
profile (bool): Print the computation time of each layer if True, defaults to False.
|
116 |
visualize (bool): Save the feature maps of the model if True, defaults to False.
|
117 |
embed (list, optional): A list of feature vectors/embeddings to return.
|
|
|
118 |
Returns:
|
119 |
(torch.Tensor): The last output of the model.
|
120 |
"""
|
|
|
146 |
"""
|
147 |
Profile the computation time and FLOPs of a single layer of the model on a given input. Appends the results to
|
148 |
the provided list.
|
|
|
149 |
Args:
|
150 |
m (nn.Module): The layer to be profiled.
|
151 |
x (torch.Tensor): The input data to the layer.
|
152 |
dt (list): A list to store the computation time of the layer.
|
|
|
153 |
Returns:
|
154 |
None
|
155 |
"""
|
|
|
169 |
"""
|
170 |
Fuse the `Conv2d()` and `BatchNorm2d()` layers of the model into a single layer, in order to improve the
|
171 |
computation efficiency.
|
|
|
172 |
Returns:
|
173 |
(nn.Module): The fused model is returned.
|
174 |
"""
|
|
|
197 |
def is_fused(self, thresh=10):
|
198 |
"""
|
199 |
Check if the model has less than a certain threshold of BatchNorm layers.
|
|
|
200 |
Args:
|
201 |
thresh (int, optional): The threshold number of BatchNorm layers. Default is 10.
|
|
|
202 |
Returns:
|
203 |
(bool): True if the number of BatchNorm layers in the model is less than the threshold, False otherwise.
|
204 |
"""
|
|
|
208 |
def info(self, detailed=False, verbose=True, imgsz=640):
|
209 |
"""
|
210 |
Prints model information.
|
|
|
211 |
Args:
|
212 |
detailed (bool): if True, prints out detailed information about the model. Defaults to False
|
213 |
verbose (bool): if True, prints out the model information. Defaults to False
|
|
|
218 |
def _apply(self, fn):
|
219 |
"""
|
220 |
Applies a function to all the tensors in the model that are not parameters or registered buffers.
|
|
|
221 |
Args:
|
222 |
fn (function): the function to apply to the model
|
|
|
223 |
Returns:
|
224 |
(BaseModel): An updated BaseModel object.
|
225 |
"""
|
|
|
234 |
def load(self, weights, verbose=True):
|
235 |
"""
|
236 |
Load the weights into the model.
|
|
|
237 |
Args:
|
238 |
weights (dict | torch.nn.Module): The pre-trained weights to be loaded.
|
239 |
verbose (bool, optional): Whether to log the transfer progress. Defaults to True.
|
|
|
248 |
def loss(self, batch, preds=None):
|
249 |
"""
|
250 |
Compute loss.
|
|
|
251 |
Args:
|
252 |
batch (dict): Batch to compute loss on
|
253 |
preds (torch.Tensor | List[torch.Tensor]): Predictions.
|
|
|
439 |
class RTDETRDetectionModel(DetectionModel):
|
440 |
"""
|
441 |
RTDETR (Real-time DEtection and Tracking using Transformers) Detection Model class.
|
|
|
442 |
This class is responsible for constructing the RTDETR architecture, defining loss functions, and facilitating both
|
443 |
the training and inference processes. RTDETR is an object detection and tracking model that extends from the
|
444 |
DetectionModel base class.
|
|
|
445 |
Attributes:
|
446 |
cfg (str): The configuration file path or preset string. Default is 'rtdetr-l.yaml'.
|
447 |
ch (int): Number of input channels. Default is 3 (RGB).
|
448 |
nc (int, optional): Number of classes for object detection. Default is None.
|
449 |
verbose (bool): Specifies if summary statistics are shown during initialization. Default is True.
|
|
|
450 |
Methods:
|
451 |
init_criterion: Initializes the criterion used for loss calculation.
|
452 |
loss: Computes and returns the loss during training.
|
|
|
456 |
def __init__(self, cfg="rtdetr-l.yaml", ch=3, nc=None, verbose=True):
|
457 |
"""
|
458 |
Initialize the RTDETRDetectionModel.
|
|
|
459 |
Args:
|
460 |
cfg (str): Configuration file name or path.
|
461 |
ch (int): Number of input channels.
|
|
|
473 |
def loss(self, batch, preds=None):
|
474 |
"""
|
475 |
Compute the loss for the given batch of data.
|
|
|
476 |
Args:
|
477 |
batch (dict): Dictionary containing image and label data.
|
478 |
preds (torch.Tensor, optional): Precomputed model predictions. Defaults to None.
|
|
|
479 |
Returns:
|
480 |
(tuple): A tuple containing the total loss and main three losses in a tensor.
|
481 |
"""
|
|
|
516 |
def predict(self, x, profile=False, visualize=False, batch=None, augment=False, embed=None):
|
517 |
"""
|
518 |
Perform a forward pass through the model.
|
|
|
519 |
Args:
|
520 |
x (torch.Tensor): The input tensor.
|
521 |
profile (bool, optional): If True, profile the computation time for each layer. Defaults to False.
|
|
|
523 |
batch (dict, optional): Ground truth data for evaluation. Defaults to None.
|
524 |
augment (bool, optional): If True, perform data augmentation during inference. Defaults to False.
|
525 |
embed (list, optional): A list of feature vectors/embeddings to return.
|
|
|
526 |
Returns:
|
527 |
(torch.Tensor): Model's output tensor.
|
528 |
"""
|
|
|
578 |
def predict(self, x, profile=False, visualize=False, augment=False, embed=None):
|
579 |
"""
|
580 |
Perform a forward pass through the model.
|
|
|
581 |
Args:
|
582 |
x (torch.Tensor): The input tensor.
|
583 |
profile (bool, optional): If True, profile the computation time for each layer. Defaults to False.
|
584 |
visualize (bool, optional): If True, save feature maps for visualization. Defaults to False.
|
585 |
augment (bool, optional): If True, perform data augmentation during inference. Defaults to False.
|
586 |
embed (list, optional): A list of feature vectors/embeddings to return.
|
|
|
587 |
Returns:
|
588 |
(torch.Tensor): Model's output tensor.
|
589 |
"""
|
|
|
642 |
def temporary_modules(modules=None):
|
643 |
"""
|
644 |
Context manager for temporarily adding or modifying modules in Python's module cache (`sys.modules`).
|
|
|
645 |
This function can be used to change the module paths during runtime. It's useful when refactoring code,
|
646 |
where you've moved a module from one location to another, but you still want to support the old import
|
647 |
paths for backwards compatibility.
|
|
|
648 |
Args:
|
649 |
modules (dict, optional): A dictionary mapping old module paths to new module paths.
|
|
|
650 |
Example:
|
651 |
```python
|
652 |
with temporary_modules({'old.module.path': 'new.module.path'}):
|
653 |
import old.module.path # this will now import new.module.path
|
654 |
```
|
|
|
655 |
Note:
|
656 |
The changes are only in effect inside the context manager and are undone once the context manager exits.
|
657 |
Be aware that directly manipulating `sys.modules` can lead to unpredictable results, especially in larger
|
|
|
681 |
This function attempts to load a PyTorch model with the torch.load() function. If a ModuleNotFoundError is raised,
|
682 |
it catches the error, logs a warning message, and attempts to install the missing module via the
|
683 |
check_requirements() function. After installation, the function again attempts to load the model using torch.load().
|
|
|
684 |
Args:
|
685 |
weight (str): The file path of the PyTorch model.
|
|
|
686 |
Returns:
|
687 |
(dict): The loaded PyTorch model.
|
688 |
"""
|
|
|
698 |
"ultralytics.yolo.data": "ultralytics.data",
|
699 |
}
|
700 |
): # for legacy 8.0 Classify and Pose models
|
701 |
+
ckpt = torch.load(file, map_location="cpu",weights_only=False)
|
702 |
|
703 |
except ModuleNotFoundError as e: # e.name is missing module name
|
704 |
if e.name == "models":
|
|
|
940 |
Takes a path to a YOLO model's YAML file as input and extracts the size character of the model's scale. The function
|
941 |
uses regular expression matching to find the pattern of the model scale in the YAML file name, which is denoted by
|
942 |
n, s, m, l, or x. The function returns the size character of the model scale as a string.
|
|
|
943 |
Args:
|
944 |
model_path (str | Path): The path to the YOLO model's YAML file.
|
|
|
945 |
Returns:
|
946 |
(str): The size character of the model's scale, which can be n, s, m, l, or x.
|
947 |
"""
|
|
|
955 |
def guess_model_task(model):
|
956 |
"""
|
957 |
Guess the task of a PyTorch model from its architecture or configuration.
|
|
|
958 |
Args:
|
959 |
model (nn.Module | dict): PyTorch model or model configuration in YAML format.
|
|
|
960 |
Returns:
|
961 |
(str): Task of the model ('detect', 'segment', 'classify', 'pose').
|
|
|
962 |
Raises:
|
963 |
SyntaxError: If the task of the model could not be determined.
|
964 |
"""
|