FL / load.py
PHZane's picture
Update load.py
68366a8
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("=============================")