nicknochnack / RealTimeObjectDetection

363 stars 567 forks source link

ImportError: No module named object_detection.utils or ModuleNotFoundError: No module named 'object_detection' #1

Open SidMalladi opened 3 years ago

SidMalladi commented 3 years ago

Traceback (most recent call last): File "/content/generate_tfrecord.py", line 29, in from object_detection.utils import dataset_util, label_map_util ModuleNotFoundError: No module named 'object_detection' Traceback (most recent call last): File "/content/generate_tfrecord.py", line 29, in from object_detection.utils import dataset_util, label_map_util ModuleNotFoundError: No module named 'object_detection'

I've already installed tensorflow, tensorflow object detection API through pip, changed line 111 into GFile.... but still the issue persists.

MacOS Catalina 10.15.3, python3.8, tensorflow (latest official update)

nicknochnack commented 3 years ago

Hiya @SidMalladi, just checking have you installed it on the same platform you're coding on? I think I saw you mentioned Colab and local Jupyter Notebooks.

SidMalladi commented 3 years ago

@nicknochnack Hey Nick, yes i have both locally installed and also on Collab.. i saw on the tensorflow object-detection API installation website that they only mentioned for Windows and Linux.. but I'm using a mac.. does that mean its not supported for mac?

VikranthMaster commented 3 years ago

Same error

007rohitSaini commented 3 years ago

try pip install tensorflow-object-detection-api it works for me

VikranthMaster commented 3 years ago

try pip install tensorflow-object-detection-api it works for me

Do we have to download the tensorflow object detection ?

mechaphantom commented 2 years ago

try pip install tensorflow-object-detection-api it works for me

I tried but now I see another error which says File "Tensorflow/scripts/generate_tfrecord.py", line 61, in label_map = label_map_util.load_labelmap(args.labels_path) File "C:\Python38\lib\site-packages\object_detection\utils\label_map_util.py", line 132, in load_labelmap with tf.gfile.GFile(path, 'r') as fid: AttributeError: module 'tensorflow' has no attribute 'gfile'

Do you have any idea how to fix? Is 'Creating TF Records' part necessary or should I pass this part?

007rohitSaini commented 2 years ago

just replace it with tf.io.gfile.GFile(path,'r')

mechaphantom commented 2 years ago

just replace it with tf.io.gfile.GFile(path,'r')

In generate_tfrecord.py file, I make this change: def create_tf_example(group, path): with tf.io.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

But it still doesnt work. The same error.

007rohitSaini commented 2 years ago

change file code with this one

""" Sample TensorFlow XML-to-TFRecord converter

usage: generate_tfrecord.py [-h] [-x XML_DIR] [-l LABELS_PATH] [-o OUTPUT_PATH] [-i IMAGE_DIR] [-c CSV_PATH]

optional arguments: -h, --help show this help message and exit -x XML_DIR, --xml_dir XML_DIR Path to the folder where the input .xml files are stored. -l LABELS_PATH, --labels_path LABELS_PATH Path to the labels (.pbtxt) file. -o OUTPUT_PATH, --output_path OUTPUT_PATH Path of output TFRecord (.record) file. -i IMAGE_DIR, --image_dir IMAGE_DIR Path to the folder where the input image files are stored. Defaults to the same directory as XML_DIR. -c CSV_PATH, --csv_path CSV_PATH Path of output .csv file. If none provided, then no file will be written. """

import os import glob import pandas as pd import io import xml.etree.ElementTree as ET import argparse

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # Suppress TensorFlow logging (1) import tensorflow.compat.v1 as tf from PIL import Image from object_detection.utils import dataset_util, label_map_util from collections import namedtuple

Initiate argument parser

parser = argparse.ArgumentParser( description="Sample TensorFlow XML-to-TFRecord converter") parser.add_argument("-x", "--xml_dir", help="Path to the folder where the input .xml files are stored.", type=str) parser.add_argument("-l", "--labels_path", help="Path to the labels (.pbtxt) file.", type=str) parser.add_argument("-o", "--output_path", help="Path of output TFRecord (.record) file.", type=str) parser.add_argument("-i", "--image_dir", help="Path to the folder where the input image files are stored. " "Defaults to the same directory as XML_DIR.", type=str, default=None) parser.add_argument("-c", "--csv_path", help="Path of output .csv file. If none provided, then no file will be " "written.", type=str, default=None)

args = parser.parse_args()

if args.image_dir is None: args.image_dir = args.xml_dir

label_map = label_map_util.load_labelmap(args.labels_path)

label_map_dict = label_map_util.get_label_map_dict(label_map)

label_map_dict = label_map_util.get_label_map_dict(args.labels_path)

def xml_to_csv(path): """Iterates through all .xml files (generated by labelImg) in a given directory and combines them in a single Pandas dataframe.

Parameters:
----------
path : str
    The path containing the .xml files
Returns
-------
Pandas DataFrame
    The produced dataframe
"""

xml_list = []
for xml_file in glob.glob(path + '/*.xml'):
    tree = ET.parse(xml_file)
    root = tree.getroot()
    filename = root.find('filename').text
    width = int(root.find('size').find('width').text)
    height = int(root.find('size').find('height').text)
    for member in root.findall('object'):
        bndbox = member.find('bndbox')
        value = (filename,
                 width,
                 height,
                 member.find('name').text,
                 int(bndbox.find('xmin').text),
                 int(bndbox.find('ymin').text),
                 int(bndbox.find('xmax').text),
                 int(bndbox.find('ymax').text),
                 )
        xml_list.append(value)
column_name = ['filename', 'width', 'height',
               'class', 'xmin', 'ymin', 'xmax', 'ymax']
xml_df = pd.DataFrame(xml_list, columns=column_name)
return xml_df

def class_text_to_int(row_label): return label_map_dict[row_label]

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(args.output_path)
path = os.path.join(args.image_dir)
examples = xml_to_csv(args.xml_dir)
grouped = split(examples, 'filename')
for group in grouped:
    tf_example = create_tf_example(group, path)
    writer.write(tf_example.SerializeToString())
writer.close()
print('Successfully created the TFRecord file: {}'.format(args.output_path))
if args.csv_path is not None:
    examples.to_csv(args.csv_path, index=None)
    print('Successfully created the CSV file: {}'.format(args.csv_path))

if name == 'main': tf.app.run()

007rohitSaini commented 2 years ago

or try importing tf with import tensorflow.compat.v1 as tf and with tf.io.gfile.GFile(path,'r') tf.gfile.Gfile

mechaphantom commented 2 years ago

import tensorflow.compat.v1 as tf

no dude. both of your answers did not work. I think I quit this tutorail and look Nick's other tensorflow object detection tutorial. I hope it will work

pbilwanikar commented 2 years ago

try pip install tensorflow-object-detection-api it works for me

I have tried this, but am still getting the same error.

mayuri-kulkarni commented 2 years ago

There seems to be some issue with version compatibility. The tensorflow-object-detection-api we install is not compatible of tensorflow 2, its also not compatible with the tensorflow 2 model. @nicknochnack could you please help us here with the version of tensorflow-object-detection-api?

AASTHASHAH2914 commented 5 months ago

I have problem in create TF reords:-

Traceback (most recent call last): File "C:\Users\shaha\RealTimeObjectDetection\Tensorflow\scripts\generate_tfrecord.py", line 29, in from object_detection.utils import dataset_util, label_map_util ModuleNotFoundError: No module named 'object_detection' Traceback (most recent call last): File "C:\Users\shaha\RealTimeObjectDetection\Tensorflow\scripts\generate_tfrecord.py", line 29, in from object_detection.utils import dataset_util, label_map_util ModuleNotFoundError: No module named 'object_detection'

Can anyone help me, please?

oskarsonc commented 3 months ago

I have problem in create TF reords:-

Traceback (most recent call last): File "C:\Users\shaha\RealTimeObjectDetection\Tensorflow\scripts\generate_tfrecord.py", line 29, in from object_detection.utils import dataset_util, label_map_util ModuleNotFoundError: No module named 'object_detection' Traceback (most recent call last): File "C:\Users\shaha\RealTimeObjectDetection\Tensorflow\scripts\generate_tfrecord.py", line 29, in from object_detection.utils import dataset_util, label_map_util ModuleNotFoundError: No module named 'object_detection'

Can anyone help me, please?

Same problem