Last active
October 4, 2018 18:10
-
-
Save minhoolee/27b68aba47aab18a3b86c069a06e31d0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/bash | |
# Written by Mark Lee (github.com/minhoolee), August 9, 2018 | |
set -e | |
# Define the directory that contains specific file structure | |
# Must be DEFECTS > SAMPLES > MODEL_IMAGES | |
test_dir=./data | |
# Define the model names | |
models=("DF_H" | |
"DF_V" | |
"NDF_H" | |
"NDF_V") | |
# Input: name of directory holding samples and | |
# name of directory that contains the model-named images | |
# | |
# Create files labelled as <DEFECT>_<SAMPLE>_<MODEL_NAME>.txt | |
run_models() | |
{ | |
defect=${1#$test_dir/} | |
sample=${2##*/} | |
# Loop through the models | |
for (( i = 0; i < ${#models[@]}; ++i )) | |
do | |
model_name="${models[$i]}" | |
predictions_file="$test_dir/$defect/$sample/${defect}_${sample}_${model_name}.txt" | |
img_dir="$model_name.jpg" | |
# Call darknet on each of the models for its image | |
printf "\nRunning darknet model ${model_name} and saving predictions to $predictions_file\n\n" | |
darknet classifier predict \ | |
"cfg/defect_${model_name}.data" \ | |
"cfg/densenet201_defect_${model_name}.cfg" \ | |
"backup/densenet201_defect_${model_name}.backup" \ | |
"${model_name}.jpg" >> $predictions_file | |
done | |
} | |
# Input: name of directory holding samples and | |
# name of directory that contains the model-named images | |
# | |
# Average predictions (line by line) from <DEFECT>_<SAMPLE>_<MODEL_NAME>.txt | |
# files into a new file labelled as <DEFECT>_<SAMPLE>_avg_pred.txt | |
average_models() | |
{ | |
defect=${1#$test_dir/} | |
sample=${2##*/} | |
declare -a sum | |
out_file="$test_dir/$defect/$sample/${defect}_${sample}_avg_preds.txt" | |
# Loop through the models | |
for (( i = 0; i < ${#models[@]}; ++i )) | |
do | |
model_name="${models[$i]}" | |
predictions_file="$test_dir/$defect/$sample/${defect}_${sample}_${model_name}.txt" | |
if ! [[ -s $predictions_file ]] | |
then | |
printf "\nERROR: No prediction file found: $predictions_file)\n" | |
exit 64 | |
fi | |
# Read lines from prediction file and save to pred array | |
IFS=$'\r\n' GLOBIGNORE='*' command eval "pred=($(cat ${predictions_file} | sed 's/[^0-9.]//g'))" | |
# Check if # of values in model predictions is not same as average | |
if [[ $i > 0 && ${#pred[@]} != ${#sum[@]} ]] | |
then | |
printf "\nWARNING: # of predictions in ${predictions_file} is incorrect\n\n" | |
fi | |
# Loop through all the lines in the predictions file | |
for (( line = 0; line < ${#pred[@]}; ++line )) | |
do | |
if [[ -z ${sum[line]} ]] | |
then | |
sum[$line]=0 | |
fi | |
# Add prediction / number of models to the current average | |
sum[$line]=`bc <<< "scale = 10; ${sum[line]} + \ | |
(${pred[line]}/${#models[@]})"` | |
done | |
done | |
printf "\nCreating $out_file\n\n" | |
printf "%s\n" "${sum[@]}" > $out_file | |
} | |
for defect in $test_dir/* | |
do | |
for sample in "$defect"/* | |
do | |
run_models $defect $sample | |
average_models $defect $sample | |
done | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment