File size: 8,743 Bytes
2abebee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c747380
 
 
2abebee
 
 
 
68366a8
2abebee
 
 
 
 
 
68366a8
2abebee
 
68366a8
2abebee
 
 
68366a8
2abebee
 
 
 
 
68366a8
2abebee
 
 
 
 
 
 
 
 
 
 
 
 
68366a8
2abebee
68366a8
2abebee
68366a8
2abebee
 
 
 
 
 
 
 
 
68366a8
2abebee
 
 
 
 
68366a8
2abebee
 
 
 
 
 
68366a8
2abebee
 
 
 
 
 
 
 
 
68366a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2abebee
 
 
 
 
 
 
 
 
 
 
 
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import torch
import pandas as pd
from sklearn.model_selection import train_test_split
class GetDataSet(object):
    def __init__(self):
        self.train_data = None
        self.train_label = None
        self.test_data = None
        self.test_label = None
        self.copdDataSetConstruct()

    def copdDataSetConstruct(self):
        data = pd.read_csv('408-h2.csv',encoding='gbk')

        x = data.drop(['id','level'], axis=1)

        y = data['level']

        x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.4, random_state=5)  # random_state 随机种子
        #print(x_train)
        #print(y_train)
        x_tr = x_train.loc[:].values
        y_tr = y_train.loc[:].values
        x_te = x_test.loc[:].values
        y_te = y_test.loc[:].values

        self.train_data = x_tr
        self.train_label = y_tr
        self.test_data = x_te
        self.test_label = y_te
class Neuro_net(torch.nn.Module):
    def __init__(self):
        super(Neuro_net, self).__init__()
        self.layer1 = torch.nn.Linear(40, 20)
        self.layer2 = torch.nn.Linear(20, 10)
        self.layer3 = torch.nn.Linear(10, 5)
        self.layer4 = torch.nn.Linear(5, 2)
        self.layer5 = torch.nn.Softmax(dim=0)

    def forward(self, input):
        tensor = torch.relu(self.layer1(input))
        tensor = torch.relu(self.layer2(tensor))
        tensor = torch.relu(self.layer3(tensor))
        tensor = self.layer4(tensor)
        tensor = self.layer5(tensor)
        return tensor

def load_model(fl,input):
    # data_test_h1 = GetDataSet()
    # data_test_h1.test_data[0] = input
    # input = data_test_h1.test_data
    if fl == "Yes":
        model = torch.load("net_gb.pt")
        print(fl)
        print(input)
        # input = torch.from_numpy(input).to(torch.float32)
        # model.eval()
        with torch.no_grad():
            # output = model(torch.tensor([0.0,0.0,87.0,5.0,36.9,81.0,33.0,138.0,62.0,0.0,0.0,2.0,2.0,2.0,0.0,0.0,0.0,0.0,0.0,2.0,1.0,0.0,0.0,1.0,2.0,2.0,2.0,1.0,0.0,0.0,0.0,1.0,1.0,0.0,0.0,0.0,0.0,2.0,2.0,1.0]))
            output = model(torch.tensor(input))
        print(output)
        # print(len([0.0,0.0,87.0,5.0,36.9,81.0,33.0,138.0,62.0,0.0,0.0,2.0,2.0,2.0,0.0,0.0,0.0,0.0,0.0,2.0,1.0,0.0,0.0,1.0,2.0,2.0,2.0,1.0,0.0,0.0,0.0,1.0,1.0,0.0,0.0,0.0,0.0,2.0,2.0,1.0]))
        pridect_h1_y = torch.max(output,dim=0)[1]
        pridect_h1_label = pridect_h1_y.data.numpy()
        print(pridect_h1_y)
        if int(pridect_h1_label)==1:
            return "FL predict: Height."
        else:
            return "FL predict: Low."
    else:#55 60
        model_h1 = torch.load("net_h1.pt")
        model_h2 = torch.load("net_h2.pt")
        model_h3 = torch.load("net_h3.pt")
        print(fl)
        print(input)
        # input = torch.from_numpy(input).to(torch.float32)
        model_h1.eval()
        model_h2.eval()
        model_h3.eval()
        with torch.no_grad():
            # output = model(torch.tensor(1.0,1.0,64.0,2.0,37.1,98.0,20.0,120.0,70.0,3.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,2.0,1.0,0.0,1.0,2.0,2.0,1.0,2.0,0.0,2.0,0.0,2.0,2.0,0.0,0.0,0.0,0.0,0.0,1.0,1.0,1.0))
            output_h1 = model_h1(torch.tensor(input))
            output_h2 = model_h2(torch.tensor(input))
            output_h3 = model_h3(torch.tensor(input))
        print(output_h1)
        print(output_h2)
        print(output_h3)
        # print(len([0.0,0.0,87.0,5.0,36.9,81.0,33.0,138.0,62.0,0.0,0.0,2.0,2.0,2.0,0.0,0.0,0.0,0.0,0.0,2.0,1.0,0.0,0.0,1.0,2.0,2.0,2.0,1.0,0.0,0.0,0.0,1.0,1.0,0.0,0.0,0.0,0.0,2.0,2.0,1.0]))
        # print(len(output_h1))
        pridect_h1_y = torch.max(output_h1,dim = 0)[1]
        pridect_h1_label = pridect_h1_y.data.numpy()
        pridect_h2_y = torch.max(output_h2,dim = 0)[1]
        pridect_h2_label = pridect_h2_y.data.numpy()
        pridect_h3_y = torch.max(output_h3,dim = 0)[1]
        pridect_h3_label = pridect_h3_y.data.numpy()

        # print(pridect_h1_y)
        # print(pridect_h2_y)
        # print(pridect_h3_y)
        print(pridect_h1_label)
        print(pridect_h2_label)
        print(pridect_h3_label)
        output = ""
        if int(pridect_h1_label) == 1:
            print("sick")
            output +="H1 predict: Height.\n"
        else:
            print("no sick")
            output += "H1 predict: Low.\n"
        if int(pridect_h2_label) == 1:
            print("sick")
            output += "H2 predict: Height.\n"

        else:
            print("no sick")
            output += "H2 predict: Low.\n"
        if int(pridect_h3_label) == 1:
            print("sick")
            output += "H3 predict: Height.\n"

        else:
            print("no sick")
            output += "H3 predict: Low.\n"
        return output


# def load_model(fl,input):
#     # data_test_h1 = GetDataSet()
#     # data_test_h1.test_data[0] = input
#     # input = data_test_h1.test_data
#     if fl == "Yes":
#         model = torch.load("net_gb.pt")
#         print(fl)
#         print(input)
#         input = torch.from_numpy(input).to(torch.float32)
#         # model.eval()
#         with torch.no_grad():
#             # output = model(torch.tensor([0.0,0.0,87.0,5.0,36.9,81.0,33.0,138.0,62.0,0.0,0.0,2.0,2.0,2.0,0.0,0.0,0.0,0.0,0.0,2.0,1.0,0.0,0.0,1.0,2.0,2.0,2.0,1.0,0.0,0.0,0.0,1.0,1.0,0.0,0.0,0.0,0.0,2.0,2.0,1.0]))
#             output = model(torch.tensor(input))
#         print(output)
#         # print(len([0.0,0.0,87.0,5.0,36.9,81.0,33.0,138.0,62.0,0.0,0.0,2.0,2.0,2.0,0.0,0.0,0.0,0.0,0.0,2.0,1.0,0.0,0.0,1.0,2.0,2.0,2.0,1.0,0.0,0.0,0.0,1.0,1.0,0.0,0.0,0.0,0.0,2.0,2.0,1.0]))
#         pridect_h1_y = torch.max(output,dim=1)[1]
        
#         pridect_h1_label = pridect_h1_y.data.numpy()
#         print(pridect_h1_y)
#         if int(pridect_h1_label[0])==1:
#             return "FL predict: Height."
#         else:
#             return "FL predict: Low."
#     else:
#         model_h1 = torch.load("net_h1.pt")
#         model_h2 = torch.load("net_h2.pt")
#         model_h3 = torch.load("net_h3.pt")
#         print(fl)
#         print(input)
#         input = torch.from_numpy(input).to(torch.float32)
#         model_h1.eval()
#         model_h2.eval()
#         model_h3.eval()
#         with torch.no_grad():
#             # output = model(torch.tensor(1.0,1.0,64.0,2.0,37.1,98.0,20.0,120.0,70.0,3.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,2.0,1.0,0.0,1.0,2.0,2.0,1.0,2.0,0.0,2.0,0.0,2.0,2.0,0.0,0.0,0.0,0.0,0.0,1.0,1.0,1.0))
#             output_h1 = model_h1(torch.tensor(input))
#             output_h2 = model_h2(torch.tensor(input))
#             output_h3 = model_h3(torch.tensor(input))
#         print(output_h1)
#         print(output_h2)
#         print(output_h3)
#         # print(len([0.0,0.0,87.0,5.0,36.9,81.0,33.0,138.0,62.0,0.0,0.0,2.0,2.0,2.0,0.0,0.0,0.0,0.0,0.0,2.0,1.0,0.0,0.0,1.0,2.0,2.0,2.0,1.0,0.0,0.0,0.0,1.0,1.0,0.0,0.0,0.0,0.0,2.0,2.0,1.0]))
#         # print(len(output_h1))
#         pridect_h1_y = torch.max(output_h1,dim = 1)[1]
#         pridect_h1_label = pridect_h1_y.data.numpy()
#         pridect_h2_y = torch.max(output_h2,dim = 1)[1]
#         pridect_h2_label = pridect_h2_y.data.numpy()
#         pridect_h3_y = torch.max(output_h3,dim = 1)[1]
#         pridect_h3_label = pridect_h3_y.data.numpy()

#         # print(pridect_h1_y)
#         # print(pridect_h2_y)
#         # print(pridect_h3_y)
#         print(pridect_h1_label)
#         print(pridect_h2_label)
#         print(pridect_h3_label)
#         output = ""
#         if int(pridect_h1_label[0]) == 1:
#             print("sick")
#             output +="H1 predict: Height.\n"
#         else:
#             print("no sick")
#             output += "H1 predict: Low.\n"
#         if int(pridect_h2_label[0]) == 1:
#             print("sick")
#             output += "H2 predict: Height.\n"

#         else:
#             print("no sick")
#             output += "H2 predict: Low.\n"
#         if int(pridect_h3_label[0]) == 1:
#             print("sick")
#             output += "H3 predict: Height.\n"

#         else:
#             print("no sick")
#             output += "H3 predict: Low.\n"
#         return output



# print(data_test_h1.train_data[0])
# print(len(data_test_h1.train_data))

# test_h1_x = torch.from_numpy(data_test_h1.test_data).float()
# test_h1_y = torch.tensor(data_test_h1.test_label)
# a = [1.0,1.0,78.0,7.0,37.3,110.0,21.0,130.0,81.0,3.0,0.0,2.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,1.0,2.0,0.0,2.0,1.0,1.0,2.0,2.0,1.0,0.0,0.0,0.0,2.0,2.0,1.0,2.0,1.0,1.0,1.0,2.0,1.0]
# data_test_h1.train_data[0] = a
# print(data_test_h1.train_data[25])
# print(load_model("No",data_test_h1.train_data))
# print(load_model("Yes",data_test_h1.train_data))
# print("=============================")