2023-06-24 00:22:05 +00:00
|
|
|
set -e
|
|
|
|
|
|
|
|
if [ -f .env ]; then
|
|
|
|
source .env
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -z ${1} ]; then
|
|
|
|
echo "Please provide a JSON object"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
function fail () {
|
|
|
|
ID="${1}"
|
|
|
|
META=${2}
|
|
|
|
URL="${YOLO_WEB_URL}/job/fail/${ID}"
|
|
|
|
echo "${META}"
|
|
|
|
curl -s -X POST \
|
|
|
|
-H 'Content-Type: application/json' \
|
|
|
|
-d "{\"meta\":\"${META}\"}" "${URL}"
|
|
|
|
}
|
|
|
|
|
2023-06-30 14:45:07 +00:00
|
|
|
trap 'fail $ID' ERR
|
|
|
|
|
2023-06-24 00:22:05 +00:00
|
|
|
JSON="${1}"
|
|
|
|
|
|
|
|
ID=$(echo $JSON | jq -r '.id')
|
|
|
|
DATASET=$(echo $JSON | jq -r '.dataset')
|
|
|
|
MODEL=$(echo $JSON | jq -r '.model')
|
|
|
|
NAME=$(echo $JSON | jq -r '.name')
|
|
|
|
FILEPATH=$(echo $JSON | jq -r '.path')
|
|
|
|
|
|
|
|
DOWNLOAD="${YOLO_WEB_URL}${FILEPATH}"
|
2023-06-29 13:39:17 +00:00
|
|
|
DEST="${DATASETS}/${DATASET}.zip"
|
|
|
|
UNZIPPED="${DATASETS}/${DATASET}"
|
2023-06-24 00:22:05 +00:00
|
|
|
|
|
|
|
echo "Downloading ${DOWNLOAD}"
|
|
|
|
|
|
|
|
if [ ! -f "${DEST}" ]; then
|
|
|
|
wget -q -O "${DEST}" "${DOWNLOAD}"
|
|
|
|
fi
|
|
|
|
|
2023-06-29 13:28:10 +00:00
|
|
|
unzip -o "${DEST}" -d "${UNZIPPED}"
|
2023-06-24 00:22:05 +00:00
|
|
|
|
|
|
|
if [ ! -f "${UNZIPPED}/data.yaml" ]; then
|
|
|
|
fail "${ID}" "Invalid dataset"
|
|
|
|
rm -rf "${UNZIPPED}"
|
|
|
|
rm "${DEST}"
|
|
|
|
exit 1
|
2023-06-24 00:36:38 +00:00
|
|
|
fi
|
|
|
|
|
2023-06-29 04:17:03 +00:00
|
|
|
FULLDEST=$(realpath "${UNZIPPED}")
|
2023-12-15 17:15:33 +00:00
|
|
|
python scripts/data.py "${FULLDEST}/data.yaml" "${NAME}"
|
2023-06-29 04:17:03 +00:00
|
|
|
|
|
|
|
cd yolov5
|
|
|
|
source env/bin/activate
|
|
|
|
|
|
|
|
WEIGHTS="yolov5s.pt"
|
|
|
|
|
|
|
|
if [[ "${MODEL}" == "yolov5m_onnx" ]]; then
|
2023-11-29 17:57:57 +00:00
|
|
|
echo "Using medium weights [${MODEL}]"
|
2023-06-29 04:17:03 +00:00
|
|
|
WEIGHTS="yolov5m.pt"
|
2023-06-29 13:28:10 +00:00
|
|
|
elif [[ "${MODEL}" == "yolov5l_onnx" ]]; then
|
2023-11-29 17:57:57 +00:00
|
|
|
echo "Using large weights [${MODEL}]"
|
2023-06-29 04:17:03 +00:00
|
|
|
WEIGHTS="yolov5l.pt"
|
|
|
|
fi
|
|
|
|
|
|
|
|
python train.py --img 640 --batch 16 --epochs 300 --data "${FULLDEST}/data.yaml" --weights "${WEIGHTS}" --cache --project ../models/ --name "${ID}"
|
|
|
|
|
2023-06-30 14:45:07 +00:00
|
|
|
if [ ! -f "../models/${ID}/weights/best.pt" ]; then
|
|
|
|
fail "${ID}" "Error training model"
|
|
|
|
rm -rf "${UNZIPPED}"
|
|
|
|
rm "${DEST}"
|
|
|
|
exit 3
|
|
|
|
fi
|
|
|
|
|
2023-11-29 17:43:54 +00:00
|
|
|
python export.py --weights "../models/${ID}/weights/best.pt" --opset 12 --simplify --include onnx
|
2023-06-30 14:45:07 +00:00
|
|
|
|
|
|
|
if [ ! -f "../models/${ID}/weights/best.onnx" ]; then
|
|
|
|
fail "${ID}" "Error exporting model"
|
|
|
|
rm -rf "${UNZIPPED}"
|
|
|
|
rm "${DEST}"
|
|
|
|
exit 3
|
|
|
|
fi
|
|
|
|
|
|
|
|
MODELPATH=$(realpath "../models/${ID}/weights/best.onnx")
|
2023-11-29 17:43:54 +00:00
|
|
|
PTPATH=$(realpath "../models/${ID}/weights/best.pt")
|
2023-12-15 17:15:33 +00:00
|
|
|
JSONPATH=$(realpath "${FULLDEST}/${NAME}.json")
|
2023-11-29 17:43:54 +00:00
|
|
|
ZIPPATH="${ID}.zip"
|
|
|
|
|
|
|
|
mkdir zip
|
|
|
|
cp "${MODELPATH}" "zip/${NAME}.onnx"
|
|
|
|
cp "${PTPATH}" "zip/${NAME}.pt"
|
2023-12-15 17:15:33 +00:00
|
|
|
cp "${JSONPATH}" "zip/${NAME}.json"
|
2023-11-29 17:43:54 +00:00
|
|
|
cd zip
|
|
|
|
zip "../${ZIPPATH}" *
|
|
|
|
cd ..
|
|
|
|
rm -r zip/
|
2023-06-30 14:45:07 +00:00
|
|
|
|
|
|
|
curl -s -X POST \
|
2023-11-29 17:43:54 +00:00
|
|
|
-F "model=@${ZIPPATH}" \
|
2023-06-30 14:45:07 +00:00
|
|
|
"${YOLO_WEB_URL}/job/${ID}"
|
2023-06-30 14:56:11 +00:00
|
|
|
|
2023-11-29 17:43:54 +00:00
|
|
|
rm "${ZIPPATH}"
|
|
|
|
|
2023-06-30 18:10:55 +00:00
|
|
|
deactivate
|
|
|
|
cd ..
|
|
|
|
|