def setUp(self): super(UtilsTest, self).setUp() tf1.reset_default_graph() self.sess = tf1.Session() tf1.keras.backend.set_session(self.sess) self.flags = Flags() self.flags.desired_samples = 16000 self.flags.window_size_ms = 30.0 self.flags.window_stride_ms = 20.0 self.flags.sample_rate = 16000.0 self.flags.window_stride_samples = 320 self.flags.window_size_samples = 480 self.flags.label_count = 3 self.flags.preemph = 0.0 self.flags.window_type = 'hann' self.flags.mel_num_bins = 40 self.flags.mel_lower_edge_hertz = 20 self.flags.mel_upper_edge_hertz = 4000 self.flags.fft_magnitude_squared = False self.flags.dct_num_features = 10 self.flags.use_tf_fft = False self.flags.units1 = '32' self.flags.act1 = "'relu'" self.flags.pool_size = 2 self.flags.strides = 2 self.flags.dropout1 = 0.1 self.flags.units2 = '256,256' self.flags.act2 = "'relu','relu'" self.flags.train_dir = FLAGS.test_tmpdir self.flags.mel_non_zero_only = 1 self.flags.batch_size = 1 self.model = dnn.model(self.flags) self.model.summary()
def setUp(self): super(UtilsTest, self).setUp() tf1.reset_default_graph() config = tf1.ConfigProto() config.gpu_options.allow_growth = True self.sess = tf1.Session(config=config) tf1.keras.backend.set_session(self.sess)
def test_streaming_on_2d_data_strides(self, stride): """Tests Conv2DTranspose on 2d in streaming mode with different strides. Args: stride: controls the upscaling factor """ tf1.reset_default_graph() config = tf1.ConfigProto() config.gpu_options.allow_growth = True sess = tf1.Session(config=config) tf1.keras.backend.set_session(sess) # model and data parameters step = 1 # amount of data fed into streaming model on every iteration params = test_utils.Params([step], clip_duration_ms=0.25) input_features = 3 # prepare input data: [batch, time, features, channels] x = np.random.rand(1, params.desired_samples, input_features, self.input_channels) inp_audio = x # prepare non-streaming model model = conv2d_transpose_model( params, filters=1, kernel_size=(3, 3), strides=(stride, stride), features=input_features, channels=self.input_channels) model.summary() # set weights with bias for layer in model.layers: if isinstance(layer, tf.keras.layers.Conv2DTranspose): layer.set_weights([ np.ones(layer.weights[0].shape), np.zeros(layer.weights[1].shape) + 0.5 ]) params.data_shape = (1, input_features, self.input_channels) # prepare streaming model model_stream = utils.to_streaming_inference( model, params, modes.Modes.STREAM_INTERNAL_STATE_INFERENCE) model_stream.summary() # run inference non_stream_out = model.predict(inp_audio) stream_out = inference.run_stream_inference(params, model_stream, inp_audio) self.assertAllClose(stream_out, non_stream_out)
def test_streaming_on_1d_data_strides(self, stride): """Tests Conv2DTranspose on 1d in streaming mode with different strides. Args: stride: controls the upscaling factor """ tf1.reset_default_graph() config = tf1.ConfigProto() config.gpu_options.allow_growth = True sess = tf1.Session(config=config) tf1.keras.backend.set_session(sess) # model and data parameters step = 1 # amount of data fed into streaming model on every iteration params = test_utils.Params([step], clip_duration_ms=0.25) # prepare input data: [batch, time, 1, channels] x = np.random.rand(1, params.desired_samples, 1, self.input_channels) inp_audio = x # prepare non-streaming model model = conv2d_transpose_model( params, filters=1, kernel_size=(3, 1), strides=(stride, 1), channels=self.input_channels) model.summary() # set weights with bias for layer in model.layers: if isinstance(layer, tf.keras.layers.Conv2DTranspose): layer.set_weights([ np.ones(layer.weights[0].shape), np.zeros(layer.weights[1].shape) + 0.5 ]) params.data_shape = (1, 1, self.input_channels) # prepare streaming model model_stream = utils.to_streaming_inference( model, params, modes.Modes.STREAM_INTERNAL_STATE_INFERENCE) model_stream.summary() # run inference non_stream_out = model.predict(inp_audio) stream_out = inference.run_stream_inference(params, model_stream, inp_audio) self.assertAllClose(stream_out, non_stream_out) # Convert TF non-streaming model to TFLite external-state streaming model. tflite_streaming_model = utils.model_to_tflite( sess, model, params, modes.Modes.STREAM_EXTERNAL_STATE_INFERENCE) self.assertTrue(tflite_streaming_model) # Run TFLite external-state streaming inference. interpreter = tf.lite.Interpreter(model_content=tflite_streaming_model) interpreter.allocate_tensors() input_details = interpreter.get_input_details() input_states = [] # before processing test sequence we create model state for s in range(len(input_details)): input_states.append(np.zeros(input_details[s]['shape'], dtype=np.float32)) stream_out_tflite_external_st = inference.run_stream_inference_tflite( params, interpreter, inp_audio, input_states, concat=True) # compare streaming TFLite with external-state vs TF non-streaming self.assertAllClose(stream_out_tflite_external_st, non_stream_out)