OlafenwaMoses / ImageAI

A python library built to empower developers to build applications and systems with self-contained Computer Vision capabilities
https://www.genxr.co/#products
MIT License
8.55k stars 2.19k forks source link

How to use the models made from training #225

Closed rayoh123 closed 5 years ago

rayoh123 commented 5 years ago

Hi, how do I use the models that ImageAI created from its training? The 100 models I made from training were ResNet type models.

Here is a screenshot of the 100 models I created from training with the path displayed: https://imgur.com/a/lsh62j3 I keep getting this exception:

Using TensorFlow backend. WARNING:tensorflow:From C:\Users\Owner\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\framework\op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version. Instructions for updating: Colocations handled automatically by placer. Traceback (most recent call last): File "C:\Users\Owner\Desktop\maybe.py", line 9, in detector.loadModel() File "C:\Users\Owner\AppData\Local\Programs\Python\Python37\lib\site-packages\imageai\Detection__init__.py", line 185, in loadModel model.load_weights(self.modelPath) File "C:\Users\Owner\AppData\Local\Programs\Python\Python37\lib\site-packages\keras\engine\network.py", line 1166, in load_weights f, self.layers, reshape=reshape) File "C:\Users\Owner\AppData\Local\Programs\Python\Python37\lib\site-packages\keras\engine\saving.py", line 1030, in load_weights_from_hdf5_group str(len(filtered_layers)) + ' layers.') ValueError: You are trying to load a weight file containing 107 layers into a model with 116 layers.

Here is the code I used to produce my models: from imageai.Prediction.Custom import ModelTraining

model_trainer = ModelTraining() model_trainer.setModelTypeAsResNet() model_trainer.setDataDirectory(r"C://Users//Owner//Desktop//Pieces") model_trainer.trainModel(num_objects=3, num_experiments=100, enhance_data=True, batch_size=32, show_network_summary=True)

Here is the code in which I tried to use the models I made: from imageai.Detection import ObjectDetection import os

execution_path = os.getcwd()

detector = ObjectDetection() detector.setModelTypeAsRetinaNet() detector.setModelPath( "C:\Users\Owner\Desktop\Pieces\models\model_ex-100_acc-0.383333.h5") detector.loadModel() detections = detector.detectObjectsFromImage(input_image=os.path.join(execution_path , "easier.jpg"), output_image_path=os.path.join(execution_path , "imagenew.jpg"), minimum_percentage_probability=30)

for eachObject in detections: print(eachObject["name"] , " : ", eachObject["percentage_probability"], " : ", eachObject["box_points"] ) print("--------------------------------")

Note: When I download the "resnet50_coco_best_v2.0.1.h5" file and use that model in object detection, it starts trying to identify "teddy bears", which are classes of objects I never trained my pictures on.

rayoh123 commented 5 years ago

Also, using the sample model_json and idenprof.h5 dataset on your medium post, this picture outputs a result of 63% doctor when it is a chef...Am I doing something wrong?

OlafenwaMoses commented 5 years ago

Hi, how do I use the models that ImageAI created from its training? The 100 models I made from training were ResNet type models.

Here is a screenshot of the 100 models I created from training with the path displayed: https://imgur.com/a/lsh62j3 I keep getting this exception:

Using TensorFlow backend. WARNING:tensorflow:From C:\Users\Owner\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\framework\op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version. Instructions for updating: Colocations handled automatically by placer. Traceback (most recent call last): File "C:\Users\Owner\Desktop\maybe.py", line 9, in detector.loadModel() File "C:\Users\Owner\AppData\Local\Programs\Python\Python37\lib\site-packages\imageai\Detectioninit.py", line 185, in loadModel model.load_weights(self.modelPath) File "C:\Users\Owner\AppData\Local\Programs\Python\Python37\lib\site-packages\keras\engine\network.py", line 1166, in load_weights f, self.layers, reshape=reshape) File "C:\Users\Owner\AppData\Local\Programs\Python\Python37\lib\site-packages\keras\engine\saving.py", line 1030, in load_weights_from_hdf5_group str(len(filtered_layers)) + ' layers.') ValueError: You are trying to load a weight file containing 107 layers into a model with 116 layers.

Here is the code I used to produce my models: from imageai.Prediction.Custom import ModelTraining

model_trainer = ModelTraining() model_trainer.setModelTypeAsResNet() model_trainer.setDataDirectory(r"C://Users//Owner//Desktop//Pieces") model_trainer.trainModel(num_objects=3, num_experiments=100, enhance_data=True, batch_size=32, show_network_summary=True)

Here is the code in which I tried to use the models I made: from imageai.Detection import ObjectDetection import os

execution_path = os.getcwd()

detector = ObjectDetection() detector.setModelTypeAsRetinaNet() detector.setModelPath( "C:\Users\Owner\Desktop\Pieces\models\model_ex-100_acc-0.383333.h5") detector.loadModel() detections = detector.detectObjectsFromImage(input_image=os.path.join(execution_path , "easier.jpg"), output_image_path=os.path.join(execution_path , "imagenew.jpg"), minimum_percentage_probability=30)

for eachObject in detections: print(eachObject["name"] , " : ", eachObject["percentage_probability"], " : ", eachObject["box_points"] ) print("--------------------------------")

Note: When I download the "resnet50_coco_best_v2.0.1.h5" file and use that model in object detection, it starts trying to identify "teddy bears", which are classes of objects I never trained my pictures on.

Please note that ImageAI is yet to support training custom models for object detection. The model you trained is for prediction and not detection., which means you will be using the CustomImagePrediction class instead of ObjectDetection

OlafenwaMoses commented 5 years ago

Also, using the sample model_json and idenprof.h5 dataset on your medium post, this picture outputs a result of 63% doctor when it is a chef...Am I doing something wrong?

The model is not always 100% correct, and considering a doctors and chefs do have outfits with very similar color and structure, this may a occur a number of times.

rahul221389 commented 5 years ago

Please note that ImageAI is yet to support training custom models for object detection. The model you trained is for prediction and not detection., which means you will be using the CustomImagePrediction class instead of ObjectDetection

When will ImageAI support training custom models for object detection? can you give us some rough idea? And you guys are doing very well.

OlafenwaMoses commented 5 years ago

Kindly follow this thread for more on this. #8

rahul221389 commented 5 years ago

@OlafenwaMoses Ok.