weecology / DeepForest

Python Package for Airborne RGB machine learning
https://deepforest.readthedocs.io/
MIT License
517 stars 174 forks source link

How read custom file classes? #150

Closed anderson-lactec closed 3 years ago

anderson-lactec commented 3 years ago

I created my model on Google Colab to enjoy the GPU mode with a custom class. After, I saved the model model.model.save('model.h5') and downloaded only this file. In my machine, I loaded the model model = deepforest.deepforest(saved_model=MODEL_PATH) and I can predict the custom class, but the class name is only Tree. I created a classes.csv file in the root folder, but how read this file to save the correct label on the predict? Thank's

bw4sz commented 3 years ago

I'm not sure how I've missed this issue. Probably during the holiday break. My apologies. I'm sure its too late, but I just ran into the same issue myself and will update the code base. The key was model.read_classes().

    def read_classes(self):
        """Read class file in case of multi-class training.

        If no file has been created, DeepForest assume there is 1 class,
        Tree
        """
        # parse the provided class file
        self.labels = {}
        try:
            with open(self.classes_file, 'r') as file:
                self.classes = _read_classes(csv.reader(file, delimiter=','))
            for key, value in self.classes.items():
                self.labels[value] = key
        except:
            self.labels[0] = "Tree"
model.classes_file = <path containing your classes file>
model.read_classes()

then you can proceed as normal. I'm updating the code to make it all one step.

bw4sz commented 3 years ago

be careful its in the same order, probably safer to use the same tool as during training

from deepforest import utilities
model.classes_file = utilities.create_classes(<path_to_annotations>)
bw4sz commented 3 years ago

Added example to docs A couple notes on multi-class, if you trained a multi-class model and need to reload the model, make sure to reload the classes file, or else all objects will be labeled (tree)[https://github.com/weecology/DeepForest/issues/150]

from deepforest import deepforest
from deepforest import utilities
m = deepforest.deepforest("/orange/ewhite/everglades/Zooniverse/predictions/20210211_072221.h5")
m.classes_file = utilities.create_classes("/orange/ewhite/everglades/Zooniverse/parsed_images/test.csv")
m.read_classes()

Also note there is likely some integration errors with the comet dashboard, I recommend not using comet for multi-species models, as there are too many assumptions for single tree species.

anderson-lactec commented 3 years ago

Thank's for your reply.

bw4sz commented 3 years ago

Thank you, sorry for the very late reply, normally i'm on top of these. I pushed 0.3.6 to make sure that custom classes are sorted alphanumeric, this will help among runs. I still see some issues restarting a saved model with custom classes. I expect to address this at the beginning of next week. Feel free to open another issue.

On Fri, Feb 12, 2021 at 9:25 AM Anderson notifications@github.com wrote:

Thank's for your reply.

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub https://github.com/weecology/DeepForest/issues/150#issuecomment-778330611, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAJHBLEZBMX6NZB7G3HIIYLS6VQCPANCNFSM4UVDBUKA .

-- Ben Weinstein, Ph.D. Postdoctoral Fellow University of Florida http://benweinstein.weebly.com/

jyo94 commented 11 months ago

from deepforest import utilities model.classes_file = utilities.create_classes()

In the above, what is the classes file contain? Is it same as the annotation file with image_path,xmin,ymin,xmax,ymax,label?

Usecase: I have a custom data, for which I want to identify 2 classes (cropname and weed). Steps followed as per documentation:

  1. split raster into smaller tiles.
  2. Prepared train and test. Trained for few epochs
  3. initialized deepforest model and changed the corresponding config file
  4. created pytorch lighting trainer
  5. loaded the lastest release model
  6. Saved the prediction result Result: The above works fine for single class problem: 1 . the identified class is tree and not cropname
    1. How to change this task from single to multiclass problem