Object Detection with Tensorflow [TENSORFLOW #1]

in #utopian-io3 years ago (edited)

Repository

https://github.com/tensorflow/models

What Will I Learn?

  • You will learn Tensorflow Object Detection Setup
  • You will learn Creating Virtual Environment in Anaconda
  • You will learn Collecting and Labeling Images
  • You will learn Creating Training Data, Label Map Creation and Training Settings

Requirements

Difficulty

  • Intermediate

1- Tensorflow Object Detection Setup and Creating Virtual Environment in Anaconda


  1. Download the TensorFlow Object Detection API. https://github.com/tensorflow/models
  2. Create a folder called tensorflow1 in C and export the models folder that you downloaded into this folder. models-master. You can make models to simplify the name. This should be the path to the object detection folder: C:\tensioflow1\models \ research\object_detection
  3. Download the Faster-RCNN-Inception-V2-COCO from the Model ZOO. Extract the tar.gz file into the object detection folder. https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/detection_model_zoo.md
  4. Download the .rar file that I share with you in the resources of this tutorial. Extract the .rar file to the object detection folder and say you want to change each file to be changed. The contents of the Objects detection folder should look like this:

NOTE: If you want to perform object recognition on a different dataset, delete the files in the following folders. (Do not delete folders)

\object_detection\images\train and \object_detection\images\test in everything.
"test_labels.csv" and "train_labels.csv" in \object_detection\images
\object_detection\training in everything.

ANACONDA VIRTUAL ENVIRONMENT INSTALLATION


Open the command window as an administrator. To create a virtual environment, type the following command:

C:\> conda create -n tensorflow1 pip


Use the following command to activate the virtual environment:

C:\> activate tensorflow1


If you activate the virtual environment, you will see it in brackets (tensorflow1). Set up all of the following libraries on this virtual environment one by one:

(tensorflow1) C:\> pip install --ignore-installed --upgrade tensorflow-gpu
(tensorflow1) C:\> conda install -c anaconda protobuf
(tensorflow1) C:\> pip install pillow
(tensorflow1) C:\> pip install lxml
(tensorflow1) C:\> pip install jupyter
(tensorflow1) C:\> pip install matplotlib
(tensorflow1) C:\> pip install pandas
(tensorflow1) C:\> pip install opencv-python



NOTE: If you install Tensorflow for the CPU, delete the GPU from the PIP install.

SETTING PYTHONPATH

You need to be in the research folder to compile the Protobufs. Compile the protobufs with the following lines:

(tensorflow1) C:\> cd C:\tensorflow1\models\research
protoc --python_out=. .\object_detection\protos\anchor_generator.proto .\object_detection\protos\argmax_matcher.proto .\object_detection\protos\bipartite_matcher.proto .\object_detection\protos\box_coder.proto .\object_detection\protos\box_predictor.proto .\object_detection\protos\eval.proto .\object_detection\protos\faster_rcnn.proto .\object_detection\protos\faster_rcnn_box_coder.proto .\object_detection\protos\grid_anchor_generator.proto .\object_detection\protos\hyperparams.proto .\object_detection\protos\image_resizer.proto .\object_detection\protos\input_reader.proto .\object_detection\protos\losses.proto .\object_detection\protos\matcher.proto .\object_detection\protos\mean_stddev_box_coder.proto .\object_detection\protos\model.proto .\object_detection\protos\optimizer.proto .\object_detection\protos\pipeline.proto .\object_detection\protos\post_processing.proto .\object_detection\protos\preprocessor.proto .\object_detection\protos\region_similarity_calculator.proto .\object_detection\protos\square_box_coder.proto .\object_detection\protos\ssd.proto .\object_detection\protos\ssd_anchor_generator.proto .\object_detection\protos\string_int_label_map.proto .\object_detection\protos\train.proto .\object_detection\protos\keypoint_box_coder.proto .\object_detection\protos\multiscale_anchor_generator.proto


With this line, every .proto file in the protos folder will be compiled and .py files will be added. More .proto files can be added to the protos folder in the future. If you run this command and see the uncompiled .proto file in the /object_detection/protos folder, you can add this proto file to the command with

\Object_detection\protos\FILENAME.proto


Run the setup.py file with the following lines. First you need to Install, after Build.

(tensorflow1) C:\tensorflow1\models\research> python setup.py build
(tensorflow1) C:\tensorflow1\models\research> python setup.py install



Finally, you can try running the tutorial file to see if it works. First you have to go to the object detection folder with:

cd object_detection
(tensorflow1) C:\tensorflow1\models\research\object_detection> jupyter notebook object_detection_tutorial.ipynb


After you have done the experiment on the Jupiter notebook, you can close the Jupiter notebook with CTRL + C at the command window.

2. Collecting and Labeling Images



If you want to train your own objects, you first need to create a dataset. Collect 200-300 images from the Internet or your own photographs. If the pictures are in different environments, you can train a stronger model. You will not get good results if you use only one angle of the object. So you have plenty of variety in your dataset. Collect pictures of objects with some places closed and different lighting.

Minimize the size of your images, the training of very large pictures will take a long time. Take care not to exceed 200kb. I have resizer.py file in my shared code, you can set the size of your images using this file.

Once you have collected the images, you can save 20% to the \object_detection\images\test folder, 80% in the \object_detection\images\train folder.

You need to label each one individually after you've dropped the pictures into the appropriate folders. Label with Labellmg the individual objects you want to recognize in all images.

Github: https://github.com/tzutalin/labelImg

Download: https://www.dropbox.com/s/tq7zfrcwl44vxan/windows_v1.6.0.zip?dl=1

An .xml file will be created for each tagged image. These .xml files will be converted to TfRecords for training. There should be one .xml file for each image in the test and train folders.

3. Creating Training Data


After you tag each image, you need to create a TFRecord. We will first convert the .xml files that contain the label information to the .csv file. To do this you need to be in the object_detection folder in the command window. Then run the following command. There will be 2 .csv files in the Images folder.

(tensorflow1) C:\tensorflow1\models\research\object_detection> python xml_to_csv.py

Once the .csv files have been created, open generate_tfrecord.py with any text editor. In line 31, enter your class information. I have 6 different IDs in my dataset because it has 6 classes. Assign IDs up to your class count starting from 1 by changing them according to the class count.

def class_text_to_int(row_label):
    if row_label == 'dokuz':
        return 1
    elif row_label == 'on':
        return 2
    elif row_label == 'vale':
        return 3
    elif row_label == 'kiz':
        return 4
    elif row_label == 'papaz':
        return 5
    elif row_label == 'as':
        return 6
    else:
        None

Once you have assigned your classes ID, create the TFRecord file for both test and train with the following commands. After running the commands, test.record and train.record files will be created in the object_detection folder.

python generate_tfrecord.py --csv_input=images\train_labels.csv --image_dir=images\train --output_path=train.record
python generate_tfrecord.py --csv_input=images\test_labels.csv --image_dir=images\test --output_path=test.record

4. Label Map Creation and Training Settings


LABEL MAP



The label map tells which class the ID has in which class. Go to the object_detection/training folder and create a new file. Give "labelmap.pbtxt" to the name of the file you created (do not give .txt if the extension is .pbtxt). Open the file with the text editor and enter the class and ID information as follows. You need to edit these class IDs according to your dataset.

item {
  id: 1
  name: 'dokuz'
}
 
item {
  id: 2
  name: 'on'
}
 
item {
  id: 3
  name: 'vale'
}
 
item {
  id: 4
  name: 'kiz'
}
 
item {
  id: 5
  name: 'papaz'
}
 
item {
  id: 6
  name: 'as'
}


Proof of Work Done

https://github.com/avnigenc/tensorflow-object-detection/

Sort:  

Thank you for your contribution.
The content of your post does not offer any added value to the community, as effectively most of the steps, code, images,... are actually taken as are from the following documentation https://github.com/EdjeElectronics/TensorFlow-Object-Detection-API-Tutorial-Train-Multiple-Objects-Windows-10/blob/master/README.md
This is considered plagiarism, and that is a serious offense.
Accordingly you have been banned for 60 days from utopian submissions.


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

I sent a ticket.

Congratulations @avnigenc! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

Award for the number of upvotes

Click on the badge to view your Board of Honor.
If you no longer want to receive notifications, reply to this comment with the word STOP

Do not miss the last post from @steemitboard!


Participate in the SteemitBoard World Cup Contest!
Collect World Cup badges and win free SBD
Support the Gold Sponsors of the contest: @good-karma and @lukestokes


Do you like SteemitBoard's project? Then Vote for its witness and get one more award!

@avnigenc you were flagged by a worthless gang of trolls, so, I gave you an upvote to counteract it! Enjoy!!

Congratulations @avnigenc! You have completed the following achievement on Steemit and have been rewarded with new badge(s) :

Award for the number of upvotes

Click on the badge to view your Board of Honor.
If you no longer want to receive notifications, reply to this comment with the word STOP

Do you like SteemitBoard's project? Then Vote for its witness and get one more award!

Congratulations @avnigenc! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

You made more than 2000 upvotes. Your next target is to reach 3000 upvotes.

Click here to view your Board of Honor
If you no longer want to receive notifications, reply to this comment with the word STOP

Do not miss the last post from @steemitboard:

SteemFest³ - SteemitBoard Contest Teaser
The new Steemfest³ Award is ready!

Support SteemitBoard's project! Vote for its witness and get one more award!

Congratulations @avnigenc! You received a personal award!

1 Year on Steemit

Click here to view your Board

Do not miss the last post from @steemitboard:

SteemWhales has officially moved to SteemitBoard Ranking
SteemitBoard - Witness Update

Support SteemitBoard's project! Vote for its witness and get one more award!

Congratulations @avnigenc! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 2 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Vote for @Steemitboard as a witness to get one more award and increased upvotes!