Repository navigation
Expand file tree
/
Copy pathmakeTFRecord.py
More file actions
67 lines (58 loc) · 2.12 KB
/
Copy pathmakeTFRecord.py
File metadata and controls
67 lines (58 loc) · 2.12 KB
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
62
63
64
65
66
#!/usr/bin/env python
# _*_coding:utf-8_*_
import tensorflow as tf
import random
import os
import cv2
import pandas as pd
# 生成整数型的属性
def _int64_feature(value):
return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
# 生成字符串型的属性
def _bytes_feature(value):
return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
def creatTrainData(HOME_PATH):
json = pd.read_json(os.path.join(HOME_PATH, 'scene_train_annotations.json'))
image_id = json['image_id']
label_id = json['label_id']
filenameTrain = 'TFRecord/train.tfrecords'
writerTrain = tf.python_io.TFRecordWriter(filenameTrain)
i = 0
image_folder = os.path.join(HOME_PATH, 'scene_train_images')
for name in image_id:
path = os.path.join(image_folder, name)
raw = cv2.imread(path)
res = cv2.resize(raw, (229, 229))
data = res.tostring()
example = tf.train.Example(features=tf.train.Features(feature={
'label': _int64_feature(label_id[i]),
'image_raw': _bytes_feature(data)
}))
writerTrain.write(example.SerializeToString())
i += 1
writerTrain.close()
def creatTestData(HOME_PATH):
json = pd.read_json(os.path.join(HOME_PATH, 'scene_validation_annotations.json'))
image_id = json['image_id']
label_id = json['label_id']
filenameTest = 'TFRecord/test.tfrecords'
writerTest = tf.python_io.TFRecordWriter(filenameTest)
i = 0
image_folder = os.path.join(HOME_PATH, 'scene_validation_images')
for name in image_id:
path = os.path.join(image_folder, name)
raw = cv2.imread(path)
res = cv2.resize(raw, (229, 229))
data = res.tostring()
example = tf.train.Example(features=tf.train.Features(feature={
'label': _int64_feature(label_id[i]),
'image_raw': _bytes_feature(data)
}))
writerTest.write(example.SerializeToString())
i += 1
writerTest.close()
if __name__ == '__main__':
creatTestData('data/scene_validation/')
print('test done')
creatTrainData('data/scene_train/')
print('train done')