2020 tensorflow custom training model notes (1) - object detection installation

After looking at many tutorials on the Internet and groping for several days, I was finally able to train by myself. In fact, there are still a lot of tutorials about this API on the Internet, but when I actually did it, I found that some steps were missing in some key parts, which would make a novice like me confused and unable to start, and finally in the endless Crash in the error report. So I decided to write this note, first to help Xiaobai like me to get it done easily, and second, to make notes for myself, in case I forget it in the future, I can come back and look back.

Computer Configuration

  • cpu: i7-8750H
  • gpu: 1060 6G
  • Memory: 16G
  • Operating system: win10

my environment

  • anaconda 3.x
  • python 3.6
  • tensorflow 1.15

Both cpu and gpu versions are available. I won’t introduce how to install them. I have also tried version 2.0. There is only one step. I can’t use 2.0 to solve it, so I won’t write 2.0.

Some differences between tf2.0 and 1.0

Error: AttributeError: module 'tensorflow' has no attribute 'contrib'
This library is used in the test and training sessions, but tf2.0 removes contrib. This is the reason why I cannot successfully train the model with tf2.0.
Other steps 1.0 and 2.0 are common. Because I am using a higher version of 1.0, I can recognize some 2.0 content, so all my codes are changed to the content suitable for 2.0. For example tf.Session-> tf.compat.v1.Session
of course tf2.0 also has special upgrade code:

tf_upgrade_v2 --infile foo.py --outfile foo-upgraded.py
#tf_upgrade_v2 --infile 1.*版.py文件 --outfile 生成的2.0版.py文件

Before the official start, I also want to explain that there are some problems like error reporting that the library cannot be found. I won’t talk about it here. Go directly to anaconda to download the missing library. I will talk about some special libraries in particular.
In addition, this article is my own study notes, and I have also used a lot of other people's code, I will basically write the source of what I use, if it is inappropriate, please be merciful.

1.object detection

1.1 download

First, we have to download object detection from GitHub. The download address is as follows:
https://github.com/tensorflow/models
After downloading and decompressing, you will get the models-master folder (ps: the path in his file is called models, I don’t know why it is called models-master after decompression, This is because I changed it to models myself, and for the convenience of my own screenshot demonstration, all the models that appear later)

1.2 configuration

Activate the tensorflow environment, go to the models/research/object_detection/ folder and you will see an object_detection_tutorial.ipynb file, which is a demo file. Open it with jupyter in the tensorflow environment and the following interface will appear: (My current time is 2020.2. 26, this is the latest version) insert image description here
Let’s follow his install steps step by step:
the first line: install tensorflow2.0, let’s ignore him, 1.0 is also available, comment out this sentence with # or delete it directly Lose.
The second line: install pycocotools, Windows installation pycocotools must first install cython, so we first download cython to the tensorflow environment. You can use the command line, but I haven't tried it, I did it in anaconda, I personally feel very convenient.
After installing cython, you have to install git online.
Then you either run this line of code in jupyter. I felt that it was too slow in actual operation, so I went online to find another method. Use the command line in the tf environment:

pip install git+https://github.com/philferriere/cocoapi.git#subdirectory=PythonAPI

This seems to be the COCO address that supports Windows written by some big guy. The original URL is: https://www.jianshu.com/p/8658cda3d553
It should be noted that this website is foreign, so domestic downloads will still be a bit slow. You Either wait or that (not to mention)
the third line: can run or not, just download this API
The fourth line: configuration path, running this code in jupyter is a bit slow, so I operate here directly on the command
line You also need to install protoc in advance

cd models/research/
protoc object_detection/protos/*.proto --python_out=.

Line 5: Install the package, also operate directly on the command line

pip install .

1.3 Test

At this point, we have completely set up the environment, and the install code will basically not be used in the future, so comment it out or delete it all. Next, run all the code directly, maybe you will succeed, but I failed. When I finally got the result, the kernel crashed. At the beginning, I searched for the answer to this question for a long time, and I didn’t realize it until recently. It is a system problem, because the person who wrote this code is running on Linux, which part of the code may conflict with win, anyway, there is a piece of content in the demo code that cannot be run, it is not a problem of our environment and computer .
Well, there is a way. I found a code suitable for our situation after searching a lot of tutorials.

import os
import sys
import cv2
import numpy as np
import tensorflow as tf
sys.path.append("..")

from utils import label_map_util
from utils import visualization_utils as vis_util


class TOD(object):
    def __init__(self):
        # 这是用于对象检测的实际模型的路径,如果没有这个pb文件,说明你还未下载。可以用demo里的下载代码来替换。
        self.PATH_TO_CKPT = 'ssd_mobilenet_v1_coco_2017_11_17/frozen_inference_graph.pb'
        #  用于为每个框添加正确标签的字符串的列表的路径。
        self.PATH_TO_LABELS = os.path.join('data', 'mscoco_label_map.pbtxt')

        self.NUM_CLASSES = 90

        self.detection_graph = self._load_model()
        self.category_index = self._load_label_map()

    def _load_model(self):
        detection_graph = tf.Graph()
        with detection_graph.as_default():
            od_graph_def = tf.compat.v1.GraphDef()
            with tf.io.gfile.GFile(self.PATH_TO_CKPT, 'rb') as fid:
                serialized_graph = fid.read()
                od_graph_def.ParseFromString(serialized_graph)
                tf.import_graph_def(od_graph_def, name='')
        return detection_graph

    def _load_label_map(self):
        label_map = label_map_util.load_labelmap(self.PATH_TO_LABELS)
        categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=self.NUM_CLASSES, use_display_name=True)
        category_index = label_map_util.create_category_index(categories)
        return category_index

    def detect(self, image):
        with self.detection_graph.as_default():
            with tf.compat.v1.Session(graph=self.detection_graph) as sess:
                # Expand dimensions since the model expects images to have shape: [1, None, None, 3]  扩展维度,因为模型期望图像具有以下形状:[1,None, None, 3]
                image_np_expanded = np.expand_dims(image, axis=0)
                image_tensor = self.detection_graph.get_tensor_by_name('image_tensor:0')
                # Each box represents a part of the image where a particular object was detected.  每个框表示检测到特定对象的图像的一部分。
                boxes = self.detection_graph.get_tensor_by_name('detection_boxes:0')
                # Each score represent how level of confidence for each of the objects.  每个分数表示每个对象的置信度。
                # Score is shown on the result image, together with the class label.  分数与类标签一起显示在结果图像上。
                scores = self.detection_graph.get_tensor_by_name('detection_scores:0')
                classes = self.detection_graph.get_tensor_by_name('detection_classes:0')
                num_detections = self.detection_graph.get_tensor_by_name('num_detections:0')
                # Actual detection.  实际检测。
                (boxes, scores, classes, num_detections) = sess.run(
                    [boxes, scores, classes, num_detections],
                    feed_dict={
    
    image_tensor: image_np_expanded})
                # Visualization of the results of a detection.  检测结果的可视化。
                vis_util.visualize_boxes_and_labels_on_image_array(
                    image,
                    np.squeeze(boxes),
                    np.squeeze(classes).astype(np.int32),
                    np.squeeze(scores),
                    self.category_index,
                    use_normalized_coordinates=True,
                    line_thickness=8)

        while True:
            cv2.namedWindow("detection", cv2.WINDOW_NORMAL)
            cv2.imshow("detection", image)
            if cv2.waitKey(110) & 0xff == 27:
                break


if __name__ == '__main__':
    image = cv2.imread('test_images/image1.jpg')#测试照片的路径
    detecotr = TOD()
    detecotr.detect(image)

The code comes from the article Tensorflow object detection API to build your own object recognition model (reprinted and modified) . Personally, I am very grateful to this blogger. It solved the problem of my unsuccessful test. The above code should pay attention to three paths. I have marked them in the code. These three places are based on the folder of my own Circumstances change on their own. You can also change the test path to a for loop to test a set of pictures.
Running this code, this annotated photo finally appears:
insert image description here

summary

Because the kernel was dead and crashed for too long before, not only the new version of the demo will crash, but the old version of the demo can't load the photos, only this code that uses cv can do it. As soon as this photo came out, I breathed a sigh of relief, and I was immediately full of confidence in the following training.
We will continue in the next article, and the next step is to generate training files...
If you know the specific reason, please teach me if you can, leave a comment in the comments below or private me.

2020 tensorflow custom training model notes (1) - object detection installation
2020 tensorflow custom training model notes (2) - making labels
2020 tensorflow custom training model notes (3) - start training

At present, the official library has been updated. The demo file I provided will report some errors. If you cannot solve these errors, please use the official demo.
If you can't use the official demo like me, and you don't care which library to use to complete the target detection, you can move to my notes about yolov5 yolov5 notes (1)
- install pytorch_GPU (win10+anaconda3)

Guess you like

Origin blog.csdn.net/weixin_45569617/article/details/104511366