diff --git a/README.md b/README.md new file mode 100644 index 0000000..76f02e0 --- /dev/null +++ b/README.md @@ -0,0 +1,28 @@ +# 4296_gp + +### prerequisite + +``` +pip install -r requirements.txt +``` + +### Usage + +Cloud computing + +``` +mv src/ cloud_computing/server +mv image/ cloud_computing/client +python3 mserver.py -ip xx +python3 mclient.py -ip xx +``` + +Edge computing + +``` +mv src/ edge_computing/server +mv image/ edge_computing/client +python3 mserver.py -ip 0.0.0.0 +python3 mclient.py -ip xx +``` + diff --git a/cloud_computing/client/mclient.py b/cloud_computing/client/mclient.py new file mode 100644 index 0000000..5200a9b --- /dev/null +++ b/cloud_computing/client/mclient.py @@ -0,0 +1,54 @@ +import socket +import zipfile +import os +import argparse +import time + +parser = argparse.ArgumentParser() +parser.add_argument('-ip', type=str, help='ip address', default="127.0.0.1") +args = parser.parse_args() + +ip_port = (args.ip, 9999) +s = socket.socket() +s.connect(ip_port) + + +def zip_and_transfer(path='images', zip_name='tmp.zip'): + with zipfile.ZipFile(zip_name, 'w') as file: + for fn in os.listdir(path): + file.write(os.path.join(path, fn)) + filesize = str(os.path.getsize('tmp.zip')) + f = open('tmp.zip', 'rb') + l = f.read() + s.sendall(l) + + +def receive_and_unzip(): + filename = "tmp.zip" + f = open(filename, 'wb') + client_data = s.recv(1024) + total = 0 + while (client_data): + f.write(client_data) + total += len(client_data) + client_data = s.recv(1024) + f.close() + with zipfile.ZipFile(filename, 'r') as file: + print('[+] Extracting files...') + file.extractall() + print('[+] Done') + + +start_time = time.time() +zip_and_transfer() +s.close() + +s = socket.socket() +s.connect(ip_port) +server_reply = s.recv(1024).decode('utf-8') +print(server_reply) + +receive_and_unzip() +print("total time: ", time.time() - start_time) +os.remove("tmp.zip") +s.close() diff --git a/cloud_computing/server/mserver.py b/cloud_computing/server/mserver.py new file mode 100644 index 0000000..d17ee9d --- /dev/null +++ b/cloud_computing/server/mserver.py @@ -0,0 +1,78 @@ +import socket +import zipfile +import torch +import os +import shutil +import argparse +import time + +parser = argparse.ArgumentParser() +parser.add_argument('-ip', type=str, help='ip address',default="127.0.0.1") +args = parser.parse_args() + + +def zip_and_transfer(path = 'images',zip_name = 'tmp.zip'): + with zipfile.ZipFile(zip_name, 'w') as file: + for fn in os.listdir(path): + file.write(os.path.join(path,fn)) + filesize = str(os.path.getsize('tmp.zip')) + print(filesize) + f = open('tmp.zip', 'rb') + l = f.read() + conn.sendall(l) + +def receive_and_unzip(): + print(f"receive message from {str(address)}" ) + filename = "tmp.zip" + f = open(filename, 'wb') + client_data = conn.recv(1024) + total = 0 + while(client_data): + f.write(client_data) + total += len(client_data) + client_data = conn.recv(1024) + f.close() + with zipfile.ZipFile(filename, 'r') as file: + print('[+] Extracting files...') + file.extractall() + print('[+] Done') + +# 1. set up socket +ip_port = (args.ip, 9999) +sk = socket.socket() +sk.bind(ip_port) +sk.listen(5) +print('socket service start,wait for client to connect...') +conn, address = sk.accept() + +# 2. receive transfer images +receive_and_unzip() +conn.close() + +conn, address = sk.accept() +# 3. start inference +model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True) + +imgs = os.listdir('images') # batch of images +imgs = list(map(lambda img: os.path.join('images/', img), imgs)) +imgs = imgs[1:] # remove .DS_Store file + + +# Inference +start_time = time.time() +results = model(imgs[:40]) +print("total computing time for yolo:", time.time() - start_time, 's') + +results.save() +conn.sendall('inference finished'.encode('utf-8')) + +# 4. reply the prediction results +zip_and_transfer(path="runs/detect/exp") + +# 5. do the clear and close the connection +os.remove("tmp.zip") +shutil.rmtree("images") + +conn.close() +sk.close() + diff --git a/cloud_computing/server/yolov5s.pt b/cloud_computing/server/yolov5s.pt new file mode 100644 index 0000000..841108f Binary files /dev/null and b/cloud_computing/server/yolov5s.pt differ diff --git a/client1/client01.py b/distributed_computing/client1/client01.py similarity index 89% rename from client1/client01.py rename to distributed_computing/client1/client01.py index d0a8a95..fee7a50 100644 --- a/client1/client01.py +++ b/distributed_computing/client1/client01.py @@ -4,9 +4,14 @@ import shutil import torch import time +import argparse -TCP_IP = 'localhost' -TCP_PORT = 9001 +parser = argparse.ArgumentParser() +parser.add_argument('-ip', type=str, help='ip address', default="127.0.0.1") +args = parser.parse_args() + +TCP_IP = args.ip +TCP_PORT = 9999 BUFFER_SIZE = 1024 diff --git a/client1/yolov5s.pt b/distributed_computing/client1/yolov5s.pt similarity index 100% rename from client1/yolov5s.pt rename to distributed_computing/client1/yolov5s.pt diff --git a/client2/client02.py b/distributed_computing/client2/client02.py similarity index 89% rename from client2/client02.py rename to distributed_computing/client2/client02.py index 68819de..cb8d253 100644 --- a/client2/client02.py +++ b/distributed_computing/client2/client02.py @@ -4,9 +4,14 @@ import shutil import torch import time +import argparse -TCP_IP = 'localhost' -TCP_PORT = 9001 +parser = argparse.ArgumentParser() +parser.add_argument('-ip', type=str, help='ip address', default="127.0.0.1") +args = parser.parse_args() + +TCP_IP = args.ip +TCP_PORT = 9999 BUFFER_SIZE = 1024 diff --git a/client2/yolov5s.pt b/distributed_computing/client2/yolov5s.pt similarity index 100% rename from client2/yolov5s.pt rename to distributed_computing/client2/yolov5s.pt diff --git a/server/server.py b/distributed_computing/server/server.py similarity index 93% rename from server/server.py rename to distributed_computing/server/server.py index be764e3..171e4c7 100644 --- a/server/server.py +++ b/distributed_computing/server/server.py @@ -3,9 +3,14 @@ import zipfile import os import time +import argparse -TCP_IP = 'localhost' -TCP_PORT = 9001 +parser = argparse.ArgumentParser() +parser.add_argument('-ip', type=str, help='ip address', default="127.0.0.1") +args = parser.parse_args() + +TCP_IP = args.ip +TCP_PORT = 9999 BUFFER_SIZE = 1024 diff --git a/edge_computing/client/mclient.py b/edge_computing/client/mclient.py new file mode 100644 index 0000000..5200a9b --- /dev/null +++ b/edge_computing/client/mclient.py @@ -0,0 +1,54 @@ +import socket +import zipfile +import os +import argparse +import time + +parser = argparse.ArgumentParser() +parser.add_argument('-ip', type=str, help='ip address', default="127.0.0.1") +args = parser.parse_args() + +ip_port = (args.ip, 9999) +s = socket.socket() +s.connect(ip_port) + + +def zip_and_transfer(path='images', zip_name='tmp.zip'): + with zipfile.ZipFile(zip_name, 'w') as file: + for fn in os.listdir(path): + file.write(os.path.join(path, fn)) + filesize = str(os.path.getsize('tmp.zip')) + f = open('tmp.zip', 'rb') + l = f.read() + s.sendall(l) + + +def receive_and_unzip(): + filename = "tmp.zip" + f = open(filename, 'wb') + client_data = s.recv(1024) + total = 0 + while (client_data): + f.write(client_data) + total += len(client_data) + client_data = s.recv(1024) + f.close() + with zipfile.ZipFile(filename, 'r') as file: + print('[+] Extracting files...') + file.extractall() + print('[+] Done') + + +start_time = time.time() +zip_and_transfer() +s.close() + +s = socket.socket() +s.connect(ip_port) +server_reply = s.recv(1024).decode('utf-8') +print(server_reply) + +receive_and_unzip() +print("total time: ", time.time() - start_time) +os.remove("tmp.zip") +s.close() diff --git a/edge_computing/server/mserver.py b/edge_computing/server/mserver.py new file mode 100644 index 0000000..49789e0 --- /dev/null +++ b/edge_computing/server/mserver.py @@ -0,0 +1,78 @@ +import socket +import zipfile +import os +import shutil +import argparse +import torch +import time + +parser = argparse.ArgumentParser() +parser.add_argument('-ip', type=str, help='ip address', default="127.0.0.1") +args = parser.parse_args() + + +def zip_and_transfer(path='image', zip_name='tmp.zip'): + with zipfile.ZipFile(zip_name, 'w') as file: + for fn in os.listdir(path): + file.write(os.path.join(path, fn)) + filesize = str(os.path.getsize('tmp.zip')) + print(filesize) + f = open('tmp.zip', 'rb') + l = f.read() + conn.sendall(l) + + +def receive_and_unzip(): + print(f"receive message from {str(address)}") + filename = "tmp.zip" + f = open(filename, 'wb') + client_data = conn.recv(1024) + total = 0 + while (client_data): + f.write(client_data) + total += len(client_data) + client_data = conn.recv(1024) + f.close() + with zipfile.ZipFile(filename, 'r') as file: + print('[+] Extracting files...') + file.extractall() + print('[+] Done') + + +# 1. set up socket +ip_port = (args.ip, 9999) +sk = socket.socket() +sk.bind(ip_port) +sk.listen(5) +print('socket service start,wait for client to connect...') +conn, address = sk.accept() + +# 2. receive transfer images +receive_and_unzip() +conn.close() + +conn, address = sk.accept() +# 3. start inference +model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True) + +imgs = os.listdir('images') # batch of images +imgs = list(map(lambda img: os.path.join('images/', img), imgs)) + + +# Inference +start_time = time.time() +results = model(imgs) +print("total computing time for yolo:", time.time() - start_time, 's') + +results.save() +conn.sendall('inference finished'.encode('utf-8')) + +# 4. reply the prediction results +zip_and_transfer(path="runs/detect/exp") + +# 5. do the clear and close the connection +os.remove("tmp.zip") +shutil.rmtree("images") + +conn.close() +sk.close() diff --git a/edge_computing/server/yolov5s.pt b/edge_computing/server/yolov5s.pt new file mode 100644 index 0000000..841108f Binary files /dev/null and b/edge_computing/server/yolov5s.pt differ diff --git a/server/images/0.jpg b/images/0.jpg similarity index 100% rename from server/images/0.jpg rename to images/0.jpg diff --git a/server/images/1.jpg b/images/1.jpg similarity index 100% rename from server/images/1.jpg rename to images/1.jpg diff --git a/server/images/10.jpg b/images/10.jpg similarity index 100% rename from server/images/10.jpg rename to images/10.jpg diff --git a/server/images/11.jpg b/images/11.jpg similarity index 100% rename from server/images/11.jpg rename to images/11.jpg diff --git a/server/images/12.jpg b/images/12.jpg similarity index 100% rename from server/images/12.jpg rename to images/12.jpg diff --git a/server/images/13.jpg b/images/13.jpg similarity index 100% rename from server/images/13.jpg rename to images/13.jpg diff --git a/server/images/14.jpg b/images/14.jpg similarity index 100% rename from server/images/14.jpg rename to images/14.jpg diff --git a/server/images/15.jpg b/images/15.jpg similarity index 100% rename from server/images/15.jpg rename to images/15.jpg diff --git a/server/images/16.jpg b/images/16.jpg similarity index 100% rename from server/images/16.jpg rename to images/16.jpg diff --git a/server/images/17.jpg b/images/17.jpg similarity index 100% rename from server/images/17.jpg rename to images/17.jpg diff --git a/server/images/18.jpg b/images/18.jpg similarity index 100% rename from server/images/18.jpg rename to images/18.jpg diff --git a/server/images/19.jpg b/images/19.jpg similarity index 100% rename from server/images/19.jpg rename to images/19.jpg diff --git a/server/images/2.jpg b/images/2.jpg similarity index 100% rename from server/images/2.jpg rename to images/2.jpg diff --git a/server/images/20.jpg b/images/20.jpg similarity index 100% rename from server/images/20.jpg rename to images/20.jpg diff --git a/server/images/21.jpg b/images/21.jpg similarity index 100% rename from server/images/21.jpg rename to images/21.jpg diff --git a/server/images/22.jpg b/images/22.jpg similarity index 100% rename from server/images/22.jpg rename to images/22.jpg diff --git a/server/images/23.jpg b/images/23.jpg similarity index 100% rename from server/images/23.jpg rename to images/23.jpg diff --git a/server/images/24.jpg b/images/24.jpg similarity index 100% rename from server/images/24.jpg rename to images/24.jpg diff --git a/server/images/25.jpg b/images/25.jpg similarity index 100% rename from server/images/25.jpg rename to images/25.jpg diff --git a/server/images/26.jpg b/images/26.jpg similarity index 100% rename from server/images/26.jpg rename to images/26.jpg diff --git a/server/images/28.jpg b/images/28.jpg similarity index 100% rename from server/images/28.jpg rename to images/28.jpg diff --git a/server/images/29.jpg b/images/29.jpg similarity index 100% rename from server/images/29.jpg rename to images/29.jpg diff --git a/server/images/3.jpg b/images/3.jpg similarity index 100% rename from server/images/3.jpg rename to images/3.jpg diff --git a/server/images/30.jpg b/images/30.jpg similarity index 100% rename from server/images/30.jpg rename to images/30.jpg diff --git a/server/images/31.jpg b/images/31.jpg similarity index 100% rename from server/images/31.jpg rename to images/31.jpg diff --git a/server/images/32.jpg b/images/32.jpg similarity index 100% rename from server/images/32.jpg rename to images/32.jpg diff --git a/server/images/33.jpg b/images/33.jpg similarity index 100% rename from server/images/33.jpg rename to images/33.jpg diff --git a/server/images/34.jpg b/images/34.jpg similarity index 100% rename from server/images/34.jpg rename to images/34.jpg diff --git a/server/images/36.jpg b/images/36.jpg similarity index 100% rename from server/images/36.jpg rename to images/36.jpg diff --git a/server/images/37.jpg b/images/37.jpg similarity index 100% rename from server/images/37.jpg rename to images/37.jpg diff --git a/server/images/38.jpg b/images/38.jpg similarity index 100% rename from server/images/38.jpg rename to images/38.jpg diff --git a/server/images/39.jpg b/images/39.jpg similarity index 100% rename from server/images/39.jpg rename to images/39.jpg diff --git a/server/images/4.jpg b/images/4.jpg similarity index 100% rename from server/images/4.jpg rename to images/4.jpg diff --git a/server/images/40.jpg b/images/40.jpg similarity index 100% rename from server/images/40.jpg rename to images/40.jpg diff --git a/server/images/41.jpg b/images/41.jpg similarity index 100% rename from server/images/41.jpg rename to images/41.jpg diff --git a/server/images/42.jpg b/images/42.jpg similarity index 100% rename from server/images/42.jpg rename to images/42.jpg diff --git a/server/images/43.jpg b/images/43.jpg similarity index 100% rename from server/images/43.jpg rename to images/43.jpg diff --git a/server/images/44.jpg b/images/44.jpg similarity index 100% rename from server/images/44.jpg rename to images/44.jpg diff --git a/server/images/45.jpg b/images/45.jpg similarity index 100% rename from server/images/45.jpg rename to images/45.jpg diff --git a/server/images/46.jpg b/images/46.jpg similarity index 100% rename from server/images/46.jpg rename to images/46.jpg diff --git a/server/images/47.jpg b/images/47.jpg similarity index 100% rename from server/images/47.jpg rename to images/47.jpg diff --git a/server/images/48.jpg b/images/48.jpg similarity index 100% rename from server/images/48.jpg rename to images/48.jpg diff --git a/server/images/49.jpg b/images/49.jpg similarity index 100% rename from server/images/49.jpg rename to images/49.jpg diff --git a/server/images/5.jpg b/images/5.jpg similarity index 100% rename from server/images/5.jpg rename to images/5.jpg diff --git a/server/images/50.jpg b/images/50.jpg similarity index 100% rename from server/images/50.jpg rename to images/50.jpg diff --git a/server/images/51.jpg b/images/51.jpg similarity index 100% rename from server/images/51.jpg rename to images/51.jpg diff --git a/server/images/52.jpg b/images/52.jpg similarity index 100% rename from server/images/52.jpg rename to images/52.jpg diff --git a/server/images/53.jpg b/images/53.jpg similarity index 100% rename from server/images/53.jpg rename to images/53.jpg diff --git a/server/images/54.jpg b/images/54.jpg similarity index 100% rename from server/images/54.jpg rename to images/54.jpg diff --git a/server/images/55.jpg b/images/55.jpg similarity index 100% rename from server/images/55.jpg rename to images/55.jpg diff --git a/server/images/56.jpg b/images/56.jpg similarity index 100% rename from server/images/56.jpg rename to images/56.jpg diff --git a/server/images/57.jpg b/images/57.jpg similarity index 100% rename from server/images/57.jpg rename to images/57.jpg diff --git a/server/images/58.jpg b/images/58.jpg similarity index 100% rename from server/images/58.jpg rename to images/58.jpg diff --git a/server/images/59.jpg b/images/59.jpg similarity index 100% rename from server/images/59.jpg rename to images/59.jpg diff --git a/server/images/6.jpg b/images/6.jpg similarity index 100% rename from server/images/6.jpg rename to images/6.jpg diff --git a/server/images/60.jpg b/images/60.jpg similarity index 100% rename from server/images/60.jpg rename to images/60.jpg diff --git a/server/images/61.jpg b/images/61.jpg similarity index 100% rename from server/images/61.jpg rename to images/61.jpg diff --git a/server/images/62.jpg b/images/62.jpg similarity index 100% rename from server/images/62.jpg rename to images/62.jpg diff --git a/server/images/63.jpg b/images/63.jpg similarity index 100% rename from server/images/63.jpg rename to images/63.jpg diff --git a/server/images/64.jpg b/images/64.jpg similarity index 100% rename from server/images/64.jpg rename to images/64.jpg diff --git a/server/images/65.jpg b/images/65.jpg similarity index 100% rename from server/images/65.jpg rename to images/65.jpg diff --git a/server/images/66.jpg b/images/66.jpg similarity index 100% rename from server/images/66.jpg rename to images/66.jpg diff --git a/server/images/67.jpg b/images/67.jpg similarity index 100% rename from server/images/67.jpg rename to images/67.jpg diff --git a/server/images/68.jpg b/images/68.jpg similarity index 100% rename from server/images/68.jpg rename to images/68.jpg diff --git a/server/images/69.jpg b/images/69.jpg similarity index 100% rename from server/images/69.jpg rename to images/69.jpg diff --git a/server/images/7.jpg b/images/7.jpg similarity index 100% rename from server/images/7.jpg rename to images/7.jpg diff --git a/server/images/70.jpg b/images/70.jpg similarity index 100% rename from server/images/70.jpg rename to images/70.jpg diff --git a/server/images/71.jpg b/images/71.jpg similarity index 100% rename from server/images/71.jpg rename to images/71.jpg diff --git a/server/images/72.jpg b/images/72.jpg similarity index 100% rename from server/images/72.jpg rename to images/72.jpg diff --git a/server/images/73.jpg b/images/73.jpg similarity index 100% rename from server/images/73.jpg rename to images/73.jpg diff --git a/server/images/74.jpg b/images/74.jpg similarity index 100% rename from server/images/74.jpg rename to images/74.jpg diff --git a/server/images/75.jpg b/images/75.jpg similarity index 100% rename from server/images/75.jpg rename to images/75.jpg diff --git a/server/images/76.jpg b/images/76.jpg similarity index 100% rename from server/images/76.jpg rename to images/76.jpg diff --git a/server/images/77.jpg b/images/77.jpg similarity index 100% rename from server/images/77.jpg rename to images/77.jpg diff --git a/server/images/78.jpg b/images/78.jpg similarity index 100% rename from server/images/78.jpg rename to images/78.jpg diff --git a/server/images/79.jpg b/images/79.jpg similarity index 100% rename from server/images/79.jpg rename to images/79.jpg diff --git a/server/images/8.jpg b/images/8.jpg similarity index 100% rename from server/images/8.jpg rename to images/8.jpg diff --git a/server/images/80.jpg b/images/80.jpg similarity index 100% rename from server/images/80.jpg rename to images/80.jpg diff --git a/server/images/81.jpg b/images/81.jpg similarity index 100% rename from server/images/81.jpg rename to images/81.jpg diff --git a/server/images/82.jpg b/images/82.jpg similarity index 100% rename from server/images/82.jpg rename to images/82.jpg diff --git a/server/images/83.jpg b/images/83.jpg similarity index 100% rename from server/images/83.jpg rename to images/83.jpg diff --git a/server/images/84.jpg b/images/84.jpg similarity index 100% rename from server/images/84.jpg rename to images/84.jpg diff --git a/server/images/85.jpg b/images/85.jpg similarity index 100% rename from server/images/85.jpg rename to images/85.jpg diff --git a/server/images/86.jpg b/images/86.jpg similarity index 100% rename from server/images/86.jpg rename to images/86.jpg diff --git a/server/images/87.jpg b/images/87.jpg similarity index 100% rename from server/images/87.jpg rename to images/87.jpg diff --git a/server/images/88.jpg b/images/88.jpg similarity index 100% rename from server/images/88.jpg rename to images/88.jpg diff --git a/server/images/89.jpg b/images/89.jpg similarity index 100% rename from server/images/89.jpg rename to images/89.jpg diff --git a/server/images/9.jpg b/images/9.jpg similarity index 100% rename from server/images/9.jpg rename to images/9.jpg diff --git a/server/images/90.jpg b/images/90.jpg similarity index 100% rename from server/images/90.jpg rename to images/90.jpg diff --git a/server/images/91.jpg b/images/91.jpg similarity index 100% rename from server/images/91.jpg rename to images/91.jpg diff --git a/server/images/92.jpg b/images/92.jpg similarity index 100% rename from server/images/92.jpg rename to images/92.jpg diff --git a/server/images/93.jpg b/images/93.jpg similarity index 100% rename from server/images/93.jpg rename to images/93.jpg diff --git a/server/images/94.jpg b/images/94.jpg similarity index 100% rename from server/images/94.jpg rename to images/94.jpg diff --git a/server/images/95.jpg b/images/95.jpg similarity index 100% rename from server/images/95.jpg rename to images/95.jpg diff --git a/server/images/96.jpg b/images/96.jpg similarity index 100% rename from server/images/96.jpg rename to images/96.jpg diff --git a/server/images/97.jpg b/images/97.jpg similarity index 100% rename from server/images/97.jpg rename to images/97.jpg diff --git a/server/images/98.jpg b/images/98.jpg similarity index 100% rename from server/images/98.jpg rename to images/98.jpg diff --git a/server/images/99.jpg b/images/99.jpg similarity index 100% rename from server/images/99.jpg rename to images/99.jpg diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..22b51fc --- /dev/null +++ b/requirements.txt @@ -0,0 +1,36 @@ +# pip install -r requirements.txt + +# Base ---------------------------------------- +matplotlib>=3.2.2 +numpy>=1.18.5 +opencv-python>=4.1.2 +Pillow>=7.1.2 +PyYAML>=5.3.1 +requests>=2.23.0 +scipy>=1.4.1 +torch>=1.7.0 +torchvision>=0.8.1 +tqdm>=4.41.0 + +# Logging ------------------------------------- +tensorboard>=2.4.1 +# wandb + +# Plotting ------------------------------------ +pandas>=1.1.4 +seaborn>=0.11.0 + +# Export -------------------------------------- +# coremltools>=4.1 # CoreML export +# onnx>=1.9.0 # ONNX export +# onnx-simplifier>=0.3.6 # ONNX simplifier +# scikit-learn==0.19.2 # CoreML quantization +# tensorflow>=2.4.1 # TFLite export +# tensorflowjs>=3.9.0 # TF.js export + +# Extras -------------------------------------- +# albumentations>=1.0.3 +# Cython # for pycocotools https://github.com/cocodataset/cocoapi/issues/172 +# pycocotools>=2.0 # COCO mAP +# roboflow +thop # FLOPs computation