Skip to content

Commit

Permalink
update three mode in separate folder
Browse files Browse the repository at this point in the history
  • Loading branch information
yangqu2000 committed Apr 22, 2022
1 parent 11e9314 commit 78feaef
Show file tree
Hide file tree
Showing 111 changed files with 349 additions and 6 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
```

54 changes: 54 additions & 0 deletions cloud_computing/client/mclient.py
Original file line number Diff line number Diff line change
@@ -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()
78 changes: 78 additions & 0 deletions cloud_computing/server/mserver.py
Original file line number Diff line number Diff line change
@@ -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()

Binary file added cloud_computing/server/yolov5s.pt
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
File renamed without changes.
9 changes: 7 additions & 2 deletions server/server.py → distributed_computing/server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
54 changes: 54 additions & 0 deletions edge_computing/client/mclient.py
Original file line number Diff line number Diff line change
@@ -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()
78 changes: 78 additions & 0 deletions edge_computing/server/mserver.py
Original file line number Diff line number Diff line change
@@ -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()
Binary file added edge_computing/server/yolov5s.pt
Binary file not shown.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
36 changes: 36 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 78feaef

Please sign in to comment.