Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code for Yolov8 layer #12131

Open
1 task done
tjasmin111 opened this issue May 11, 2024 · 5 comments
Open
1 task done

Code for Yolov8 layer #12131

tjasmin111 opened this issue May 11, 2024 · 5 comments
Labels
question Further information is requested

Comments

@tjasmin111
Copy link

tjasmin111 commented May 11, 2024

Search before asking

Question

I have a conventional code that uses this YOLOLayer code from yolov7. What is the equivalent of this code for Yolov8? How can I convert it to Yolov8?

def sigmoid(x):
        s = 1 / (1 + np.exp(-x))
        return s

def create_grids(self, img_size=640, ng=(20, 20), type=np.float32):
        nx, ny = ng  # x and y grid size
        self.img_size = max(img_size)
        self.stride = self.img_size / max(ng)

        # build xy offsets, torch.meshgrid() return yv,xv
        xv, yv = np.meshgrid(np.arange(ny), np.arange(nx))
        self.grid_xy = np.stack((xv, yv), 2).astype(type).reshape((1, 1, ny, nx, 2))

        # build wh gains
        self.anchor_vec = self.anchors / self.stride
        self.anchor_wh = self.anchor_vec.reshape(1, self.na, 1, 1, 2).astype(type)
        self.ng = ng
        self.nx = nx
        self.ny = ny

class YOLOLayer():
        def __init__(self, anchors, nc, img_size, arc='default'):
                self.anchors = anchors
                self.na = len(anchors)  # number of anchors (3)
                self.nc = nc  # number of classes (80)
                self.no = nc + 5  # number of outputs
                self.nx = 0  # initialize number of x gridpoints
                self.ny = 0  # initialize number of y gridpoints
                self.arc = arc
                self.img_size = img_size

        def forward(self, p):
                bs, _, ny, nx = p.shape  # bs, 255, 13, 13
                if (self.nx, self.ny) != (nx, ny):
                        create_grids(self, self.img_size, (nx, ny), p.dtype)

                # p.view(bs, 255, 13, 13) -- > (bs, 3, 13, 13, 85)  # (bs, anchors, grid, grid, classes + xywh)
                p = p.reshape(bs, self.na, self.no, self.ny, self.nx).transpose(0, 1, 3, 4, 2)  # prediction
                p = np.ascontiguousarray(p)

                # s = 1.5  # scale_xy  (pxy = pxy * s - (s - 1) / 2)
                io = p.copy()  # inference output
                y = sigmoid(io)
                y[..., 0:2] = (y[..., 0:2] * 2. - 0.5 + self.grid_xy)  # xy
                y[..., 2:4] = (y[..., 2:4] * 2) ** 2 * self.anchor_wh  # wh
                y[..., :4] = y[..., :4] * self.stride

                return y.reshape(bs, -1, self.no), p

Additional

No response

@tjasmin111 tjasmin111 added the question Further information is requested label May 11, 2024
@glenn-jocher
Copy link
Member

Hello! 👋 In YOLOv8, the architecture and code structure have been updated for more integrated and efficient performance, which makes direct equivalents for certain YOLOv7 components, like YOLOLayer, somewhat different.

In YOLOv8, most functionalities that were manually handled in the YOLOLayer are now internally integrated within the model’s forward pass. You typically won't need to manually define layers or operations like grid creation or applying activation functions as these are managed within the model itself during training and inference.

Here’s a simplified version of how predictions can be handled in YOLOv8 without needing to define a YOLOLayer type structure:

from ultralytics import YOLO

# Load your YOLOv8 model
model = YOLO('path/to/your/yolov8_model.pt')

# Load an image or image tensor
img = 'path/to/your/image.jpg'  # You can also use loaders to bring your image to tensor format

# Perform prediction
results = model(img)

# Accessing result details
detections = results.pred  # detections tensor

You generally interact at a higher level, without needing to dive into the specificities of grids, anchors, etc., unless you are customizing the core architecture itself. If you’re looking to adapt detailed custom changes that were present in YOLOLayer of YOLOv7, you might look into defining custom modules within the YOLOv8 setup, or directly adjust configurations and parameters through the YAML config files.

For further details, feel free to browse the official Ultralytics YOLOv8 documentation or their repository code to understand the structural operations better.

@tjasmin111
Copy link
Author

Hi,
Unfortunately I can't import YOLO. I have to use the actual functions. That's why.

Can you help how to write those functions for YOLOv8?

@glenn-jocher
Copy link
Member

Hello! 🌟

In YOLOv8, the functionality is well-integrated into the model's API, which aims to streamline implementation and use. However, if you need to work directly with the lower-level functions for a specific reason, you can delve into the source code to understand the underlying implementation. Here's how you might typically start making predictions with YOLOv8:

from ultralytics import YOLO

# Load your model
model = YOLO('yolov8n.pt')  # Example with a YOLOv8 Nano model

# For images or videos
results = model('path/to/image_or_video')

# Access predictions
print(results.pandas().xyxy[0])  # Results in a pandas DataFrame

For your specific requirement (such as directly manipulating or calling the functions working with tensors), you would need to explore the YOLO class in more depth, which is part of the Ultralytics repository. If you need direct access to operations such as grid setups or activation functions, consider examining how these are implemented internally in the model's forward pass.

Let me know if you need more specific guidance or if you have particular functions in question! 😊

@tjasmin111
Copy link
Author

Where is the model's forward pass located exactly? Which file?

@glenn-jocher
Copy link
Member

Hello! 😊

For YOLOv8, the model's forward pass logic is typically encapsulated within the model definition file. You can find these details in the source code, particularly looking in files named like model.py. If it’s structured similarly to previous versions, you’d likely inspect methods in classes that inherit from nn.Module, specifically the forward method.

Here's a snippet that generally illustrates how to look for it:

class Model(nn.Module):
    def forward(self, x):
        # Forward pass logic here
        return x

Please, check the repository for the exact naming as file structures might have minor variations. Hope this helps! Let me know if you need further assistance! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants