Alaawehbe12 / Feature-extraction-YOLOv8

This repository contains the code for feature extraction from the latest version of YOLO, Version 8..
5 stars 0 forks source link

why it did not work #1

Open liutao23 opened 4 months ago

liutao23 commented 4 months ago

/home/liutao/anaconda3/envs/yolo/bin/python3.10 /home/liutao/Videos/demo/yolov8/1.py Traceback (most recent call last): File "/home/liutao/Videos/demo/yolov8/1.py", line 40, in feature_extracted = extract_features(img,model,layer_index= 12) #set on perfer feature File "/home/liutao/Videos/demo/yolov8/1.py", line 17, in extract_features hook = model.model.model[layer_index].register_forward_hook(hook_fn) AttributeError: 'Tensor' object has no attribute 'model'. Did you mean: 'mode'?

Alaawehbe12 commented 4 months ago

Hello , Thank you first for using this repository Let's try to solve this issue ;Here is an example of how you can load a YOLOv8 model using the YOLO library **from pathlib import Path from ultralytics import YOLO

Load YOLOv8 model

weights_path = Path("path/to/your/weights.pt") model = YOLO(weights_path)**

The error you are encountering indicates that the model object does not have the attribute model. This might be specific to the structure of the YOLOv8 model you are using.

Based on the error message, it seems like model is a Tensor object rather than the YOLOv8 model object. You should ensure that you are loading the YOLOv8 model correctly.

YouROS12 commented 4 months ago

I got the same issue, even if I'm uploading the model correctly

AttributeError                            Traceback (most recent call last)
[<ipython-input-31-4964e9dccdef>](https://localhost:8080/#) in <cell line: 32>()
     30 model = YOLO(model_path)
     31 img = preprocess_image(img)
---> 32 feature_extracted = extract_features(img,model,layer_index= 12) #set on perfer feature

[<ipython-input-31-4964e9dccdef>](https://localhost:8080/#) in extract_features(model, img, layer_index)
      6     global intermediate_features
      7     intermediate_features = []
----> 8     hook = model.model.model[layer_index].register_forward_hook(hook_fn)
      9     with torch.no_grad():
     10       model(img)

AttributeError: 'Tensor' object has no attribute 'model'
Alaawehbe12 commented 4 months ago

I have implemented some modifications to the code. Please take a moment to review it; it is expected to be functional now. I welcome and appreciate any feedback you may have on the changes. Thank you!

YouROS12 commented 4 months ago

Worked smoothly, thank you mate! any ideas how to use these extracted features as inputs for popular ML classifiers ?

Alaawehbe12 commented 4 months ago

You can extract features from various images and save them in an Excel file. Afterward, you can employ different machine learning algorithms, such as SVM, Random Forest, CNN, etc., to perform classification based on these extracted features.