2021-12-20 17:52:32 +01:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
########### SUDO COMMANDS WITHOUT PASSWORD #############################################
|
|
|
|
#
|
|
|
|
# Specific commands must be executable without sudo
|
|
|
|
#
|
|
|
|
# Add to sudoers file (sudo visudo):
|
|
|
|
#
|
|
|
|
# Disable password for specific commands
|
|
|
|
# pi ALL=(ALL:ALL) NOPASSWD: /usr/bin/chmod -R 755 /data/public/capserver/images
|
|
|
|
# pi ALL=(ALL:ALL) NOPASSWD: /usr/bin/mv /home/pi/classifier.mlmodel /data/public/capserver/
|
|
|
|
# pi ALL=(ALL:ALL) NOPASSWD: /usr/bin/mv /home/pi/classifier.version /data/public/capserver/
|
|
|
|
# pi ALL=(ALL:ALL) NOPASSWD: /usr/bin/chown -R www-data\:www-data /data/public/capserver/
|
|
|
|
#
|
|
|
|
########################################################################################
|
|
|
|
|
|
|
|
########### EXPLANATION OF RSYNC FLAGS #################################################
|
|
|
|
#
|
|
|
|
# -h human readable output
|
|
|
|
# -v verbose output
|
|
|
|
# -r recursive
|
|
|
|
# -P print information about long-running transfers, keep partial files (—-progress --partial)
|
|
|
|
# -t preserves modification times
|
|
|
|
# -u only update if newer
|
|
|
|
#
|
|
|
|
########################################################################################
|
|
|
|
|
|
|
|
########### PATHS ######################################################################
|
|
|
|
|
2022-05-02 13:48:11 +02:00
|
|
|
WORK_DIR="${HOME}/Projects/Caps/Caps-Server/Training"
|
2021-12-20 18:08:53 +01:00
|
|
|
BACKUP_DIR="${WORK_DIR}/backup"
|
2021-12-20 17:52:32 +01:00
|
|
|
IMAGE_DIR="${WORK_DIR}/images"
|
|
|
|
VERSION_FILE="${WORK_DIR}/classifier.version"
|
|
|
|
MODEL_FILE="${WORK_DIR}/classifier.mlmodel"
|
|
|
|
|
|
|
|
TRAINING_ITERATIONS="17"
|
|
|
|
SSH_PORT="5432"
|
2022-05-02 13:48:11 +02:00
|
|
|
SERVER="pi@christophhagen.de"
|
2022-03-21 20:17:51 +01:00
|
|
|
SERVER_ROOT_PATH="/data/servers/caps/Public"
|
2021-12-20 17:52:32 +01:00
|
|
|
|
|
|
|
########################################################################################
|
|
|
|
|
|
|
|
echo "[INFO] Working in directory ${WORK_DIR}"
|
2022-03-21 20:17:51 +01:00
|
|
|
cd $WORK_DIR
|
|
|
|
|
|
|
|
retVal=$?
|
|
|
|
if [ $retVal -ne 0 ]; then
|
|
|
|
echo '[ERROR] Directory not found'
|
|
|
|
return $retVal
|
|
|
|
fi
|
2021-12-20 17:52:32 +01:00
|
|
|
|
|
|
|
echo "[INFO] Getting classifier version from server..."
|
|
|
|
scp -P $SSH_PORT ${SERVER}:/${SERVER_ROOT_PATH}/classifier.version $WORK_DIR
|
|
|
|
|
2021-12-20 20:45:27 +01:00
|
|
|
retVal=$?
|
|
|
|
if [ $retVal -ne 0 ]; then
|
|
|
|
echo '[ERROR] Failed to get classifier version'
|
|
|
|
return $retVal
|
|
|
|
fi
|
|
|
|
|
2021-12-20 17:52:32 +01:00
|
|
|
# Read classifier version from file
|
|
|
|
OLD_VERSION=$(< $VERSION_FILE)
|
|
|
|
NEW_VERSION=$(($OLD_VERSION + 1))
|
|
|
|
echo "[INFO] Creating classifier version ${NEW_VERSION}"
|
|
|
|
|
|
|
|
echo "[INFO] Ensuring permissions for images on server..."
|
|
|
|
ssh -p $SSH_PORT ${SERVER} "sudo chmod -R 755 ${SERVER_ROOT_PATH}/images"
|
|
|
|
|
2021-12-20 20:45:27 +01:00
|
|
|
retVal=$?
|
|
|
|
if [ $retVal -ne 0 ]; then
|
|
|
|
echo '[ERROR] Failed to change image permissions'
|
|
|
|
return $retVal
|
|
|
|
fi
|
|
|
|
|
2021-12-20 17:52:32 +01:00
|
|
|
echo "[INFO] Transferring images from server..."
|
2022-05-02 13:48:11 +02:00
|
|
|
rsync -hrut --info=progress2 -e "ssh -p ${SSH_PORT}" ${SERVER}:/${SERVER_ROOT_PATH}/images/ "${IMAGE_DIR}"
|
2021-12-20 17:52:32 +01:00
|
|
|
|
2021-12-20 20:45:27 +01:00
|
|
|
retVal=$?
|
|
|
|
if [ $retVal -ne 0 ]; then
|
|
|
|
echo '[ERROR] Failed to transfer images from server'
|
|
|
|
return $retVal
|
|
|
|
fi
|
|
|
|
|
2021-12-20 17:52:32 +01:00
|
|
|
echo "[INFO] Training the model..."
|
|
|
|
swift train.swift $IMAGE_DIR $TRAINING_ITERATIONS $MODEL_FILE
|
|
|
|
|
2021-12-20 20:45:27 +01:00
|
|
|
retVal=$?
|
|
|
|
if [ $retVal -ne 0 ]; then
|
|
|
|
echo '[ERROR] Failed to train model'
|
|
|
|
return $retVal
|
|
|
|
fi
|
2021-12-20 17:52:32 +01:00
|
|
|
|
|
|
|
echo "[INFO] Incrementing version file..."
|
|
|
|
echo "${NEW_VERSION}" > $VERSION_FILE
|
|
|
|
|
|
|
|
echo "[INFO] Copying the files to the server..."
|
|
|
|
scp -P $SSH_PORT $MODEL_FILE $VERSION_FILE ${SERVER}:~/
|
|
|
|
|
2021-12-20 20:45:27 +01:00
|
|
|
retVal=$?
|
|
|
|
if [ $retVal -ne 0 ]; then
|
|
|
|
echo '[ERROR] Failed to copy new files to server'
|
|
|
|
return $retVal
|
|
|
|
fi
|
|
|
|
|
2021-12-20 17:52:32 +01:00
|
|
|
echo "[INFO] Moving files into public directory..."
|
2022-01-05 23:23:54 +01:00
|
|
|
ssh -p ${SSH_PORT} ${SERVER} "sudo mv /home/pi/classifier.* ${SERVER_ROOT_PATH}"
|
2021-12-20 17:52:32 +01:00
|
|
|
|
2021-12-20 20:45:27 +01:00
|
|
|
retVal=$?
|
|
|
|
if [ $retVal -ne 0 ]; then
|
|
|
|
echo '[ERROR] Failed to move files on server'
|
|
|
|
return $retVal
|
|
|
|
fi
|
|
|
|
|
2021-12-20 17:52:32 +01:00
|
|
|
echo "[INFO] Updating permissions..."
|
2022-01-05 23:23:54 +01:00
|
|
|
ssh -p ${SSH_PORT} ${SERVER} "sudo chown -R www-data\:www-data ${SERVER_ROOT_PATH}"
|
2021-12-20 17:52:32 +01:00
|
|
|
|
2021-12-20 20:45:27 +01:00
|
|
|
retVal=$?
|
|
|
|
if [ $retVal -ne 0 ]; then
|
|
|
|
echo '[ERROR] Failed to update file permissions on server'
|
|
|
|
return $retVal
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "[INFO] Backing up model..."
|
|
|
|
mv $MODEL_FILE "${BACKUP_DIR}/classifier${NEW_VERSION}.mlmodel"
|
|
|
|
|
|
|
|
retVal=$?
|
|
|
|
if [ $retVal -ne 0 ]; then
|
|
|
|
echo '[WARNING] Failed to back up model'
|
|
|
|
rm $MODEL_FILE
|
|
|
|
fi
|
|
|
|
|
2021-12-20 17:52:32 +01:00
|
|
|
echo "[INFO] Cleaning up..."
|
2021-12-20 20:45:27 +01:00
|
|
|
rm $VERSION_FILE
|
2021-12-20 17:52:32 +01:00
|
|
|
|
|
|
|
echo "[INFO] Process finished"
|