models icon indicating copy to clipboard operation
models copied to clipboard

ModuleNotFoundError: No module named 'utils'

Open karurosu-cpp opened this issue 8 years ago • 11 comments

Hi, How are you :)? I am a simple problem, my code dont work cause my terminal indicates that the object-detection utils not found

 from utils import label_map_util
ModuleNotFoundError: No module named 'utils'

I dont know the reason, the API work okey in the

/anaconda3/lib/python3.6/site-packages/tensorflow/models/research

but in other path doesnt work :(

the code is

from __future__ import division
from __future__ import print_function
from __future__ import absolute_import

import sys
sys.path.append('/home/juank/anaconda3/lib/python3.6/site-packages/tensorflow/models') # point to your tensorflow dir
sys.path.append('/home/juank/anaconda3/lib/python3.6/site-packages/tensorflow/contrib/slim') # point ot your slim dir

import os
import io
import pandas as pd
import tensorflow as tf

from PIL import Image
#from utils import label_map_util
from object_detection.utils import dataset_util
from collections import namedtuple, OrderedDict

flags = tf.app.flags
flags.DEFINE_string('csv_input', '', 'Path to the CSV input')
flags.DEFINE_string('output_path', '', 'Path to output TFRecord')
FLAGS = flags.FLAGS


# TO-DO replace this with label map
def class_text_to_int(row_label):
    if row_label == 'naranjas':
        return 1
    else:
        None


def split(df, group):
    data = namedtuple('data', ['filename', 'object'])
    gb = df.groupby(group)
    return [data(filename, gb.get_group(x)) for filename, x in zip(gb.groups.keys(), gb.groups)]


def create_tf_example(group, path):
    with tf.gfile.GFile(os.path.join(path, '{}'.format(group.filename)), 'rb') as fid:
        encoded_jpg = fid.read()
    encoded_jpg_io = io.BytesIO(encoded_jpg)
    image = Image.open(encoded_jpg_io)
    width, height = image.size

    filename = group.filename.encode('utf8')
    image_format = b'jpg'
    xmins = []
    xmaxs = []
    ymins = []
    ymaxs = []
    classes_text = []
    classes = []

    for index, row in group.object.iterrows():
        xmins.append(row['xmin'] / width)
        xmaxs.append(row['xmax'] / width)
        ymins.append(row['ymin'] / height)
        ymaxs.append(row['ymax'] / height)
        classes_text.append(row['class'].encode('utf8'))
        classes.append(class_text_to_int(row['class']))

    tf_example = tf.train.Example(features=tf.train.Features(feature={
        'image/height': dataset_util.int64_feature(height),
        'image/width': dataset_util.int64_feature(width),
        'image/filename': dataset_util.bytes_feature(filename),
        'image/source_id': dataset_util.bytes_feature(filename),
        'image/encoded': dataset_util.bytes_feature(encoded_jpg),
        'image/format': dataset_util.bytes_feature(image_format),
        'image/object/bbox/xmin': dataset_util.float_list_feature(xmins),
        'image/object/bbox/xmax': dataset_util.float_list_feature(xmaxs),
        'image/object/bbox/ymin': dataset_util.float_list_feature(ymins),
        'image/object/bbox/ymax': dataset_util.float_list_feature(ymaxs),
        'image/object/class/text': dataset_util.bytes_list_feature(classes_text),
        'image/object/class/label': dataset_util.int64_list_feature(classes),
    }))
    return tf_example


def main(_):
    writer = tf.python_io.TFRecordWriter(FLAGS.output_path)
    path = os.path.join(os.getcwd(), 'images')
    examples = pd.read_csv(FLAGS.csv_input)
    grouped = split(examples, 'filename')
    for group in grouped:
        tf_example = create_tf_example(group, path)
        writer.write(tf_example.SerializeToString())

    writer.close()
    output_path = os.path.join(os.getcwd(), FLAGS.output_path)
    print('Successfully created the TFRecords: {}'.format(output_path))


if __name__ == '__main__':
    tf.app.run()

and the problem stay in the line 25

from object_detection.utils import dataset_util

i intented the other way on the 24 line but doesnt work, and the complete text in te terminal is

/home/juank/anaconda3/lib/python3.6/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters
Traceback (most recent call last):
  File "generate_tfrecord.py", line 24, in <module>
    from utils import label_map_util
ModuleNotFoundError: No module named 'utils'

in all codes its the same problem, that UTILS not found an i dont know if its a path problem or writing error in the code

NOTE: I already intented

protoc object_detection/protos/*.proto --python_out=.

 export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim

And the API works in the directory test

OS Platform and Distribution Ubuntu 18.04 LTS

TensorFlow installed from Three months Updated 1.0.0 to 1.8.0

Version 1.8.0

Bazel version N/A

CUDA/cuDNN version N/A I have AMD A9 CPU

GPU model and memory N/A

Exact command to reproduce

from object_detection.utils import dataset_util

I hope your help, Thank you :)

karurosu-cpp avatar Jul 23 '18 02:07 karurosu-cpp

Thank you for your post. We noticed you have not filled out the following field in the issue template. Could you update them if they are relevant in your case, or leave them as N/A? Thanks. What is the top-level directory of the model you are using Have I written custom code TensorFlow version

tensorflowbutler avatar Jul 24 '18 08:07 tensorflowbutler

I want to import dataset_util.py

/home/user/anaconda3/lib/python3.6/site-packages/tensorflow/models/research/object_detection/utils/dataset_util.py

with the instruccion

from object_detection.utils import dataset_util

I want to create a tf record with the script of https://github.com/datitran/raccoon_dataset called generate_tfrecord.py

following the tutorial of https://pythonprogramming.net/creating-tfrecord-files-tensorflow-object-detection-api-tutorial/

Tensorflow version 1.8.0

karurosu-cpp avatar Jul 24 '18 15:07 karurosu-cpp

This looks like an issue with your Python path. Based on the text, I'm not sure which of the two versions you want to use (from object_detection... or from utils...), so I will assume the first.

In order to import from object_detection, you should add the object_detection directory to your PYTHONPATH. Have you gone through all of the installation instructions, including the part about PYTHONPATH?

karmel avatar Jul 24 '18 16:07 karmel

I just fix this by adding these codes in my generate_tfrecord.py. '''python import sys sys.path.append('/anaconda3/lib/python3.6/site-packages/tensorflow/models/research/') ''' Hoping that will work for you.

5ertsa avatar Jul 25 '18 01:07 5ertsa

Karmel, i followed all steps, and i dont know why its fail :( , how i could be fix it? in every code happens the same situation with the paths! xDooot, really good, you helped me! but now I have a new message during the

  python generate_tfrecord.py --csv_input=data/test_labels.csv  --output_path=data/test.record

its:...

  File "/home/usr/anaconda3/lib/python3.6/site-packages/tensorflow/models/research/object_detection/utils/dataset_util.py", line 26, in int64_list_feature
    return tf.train.Feature(int64_list=tf.train.Int64List(value=value))
TypeError: None has type NoneType, but expected one of: int, long

karurosu-cpp avatar Jul 25 '18 19:07 karurosu-cpp

Check your generate_tfrecord.py again. Make sure that you did not miss class labels to return, which in your xml files. def class_text_to_int(row_label): if row_label == 'xxx': return 1 elif row_label == 'xxx': return 2 elif row_label == 'xxx': return 3 elif row_label == 'xxx': return 4 else: None That maybe work for you. Good luck :)

5ertsa avatar Jul 26 '18 08:07 5ertsa

Very well! Thank you very much!

karurosu-cpp avatar Jul 26 '18 22:07 karurosu-cpp

you are incredible guys ;)

karurosu-cpp avatar Jul 26 '18 22:07 karurosu-cpp

My pleasure. :)

5ertsa avatar Jul 27 '18 00:07 5ertsa

Closing as this is resolved

wt-huang avatar Nov 03 '18 00:11 wt-huang

ModuleNotFoundError: No module named 'utils'

sakshamceoo avatar Sep 17 '21 14:09 sakshamceoo