import tensorflow as tf from tensorflow.python.ops import init_ops from odin import training from odin.utils import (args_parse, ctext, Progbar, as_tuple_of_shape, crypto, stdio) from odin import fuel as F, visual as V, nnet as N, backend as K from helpers import (get_model_path, prepare_dnn_data, BATCH_SIZE, EPOCH, LEARNING_RATE, SCORE_SYSTEM_ID, GRADIENT_CLIPPING) # =========================================================================== # Create data feeder # =========================================================================== (EXP_DIR, MODEL_PATH, LOG_PATH) = get_model_path(system_name='xvec') stdio(LOG_PATH) # ====== load the data ====== # (train, valid, all_speakers) = prepare_dnn_data(save_dir=EXP_DIR) n_speakers = len(all_speakers) # ====== print some log ====== # print("Training info:") print(' ', "Batch size :", ctext(BATCH_SIZE, 'cyan')) print(' ', "Epoch :", ctext(EPOCH, 'cyan')) # =========================================================================== # Create the network # =========================================================================== inputs = [K.placeholder(shape=(None,) + shape[1:], dtype='float32', name='input%d' % i)
# We add 1 class for background hyper_params["total_labels"] = len(labels) + 1 # If you want to use different dataset and don't know max height and width values # You can use calculate_max_height_width method in helpers max_height, max_width = helpers.VOC["max_height"], helpers.VOC["max_width"] VOC_test_data = VOC_test_data.map(lambda x : helpers.preprocessing(x, max_height, max_width)) padded_shapes, padding_values = helpers.get_padded_batch_params() VOC_test_data = VOC_test_data.padded_batch(batch_size, padded_shapes=padded_shapes, padding_values=padding_values) base_model = VGG16(include_top=False) if hyper_params["stride"] == 16: base_model = Sequential(base_model.layers[:-1]) rpn_model = rpn.get_model(base_model, hyper_params) frcnn_model_path = helpers.get_model_path("frcnn", hyper_params["stride"]) rpn_model_path = helpers.get_model_path("rpn", hyper_params["stride"]) model_path = frcnn_model_path if load_weights_from_frcnn else rpn_model_path rpn_model.load_weights(model_path, by_name=True) for image_data in VOC_test_data: img, gt_boxes, gt_labels = image_data input_img, anchors = rpn.get_step_data(image_data, hyper_params, preprocess_input, mode="inference") rpn_bbox_deltas, rpn_labels = rpn_model.predict_on_batch(input_img) # anchors_shape = tf.shape(anchors) batch_size, anchor_row_size = anchors_shape[0], anchors_shape[1] rpn_bbox_deltas = tf.reshape(rpn_bbox_deltas, (batch_size, anchor_row_size, 4)) rpn_labels = tf.reshape(rpn_labels, (batch_size, anchor_row_size, 1)) # rpn_bboxes = helpers.get_bboxes_from_deltas(anchors, rpn_bbox_deltas)
n_indices = len(indices) indices.close() except Exception as e: import traceback traceback.print_exc() print("Loading indices error: '%s'" % str(e), "at:", indices_path) return True if n_indices != n_files: return True return False # =========================================================================== # Searching for trained system # =========================================================================== sys_dir, _, _ = get_model_path(system_name=SCORE_SYSTEM_NAME, logging=False) sys_name = os.path.basename(sys_dir) all_sys = [] for path in os.listdir(sys_dir): path = os.path.join(sys_dir, path) if 'model.ai.' in path: all_sys.append(path) # ====== get the right model based on given system index ====== # if len(all_sys) == 0: final_sys = os.path.join(sys_dir, 'model.ai') sys_index = '' assert os.path.exists(final_sys), \ "Cannot find pre-trained model at path: %s" % sys_dir else: all_sys = sorted(all_sys, key=lambda x: int(x.split('.')[-1])) final_sys = all_sys[SCORE_SYSTEM_ID]