-
Notifications
You must be signed in to change notification settings - Fork 1
/
weights_coverter.py
61 lines (45 loc) · 1.77 KB
/
weights_coverter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import os
import tensorflow as tf
import numpy as np
from resources.models.yolov3_tiny import YoloV3Tiny
from resources.models.utils import load_darknet_weights
import requests
# Downloading weights from URL
weights = requests.get("https://pjreddie.com/media/files/yolov3-tiny.weights")
base_dir = os.getcwd()
weights_folder_path = os.path.join(base_dir, 'data/yolo_tiny/weights/')
# Checking if folder is created, if no than flash error
def create_folder(directory):
try:
if not os.path.exists(directory):
print(":creating folder")
os.makedirs(directory)
else:
print('path exists')
except:
print('Error: Creating weights directory' + directory)
create_folder(weights_folder_path)
# Add weights file to the directory
with open(weights_folder_path + "yolov3-tiny.weights", 'wb') as pickle_file:
pickle_file.write(weights.content)
initial_weights_file_path = os.path.join(base_dir, 'data/yolo_tiny/weights/yolov3-tiny.weights')
converted_weights_file_path = os.path.join(base_dir, 'data/yolo_tiny/weights/yolov3-tiny.tf')
def main():
physical_devices = tf.config.experimental.list_physical_devices('GPU')
if len(physical_devices) > 0:
tf.config.experimental.set_memory_growth(physical_devices[0], True)
yolo = YoloV3Tiny(classes=80)
yolo.summary()
print('model created')
load_darknet_weights(yolo, initial_weights_file_path)
print('weights loaded')
img = np.random.random((1, 320, 320, 3)).astype(np.float32)
output = yolo(img)
print('sanity check passed')
yolo.save_weights(converted_weights_file_path)
print('weights saved')
if __name__ == '__main__':
try:
main()
except SystemExit:
pass