Skip to content

Instantly share code, notes, and snippets.

@tsulej
Last active November 14, 2016 22:51
Show Gist options
  • Save tsulej/c0f29685f7e297db9c70 to your computer and use it in GitHub Desktop.
Save tsulej/c0f29685f7e297db9c70 to your computer and use it in GitHub Desktop.
Bash scripts for preparing images for deep neural network training
#!/bin/bash
# script creates train.txt and val.txt prepared by processImages.sh script
# it assumes that all images are processed and there are 10 images minimum in each category
results_folder="images"
rm train.txt
rm val.txt
categories=""
for i in [0-9]*
do
categories+="$i "
done
cd $results_folder
for i in $categories
do
for n in ${i}_*.jpg
do
echo "$n $i" >> ../train.txt
done
for n in ${i}_*9.jpg
do
echo "$n $i" >> ../val.txt
done
done
cd ..
#!/bin/bash
# this script prepares many variants of images which are used to train
# deepdream network
# variants are: flip, flop, hsb modulation, rotation, unsharp, blur,
# crop with variable offset, low quality jpg, normalization and equalization
# mixed in random ways
#
# SETTINGS
# desired size of image, it is set to typical googlenet
# remove crop_size setting from train_val.prototxt
crop_size=227
# folder name for resultin images
results_folder="images"
# NOTE: all images to process must be put into folder named by category! (0,1,2,3, etc...)
# script traverse all numerical folders and create images which will be ready to feed the net
# checkout second script createidx.sh which preapares train.txt and val.txt files automaticaly
# script begins here
mkdir -p $results_folder
croppos=$(( 256 - $crop_size ))
cnt=0
for dir in [0-9]*
do
for file in "$dir"/*.jp*g
do
echo -n "Processing $file "
resname="${dir}_${cnt}"
convert "$file" -resize 227x227! -type truecolor ${results_folder}/${resname}.jpg
for s in `seq -w 1 10`
do
options=" -resize 256x256!"
if (($RANDOM < 500))
then
options+=" -flip"
fi
if (($RANDOM < 4000))
then
options+=" -flop"
fi
if (($RANDOM < 12000))
then
mh=$(($RANDOM % 200))
ms=$(( ($RANDOM % 100)+50 ))
mb=$(( ($RANDOM % 100)+50 ))
options+=" -modulate $mb,$ms,$mh"
fi
if (($RANDOM < 1000))
then
rot=$(( (($RANDOM % 3)+1)*90 ))
options+=" -rotate $rot"
fi
if (($RANDOM < 6096))
then
options+=" -unsharp 2x2"
fi
if (($RANDOM < 6096))
then
options+=" -blur 2x2"
fi
cx=$(( $RANDOM % $croppos ))
cy=$(( $RANDOM % $croppos ))
options+=" -crop 227x227+$cx+$cy"
if (($RANDOM < 16384))
then
options+=" -equalize"
fi
options+=" -normalize"
if (($RANDOM < 8096))
then
q=$(( (RANDOM % 90)+1 ))
options+=" -quality $q"
fi
convert "$file" $options -type truecolor ${results_folder}/${resname}_${s}.jpg
echo -n "."
# echo $options
done
echo ""
(( cnt++ ))
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment