Search code examples
pythontensorflowartificial-intelligencetensorflow-lite

How to define a file path when dealing with Tensorflow?


This issue has been solved sorry for any time wasted I'm using notepad++ due to hardware constraints so I wasn't aware of needing to import OS to define a filepath

I am trying to create a TFLite model, I have drawn an arrow to where I'm getting the file path error:

Error: (NameError: name 'oranges' is not defined)

from __future__ import absolute_import, division, print_function, unicode_literals

import numpy as np

import tensorflow as tf
assert tf.__version__.startswith('2')

from tensorflow_examples.lite.model_customization.core.data_util.image_dataloader import ImageClassifierDataLoader
from tensorflow_examples.lite.model_customization.core.task import image_classifier
from tensorflow_examples.lite.model_customization.core.task.model_spec import efficientnet_b0_spec
from tensorflow_examples.lite.model_customization.core.task.model_spec import ImageModelSpec

import matplotlib.pyplot as plt

data = ImageClassifierDataLoader.from_folder(oranges) <-- oranges is a folder containing the test 
                                                          images. It is in the same folder as this file
model = image_classifier.create(data)

loss, accuracy = model.evaluate()

model.export('image_classifier.tflite', 'image_labels.txt')

Solution

  • The path must be a folder containing your images for modelling. In this case, the entry oranges is not defined as a folder path anywhere in the code.

    To create a folder path, run:

    import os
    oranges = os.path.abspath('oranges')
    

    Before executing the code:

    data = ImageClassifierDataLoader.from_folder(oranges)