Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #1

Merged
merged 53 commits into from
Sep 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
a2a2f85
Initital Commit
Jul 17, 2019
0fe9ef5
Added Cat Images
Sep 10, 2019
438f04f
added annotation folder
AntonMu Sep 11, 2019
5a74a90
added convert format option
AntonMu Sep 11, 2019
32151a3
updated training
AntonMu Sep 12, 2019
16430ae
updated reqs
AntonMu Sep 12, 2019
3dcf56b
added inference routine
AntonMu Sep 13, 2019
6d95a13
Clean up names
Sep 13, 2019
b4ec27a
Create README.md
Sep 13, 2019
05998da
Update README
Sep 13, 2019
0a4b0fd
Update README.md
Sep 13, 2019
530d4bf
Update README.md
Sep 13, 2019
b3c8056
Update README
Sep 13, 2019
f360ffa
Update README
Sep 13, 2019
acf2e36
Update Minimal_Example.py
AntonMu Sep 13, 2019
f479f03
Update Download_Weights.sh
AntonMu Sep 13, 2019
3015f5f
Clean up
Sep 13, 2019
bc4a8e6
Update README.md
Sep 13, 2019
fcdd4b7
Update README.md
Sep 13, 2019
dd7d727
updated minimal example
AntonMu Sep 14, 2019
641cf4a
updated minimal example
AntonMu Sep 14, 2019
64ba283
updated minimal example
AntonMu Sep 14, 2019
7276198
updated minimal example
AntonMu Sep 14, 2019
19da3d8
updated minimal example
AntonMu Sep 14, 2019
6189314
updated minimal example
AntonMu Sep 14, 2019
05c9c20
simplified weight download
AntonMu Sep 16, 2019
34b89cc
Updated Readmes
Sep 16, 2019
f474444
Updated VoTT Readme
Sep 16, 2019
f4a599b
Added GIF
Sep 16, 2019
1cda6b0
Update README
Sep 16, 2019
ed0c7c1
Updated GIFs
Sep 16, 2019
b312a39
Updated Readme
Sep 16, 2019
041ac21
Update New_Project.gif
Sep 16, 2019
0edf2a0
Update README.md
Sep 16, 2019
4ad66ed
Update README.md
Sep 16, 2019
b406586
Update README.md
Sep 16, 2019
5c4d920
Update README.md
Sep 16, 2019
46381a3
Update README.md
Sep 16, 2019
a26faa3
AWS instructions
Sep 17, 2019
4c0b3fb
Updated README
Sep 17, 2019
ca7e3a8
Update README.md
Sep 17, 2019
809430f
Update README.md
Sep 17, 2019
ff9c591
Update README.md
Sep 17, 2019
15fcdb0
Delete events.out.tfevents.1568665915.Ubuntu-Precision
Sep 17, 2019
5a0bed0
Update README.md
Sep 17, 2019
772547b
Update README.md
Sep 17, 2019
252b055
Update README.md
Sep 17, 2019
6714da5
Renames Output file
Sep 17, 2019
813d0cc
Update README.md
Sep 17, 2019
c61c64c
Update README.md
Sep 17, 2019
4d3f103
Update README.md
Sep 17, 2019
bba9f3c
Update README.md
Sep 17, 2019
9debcbe
Update README.md
Sep 17, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

*.h5
env
78 changes: 78 additions & 0 deletions 1_Image_Annotation/Convert_to_YOLO_format.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
from PIL import Image
from os import path, makedirs
import os
import re
import pandas as pd
import sys
import argparse
def get_parent_dir(n=1):
""" returns the n-th parent dicrectory of the current
working directory """
current_path = os.path.dirname(os.path.abspath(__file__))
for k in range(n):
current_path = os.path.dirname(current_path)
return current_path

sys.path.append(os.path.join(get_parent_dir(1),'Utils'))
from Convert_Format import convert_vott_csv_to_yolo

Data_Folder = os.path.join(get_parent_dir(1),'Data')
VoTT_Folder = os.path.join(Data_Folder,'Source_Images','vott-csv-export')
VoTT_csv = os.path.join(VoTT_Folder,'Annotations-export.csv')
YOLO_filename = os.path.join(VoTT_Folder,'data_train.txt')

model_folder = os.path.join(Data_Folder,'Model_Weights')
classes_filename = os.path.join(model_folder,'data_classes.txt')

AWS_path = '/home/ubuntu/TrainYourOwnYOLO/Data/Source_Images/vott-csv-export/'




if __name__ == '__main__':
# surpress any inhereted default values
parser = argparse.ArgumentParser(argument_default=argparse.SUPPRESS)
'''
Command line options
'''
parser.add_argument(
"--VoTT_Folder", type=str, default=VoTT_Folder,
help = "absolute path to the exported files from the image tagging step with VoTT."
)

parser.add_argument(
"--VoTT_csv", type=str, default=VoTT_csv,
help = "absolute path to the *.csv file exported from VoTT. The default name is 'Houses-export.csv'."
)
parser.add_argument(
"--YOLO_filename", type=str, default=YOLO_filename,
help = "absolute path to the file where the annotations in YOLO format should be saved. The default name is 'data_train.txt' and is saved in the VoTT folder."
)

parser.add_argument(
'--AWS', default=False, action="store_true",
help='Enable this flag if you plan to train on AWS but did your pre-processing on a local machine.'
)

FLAGS = parser.parse_args()

#Prepare the dataset for YOLO
multi_df = pd.read_csv(FLAGS.VoTT_csv)
labels = multi_df['label'].unique()
labeldict = dict(zip(labels,range(len(labels))))
multi_df.drop_duplicates(subset=None, keep='first', inplace=True)
if FLAGS.AWS:
train_path = AWS_path
else:
train_path = FLAGS.VoTT_Folder
convert_vott_csv_to_yolo(multi_df,labeldict,path = train_path,target_name=FLAGS.YOLO_filename)

# Make classes file
file = open(classes_filename,"w")
#Sort Dict by Values
SortedLabelDict = sorted(labeldict.items() , key=lambda x: x[1])
for elem in SortedLabelDict:
file.write(elem[0])
file.close()


37 changes: 37 additions & 0 deletions 1_Image_Annotation/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# TrainYourOwnYOLO: Image Annotation
To train our YOLO detector, we first annotate images located in [`TrainYourOwnYOLO/Data/Source_Images`](/Data/Source_Images) with the help of Microsoft's Visual Object Tagging Tool (VoTT).

## Download VoTT
Head to VoTT [releases](https://github.com/Microsoft/VoTT/releases) and download and install the version for your operation system.

## Create a New Project

![New Project](/1_Image_Annotation/Screenshots/New_Project.gif)

Create a **New Project** and call it `Annotations`. It is highly recommeded to use `Annotations` as your project name. If you like to use a different name for your project, you will have to modify the command line arguments of subsequent scripts accordingly.

Under **Source Connection** choose **Add Connection** and put `Images` as **Display Name**. Under **Provider** choose **Local File System** and select the folder with [`Source Images`](/Data/Source_Images). For **Target Connection** choose the same folder as for **Source Connection**. Hit **Save Project** to finish project creation.

## Export Settings
Navigate to **Export Settings** in the sidebar and then change the **Provider** to `Comma Seperated Values (CSV)`, then hit **Save Export Settings**.

![New Project](/1_Image_Annotation/Screenshots/Export_Settings.gif)


## Labeling
First create a new tag on the right and give it a relevant tag name. In our example, we choose `Cat_Face`. Then draw bounding boxes around your objects. You can use the number key to quickly assign tags to the current bounding box.

![New Project](/1_Image_Annotation/Screenshots/Labeling.gif)

## Export Results
Once you have labeled enough images (try to label at least 100 objects) press **CRTL+E** to export the project. You should now see a folder called `vott-csv-export` in the [`Source Images`](/Data/Source_Images) directory. Within that folder, you should see a `*.csv` file called `Annotations-export.csv` which contains information on all bounding boxes.

## Convert to YOLO Format
As a final step, convert the VoTT csv format to the YOLOv3 format. To do so, run the conversion script:

```
python Convert_to_YOLO_format.py
```
The script generates two output files: `data_train.txt` located in the [`vott-csv-export`](/Data/Source_Images/vott-csv-export)) folder and `data_classes.txt` located in the [`TrainYourOwnYOLO/Data/Model_Weights`](/Data/Model_Weights/) folder.

That's all for annotation! Next, go to [`TrainYourOwnYOLO/2_Training`](/2_Training) to train your YOLOv3 detector.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 1_Image_Annotation/Screenshots/Labeling.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 1_Image_Annotation/Screenshots/Medium.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 1_Image_Annotation/Screenshots/New_Project.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions 2_Training/AWS/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# TrainYourOwnYOLO: Training on AWS

If your local machine does not have a GPU, training could take a very long time. To speed things up use an AWS GPU instance.

## Spinning up a GPU Instance
To spin up a GPU instance, go to **EC2** and select **Launch Instance**. Then go to **Deep Learning AMI (Ubuntu) Version xx.x ** and hit **Select**. Under instance type, select **p2.xlarge** as the Instance Type. Proceed by hitting **Review and Launch**.

![Deep_Learning_AMI](/2_Training/AWS/Screenshots/AWS_Deep_Learning_AMI.gif)

## Start the Training
Connect to your instance and follow the same steps as on your local machine. Make sure that all your Source Images are in [`TrainYourOwnYOLO/Data/Source_Images`](/Data/Source_Images) and that the [`data_train.txt`](/Data/Source_Images/vott-csv-export/data_train.txt) and [`data_classes.txt`](/Data/Model_Weights/data_classes.txt) are up-to-date.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
84 changes: 84 additions & 0 deletions 2_Training/Download_YOLO_weights.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import os
import subprocess
import time
import sys

# TrainYourOwnYOLO/Training/src/keras_yolo3

root_folder = os.path.dirname(os.path.abspath(__file__))


download_folder = os.path.join(root_folder,'src','keras_yolo3')

convert_script = os.path.join(download_folder,'Download_YOLO_weights.sh')

call_string = 'bash ' + convert_script

subprocess.call(call_string , shell=True)


# weight_file = os.path.join(download_folder,'yolov3.weights')
# print(download_folder)
# # wget https://pjreddie.com/media/files/yolov3.weights
# # python convert.py yolov3.cfg yolov3.weights model_data/yolo.h5

# # call_string = 'wget https://pjreddie.com/media/files/yolov3.weights -O ' + weight_file

# # subprocess.call(call_string , shell=True)


# # wget http://download.files.com/software/files.tar.gz -O /home/yourname/Downloads

# convert_script = weight_file = os.path.join(download_folder,'convert.py')

# call_string = 'python ' + convert_script + ' yolov3.cfg yolov3.weights model_data/yolo.h5'
# subprocess.call(call_string , shell=True)

# # python convert.py yolov3.cfg yolov3.weights model_data/yolo.h5


# def make_call_string(arglist):
# result_string = ''
# for arg in arglist:
# result_string+= ''.join(['--',arg[0],' ', arg[1],' '])
# return result_string
# root_folder = os.path.dirname(os.path.abspath(__file__))
# data_folder = os.path.join(root_folder,'Data')
# model_folder = os.path.join(data_folder,'Model_Weights')
# image_folder = os.path.join(data_folder,'Source_Images')
# input_folder = os.path.join(image_folder,'Test_Images')
# output_folder = os.path.join(image_folder,'Test_Image_Detection_Results')


# if not os.path.exists(output_folder):
# os.mkdir(output_folder)

# #First download the pre-trained weights
# download_script = os.path.join(model_folder,'Download_Weights.py')

# print('Downloading Pretrained Weights')
# start = time.time()
# call_string = ' '.join(['python',download_script,'1MGXAP_XD_w4OExPP10UHsejWrMww8Tu7', os.path.join(model_folder,'trained_weights_final.h5') ])

# subprocess.call(call_string , shell=True)

# end = time.time()
# print('Downloaded Pretrained Weights in {0:.1f} seconds'.format(end-start))

# # Now run the cat face detector
# detector_script = os.path.join(os.path.dirname(os.path.abspath(__file__)),'3_Inference','Detector.py')


# result_file = os.path.join(output_folder, 'Cat_Faces_Results.csv')
# model_weights = os.path.join(model_folder,'trained_weights_final.h5')
# classes_file = os.path.join(model_folder,'data_classes.txt')
# anchors = os.path.join(root_folder,'2_Training','src','keras_yolo3','model_data','yolo_anchors.txt')

# arglist = [['input_images',input_folder],['classes',classes_file],['output',output_folder],['yolo_model',model_weights],['box_file',result_file],['anchors',anchors]]
# call_string = ' '.join(['python', detector_script,make_call_string(arglist)])

# print('Detecting Cat Faces by calling \n\n', call_string,'\n')
# start = time.time()
# subprocess.call(call_string, shell=True)
# end = time.time()
# print('Detected Cat Faces in {0:.1f} seconds'.format(end-start))
20 changes: 20 additions & 0 deletions 2_Training/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# TrainYourOwnYOLO: Training
Using the training images located in [`TrainYourOwnYOLO/Data/Source_Images`](/Data/Source_Images) together with our annotation file [`data_train.txt`](/Data/Source_Images/vott-csv-export) we can train our YOLOv3 detector.

## Download the Pre-Trained Weights
Before getting started we need to download the pre-trained YOLOv3 weights and convert them to the keras format. To run both steps execute:

```
python Download_YOLO_weights.py
```
## Train YOLOv3 Detector
To start the training run the training script:
```
python Train_YOLO.py
```
Depending on your set-up this process can take a few minutes to a few hours. I recommend using a GPU to speed up training. The final weights are saved in [`TrainYourOwnYOLO/Data/Model_weights`](/Data/Model_weights).

### Training on AWS
If training is too slow on your local machine, you can speed up the training by using a GPU cluster on AWS. To learn more about training on AWS navigate to the [`AWS`](/2_Training/AWS) folder.

This concludes the training step and you are now ready to detect cat faces in new images!
Binary file added 2_Training/Screenshots/Opening.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 2_Training/Screenshots/VoTT_Export.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 2_Training/Screenshots/VoTT_Export_Settings.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 2_Training/Screenshots/VoTT_Houses.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 2_Training/Screenshots/VoTT_Save.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading