Guest User

Untitled

a guest
Jul 19th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.36 KB | None | 0 0
  1. import tensorflow as tf
  2.  
  3. from PIL import Image
  4. import json
  5. import os
  6. import tensorflow as tf
  7.  
  8. from object_detection.utils import dataset_util
  9.  
  10.  
  11. flags = tf.app.flags
  12. flags.DEFINE_string('output_path', 'data/tfrecords/train.record', 'Path to output TFRecord')
  13. flags.DEFINE_string('dataset_path', 'data/train_test/', 'Path to dataset')
  14. flags.DEFINE_string('images_directory', 'formatted_train/', 'directory of images inside dataset directory')
  15. flags.DEFINE_string('boxes_filepath', 'formatted_train.json', 'json filepath inside dataset directory')
  16.  
  17. FLAGS = flags.FLAGS
  18.  
  19. IMAGES_DIRECTORY = FLAGS.dataset_path + FLAGS.images_directory
  20. BOXES_PATH = FLAGS.dataset_path + FLAGS.boxes_filepath
  21.  
  22. label_map = {
  23. "signature": 1,
  24. "paraphe": 2,
  25. "coche": 3
  26. }
  27.  
  28.  
  29. def create_tf_example(frame, image_name):
  30. with Image.open(IMAGES_DIRECTORY + image_name) as img:
  31. img_weight, img_height = img.size
  32. height = img_height # Image height
  33. width = img_weight # Image width
  34. filename = image_name # Filename of the image. Empty if image is not from file
  35. encoded_image_data = tf.gfile.FastGFile(IMAGES_DIRECTORY + filename, 'rb').read()
  36. image_format = b'png' # b'jpeg' or b'png'
  37.  
  38. print("Width: {}".format(width))
  39. print("Height: {}".format(height))
  40.  
  41. xmins = [] # List of normalized left x coordinates in bounding box (1 per box)
  42. xmaxs = [] # List of normalized right x coordinates in bounding box
  43. # (1 per box)
  44. ymins = [] # List of normalized top y coordinates in bounding box (1 per box)
  45. ymaxs = [] # List of normalized bottom y coordinates in bounding box
  46. # (1 per box)
  47. classes_text = [] # List of string class name of bounding box (1 per box)
  48. classes = [] # List of integer class id of bounding box (1 per box)
  49.  
  50. for box in frame:
  51. current_xmin = box["x1"] / box["width"]
  52. current_xmax = box["x2"] / box["width"]
  53. current_ymin = box["y1"] / box["height"]
  54. current_ymax = box["y2"] / box["height"]
  55. current_class_text = box["tags"][0]
  56. current_class = label_map[current_class_text]
  57.  
  58. print("Processing bounding box...")
  59. print("Xmin: {}".format(current_xmin))
  60. print("Xmax: {}".format(current_xmax))
  61. print("ymin: {}".format(current_ymin))
  62. print("ymax: {}".format(current_ymax))
  63. print("Class text: {}".format(current_class_text))
  64. print("Class: {}".format(current_class))
  65. print()
  66.  
  67. xmins.append(current_xmin)
  68. xmaxs.append(current_xmax)
  69. ymins.append(current_ymin)
  70. ymaxs.append(current_ymax)
  71. classes_text.append(current_class_text.encode("utf8"))
  72. classes.append(current_class)
  73.  
  74. tf_example = tf.train.Example(features=tf.train.Features(feature={
  75. 'image/height': dataset_util.int64_feature(height),
  76. 'image/width': dataset_util.int64_feature(width),
  77. 'image/filename': dataset_util.bytes_feature(filename.encode("utf8")),
  78. 'image/source_id': dataset_util.bytes_feature(filename.encode("utf8")),
  79. 'image/encoded': dataset_util.bytes_feature(encoded_image_data),
  80. 'image/format': dataset_util.bytes_feature(image_format),
  81. 'image/object/bbox/xmin': dataset_util.float_list_feature(xmins),
  82. 'image/object/bbox/xmax': dataset_util.float_list_feature(xmaxs),
  83. 'image/object/bbox/ymin': dataset_util.float_list_feature(ymins),
  84. 'image/object/bbox/ymax': dataset_util.float_list_feature(ymaxs),
  85. 'image/object/class/text': dataset_util.bytes_list_feature(classes_text),
  86. 'image/object/class/label': dataset_util.int64_list_feature(classes),
  87. }))
  88. return tf_example
  89.  
  90.  
  91. def main(_):
  92. writer = tf.python_io.TFRecordWriter(FLAGS.output_path)
  93.  
  94. image_list = os.listdir(IMAGES_DIRECTORY)
  95. full_frame_data = json.load(open(BOXES_PATH, "r"))
  96. examples = []
  97.  
  98. for img_index, img_name in enumerate(image_list):
  99. str_img_index = str(img_index)
  100. if str_img_index in full_frame_data["frames"].keys():
  101. print("Processing image {} with index {}...".format(img_name, img_index))
  102. frame_data = full_frame_data["frames"][str_img_index]
  103. print(frame_data)
  104.  
  105. current_tf_example = create_tf_example(frame_data, img_name)
  106. writer.write(current_tf_example.SerializeToString())
  107.  
  108. writer.close()
  109.  
  110.  
  111. if __name__ == '__main__':
  112. tf.app.run()
  113.  
  114. model {
  115. ssd {
  116. num_classes: 3
  117. image_resizer {
  118. fixed_shape_resizer {
  119. height: 300
  120. width: 300
  121. }
  122. }
  123. feature_extractor {
  124. type: "ssd_mobilenet_v2"
  125. depth_multiplier: 1.0
  126. min_depth: 16
  127. conv_hyperparams {
  128. regularizer {
  129. l2_regularizer {
  130. weight: 3.99999989895e-05
  131. }
  132. }
  133. initializer {
  134. truncated_normal_initializer {
  135. mean: 0.0
  136. stddev: 0.0299999993294
  137. }
  138. }
  139. activation: RELU_6
  140. batch_norm {
  141. decay: 0.999700009823
  142. center: true
  143. scale: true
  144. epsilon: 0.0010000000475
  145. train: true
  146. }
  147. }
  148. use_depthwise: true
  149. }
  150. box_coder {
  151. faster_rcnn_box_coder {
  152. y_scale: 10.0
  153. x_scale: 10.0
  154. height_scale: 5.0
  155. width_scale: 5.0
  156. }
  157. }
  158. matcher {
  159. argmax_matcher {
  160. matched_threshold: 0.5
  161. unmatched_threshold: 0.5
  162. ignore_thresholds: false
  163. negatives_lower_than_unmatched: true
  164. force_match_for_each_row: true
  165. }
  166. }
  167. similarity_calculator {
  168. iou_similarity {
  169. }
  170. }
  171. box_predictor {
  172. convolutional_box_predictor {
  173. conv_hyperparams {
  174. regularizer {
  175. l2_regularizer {
  176. weight: 3.99999989895e-05
  177. }
  178. }
  179. initializer {
  180. truncated_normal_initializer {
  181. mean: 0.0
  182. stddev: 0.0299999993294
  183. }
  184. }
  185. activation: RELU_6
  186. batch_norm {
  187. decay: 0.999700009823
  188. center: true
  189. scale: true
  190. epsilon: 0.0010000000475
  191. train: true
  192. }
  193. }
  194. min_depth: 0
  195. max_depth: 0
  196. num_layers_before_predictor: 0
  197. use_dropout: false
  198. dropout_keep_probability: 0.800000011921
  199. kernel_size: 3
  200. box_code_size: 4
  201. apply_sigmoid_to_scores: false
  202. }
  203. }
  204. anchor_generator {
  205. ssd_anchor_generator {
  206. num_layers: 6
  207. min_scale: 0.20000000298
  208. max_scale: 0.949999988079
  209. aspect_ratios: 1.0
  210. aspect_ratios: 2.0
  211. aspect_ratios: 0.5
  212. aspect_ratios: 3.0
  213. aspect_ratios: 0.333299994469
  214. }
  215. }
  216. post_processing {
  217. batch_non_max_suppression {
  218. score_threshold: 0.300000011921
  219. iou_threshold: 0.600000023842
  220. max_detections_per_class: 100
  221. max_total_detections: 100
  222. }
  223. score_converter: SIGMOID
  224. }
  225. normalize_loss_by_num_matches: true
  226. loss {
  227. localization_loss {
  228. weighted_smooth_l1 {
  229. }
  230. }
  231. classification_loss {
  232. weighted_sigmoid {
  233. }
  234. }
  235. hard_example_miner {
  236. num_hard_examples: 3000
  237. iou_threshold: 0.990000009537
  238. loss_type: CLASSIFICATION
  239. max_negatives_per_positive: 3
  240. min_negatives_per_image: 3
  241. }
  242. classification_weight: 1.0
  243. localization_weight: 1.0
  244. }
  245. }
  246. }
  247. train_config {
  248. batch_size: 24
  249. data_augmentation_options {
  250. random_horizontal_flip {
  251. }
  252. }
  253. data_augmentation_options {
  254. ssd_random_crop {
  255. }
  256. }
  257. optimizer {
  258. rms_prop_optimizer {
  259. learning_rate {
  260. exponential_decay_learning_rate {
  261. initial_learning_rate: 0.00400000018999
  262. decay_steps: 800720
  263. decay_factor: 0.949999988079
  264. }
  265. }
  266. momentum_optimizer_value: 0.899999976158
  267. decay: 0.899999976158
  268. epsilon: 1.0
  269. }
  270. }
  271. fine_tune_checkpoint: "gs://my-bucket/data/model.ckpt"
  272. num_steps: 200000
  273. fine_tune_checkpoint_type: "detection"
  274. }
  275. train_input_reader {
  276. label_map_path: "gs://my-bucket/data/label_map.pbtxt"
  277. tf_record_input_reader {
  278. input_path: "gs://my-bucket/data/train.record"
  279. }
  280. }
  281. eval_config {
  282. num_examples: 8000
  283. max_evals: 10
  284. use_moving_averages: false
  285. }
  286. eval_input_reader {
  287. label_map_path: "gs://my-bucket/data/label_map.pbtxt"
  288. shuffle: false
  289. num_readers: 1
  290. tf_record_input_reader {
  291. input_path: "gs://my-bucket/data/val.record"
  292. }
  293. }
Add Comment
Please, Sign In to add comment