-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
194 lines (158 loc) · 6.61 KB
/
utils.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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
from sklearn import model_selection
import torch.nn as nn
from torchvision import transforms
import numpy as np
import torch
import pandas as pd
import os
size_of_test = 0.1 #测试集的占比
random_state = 0
pwd = os.getcwd() # 当前目录
"""准备数据"""
def prepare_data(namelist,istransform=False):
"""加载铆压机的数据集"""
feature = []
label = []
"""构造原始数据集(1:OK;0:NOK)"""
for i in range(0,len(namelist)):
Ndir = os.path.join(pwd+'/data', namelist[i]);
csv_path = Ndir # 此处要将“\”替换为“/”
os.chdir(os.path.dirname(csv_path)) # 用os库改变目录
data = pd.read_csv(os.path.basename(csv_path), header=11, usecols=[6, 7, 10], #取特定列的数据
error_bad_lines=False) #用os.path.basename(csv_path)获取文件名
'''判断是不是ok的'''
labeljudge = pd.read_csv(os.path.basename(csv_path),header=0,usecols=[1],
error_bad_lines=False) #用os.path.basename(csv_path)获取文件名
judge = labeljudge.values.tolist() # 转换成列表
if judge[4][0]=="OK":
label.append(1)
else:
label.append(0)
exampleData = data.values.tolist() # 转换成列表
np.concatenate(exampleData, axis=0)
#print(namelist[i])
X = list()
for i in range(-257, -1): # 左开右闭
X.append(exampleData[i])
feature.append(X)
feature = np.array(feature)
if istransform:
print("use transform")
transform = transforms.Compose(
[transforms.ToTensor(), transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])])
feature = transform(feature).div(255).permute(1, 2, 0).numpy()
label = np.array(label)
return feature, label
"""对输入的单个样本数据进行处理"""
def process_data(file_path, istransform=False):
feature = []
Ndir = os.path.join(pwd+'/data', file_path);
csv_path = Ndir # 此处要将“\”替换为“/”
os.chdir(os.path.dirname(csv_path)) # 用os库改变目录
data = pd.read_csv(os.path.basename(csv_path), header=11, usecols=[6, 7, 10], #取特定列的数据
error_bad_lines=False) #用os.path.basename(csv_path)获取文件名
exampleData = data.values.tolist() # 转换成列表
np.concatenate(exampleData, axis=0)
X = list()
for i in range(-257, -1): # 左开右闭
X.append(exampleData[i])
feature.append(X)
feature = np.array(feature)
if istransform:
print("use transform")
transform = transforms.Compose(
[transforms.ToTensor(), transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])])
feature = transform(feature).div(255).permute(1, 2, 0).numpy()
return feature
"""构建训练集类"""
class train_mini_train():
def __init__(self,feature, label, istransform=False):
# 加载数据集
self.X, self.y = \
feature,label
print('样本总数:', len(self.X))
print('样本特征维度:', len(self.X[0]))
# 数据集切分
self.X_train, self.X_test, self.y_train, self.y_test = \
model_selection.train_test_split(self.X, self.y, test_size=size_of_test,random_state=random_state)
if istransform:
print("use transform")
transform = transforms.Compose(
[transforms.ToTensor(), transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])])
self.X_train = transform(self.X_train).div(255).permute(1,2,0).numpy()
print('构建训练集样本总数:', len(self.y_train))
def __len__(self):
# 返回训练集数据量
return len(self.y_train)
def __getitem__(self, index):
return torch.tensor(self.X_train[index].reshape(12, 8, 8), dtype=torch.float32), self.y_train[index]
"""构建测试集类"""
class test_mini_test():
def __init__(self, feature, label, istransform=False):
self.X, self.y = feature, label
self.X_train, self.X_test, self.y_train, self.y_test = \
model_selection.train_test_split(self.X, self.y, test_size=size_of_test, random_state=random_state)
if istransform:
print("use transform")
transform = transforms.Compose(
[transforms.ToTensor(), transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])])
self.X_test = transform(self.X_test).div(255).permute(1,2,0).numpy()
print('构建测试集样本总数:', len(self.y_test))
def __getitem__(self, index):
return torch.tensor(self.X_test[index].reshape(12, 8, 8), dtype=torch.float32), self.y_test[index]
def __len__(self):
return len(self.y_test)
"""定义神经网络结构类"""
class SimpleNet(nn.Module):
def __init__(self):
super(SimpleNet, self).__init__()
self.conv1 = nn.Sequential( # (12, 8, 8)
nn.Conv2d(in_channels=12, out_channels=4, kernel_size=2, stride=1, padding=1), # (4, 8, 8)
)
self.conv2 = nn.Sequential(
nn.ReLU(), # (4, 8, 8)
)
self.conv3 = nn.Sequential(
nn.MaxPool2d(kernel_size=2) # (4,4,4) 不改变通道数
)
self.conv4 = nn.Sequential(
nn.Conv2d(in_channels=4, out_channels=8, kernel_size=2, stride=1, padding=1), # (8,5,5)
)
self.conv5 = nn.Sequential(
nn.ReLU(), # (8,5,5)
)
self.conv6 = nn.Sequential(
nn.MaxPool2d(kernel_size=2) # (8,2,2)
)
self.fc = nn.Linear(8 * 2 * 2, 2) # (10)
def forward(self, x):
x = self.conv1(x)
x = self.conv2(x)
x = self.conv3(x)
x = self.conv4(x)
x = self.conv5(x)
x = self.conv6(x)
x = x.view(x.size(0), -1) # 相当于Flatten
x = self.fc(x)
return x
"""验证时计算精度"""
def eval_on_dataloader(name, loader, len, net):
acc = 0.0
with torch.no_grad():
for data in loader:
datas, labels = data
outputs = net(datas)
# torch.max返回两个数值,一个[0]是最大值,一个[1]是最大值的下标
predict_y = torch.max(outputs, dim=1)[1]
#print(predict_y)
acc += (predict_y == labels.long()).sum().item()
accurate = acc / len
return accurate
"""测试时计算精度"""
def eval_on_output(labels, outputs):
acc = 0.0
predict = torch.max(outputs, dim=1)[1]
#print(predict)
acc += (predict == labels.long()).sum().item()
accurate = acc / len(labels)
return accurate