-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding model design and training code
- Loading branch information
1 parent
3eb3e7b
commit c97b816
Showing
6 changed files
with
321 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import model_audio_ANN | ||
import pickle as cPickle | ||
import tensorflow as tf | ||
import tflearn.helpers.evaluator | ||
|
||
|
||
print("Load evaluation dataset") | ||
pickle_path = './pickle_data/' | ||
|
||
|
||
# f = open(pickle_path+'train_labels_22k_org.pickle', 'wb') | ||
# f = open(pickle_path+'valid_labels_22k_org.pickle', 'wb') | ||
# with open(pickle_path+"test_data_22k_org.pickle", "rb") as input_file: | ||
with open(pickle_path + 'valid_data_22k_org.pickle', 'rb') as input_file: | ||
x_test = cPickle.load(input_file) | ||
|
||
# with open(pickle_path+"test_labels_22k_org.pickle", "rb") as input_file: | ||
with open(pickle_path + "valid_labels_22k_org.pickle", "rb") as input_file: | ||
y_test = cPickle.load(input_file) | ||
|
||
|
||
tf.reset_default_graph() | ||
|
||
# ann_model_dir = './model/ANN/' | ||
ann_model_dir = './audio_model/ANN/' | ||
# ann_model_dir = './model/ANN_new/Bee_audio_ANN.tfl' | ||
|
||
ann_model = model_audio_ANN.build_tflearn_ann(x_test.shape[1]) | ||
ann_model.load(ann_model_dir+'Bee_audio_ANN.tfl', weights_only=True, create_new_session = False) | ||
validation_acc = ann_model.evaluate(x_test, y_test, batch_size=64) | ||
print(validation_acc) | ||
|
||
# Best Acc: 0.9365351630524951 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import model_audio_CNN | ||
import pickle as cPickle | ||
import tensorflow as tf | ||
import tflearn.helpers.evaluator | ||
|
||
|
||
print("Load evaluation dataset") | ||
pickle_path = './pickle_data/' | ||
|
||
with open(pickle_path + 'valid_data_22k_org.pickle', 'rb') as input_file: | ||
# with open(pickle_path+"test_data_22k_org.pickle", "rb") as input_file: | ||
x_test = cPickle.load(input_file) | ||
|
||
with open(pickle_path + "valid_labels_22k_org.pickle", "rb") as input_file: | ||
# with open(pickle_path+"test_labels_22k_org.pickle", "rb") as input_file: | ||
y_test = cPickle.load(input_file) | ||
|
||
|
||
tf.reset_default_graph() | ||
|
||
cnn_model_dir = './audio_model/CNN/Bee_audio_CNN.tfl' | ||
cnn_model = model_audio_CNN.build_tflearn_cnn(x_test.shape[1]) | ||
cnn_model.load(cnn_model_dir, weights_only=True) | ||
validation_acc = cnn_model.evaluate(x_test, y_test, batch_size=32) | ||
print(validation_acc) | ||
|
||
# Best Acc: 0.9948542024013722 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import numpy as np | ||
import tensorflow as tf | ||
import tflearn | ||
import tflearn.layers.merge_ops | ||
from tflearn.data_utils import shuffle, to_categorical | ||
from tflearn.layers.core import input_data, dropout, fully_connected, flatten, reshape | ||
from tflearn.layers.conv import conv_2d, max_pool_2d | ||
from tflearn.layers.conv import conv_1d, max_pool_1d, avg_pool_1d | ||
from tflearn.layers.estimator import regression | ||
from tflearn.layers.normalization import batch_normalization | ||
import pickle as cPickle | ||
import tflearn.datasets.mnist as mnist | ||
import os | ||
import cv2 | ||
import numpy as np | ||
import scipy.io.wavfile | ||
import random | ||
|
||
|
||
|
||
# length = 20000 | ||
drop_out_prob = 0.5 | ||
def build_tflearn_ann(length): | ||
input_layer = input_data(shape=[None, length, 1]) | ||
|
||
|
||
pool_layer_1 = max_pool_1d(input_layer, 10, name='pool_layer_1') | ||
pool_layer_2 = max_pool_1d(pool_layer_1, 5, name='pool_layer_2') | ||
pool_layer_3 = max_pool_1d(pool_layer_2, 5, name='pool_layer_3') | ||
pool_layer_4 = max_pool_1d(pool_layer_3, 5, name='pool_layer_3') | ||
|
||
fully_connect_1 = fully_connected(pool_layer_3, 512, activation='relu', name='fully_connect_1', | ||
weights_init='xavier', regularizer="L2") | ||
|
||
fully_connect_2 = fully_connected(pool_layer_2, 512, activation='relu', name='fully_connect_2', | ||
weights_init='xavier', regularizer="L2") | ||
|
||
fully_connect_3 = fully_connected(pool_layer_1, 512, activation='relu', name='fully_connect_3', | ||
weights_init='xavier', regularizer="L2") | ||
|
||
fully_connect_4 = fully_connected(pool_layer_4, 512, activation='relu', name='fully_connect_3', | ||
weights_init='xavier', regularizer="L2") | ||
# Merge above layers | ||
merge_layer = tflearn.merge_outputs([fully_connect_1, fully_connect_2, fully_connect_3, fully_connect_4]) | ||
# merge_layer = tflearn.merge_outputs( | ||
# [fully_connect_1, fully_connect_2, fully_connect_3, fully_connect_4, fully_connect_5]) | ||
# merge_layer = tflearn.merge_outputs( | ||
# [fully_connect_1, fully_connect_2, fully_connect_3, fully_connect_4, fully_connect_5, fully_connect_6, | ||
# fully_connect_7, fully_connect_8, fully_connect_9, fully_connect_10]) | ||
drop_2 = dropout(merge_layer, 0.25) | ||
|
||
|
||
fc_layer_4 = fully_connected(drop_2, 2048, activation='relu', name='fc_layer_4', regularizer='L2', | ||
weights_init='xavier', weight_decay=0.001) | ||
drop_2 = dropout(fc_layer_4, drop_out_prob) | ||
|
||
|
||
fc_layer_5 = fully_connected(drop_2, 1024, activation='relu', name='fc_layer_5', regularizer='L2', | ||
weights_init='xavier', weight_decay=0.001) | ||
drop_3 = dropout(fc_layer_5, drop_out_prob) | ||
|
||
fc_layer_6 = fully_connected(drop_3, 128, activation='relu', name='fc_layer_5', regularizer='L2', | ||
weights_init='xavier', weight_decay=0.001) | ||
drop_4 = dropout(fc_layer_6, drop_out_prob) | ||
|
||
# Output | ||
fc_layer_2 = fully_connected(drop_4, 3, activation='softmax', name='output') | ||
network = regression(fc_layer_2, optimizer='adam', loss='softmax_categorical_crossentropy', learning_rate=0.0001, | ||
metric='Accuracy') | ||
model = tflearn.DNN(network) | ||
return model |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import numpy as np | ||
import tensorflow as tf | ||
import tflearn | ||
from tflearn.data_utils import shuffle, to_categorical | ||
from tflearn.layers.core import input_data, dropout, fully_connected, flatten, reshape | ||
from tflearn.layers.conv import conv_2d, max_pool_2d | ||
from tflearn.layers.conv import conv_1d, max_pool_1d, avg_pool_1d | ||
from tflearn.layers.estimator import regression | ||
from tflearn.layers.normalization import batch_normalization | ||
import pickle as cPickle | ||
import tflearn.datasets.mnist as mnist | ||
import os | ||
import cv2 | ||
import numpy as np | ||
import scipy.io.wavfile | ||
import random | ||
|
||
|
||
|
||
drop_out_prob = 0.25 | ||
def build_tflearn_cnn(length): | ||
input_layer = input_data(shape=[None, length, 1]) | ||
|
||
|
||
# Convolution Layer | ||
conv_layer_1 = conv_1d(input_layer, | ||
nb_filter=512, | ||
filter_size=10, | ||
activation='relu', | ||
name='conv_layer_1', | ||
weights_init='xavier', | ||
regularizer="L2") | ||
pool_layer_1 = max_pool_1d(conv_layer_1, 4, name='pool_layer_1') | ||
|
||
|
||
conv_layer_2 = conv_1d(pool_layer_1, | ||
nb_filter=512, | ||
filter_size=5, | ||
activation='relu', | ||
name='conv_layer_2', | ||
weights_init='xavier', | ||
regularizer="L2") | ||
pool_layer_3 = max_pool_1d(conv_layer_2, 4, name='pool_layer_3') | ||
# flat = flatten(pool_layer_3) | ||
|
||
|
||
fc_layer_4 = fully_connected(pool_layer_3, 256, activation='relu', name='fc_layer_4', regularizer='L2') | ||
drop_2 = dropout(fc_layer_4, drop_out_prob) | ||
fc_layer_5 = fully_connected(drop_2, 128, activation='relu', name='fc_layer_5', regularizer='L2') | ||
drop_3 = dropout(fc_layer_5, drop_out_prob) | ||
|
||
# Output | ||
fc_layer_2 = fully_connected(drop_3, 3, activation='softmax', name='output') | ||
network = regression(fc_layer_2, optimizer='adam', loss='softmax_categorical_crossentropy', learning_rate=0.0001, | ||
metric='accuracy') | ||
model = tflearn.DNN(network, | ||
tensorboard_verbose=0) | ||
|
||
return model | ||
|
||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import pickle as cPickle | ||
import tensorflow as tf | ||
import model_audio_ANN | ||
import numpy as np | ||
|
||
|
||
pickle_path = './pickle_data/' | ||
print("loading pickle files for ANN") | ||
with open(pickle_path+"train_data_22k_org.pickle", "rb") as input_file: | ||
x_train = cPickle.load(input_file) | ||
|
||
with open(pickle_path+"train_labels_22k_org.pickle", "rb") as input_file: | ||
y_train = cPickle.load(input_file) | ||
|
||
with open(pickle_path+"test_data_22k_org.pickle", "rb") as input_file: | ||
x_test = cPickle.load(input_file) | ||
|
||
with open(pickle_path+"test_labels_22k_org.pickle", "rb") as input_file: | ||
y_test = cPickle.load(input_file) | ||
|
||
with open(pickle_path + 'valid_data_22k_org.pickle', 'rb') as input_file: | ||
x_valid = cPickle.load(input_file) | ||
|
||
with open(pickle_path + "valid_labels_22k_org.pickle", "rb") as input_file: | ||
y_valid = cPickle.load(input_file) | ||
|
||
|
||
|
||
x_train = np.row_stack([x_train, x_test]) | ||
y_train = np.row_stack([y_train, y_test]) | ||
|
||
|
||
ann_model_dir = './model/ANN/' | ||
|
||
############## | ||
# Train ANN ## | ||
############## | ||
NUM_EPOCHS = 500 | ||
BATCH_SIZE = 64 | ||
MODEL = model_audio_ANN.build_tflearn_ann(x_train.shape[1]) | ||
MODEL.fit(x_train, y_train, n_epoch=NUM_EPOCHS, | ||
shuffle=True, | ||
validation_set=(x_valid, y_valid), | ||
show_metric=True, | ||
batch_size=BATCH_SIZE) | ||
MODEL.save(ann_model_dir+'Bee_audio_ANN.tfl') | ||
|
||
print(MODEL.evaluate(x_test, y_test)) | ||
print(MODEL.evaluate(x_train, y_train)) | ||
|
||
|
||
|
||
# tf.reset_default_graph() | ||
# ann_model_dir = './model/ANN_new/Bee_audio_ANN.tfl' | ||
# ann_model = model_audio_ANN.build_tflearn_ann(x_test.shape[1]) | ||
# ann_model.load(ann_model_dir, weights_only=True, create_new_session = False) | ||
# print(ann_model.evaluate(x_test, y_test)) | ||
# print(ann_model.evaluate(x_train, y_train)) | ||
# validation_acc = ann_model.evaluate(x_test, y_test) | ||
# print(validation_acc) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import model_audio_CNN | ||
import pickle as cPickle | ||
import tensorflow as tf | ||
import numpy as np | ||
import os | ||
|
||
os.environ["CUDA_VISIBLE_DEVICES"]="0,1" | ||
# export CUDA_VISIBLE_DEVICE=0,1 | ||
|
||
pickle_path = './pickle_data/' | ||
print("loading pickle files for CNN") | ||
with open(pickle_path+"train_data_22k_org.pickle", "rb") as input_file: | ||
x_train = cPickle.load(input_file) | ||
|
||
with open(pickle_path+"train_labels_22k_org.pickle", "rb") as input_file: | ||
y_train = cPickle.load(input_file) | ||
|
||
with open(pickle_path+"test_data_22k_org.pickle", "rb") as input_file: | ||
x_test = cPickle.load(input_file) | ||
|
||
with open(pickle_path+"test_labels_22k_org.pickle", "rb") as input_file: | ||
y_test = cPickle.load(input_file) | ||
|
||
|
||
x_train = np.row_stack([x_train, x_test]) | ||
y_train = np.row_stack([y_train, y_test]) | ||
|
||
|
||
with open(pickle_path + 'valid_data_22k_org.pickle', 'rb') as input_file: | ||
x_valid = cPickle.load(input_file) | ||
|
||
# with open(pickle_path+"test_labels_22k_org.pickle", "rb") as input_file: | ||
with open(pickle_path + "valid_labels_22k_org.pickle", "rb") as input_file: | ||
y_valid = cPickle.load(input_file) | ||
|
||
|
||
cnn_model_dir = './model/CNN/' | ||
|
||
############## | ||
# Train CNN ## | ||
############## | ||
NUM_EPOCHS = 500 | ||
BATCH_SIZE = 8 | ||
MODEL = model_audio_CNN.build_tflearn_cnn(x_train.shape[1]) | ||
# with tf.device('/gpu:0'): | ||
MODEL.fit(x_train, y_train, n_epoch=NUM_EPOCHS, | ||
shuffle=True, | ||
validation_set=(x_valid, y_valid), | ||
show_metric=True, | ||
batch_size=BATCH_SIZE, | ||
run_id = 'Bee_audio_CNN_best_4') | ||
MODEL.save(cnn_model_dir+'Bee_audio_CNN.tfl') | ||
|
||
|
||
|
||
|
||
|
||
# ################## | ||
# ## Evaluate CNN ## | ||
# ################## | ||
# tf.reset_default_graph() | ||
|
||
# cnn_model_dir = '/home/vishal/PycharmProjects/bee_audio_project/model/CNN/Bee_audio_CNN_100.tfl' | ||
# # cnn_model = model_audio_CNN.build_tflearn_cnn(x_test.shape[1]) | ||
|
||
# MODEL.load(cnn_model_dir, weights_only=True) | ||
# validation_acc = MODEL.evaluate(x_test, y_test) | ||
# print(validation_acc) |