sparse / ms-swift /uni /run_with_progress_updates.sh
Enxin's picture
Upload folder using huggingface_hub
96fe658 verified
#!/usr/bin/env bash
#
# run_layoutsam_with_slack.sh
# β†’ Builds the filtered image list, then downloads only those images,
# posting progress & errors into Slack every 5 min.
# β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
# 1) Slack webhook setup
# β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
WEBHOOK="https://hooks.slack.com/services/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"
send_slack_message() {
local msg="$1"
curl -s -X POST -H 'Content-type: application/json' \
--data "{\"text\":\"$msg\"}" \
"$WEBHOOK"
}
# β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
# 2) Paths & startup
# β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
IMG_LIST_SCRIPT="uni/generate_image_list.py"
DOWNLOAD_SCRIPT="uni/download.py"
DATA_DIR="/home/ubuntu/ext-mamba-illinois/UCSD-project/workspace/datasets/SA-1b"
DOWNLOAD_DIR="$DATA_DIR/LayoutImgs"
MASK_DIR="$DATA_DIR/LayoutMasks"
LOG_FILE="./download_progress.log"
START_TIME=$(date +%s)
# β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
# 3) Check if JSON file exists, skip generation if it does
# β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
if [ ! -f "./images_to_download.json" ]; then
send_slack_message "πŸ” Generating LayoutSAM image list…"
python3 "$IMG_LIST_SCRIPT" >> "$LOG_FILE" 2>&1 || {
send_slack_message "❌ Failed to generate image list – aborting."
exit 1
}
else
send_slack_message "πŸ“‹ Using existing image list: images_to_download.json"
fi
# β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
# 4) Compute total images count
# β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
TOTAL_IMAGES=$(python3 - << 'PYCODE'
import json, sys
try:
data = json.load(open("images_to_download.json"))
print(len(data))
except:
sys.exit(1)
PYCODE
)
if [ -z "$TOTAL_IMAGES" ]; then
send_slack_message "❌ Could not determine total images count."
exit 1
fi
send_slack_message "βœ… Image list ready: $TOTAL_IMAGES paths."
# β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
# 5) Generate shard links for downloader
# β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
send_slack_message "πŸ”— Generating shard links from image list…"
python3 generate_shard_links.py >> "$LOG_FILE" 2>&1 || {
send_slack_message "❌ Failed to generate shard links – aborting."
exit 1
}
# β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
# 6) Launch the downloader in background
# β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
send_slack_message "πŸš€ Starting SA-1B download of LayoutSAM subset…"
python3 "$DOWNLOAD_SCRIPT" \
--processes 16 \
--input_file "./layoutsam_shard_links.txt" \
--raw_dir "$DOWNLOADER_DIR/raw" \
--images_dir "$DOWNLOAD_DIR" \
--masks_dir "$MASK_DIR" \
--images_json "./images_to_download.json" \
--skip_existing >> "$LOG_FILE" 2>&1 &
SCRIPT_PID=$!
# β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
# 7) Progress & error‐checking functions
# β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
get_progress(){
local now=$(date +%s)
local elapsed=$(( (now - START_TIME)/60 ))
local got=$(find "$DOWNLOAD_DIR" -type f | wc -l)
local usage=$(df -h "$DOWNLOAD_DIR" | tail -1 | awk '{print $5}')
echo "πŸ“Š Progress (${elapsed}m): $got / $TOTAL_IMAGES images downloaded β€’ Disk: $usage"
}
check_errors(){
if grep -iE "error|exception|traceback" "$LOG_FILE" >/dev/null; then
echo "⚠️ Last error: $(grep -iE "error|exception|traceback" "$LOG_FILE" | tail -1)"
fi
}
# β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
# 8) Monitoring loop
# β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
while kill -0 $SCRIPT_PID 2>/dev/null; do
sleep 300
prog=$(get_progress)
err=$(check_errors)
send_slack_message "$prog${err:+\n$err}"
done
# β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
# 9) Final report
# β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
wait $SCRIPT_PID
EXIT=$?
if [ $EXIT -eq 0 ]; then
FINAL=$(find "$DOWNLOAD_DIR" -type f | wc -l)
DURATION=$(( ( $(date +%s) - START_TIME )/60 ))
send_slack_message "βœ… Done! Downloaded $FINAL/$TOTAL_IMAGES images in ${DURATION}m."
else
send_slack_message "❌ Download failed (exit code $EXIT). Check log: $LOG_FILE"
send_slack_message "πŸ” Last log lines:\n$(tail -5 "$LOG_FILE")"
fi
# β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
# 10) Cleanup
# β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
rm -f "$LOG_FILE"