6868 thop = None
6969
7070
71- def feature_visualization (x , module_type , stage , n = 32 , save_dir = Path ("runs/detect/exp" )):
72- """Visualize feature maps of a given model module during inference."""
73- import matplotlib .pyplot as plt
74- import numpy as np
75-
76- if any (m in module_type for m in ("Detect" , "Segment" , "Classify" )):
77- return
78- if isinstance (x , torch .Tensor ):
79- _ , channels , height , width = x .shape
80- if height > 1 and width > 1 :
81- f = save_dir / f"stage{ stage } _{ module_type .rsplit ('.' , 1 )[- 1 ]} _features.png"
82- blocks = torch .chunk (x [0 ].cpu (), channels , dim = 0 )
83- n = min (n , channels )
84- _ , ax = plt .subplots (math .ceil (n / 8 ), 8 , tight_layout = True )
85- ax = ax .ravel ()
86- plt .subplots_adjust (wspace = 0.05 , hspace = 0.05 )
87- for i in range (n ):
88- ax [i ].imshow (blocks [i ].squeeze ().numpy ())
89- ax [i ].axis ("off" )
90- LOGGER .info (f"Saving { f } ... ({ n } /{ channels } )" )
91- plt .savefig (f , dpi = 300 , bbox_inches = "tight" )
92- plt .close ()
93- np .save (str (f .with_suffix (".npy" )), x [0 ].cpu ().numpy ())
94-
95-
9671class Detect (nn .Module ):
9772 """YOLOv5 Detect head for processing input tensors and generating detection outputs in object detection models."""
9873
@@ -178,14 +153,12 @@ def forward(self, x):
178153class BaseModel (nn .Module ):
179154 """YOLOv5 base model."""
180155
181- def forward (self , x , profile = False , visualize = False ):
182- """Executes a single-scale inference or training pass on the YOLOv5 base model, with options for profiling and
183- visualization.
184- """
185- return self ._forward_once (x , profile , visualize ) # single-scale inference, train
156+ def forward (self , x , profile = False ):
157+ """Executes a single-scale inference or training pass on the YOLOv5 base model."""
158+ return self ._forward_once (x , profile ) # single-scale inference, train
186159
187- def _forward_once (self , x , profile = False , visualize = False ):
188- """Performs a forward pass on the YOLOv5 model, enabling profiling and feature visualization options ."""
160+ def _forward_once (self , x , profile = False ):
161+ """Performs a forward pass on the YOLOv5 model, enabling profiling when requested ."""
189162 y , dt = [], [] # outputs
190163 for m in self .model :
191164 if m .f != - 1 : # if not from previous layer
@@ -194,8 +167,6 @@ def _forward_once(self, x, profile=False, visualize=False):
194167 self ._profile_one_layer (m , x , dt )
195168 x = m (x ) # run
196169 y .append (x if m .i in self .save else None ) # save output
197- if visualize :
198- feature_visualization (x , m .type , m .i , save_dir = visualize )
199170 return x
200171
201172 def _profile_one_layer (self , m , x , dt ):
@@ -289,11 +260,11 @@ def _forward(x):
289260 self .info ()
290261 LOGGER .info ("" )
291262
292- def forward (self , x , augment = False , profile = False , visualize = False ):
293- """Performs single-scale or augmented inference and may include profiling or visualization ."""
263+ def forward (self , x , augment = False , profile = False ):
264+ """Performs single-scale or augmented inference and may include profiling."""
294265 if augment :
295266 return self ._forward_augment (x ) # augmented inference, None
296- return self ._forward_once (x , profile , visualize ) # single-scale inference, train
267+ return self ._forward_once (x , profile ) # single-scale inference, train
297268
298269 def _forward_augment (self , x ):
299270 """Performs augmented inference across different scales and flips, returning combined detections."""
0 commit comments