Skip to content

Commit

Permalink
entrypoint changes for more clients
Browse files Browse the repository at this point in the history
  • Loading branch information
sowmyasris committed Sep 2, 2024
1 parent af6920a commit 45cee39
Showing 1 changed file with 36 additions and 55 deletions.
91 changes: 36 additions & 55 deletions examples/Yolo-object-detection/client/entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import fire
import numpy as np
import subprocess

from fedn.utils.dist import get_package_path
from fedn.utils.helpers.helpers import get_helper, save_metadata, save_metrics

HELPER_MODULE = "numpyhelper"
Expand All @@ -16,8 +16,7 @@


def _get_data_path():
data_path = os.environ.get("FEDN_DATA_PATH", abs_path + "/data/clients/1/mnist.npz")

data_path = os.environ.get("FEDN_DATA_PATH")
return data_path

def save_darknet2fedn(darkfile, fednfile ):
Expand All @@ -36,16 +35,23 @@ def save_fedn2darknet(fednfile, darkfile):
header = np.array([0,2, 5, 0, 0],dtype=np.int32)
header.tofile(f)
buf.tofile(f)
def number_of_lines(file):
with open(file, "r") as f:
lines = f.readlines()
line_count=len(lines)
return line_count
def init_seed(out_path="../seed.npz"):
"""Initialize seed model and save it to file.
:param out_path: The path to save the seed model to.
:type out_path: str
"""
darkfile="yolov4-tiny.weights"
print(get_package_path())
darkfile="data/yolov4-tiny.weights"
fp = open(darkfile, "rb")
buf = np.fromfile(fp, dtype=np.float32)
helper.save([buf], out_path)
print("saveddddd")
fp.close()

def train(in_model_path, out_model_path, data_path=None, batch_size=32, epochs=1):
Expand All @@ -66,89 +72,65 @@ def train(in_model_path, out_model_path, data_path=None, batch_size=32, epochs=1
:type epochs: int
"""
os.chdir("darknet")
darkfile = "example.weights"

data_path = _get_data_path()
darkfile = data_path + "/example.weights"
save_fedn2darknet(in_model_path, darkfile)
#save_darknet2fedn("/Users/sowmyasriseenivasan/workspaces/fedn/examples/Yolo-object-detection/client/darknet/yolov4-tiny.weights","client")

# cli call to darknet to train darkfile and save it to e.g. darkfile_upd.weights


# Paths to your files
data_file = "obj.data"
cfg_file = "yolov4-tiny.cfg"
yolo_converted_weights = "example.weights" # Pretrained weights file
#yolo_converted_weights = "mattias.weights" # Pretrained weights file


data_file=data_path + "/obj.data"
print("hereee",data_path)
cfg_file = data_path+"/yolov4-tiny.cfg"
# Pretrained weights file
# Darknet executable path
darknet_path = "./darknet" # Make sure this path is correct
print("cwd: ", os.getcwd())
# Command to train YOLO using Darknet
command = [darknet_path, "detector", "train", data_file, cfg_file, yolo_converted_weights]

# Run the command273,277,273,265,..,260,254,259,258...272...273..286....274....276,................274,272,271,272,262,265,
yolo_converted_weights = "example.weights"
command = [darknet_path, "detector", "train", data_file, cfg_file, yolo_converted_weights,"-dont_show"]
try:
subprocess.run(command, check=True)
except subprocess.CalledProcessError as e:
print(f"Error during training: {e}")

#save_darknet2fedn("yolov4-tiny_last.weights", out_model_path)

# saving metadata in the same way as in our example

number_of_examples=number_of_lines(data_path+"/train.txt")
print(number_of_examples)
metadata = {
# num_examples are mandatory
"num_examples": 1841,
"num_examples": number_of_examples,
"batch_size": 64,
"epochs": 1,
"lr": 0.00261,
}
# Save JSON metadata file (mandatory)
save_metadata(metadata, out_model_path)
# Save model update (mandatory)
save_darknet2fedn("yolov4_tiny/yolov4-tiny_final.weights", out_model_path)
print(out_model_path)
save_darknet2fedn(data_path+"/yolov4_tiny/yolov4-tiny_final.weights", out_model_path)
#helper.save("output.npz", out_model_path)


def validate(in_model_path, out_json_path, data_path=None):
"""Validate model.
:param in_model_path: The path to the input model.
:type in_model_path: str
:param out_json_path: The path to save the output JSON to.
:type out_json_path: str
:param data_path: The path to the data file.
:type data_path: str """


os.chdir('darknet')



:type data_path: str
"""

os.chdir("darknet")
#save model to darkfile
darkfile = "darknet.weights"
#darkfile = "/home/sowmya/yolo_computer/darknet/yolov4_tiny/yolov4-tiny_final.weights" # Pretrained weights file
#darkfile2 = "/home/sowmya/yolo_computer/darknet/yolov4_tiny/yolov4-tiny_final2.weights" # Pretrained weights file

#print("is file: ", os.path.isfile(darkfile))
#print("cwd: ", os.getcwd())
#temppath = 'temp.npz'
#save_darknet2fedn(darkfile, temppath)
#save_fedn2darknet(in_model_path, darkfile)
data_path = _get_data_path()
darkfile = data_path + "/darknet.weights"
save_fedn2darknet(in_model_path, darkfile)
data_file = "obj.data"
data_file = data_path+"/obj.data"
cfg_file = data_path+"/yolov4-tiny.cfg"

cfg_file = "yolov4-tiny.cfg"



darknet_path = "./darknet" # Make sure this path is correct


#darkfile = "/Users/sowmyasriseenivasan/yolov4-tiny_last.weights"
value=str(0.01)
# Command to validate YOLO using Darknet
command = [darknet_path, "detector", "map", data_file, cfg_file, darkfile]
command = [darknet_path, "detector", "map", data_file, cfg_file, darkfile,"-iou_thresh", value]
print("getcwd: ", os.getcwd())
# Run the command
try:
Expand All @@ -165,7 +147,6 @@ def validate(in_model_path, out_json_path, data_path=None):
#print("-- ", ln)
for line_ in line.split(", "):
if len(line_.split(" = "))>1:
#print(" ", line_.split("=")[0], ": ", line_.split("=")[1])
result_data[line_.split(" = ")[0]] = line_.split(" = ")[1]
print(result_data)

Expand All @@ -192,11 +173,11 @@ def validate(in_model_path, out_json_path, data_path=None):
fire.Fire(
{
"init_seed": init_seed,
"_get_data_path": _get_data_path,
"train": train,
"validate": validate # for testing
"validate": validate# for testing

}
)
#train("/home/sowmya/fedn/examples/Yolo-object-detection/package/client/output.npz","/tmp/tmpc4ibx4py")



0 comments on commit 45cee39

Please sign in to comment.