我正在使用线程化方法使一台服务器具有多个客户端。我正在使用带有resnet50的预训练模型来预测某些东西。 我的预测代码是
def prediction(array):
array = np.array(array , dtype = np.float64)
array = np.reshape(array, (1,224,224,3))
#print(array.shape)
a = preprocess_input(array)
model = ResNet50(weights='imagenet', include_top=False)
features = model.predict(a)
#print(features.shape)
image = features.reshape(features.shape[0] , -1)
#print(image.shape)
loaded_model = keras.models.load_model('RESNET50.h5')
y_predict = loaded_model.predict(image)
if y_predict[0][0] > y_predict[0][1]:
return "Non-Nude"
else:
return "Nude"
K.clear_session()
我的服务器代码是这个
class ClientThread(Thread):
def __init__(self,ip,port,sock):
Thread.__init__(self)
self.ip = ip
self.port = port
self.sock = sock
print("New thread started for "+ip+":"+str(port))
def run(self):
send_one_message(self.sock, self.ip)
send_one_message(self.sock, self.port)
data = recv_one_message(self.sock)
#print("data recevied")
res = prediction(data)
send_one_message(self.sock, res)
TCP_IP = ''
TCP_PORT = 12001
tcpsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcpsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
tcpsock.bind((TCP_IP, TCP_PORT))
threads = []
while True:
tcpsock.listen(5)
print("Waiting for incoming connections...")
(conn, (ip,port)) = tcpsock.accept()
print('Got connection from ', (ip,port))
newthread = ClientThread(ip,port,conn)
newthread.start()
threads.append(newthread)
for t in threads:
t.join()
它能够连接多个客户端,但是它仅对连接的第一个客户端和每个其他客户端提供良好的预测,因为
感谢您的帮助!