AIWintermuteAI / aXeleRate

Keras-based framework for AI on the Edge
MIT License
174 stars 72 forks source link

Get anchors for my dataset #22

Closed BlackEyesCoder closed 3 years ago

BlackEyesCoder commented 3 years ago

I have 2 question.

  1. I label for my data into .xml file on annotations folder. So how i can generate for my anchor for yolov2 without using default anchors ? I label for about 2500 cat's images like this. image

  2. I train for 2500 images to detect face's cat. But, my train got loss = 0.09 in only 4/100 epochs. When my train finish. it detect incorrectly. How can i fix it ?

image

Thank you so much.

AIWintermuteAI commented 3 years ago

1) You can generate the anchors for your dataset with this script https://github.com/experiencor/keras-yolo2/blob/master/gen_anchors.py In most of situations, default anchors are just fine. Meaning that your problem is very likely NOT related to anchors. By the way in the sample image you uploaded you only put cat's face in the bounding box, not the whole cat. Not sure if it's your intent. 2)Can you share a)your config file b)whole training history graph(.jpg picture found in project's folder) c)how many images are in the validation dataset?

BlackEyesCoder commented 3 years ago

Thank you for your reply, I also had get my anchor from https://github.com/experiencor/keras-yolo2/blob/master/gen_anchors.py 2 days ago. My config.json is below. And the picture is my train result. [image: YOLO_best_mAP.jpg] But when I test on my K210 dan dock, I seem not good. like this.

So how I can improve my model to detect on K210 dan dock. I use about 2500 images to train and 500 images in val_images folder. Thank you so much.

Vào Th 2, 10 thg 8, 2020 vào lúc 10:28 AIWintermuteAI < notifications@github.com> đã viết:

  1. You can generate the anchors for your dataset with this script https://github.com/experiencor/keras-yolo2/blob/master/gen_anchors.py In most of situations, default anchors are just fine. Meaning that your problem is very likely NOT related to anchors. By the way in the sample image you uploaded you only put cat's face in the bounding box, not the whole cat. Not sure if it's your intent. 2)Can you share a)your config file b)whole training history graph(.jpg picture found in project's folder) c)how many images are in the validation dataset?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/AIWintermuteAI/aXeleRate/issues/22#issuecomment-671147167, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALFNNTY5KPMJPJBLG6EU4YTR75SMRANCNFSM4PWWV2RQ .

config = { "model":{ "type": "Detector", "architecture": "MobileNet1_0", "input_size": 224, "anchors": [1.03,1.29, 1.79,2.22, 2.80,3.09, 3.37,4.45, 4.87,5.25], "labels": ["cat"], "coord_scale" : 1.0, "class_scale" : 1.0, "object_scale" : 5.0, "no_object_scale" : 1.0 }, "weights" : { "full": "", "backend": "imagenet" }, "train" : { "actual_epoch": 100, "train_image_folder": "dataset/imgs", "train_annot_folder": "dataset/anns", "train_times": 1, "valid_image_folder": "dataset/imgs_validation", "valid_annot_folder": "dataset/anns_validation", "valid_times": 1, "valid_metric": "mAP", "batch_size": 16, "learning_rate": 1e-4, "saved_folder": F"/content/drive/My Drive/cat", "first_trainable_layer": "reshape_1", "augumentation": False, "is_only_detect" : False }, "converter" : { "type": ["k210","tflite"] } }

AIWintermuteAI commented 3 years ago

The pictures are not showing in your post image

BlackEyesCoder commented 3 years ago

I am sorry, I will send some pictures. My model I trained is only to detect cat's face. But when I test, it detect dog's, raccoon's face. And if cat's is more, I have only one big bounding box around them.

Vào Th 2, 10 thg 8, 2020 vào lúc 12:49 AIWintermuteAI < notifications@github.com> đã viết:

The pictures are not showing in your post [image: image] https://user-images.githubusercontent.com/32562299/89754934-36964980-db10-11ea-8fed-21926ec866dc.png

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/AIWintermuteAI/aXeleRate/issues/22#issuecomment-671172952, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALFNNTZANE4UQYDNPUOFY3LR76C5RANCNFSM4PWWV2RQ .

BlackEyesCoder commented 3 years ago

I also have one question is: My kmodel has 3.18M, so I only run python to k210 via terminal (with minimal firmware). So how I can run my code python on k210 alltime. With full firmware, I trying copying kmodel into sdcard, then try using kpu.load but I also got the error: out of memmory. I am looking forward to hearing from you soon. Thank you.

Vào 15:25, T.2, 10 Th8, 2020 Anh Nguyễn anhnguyen2345@gmail.com đã viết:

I am sorry, I will send some pictures. My model I trained is only to detect cat's face. But when I test, it detect dog's, raccoon's face. And if cat's is more, I have only one big bounding box around them.

Vào Th 2, 10 thg 8, 2020 vào lúc 12:49 AIWintermuteAI < notifications@github.com> đã viết:

The pictures are not showing in your post [image: image] https://user-images.githubusercontent.com/32562299/89754934-36964980-db10-11ea-8fed-21926ec866dc.png

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/AIWintermuteAI/aXeleRate/issues/22#issuecomment-671172952, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALFNNTZANE4UQYDNPUOFY3LR76C5RANCNFSM4PWWV2RQ .

AIWintermuteAI commented 3 years ago

Yes, full firmware cannot run MobileNet alpha = 1.0 models, they're too big to fit in the memory with all the extra stuff full version of firmware has. You can burn your model to K210 flash memory and load it from there. To have your code start on boot, you need to copy your code to boot.py (there is an option to do that in MaixPy IDE).

Concerning your cat head problem, I'm still waiting for picture of training graph. Additional question is - how does the Keras model perform? You can check it's performance using https://github.com/AIWintermuteAI/aXeleRate/blob/master/axelerate/infer.py or if you're running in Colab, after training run setup_inference(config, model_path) If you're only doing this project as hobby and not concerned about NDA, then sharing dataset would help greatly to hone on the problem.

BlackEyesCoder commented 3 years ago

Thank you for your reply, I attached some pictures.(picture of training graph, my test, infer). I am very happy to share my dataset with you. This is my dataset to detect cat's face. Iam also increasing images to 4000images to train again. https://drive.google.com/file/d/1ZRTPkM2zRr__dwFVNZjF8B_1Aoe6Qrp8/view?usp=sharing According to your answer, MobileNet alpha = 1.0 will generate models 3.16M (It can run on full firmware) and MobileNet alpha = 0.75 will generate models 1.81M(It can run on full firmware) ? Can I use sdcard to save model 3.16M to run because I new alpha = 1.0 > alpha = 0.75 ? So can you give me some advice to improve my model ? And, My K210 using Camera 2MP can I replace it with another camera which has higher MP to improve my detect. Thank you so much.

Vào Th 5, 13 thg 8, 2020 vào lúc 16:57 AIWintermuteAI < notifications@github.com> đã viết:

Yes, full firmware cannot run MobileNet alpha = 1.0 models, they're too big to fit in the memory with all the extra stuff full version of firmware has. You can burn your model to K210 flash memory and load it from there. To have your code start on boot, you need to copy your code to boot.py (there is an option to do that in MaixPy IDE).

Concerning your cat head problem, I'm still waiting for picture of training graph. Additional question is - how does the Keras model perform? You can check it's performance using https://github.com/AIWintermuteAI/aXeleRate/blob/master/axelerate/infer.py or if you're running in Colab, after training run setup_inference(config, model_path) If you're only doing this project as hobby and not concerned about NDA, then sharing dataset would help greatly to hone on the problem.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/AIWintermuteAI/aXeleRate/issues/22#issuecomment-673383781, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALFNNTY5QEXQT4EC2XU77LDSAO2HPANCNFSM4PWWV2RQ .

BlackEyesCoder commented 3 years ago

Thank you for your reply, I attached some pictures.(picture of training graph, my test, infer). This folder inference_results : https://drive.google.com/drive/folders/1-UJBXCjj7NB-LSbC1riwCfXFiXtC5C0n?usp=sharing I am very happy to share my dataset with you. This is my dataset to detect cat's face. Iam also increasing images to 4000images to train again. https://drive.google.com/file/d/1ZRTPkM2zRr__dwFVNZjF8B_1Aoe6Qrp8/view?usp=sharing

  1. According to your answer, MobileNet alpha = 1.0 will generate models 3.16M (Can it run on mini firmware ? ) and MobileNet alpha = 0.75 will generate models 1.81M(Can it run on full firmware ?) ?
  2. Can I use sd-card to save model 3.16M to run on k210 dan dock ? because I know architecture MobileNet has alpha = 1.0 better than alpha = 0.75, is that right ?
  3. So can you give me some advice to improve my model, please ?
  4. And, My K210 using Camera 2MP can I replace it with another camera which has higher MP to improve my detection ? Thank you so much.

Vào Th 5, 13 thg 8, 2020 vào lúc 16:57 AIWintermuteAI < notifications@github.com> đã viết:

Yes, full firmware cannot run MobileNet alpha = 1.0 models, they're too big to fit in the memory with all the extra stuff full version of firmware has. You can burn your model to K210 flash memory and load it from there. To have your code start on boot, you need to copy your code to boot.py (there is an option to do that in MaixPy IDE).

Concerning your cat head problem, I'm still waiting for picture of training graph. Additional question is - how does the Keras model perform? You can check it's performance using https://github.com/AIWintermuteAI/aXeleRate/blob/master/axelerate/infer.py or if you're running in Colab, after training run setup_inference(config, model_path) If you're only doing this project as hobby and not concerned about NDA, then sharing dataset would help greatly to hone on the problem.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/AIWintermuteAI/aXeleRate/issues/22#issuecomment-673383781, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALFNNTY5QEXQT4EC2XU77LDSAO2HPANCNFSM4PWWV2RQ .

AIWintermuteAI commented 3 years ago

Hi, I've tried training MobileNet alpha = 0.75 with your dataset yesterday, trained with the following config on Google Colab

config = {
        "model":{
            "type":                 "Detector",
            "architecture":         "MobileNet7_5",
            "input_size":           224,
            "anchors":              [0.57273, 0.677385, 1.87446, 2.06253, 3.33843, 5.47434, 7.88282, 3.52778, 9.77052, 9.16828],
            "labels":               ["cat"],
            "coord_scale" :         1.0,
            "class_scale" :         1.0,
            "object_scale" :        5.0,
            "no_object_scale" :     1.0
        },
        "weights" : {
            "full":                 "",
            "backend":              "imagenet"
        },
        "train" : {
            "actual_epoch":         50,
            "train_image_folder":   "dataset/images",
            "train_annot_folder":   "dataset/annotations",
            "train_times":          1,
            "valid_image_folder":   "dataset/val_images",
            "valid_annot_folder":   "dataset/val_annotations",
            "valid_times":          1,
            "valid_metric":         "mAP",
            "batch_size":           32,
            "learning_rate":        1e-3,
            "saved_folder":         F"/content/drive/My Drive/cats",
            "first_trainable_layer": "",
            "augumentation":                True,
            "is_only_detect" :      False
        },
        "converter" : {
            "type":                 ["k210"]
        }
    }

The best mAP was 0.5(or 50 percent), which is not stellar, but not bad. A lot of inference results looked good, but then there is also a visible false positives problem, which is something that you mentioned before and it can be seen in your inference results as well. YOLO_best_mAP

results

Tweaking these two parameters influences how much network is penalized(in loss function) for false positives and false negatives. "object_scale" : 5.0, "no_object_scale" : 1.0 object_scale determines how much to penalize wrong prediction of confidence of object predictors no_object_scale determines how much to penalize wrong prediction of confidence of non-object predictors coord_scale determines how much to penalize wrong position and size predictions (x, y, w, h) class_scale determines how much to penalize wrong class prediction So raising no_object_scale will penalize network more for predicting cat face when there is in fact no cat face. It will lead to lower detection rate overall though.

According to your answer, MobileNet alpha = 1.0 will generate models 3.16M (Can it run on mini firmware ? ) and MobileNet alpha = 0.75 will generate models 1.81M(Can it run on full firmware ?) ?

Yes. Yes. But for firmware questions it is better to go to MaixPy Github - their software development team can provide better answers. Also keep in mind that you can configure and compile firmware yourself pretty easily to pick only the things you need - https://github.com/sipeed/MaixPy/blob/master/build.md

Can I use sd-card to save model 3.16M to run on k210 dan dock ? because I know architecture MobileNet has alpha = 1.0 better than alpha = 0.75, is that right ?

Well, larger network is not necessarily better. Does alpha 1.0 produces better better results(better mAP, less false positives)? In theory yes, it should be possible to run the model from SD card if it possible to run it from flash - however if you are getting out of memory errors, it means some part of memory is being used for reading files from SD card. This question is definitely better asked on MaixPy repository.

So can you give me some advice to improve my model, please ?

I think your main problem at this point is false positives. See my comment above. You can also try two-stage training, I already mentioned it before in issues:

One advanced technique that showed the improvements for some of my projects was to train the classifier first, and then use feature extractor from that classifier as backend weights for detector. For example your end goal is to train dog detector - then you could train a cat-dog classifier first(with "save_bottleneck_weights" parameter enabled) and then use bottleneck_weights.h5 file as backend_weights for when training a dog detector.

And, My K210 using Camera 2MP can I replace it with another camera which has higher MP to improve my detection ?

The model still will take much smaller image than 2MP. So I don't think changing camera will make too much difference.

BlackEyesCoder commented 3 years ago

Thank you so much. Uhmmm, I'm modifying no_object_scale, and train again. And : 1.Can you give me a full firmware which can I model 1.81M ? MobileNet alpha = 0.75 will generate models 1.81M. I looked a lot but I can run my model(1.81M) on full firmwares.

  1. Can you guide me more about training classifier pre-train .h5 for my project(cat's face). (change config file, my dataset folder,...
AIWintermuteAI commented 3 years ago

Can you give me a full firmware which can I model 1.81M ? MobileNet alpha = 0.75 will generate models 1.81M. I looked a lot but I can run my model(1.81M) on full firmwares.

sorry, I don't understand your issue. you can download full firmware from MaixPy downloads website, I myself don't have it.

Can you guide me more about training classifier pre-train .h5 for my project(cat's face). (change config file, my dataset folder,...

Basically you just need to train a classifer first and set https://github.com/AIWintermuteAI/aXeleRate/blob/7cec5e510b422c8f63ca29e31c92569e365ce6e4/configs/classifier.json#L13 option to True When training session is finished, the script will also save feature extractor(or "backend") as bottleneck_weights.h5 in the project folder. You then use this bottleneck_weights.h5 file as weights for detector network backend by specifying them in this line of config https://github.com/AIWintermuteAI/aXeleRate/blob/7cec5e510b422c8f63ca29e31c92569e365ce6e4/configs/detector.json#L15 so instead of imagenet it will be the path to your bottleneck_weights file.

BlackEyesCoder commented 3 years ago

oh, I trained model with architecture mobilenet7_5 and it generate 1.81M kmodel, I flash kmodel at 0x200000, but I only run code via terminal with minimal firmware, full firmware does not, so code does not save when board reset. So how i can save code on board to load that model when power on.

Vào 12:41, T.5, 20 Th8, 2020 AIWintermuteAI notifications@github.com đã viết:

Can you give me a full firmware which can I model 1.81M ? MobileNet alpha = 0.75 will generate models 1.81M. I looked a lot but I can run my model(1.81M) on full firmwares.

sorry, I don't understand your issue. you can download full firmware from MaixPy downloads website, I myself don't have it.

Can you guide me more about training classifier pre-train .h5 for my project(cat's face). (change config file, my dataset folder,...

Basically you just need to train a classifer first and set

https://github.com/AIWintermuteAI/aXeleRate/blob/7cec5e510b422c8f63ca29e31c92569e365ce6e4/configs/classifier.json#L13 option to True When training session is finished, the script will also save feature extractor(or "backend") as bottleneck_weights.h5 in the project folder. You then use this bottleneck_weights.h5 file as weights for detector network backend by specifying them in this line of config

https://github.com/AIWintermuteAI/aXeleRate/blob/7cec5e510b422c8f63ca29e31c92569e365ce6e4/configs/detector.json#L15 so instead of imagenet it will be the path to your bottleneck_weights file.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/AIWintermuteAI/aXeleRate/issues/22#issuecomment-677174309, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALFNNTYYXZXE3ICIJDOHWR3SBSZSLANCNFSM4PWWV2RQ .

AIWintermuteAI commented 3 years ago

maixpy_v0.5.0_123_g859098d_minimum_with_ide_support.bin Use the firmware with tag minimum_with_ide_support or build one yourself configured to use IDE, but not other modules following the instructions here https://github.com/sipeed/MaixPy/blob/master/build.md To have your code start on boot, you need to copy your code to boot.py (there is an option to do that in MaixPy IDE). https://dl.sipeed.com/MAIX/MaixPy/release/master/maixpy_v0.5.0_123_g859098d

BlackEyesCoder commented 3 years ago

Thank you, but when I run code to load kmodel 1.81 with this firmware I got error : ValueError: [MAIXPY]kpu: load error:2002, ERR_KMODEL_VERSION: only support kmodel V3. I know that nncase 0.1.0-rc5 will convert kmodel v3, nncase 0.2.0-beta will convert kmodel v4, is that right ? Following your instructions, You use nncase 0.2.0-beta to convert. I try to change the link in convert.py, and run it but nothing happens. Perhaps, Your srouce code is block. Can I replace it to convert tflife to k210 ( kmode V3) to run with "minimal firmware with IDE" you shared with me? ( https://github.com/kendryte/nncase/releases/download/v0.1.0-rc5/ncc-linux-x86_64.tar.xz ) Vào Th 5, 20 thg 8, 2020 vào lúc 14:16 AIWintermuteAI < notifications@github.com> đã viết:

maixpy_v0.5.0_123_g859098d_minimum_with_ide_support.bin Use the firmware with tag minimum_with_ide_support or build one yourself configured to use IDE, but not other modules following the instructions here https://github.com/sipeed/MaixPy/blob/master/build.md To have your code start on boot, you need to copy your code to boot.py (there is an option to do that in MaixPy IDE). https://dl.sipeed.com/MAIX/MaixPy/release/master/maixpy_v0.5.0_123_g859098d

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/AIWintermuteAI/aXeleRate/issues/22#issuecomment-677401284, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALFNNT6ZG2TOWDD6CU2I5TLSBTEUHANCNFSM4PWWV2RQ .

AIWintermuteAI commented 3 years ago

Oh, right, they switch to build firmware with kmodel v4 support turned off by default... Not sure why really. 0.1.0-rc5 and 0.2.0-beta are two very different converters, they use different command line arguments - so just changing the link will not work, if you want kmodel v3 you might be better off just converting the model manually.

What I would suggest still is that you build the firmware with both kmodel v4 and IDE support enabled. Or use this one I built just now.

maixpy.zip

BlackEyesCoder commented 3 years ago

Thank You, About training classifier, I use dataset : https://www.kaggle.com/c/dogs-vs-cats/data, I upload and unzip it to "train" and "test1" folder. Config file : config = { "model" : { "type": "Classifier", "architecture": "MobileNet7_5", "input_size": 224, "fully-connected": [100,50], "labels": [], "dropout" : 0.5 }, "weights" : { "full": "", "backend": "imagenet", "save_bottleneck": True

},
"train" : {
    "actual_epoch":         50,
    "train_image_folder":   "/content/train",
    "train_times":          1,
    "valid_image_folder":   "/content/test1",
    "valid_times":          1,
    "valid_metric":         "val_accuracy",
    "batch_size":           4,
    "learning_rate":        1e-4,
    "saved_folder":         "classifier",
    "first_trainable_layer": "65",
    "augumentation":                True
},
"converter" : {
    "type":                 ["k210","tflite"]
}

} But, I got error when I run to train: image

AIWintermuteAI commented 3 years ago

"first_trainable_layer": "65", this layer doesn't exist in the model. just delete number 65

BlackEyesCoder commented 3 years ago

I deleted number 65, and train again image

AIWintermuteAI commented 3 years ago

Looks like you don't have enough validation(or training samples) - or the folder path for validation/training samples is wrong. For errors like this, please first use Google :) it's not related to aXeleRate, but to Keras

BlackEyesCoder commented 3 years ago

thank you, i will check again.

Vào 13:35, T.7, 22 Th8, 2020 AIWintermuteAI notifications@github.com đã viết:

Looks like you don't have enough validation(or training samples) - or the folder path for validation/training samples is wrong. For errors like this, please first use Google :) it's not related to aXeleRate, but to Keras

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/AIWintermuteAI/aXeleRate/issues/22#issuecomment-678603469, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALFNNTYMZASSEIMKT5LM7WTSB5RMTANCNFSM4PWWV2RQ .

BlackEyesCoder commented 3 years ago

Hi, if i train classifier for 2 class; cat and dog . How i prepare for my dataset, e set parameter in config file.

Vào 13:36, T.7, 22 Th8, 2020 Anh Nguyễn anhnguyen2345@gmail.com đã viết:

thank you, i will check again.

Vào 13:35, T.7, 22 Th8, 2020 AIWintermuteAI notifications@github.com đã viết:

Looks like you don't have enough validation(or training samples) - or the folder path for validation/training samples is wrong. For errors like this, please first use Google :) it's not related to aXeleRate, but to Keras

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/AIWintermuteAI/aXeleRate/issues/22#issuecomment-678603469, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALFNNTYMZASSEIMKT5LM7WTSB5RMTANCNFSM4PWWV2RQ .

AIWintermuteAI commented 3 years ago

https://colab.research.google.com/github/AIWintermuteAI/aXeleRate/blob/master/resources/aXeleRate_standford_dog_classifier.ipynb Consult this notebook :)

AIWintermuteAI commented 3 years ago

Closing the issue now, because the original problem seems to have been solved and topic of discussion moved somewhere else. If you encounter other issues, please, first make sure to read through examples, try them yourself, Google for code errors if such appear and if you feel you're still stuck, open an issue. Also make sure, that issue is not related to MaixPy or nncase - these are separate projects with their own GitHub repositories. Good luck!

BlackEyesCoder commented 3 years ago

But label is empty, why the model know where is cat's image or dog's image in images folder or test folder.

Vào 22:59, T.7, 22 Th8, 2020 AIWintermuteAI notifications@github.com đã viết:

https://colab.research.google.com/github/AIWintermuteAI/aXeleRate/blob/master/resources/aXeleRate_standford_dog_classifier.ipynb Consult this notebook :)

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/AIWintermuteAI/aXeleRate/issues/22#issuecomment-678657788, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALFNNT7VDMR22MQIERNI443SB7TNDANCNFSM4PWWV2RQ .

AIWintermuteAI commented 3 years ago

https://github.com/AIWintermuteAI/aXeleRate/blob/7cec5e510b422c8f63ca29e31c92569e365ce6e4/axelerate/networks/classifier/data_gen.py#L23 It uses flow from directory method of Keras class ImageDataGenerator - you can find the description of how it works in Keras docs. Plus it will generate labels.txt in project folder upon completion of training., which will contain class labels in the exact order they appear in network output.