Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.7k views
in Technique[技术] by (71.8m points)

tensorflow2.0 - Need help to understand why my tensorflow model won't predict (Model trained with Object Detection API)

I am new to TensorFlow and I've been trying to figure out how the object detection API works.

I've been following this tutorial. I've made a few changes to pipeline.config that deals with learning rate,

post_processing > batch_non_max_suppression >

        score_threshold: 0.165
        iou_threshold: 0.49000000238418579
        max_detections_per_class: 5
        max_total_detections: 10
        use_static_shapes: false

And here are my graphs> tensorboard_graphs

After around 700+ steps when my total loss converged to around 1. So, I stopped the training and then exported the saved model. For training, exporting and inference I've used the code provided here.

model_main_tf2.py

exporter_main_v2.py

inference_from_saved_model_tf2_colab.ipynb

My model is trained with 1425 images of pistols obtained from this website. It is trained for one class(PS I was thinking that for object detection, as there is classification and localization having one class is alright because parts outside the bounding box is negative. Let me know if I'm wrong here.)

print(detections['detection_anchor_indices'])
tf.Tensor([[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]], shape=(1, 10), dtype=float32)
print(detections['detection_boxes'])
tf.Tensor(
[[[0. 0. 0. 0.]
  [0. 0. 0. 0.]
  [0. 0. 0. 0.]
  [0. 0. 0. 0.]
  [0. 0. 0. 0.]
  [0. 0. 0. 0.]
  [0. 0. 0. 0.]
  [0. 0. 0. 0.]
  [0. 0. 0. 0.]
  [0. 0. 0. 0.]]], shape=(1, 10, 4), dtype=float32)
print(detections['detection_classes'])
tf.Tensor([[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]], shape=(1, 10), dtype=float32)
print(detections['detection_multiclass_scores'])
tf.Tensor(
[[[0. 0.]
  [0. 0.]
  [0. 0.]
  [0. 0.]
  [0. 0.]
  [0. 0.]
  [0. 0.]
  [0. 0.]
  [0. 0.]
  [0. 0.]]], shape=(1, 10, 2), dtype=float32)
print(detections['detection_scores'])
tf.Tensor([[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]], shape=(1, 10), dtype=float32)
print(detections['num_detections'])
tf.Tensor([0.], shape=(1,), dtype=float32)

I have checked if the dataset was labeled properly with the help of labelImg. I dont know if I should train it more or is it a fault with my dataset. I am afraid if it would overfit. My wildest guess right now is if I have done something wrong at creating the TFRecord.

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),#tf.io.FixedLenFeature((), tf.int64),
        '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

Any help would be appreciated. Btw I am using mobilenet v2 ssd model as my pre-trained model.

And just to assure is this the best way to make a handy object detection model?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...