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.5k stars 2.19k forks source link

Failing to load custom trained model #229

Closed itspngu closed 5 years ago

itspngu commented 5 years ago

Hi! I trained a test model on DenseNet and am trying to use it with the custom prediction class.

Here is the code I used to train the model:

from imageai.Prediction.Custom import ModelTraining

model_trainer = ModelTraining()
model_trainer.setModelTypeAsDenseNet()
model_trainer.setDataDirectory("data")
model_trainer.trainModel(num_objects=2, num_experiments=200, enhance_data=True, batch_size=8, show_network_summary=True)

The training seems to work fine. I then transferred the highest accuracy file to the remote system which is supposed to do the image recognition. This is the relevant snippet of the script:

execution_path = os.getcwd()
            #print('path to model: ' + os.path.join(execution_path, "model_ex-021_acc-0.875000.h5"))

            prediction = CustomImagePrediction()
            prediction.setModelTypeAsDenseNet()
            #prediction.setModelPath(os.path.join(execution_path, "model_ex-011_acc-0.687500.h5"))
            prediction.setModelPath("./net/data/models/model_ex-011_acc-0.687500.h5")
            prediction.setJsonPath(os.path.join(execution_path, "model_class.json"))
            prediction.loadModel(num_objects=2)

            for l in listings:
                url = 'https://www.ebay.com/itm/' + str(l[1])

                session = requests.Session()
                session.headers.update({'User-Agent': 'Googlebot/2.1 (+http://www.google.com/bot.html)'})
                response = session.get(url)
                selector = Selector(text=response.text)
                img_link = selector.xpath('//img[@id = "icImg"]/@src').getall()[0]
                response = session.get(img_link, stream=True)
                if (response.status_code == 200):
                    with open('tmp_ebay_img', 'wb') as f:
                        response.raw.decode_content = True
                        shutil.copyfileobj(response.raw, f)

                    predictions, percentage_probabilities = prediction.predictImage("tmp_ebay_img", result_count=2)

I tried several ways to specify the path and moving the file to different directories, but whatever I do, I keep getting the following exception:

(<class 'ValueError'>, ValueError('You have specified an incorrect path to the DenseNet model file.',), <traceback object at 0x7fc19effcb88>)

I sat at this for about 4 hours and am starting to suspect that there's a bug somewhere. Things I tried:

I'm at wit's end here. The model file definitely is where I tell the framework to look for it. Any ideas?

itspngu commented 5 years ago

Nevermind, I sat at this for too long and got tunnel vision. Specifying the part as follows solved the issue:

prediction.setModelPath("./net/data/models/model_ex-021_acc-0.875000.h5")
OlafenwaMoses commented 5 years ago

Good to know you got this resolved. And it is a very interesting application you got there in your code.