logging.root.removeHandler(handler) logging.basicConfig(filename="results/training.log", filemode='w', level=logging.INFO) class LogHistory(Callback): def on_epoch_end(self, batch, logs={}): logging.info(logs) model = LSTMAutoencoderV2(batchsize, window_width, encoder_layer_list, decoder_layer_list) files = aux_fn.get_all_files(path) print(['Number of files', len(files)]) data = aux_fn.get_data_from_files(files, sr=sample_rate, ww=window_width) data = np.expand_dims(data, axis=2) with tf.Session() as sess: tf.global_variables_initializer().run() tf.local_variables_initializer().run() train_writer = tf.summary.FileWriter(logs_path, sess.graph) start_time = time.time() for epoch in range(epochs): epoch_loss = 0 minibatch = 0 iteration = int((data.shape[0]) / batchsize)
print("Testing folder to proceed:") print(test_folder) m = hashlib.md5() m.update(model_file.encode('utf-8')) m.update(test_param['test_path'].encode('utf-8')) response_hash = m.hexdigest() HIDDEN_LAYER_COUNT = 8 model = aux_fn.gen_model(window_width, LAYER_SIZE, HIDDEN_LAYER_COUNT) model.load_weights(model_file) with open(yaml_file, 'r') as infile: metadata = yaml.load(infile, Loader=yaml.SafeLoader) files = aux_fn.get_all_files(test_folder) sig_process = {} logging.basicConfig(filename='trained_model/inference.log', filemode='w', level=logging.INFO) for file in tqdm(files): file_time = time() print(file) logging.info(f' inference on {file}: ') filename_wav = os.path.basename(file) for i in range(len(metadata)): if filename_wav in metadata[i]['mixture_audio_filename']: file_meta = metadata[i] break
def main(): log.basicConfig(format="[ %(levelname)s ] %(message)s", level=log.INFO, stream=sys.stdout) args = build_argparser().parse_args() model_xml = args.model model_bin = os.path.splitext(model_xml)[0] + ".bin" with open(args.testparam, 'r') as stream: test_param = yaml.load(stream, Loader=yaml.FullLoader) sample_rate = test_param['sample_rate'] rolling_step = test_param['rolling_step'] # this is rolling step test_folder = test_param['test_path'] + "/audio" yaml_file = test_param['test_path'] + \ "/meta/mixture_recipes_devtest_gunshot.yaml" print("Testing folder to proceed:") print(test_folder) print(model_bin) log.info("Creating Inference Engine...") ie = IECore() if args.cpu_extension and 'CPU' in args.device: ie.add_extension(args.cpu_extension, "CPU") # Read IR log.info("Loading network files:\n\t{}\n\t{}".format(model_xml, model_bin)) net = IENetwork(model=model_xml, weights=model_bin) if "CPU" in args.device: supported_layers = ie.query_network(net, "CPU") not_supported_layers = [ l for l in net.layers.keys() if l not in supported_layers ] if len(not_supported_layers) != 0: log.error( "Following layers are not supported by the plugin for specified device {}:\n {}" .format(args.device, ', '.join(not_supported_layers))) log.error( "Please try to specify cpu extensions library path in sample's command line parameters using -l " "or --cpu_extension command line argument") sys.exit(1) info_input_blob = None input_shape_dims = None feed_dict = {} for blob_name in net.inputs: print(len(net.inputs[blob_name].shape)) if len(net.inputs[blob_name].shape) == 3: input_blob = blob_name input_shape_dims = 3 elif len(net.inputs[blob_name].shape) == 2: info_input_blob = blob_name input_shape_dims = 2 else: raise RuntimeError( "Unsupported {}D input layer '{}'. Only 2D and 4D input layers are supported" .format(len(net.inputs[blob_name].shape), blob_name)) print(len(net.outputs)) assert len(net.outputs) == 1, "Demo supports only single output topologies" out_blob = next(iter(net.outputs)) log.info("Loading IR to the plugin...") exec_net = ie.load_network(network=net, num_requests=1, device_name=args.device) # Read and pre-process input image if input_shape_dims == 3: batch_size, window_width, num_class = net.inputs[input_blob].shape elif input_shape_dims == 2: batch_size, window_width = net.inputs[input_blob].shape else: print("check input shape dimension: ") return print(net.inputs[input_blob].shape) files = aux_fn.get_all_files(test_folder) sig_process = {} file_meta = None with open(yaml_file, 'r') as infile: metadata = yaml.load(infile, Loader=yaml.SafeLoader) for file in tqdm(files): file_time = time() print(file) logging.info(f' inference on {file}: ') filename_wav = os.path.basename(file) for i in range(len(metadata)): if filename_wav in metadata[i]['mixture_audio_filename']: file_meta = metadata[i] break data = load_data(window_width, file, sample_rate, rolling_step) if input_shape_dims == 3: data = np.expand_dims(data, axis=2) iteration = data.shape[0] pred = [] for iterate in range(iteration): print(input_blob) if input_shape_dims == 3: feed_dict[input_blob] = data[iterate, :, :] else: feed_dict[input_blob] = data[iterate, :] # inf_start = time.time() exec_net.start_async(request_id=0, inputs=feed_dict) if exec_net.requests[0].wait(-1) == 0: # inf_end = time.time() # det_time = inf_end - inf_start res = exec_net.requests[0].outputs[out_blob] print("result shape", res.shape) pred.append(list(res)) sig_process[filename_wav] = get_response(data, pred, file_meta, sample_rate, rolling_step) tmp_time = time() - file_time logging.info( f" \tfile inference time = {tmp_time:.2f} s, {(tmp_time / 117) * 1000:.4f} ms per sample" )