Skip to content

Commit 7c017e9

Browse files
cencarnamclaireb
authored andcommitted
utils: add shell script counterpart for model fetch batch files
Signed-off-by: Cedric Encarnacion <cedricjustine.encarnacion@analog.com>
1 parent 1e3fb31 commit 7c017e9

File tree

2 files changed

+177
-0
lines changed

2 files changed

+177
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#!/bin/bash
2+
3+
# Variables
4+
dtln="dtln"
5+
env="python_env"
6+
model1_h="model_float_1_data.h"
7+
model1_cc="model_float_1_data.cc"
8+
model2_h="model_float_2_data.h"
9+
model2_cc="model_float_2_data.cc"
10+
11+
# Directories
12+
model_dir="./TFLM_models"
13+
example_model_dir="../../../examples/denoiser/common/model/model_fp"
14+
15+
# Check if models are already generated
16+
if [[ -f "$model_dir/$model1_h" && -f "$model_dir/$model1_cc" && -f "$model_dir/$model2_h" && -f "$model_dir/$model2_cc" ]]; then
17+
echo "TFLM models have been generated successfully"
18+
else
19+
# Clone the project if not present
20+
if [[ ! -d "$dtln" ]]; then
21+
git clone https://github.com/breizhn/DTLN.git "$dtln"
22+
else
23+
echo "Project directory already present, moving to next step"
24+
fi
25+
26+
# Copy necessary files
27+
cp ./scripts/DTLN_model.py "$dtln/DTLN_model.py"
28+
cp ./scripts/input1.npy "$dtln/input1.npy"
29+
cp ./scripts/input2.npy "$dtln/input2.npy"
30+
cp ./scripts/state1.npy "$dtln/state1.npy"
31+
cp ./scripts/state2.npy "$dtln/state2.npy"
32+
cp ./scripts/fix_error.py "$dtln/fix_error.py"
33+
34+
# Navigate to project folder
35+
cd "$dtln"
36+
37+
# Set up Python environment
38+
python -m pip install --user virtualenv --no-cache-dir
39+
python -m venv "$env"
40+
source "$env/scripts/activate"
41+
python -m pip install --upgrade pip --no-cache-dir
42+
python -m pip install tensorflow==2.15.0 soundfile wavinfo --no-cache-dir
43+
44+
# Run Python scripts
45+
python fix_error.py
46+
python convert_weights_to_tf_lite.py -m pretrained_model/model.h5 -t model_float
47+
48+
# Copy generated TFLite models
49+
cp model_float_1.tflite ../scripts/
50+
cp model_float_2.tflite ../scripts/
51+
52+
# Navigate to scripts folder
53+
cd ../scripts
54+
55+
# Generate C arrays if TFLM models folder doesn't exist
56+
if [[ ! -d "../$model_dir" ]]; then
57+
python generate_cc_arrays.py . model_float_1.tflite
58+
python generate_cc_arrays.py . model_float_2.tflite
59+
fi
60+
61+
# Clean up and move TFLM models
62+
mkdir ../$model_dir
63+
mv $model1_cc "../$model_dir/$model1_cc"
64+
mv $model1_h "../$model_dir/$model1_h"
65+
mv $model2_cc "../$model_dir/$model2_cc"
66+
mv $model2_h "../$model_dir/$model2_h"
67+
rm -f model_float_1.tflite model_float_2.tflite
68+
69+
# Remove repository
70+
cd ..
71+
rm -rf "$dtln"
72+
73+
# Check if TFLM models were generated successfully
74+
if [[ -f "$model_dir/$model1_h" && -f "$model_dir/$model1_cc" && -f "$model_dir/$model2_cc" && -f "$model_dir/$model1_h" ]]; then
75+
echo "TFLM models have been generated successfully"
76+
fi
77+
fi
78+
79+
# Copy models to example project
80+
cp "$model_dir/$model1_cc" "$example_model_dir/$model1_cc"
81+
cp "$model_dir/$model1_h" "$example_model_dir/$model1_h"
82+
cp "$model_dir/$model2_cc" "$example_model_dir/$model2_cc"
83+
cp "$model_dir/$model2_h" "$example_model_dir/$model2_h"
84+
85+
# Verify copied models
86+
if [[ -f "$example_model_dir/$model1_cc" && -f "$example_model_dir/$model1_h" && -f "$example_model_dir/$model2_cc" && -f "$example_model_dir/$model2_h" ]]; then
87+
echo "TFLM models have been copied into respective projects successfully, check the following paths to find the models:"
88+
echo "$example_model_dir"
89+
fi
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#!/bin/bash
2+
3+
# Variables
4+
project_folder="music-genre-classification"
5+
env="python_env"
6+
tflite_model_float32="float32_genre_ID.tflite"
7+
tflite_model_int8="int8_genre_ID.tflite"
8+
tflm_model1a="float32_genre_ID_model_data.h"
9+
tflm_model1b="float32_genre_ID_model_data.cc"
10+
tflm_model2a="int8_genre_ID_model_data.h"
11+
tflm_model2b="int8_genre_ID_model_data.cc"
12+
13+
# Directories
14+
tflm_models="TFLM_models"
15+
final_int8_model_path="../../../examples/genre_identification/common/model/int8_genre_ID/"
16+
final_float_model_path="../../../examples/genre_identification/common/model/float_genre_ID/"
17+
18+
# Check if TFLM models exist
19+
if [[ -f "$tflm_models/$tflm_model1a" && -f "$tflm_models/$tflm_model1b" && -f "$tflm_models/$tflm_model2a" && -f "$tflm_models/$tflm_model2b" ]]; then
20+
echo "TFLM models have been generated successfully"
21+
else
22+
# Clone the project if not present
23+
if [[ ! -d "$project_folder" ]]; then
24+
git clone https://github.com/cetinsamet/music-genre-classification.git "$project_folder"
25+
else
26+
echo "Project directory already present, moving to next step"
27+
fi
28+
29+
# Navigate to scripts folder
30+
cd "scripts"
31+
32+
# Set up Python environment
33+
if [[ ! -d "$env" ]]; then
34+
python -m pip install --user virtualenv --no-cache-dir
35+
python -m venv "$env"
36+
source "$env/scripts/activate"
37+
python -m pip install --upgrade pip --no-cache-dir
38+
cp requirements.txt reqs.txt
39+
sed -i '/tensorflow-intel==2.15.0/d' reqs.txt
40+
python -m pip install -r reqs.txt --no-cache-dir
41+
rm reqs.txt
42+
fi
43+
44+
# Generate TFLite models if not already present
45+
if [[ ! -f "$tflite_model_int8" ]]; then
46+
source "$env/scripts/activate"
47+
python convert_model.py
48+
fi
49+
50+
# Generate TFLM models if not already present
51+
if [[ ! -d "$tflm_models" ]]; then
52+
python generate_cc_arrays.py . "$tflite_model_float32"
53+
python generate_cc_arrays.py . "$tflite_model_int8"
54+
fi
55+
56+
# Clean up and move TFLM models
57+
mkdir ../$tflm_models
58+
mv $tflm_model1a ../$tflm_models/$tflm_model1a
59+
mv $tflm_model1b ../$tflm_models/$tflm_model1b
60+
mv $tflm_model2a ../$tflm_models/$tflm_model2a
61+
mv $tflm_model2b ../$tflm_models/$tflm_model2b
62+
rm -f "$tflite_model_float32" "$tflite_model_int8"
63+
64+
# Reclaim space
65+
echo "Reclaiming space..."
66+
deactivate
67+
rm -rf "$env"
68+
cd ..
69+
rm -rf "$project_folder"
70+
71+
# Check if TFLM models were generated successfully
72+
if [[ -f "$tflm_models/$tflm_model1a" && -f "$tflm_models/$tflm_model1b" && -f "$tflm_models/$tflm_model2a" && -f "$tflm_models/$tflm_model2b" ]]; then
73+
echo "TFLM models have been generated successfully"
74+
fi
75+
fi
76+
77+
# Copy models to final paths
78+
cp "$tflm_models/$tflm_model2a" "$final_int8_model_path/$tflm_model2a"
79+
cp "$tflm_models/$tflm_model2b" "$final_int8_model_path/$tflm_model2b"
80+
cp "$tflm_models/$tflm_model1a" "$final_float_model_path/$tflm_model1a"
81+
cp "$tflm_models/$tflm_model1b" "$final_float_model_path/$tflm_model1b"
82+
83+
# Verify copied models
84+
if [[ -f "$final_float_model_path/$tflm_model1a" && -f "$final_float_model_path/$tflm_model1b" && -f "$final_int8_model_path/$tflm_model2a" && -f "$final_int8_model_path/$tflm_model2b" ]]; then
85+
echo "TFLM models have been copied into respective projects successfully, check the following paths to find the models:"
86+
echo "$final_int8_model_path"
87+
echo "$final_float_model_path"
88+
fi

0 commit comments

Comments
 (0)