Repository navigation
Expand file tree
/
Copy pathDataLoad.py
More file actions
86 lines (69 loc) · 2.86 KB
/
Copy pathDataLoad.py
File metadata and controls
86 lines (69 loc) · 2.86 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import cv2
import numpy as np
from glob import glob
num_classes = 2
img_height, img_width = 64, 64
def Data_load(DirPath, CLS = None, Framework='Tensorflow'):
"""
ディレクトリ名を画像のラベルとしてLoadする.
"""
xs = [] # 学習用データ
ts = []
paths = []
#if CLS != None:
# 正解ラベルを文字として取得したい場合
#t = [i[i.find('\\'):].strip('\\') for i in glob('../../DataSet/AngleDetection/training/*')]
#cls = dir_path_1[dir_path_1.find('\\'):].strip('\\') # dir_path_1上の文字'\\'以降の文字列をtに代入
for dir_path_1 in glob(DirPath):
print(dir_path_1)
for dir_path_2 in glob(dir_path_1 + '/*'):
for path in glob(dir_path_2 + '/*'):
print(path, 'を読み込みました。')
x = cv2.imread(path)
x = cv2.resize(x, (img_width, img_height)).astype(np.float32)
x /= 255.
xs.append(x)
# 正解ラベルを番号として取得したい場合
if (Framework is 'Tensorflow') or (Framework is 'Keras'):
#t = [0 for _ in range(num_classes)]
#for i, cls in enumerate(CLS):
#if cls in path:
# t[i] = 1
t = float(dir_path_1[dir_path_1.find('\\'):].strip('\\'))
ts.append(t)
elif Framework is 'PyTorch':
t = np.zeros((1))
#t = np.array((i))
#ts = np.r_[ts, t]
paths.append(dir_path_2)
if Framework is 'Tensorflow':
xs = np.array(xs, dtype=np.float32)
ts = np.array(ts, dtype=np.int)
#elif Framework is 'PyTorch': xs = xs.transpose(0, 3, 1, 2)
return xs, ts, paths
def MiniBatch(xs, ts, mb, mbi):
train_ind = np.arange(len(xs)) # 学習dataのインデックス配列
np.random.seed(0)
np.random.shuffle(train_ind)
if mbi + mb > len(xs):
mb_ind = train_ind[mbi:]
np.random.shuffle(train_ind)
mb_ind = np.hstack((mb_ind, train_ind[:(mb-(len(xs)-mbi))]))
mbi = mb - (len(xs) - mbi)
else:
mb_ind = train_ind[mbi: mbi+mb]
mbi += mb
x = xs[mb_ind]
t = ts[mb_ind]
return x, t
if __name__ == "__main__":
#print(glob('../../DataSet/AngleDetection/training/*'))
#print(glob('../DataSet/AngleDetection/training/*'))
xs, ts, paths = Data_load('../DataSet/AngleDetection/training/*', 'Keras')
# xs, ts = TrainingData_load('../../DataSet/AngleDetection/training/*', 'PyTorch')
# t = [i[i.find('\\'):].strip('\\') for i in glob('../../DataSet/AngleDetection/training/*')]
#dir_path_1[dir_path_1.find('\\'):].strip('\\')
print(xs)
print(ts)
x, t = MiniBatch(xs, ts, 8, 0)
print(x)