<commit_before># -*- coding: utf-8 -*-
from utils.ops import unpool, variable_summaries, get_scope_variable, scope, kl_div
import os
import config
import tensorflow as tf
from itertools import combinations, permutations
from tensorflow.python.saved_model import builder as saved_model_builder
import numpy as np
from network import Network

#############################################
#     Adaptive Front and Back End Model     #
#############################################
		
class Adapt(Network):
	def __init__(self, *args, **kwargs):
		##
		## Model Configuration 
		##
		super(Adapt, self).__init__(*args, **kwargs)

		if kwargs is not None:
			self.N = kwargs['filters']
			self.max_pool_value = kwargs['max_pool']
			self.l = kwargs['regularization']
			self.beta = kwargs['beta']
			self.p = kwargs['sparsity']
			self.window = kwargs['window_size']
			self.pretraining = kwargs['pretraining']
			self.overlap_coef = kwargs['overlap_coef']
			self.overlap_value = kwargs['overlap_value']
			self.loss = kwargs['loss']
			self.separation = kwargs['separation']
			self.with_max_pool = kwargs['with_max_pool']
			self.with_average_pool = kwargs['with_average_pool']
			self.hop_size = kwargs['hop_size']

		with tf.get_default_graph().as_default():

			with tf.name_scope('preprocessing'):
				if self.pretraining:
					self.x = tf.concat([self.x_mix,  tf.reshape(self.x_non_mix, [self.B*self.S, self.L])], axis=0)
					self.B_tot = tf.shape(self.x)[0]
				else:

					self.x =tf.concat([self.x_mix,  tf.reshape(self.x_non_mix, [self.B*self.S, self.L])], axis=0)
					self.B_tot = self.B*(self.S+1)
					
			if self.pretraining:
				self.front
				self.separator
				self.back
				self.cost_model = self.cost
				self.finish_construction()
				self.optimize
			else:
				# self.sepNet = kwargs['separator']
				self.front

	def create_centroids_saver(self):
		self.centroids_saver = tf.train.Saver([self.sepNet.speaker_vectors], max_to_keep=10000000)

	def savedModel(self):
		path = os.path.join(config.log_dir,self.folder ,self.runID, 'SavedModel')
		builder = saved_model_builder.SavedModelBuilder(path)

		# Build signatures
		input_tensor_info = tf.saved_model.utils.build_tensor_info(self.x_mix)
		output_tensor_info = tf.saved_model.utils.build_tensor_info(self.back)

		signature = tf.saved_model.signature_def_utils.build_signature_def(
			inputs={
				'mixed_audio':
				input_tensor_info
			},
			outputs={
				'unmixed_audio':
				output_tensor_info
			}
			)


		builder.add_meta_graph_and_variables(
			self.sess, ['validating'],
			signature_def_map={'separate_audio':signature}
			)

		builder.save()
		print 'Successfully exported model to %s' % path

	##
	## Front End creating STFT like data
	##
	@scope
	def front(self):
		# Front-End

		# x : [ Btot , 1, L , 1]
		# Equivalent to B_tot batches of image of height = 1, width = L and 1 channel -> for Conv1D with Conv2D
		input_front =  tf.reshape(self.x, [self.B_tot, 1, self.L, 1])

		# Filter [filter_height, filter_width, input_channels, output_channels] = [1, W, 1, N]
		self.window_filter = get_scope_variable('window', 'w', shape=[self.window], initializer=tf.contrib.layers.xavier_initializer_conv2d())
		self.bases = get_scope_variable('bases', 'bases', shape=[self.window, self.N], initializer=tf.contrib.layers.xavier_initializer_conv2d())
		self.conv_filter = tf.reshape(tf.abs(tf.expand_dims(self.window_filter,1))*self.bases , [1, self.window, 1, self.N])
		# self.conv_filter = get_scope_variable('filters_front','filters_front', shape=[1, self.window, 1, self.N])
		variable_summaries(self.conv_filter)

		# 1 Dimensional convolution along T axis with a window length = self.window
		# And N = 256 filters -> Create a [Btot, 1, T, N]
		self.T = tf.shape(input_front)[2]

		if self.with_max_pool:
			self.X = tf.nn.conv2d(input_front, self.conv_filter, strides=[1, 1, 1, 1], padding="SAME", name='Conv_STFT')
			self.y, self.argmax = tf.nn.max_pool_with_argmax(self.X, [1, 1, self.max_pool_value, 1], 
										strides=[1, 1, self.max_pool_value, 1], padding="VALID", name='output')
			print self.argmax
		elif self.with_average_pool:
			self.X = tf.nn.conv2d(input_front, self.conv_filter, strides=[1, 1, 1, 1], padding="SAME", name='Conv_STFT')
			self.y = tf.layers.average_pooling2d(self.X, (1, self.max_pool_value), strides=(1, self.hop_size), name='output')		
		else:
			self.y = tf.nn.conv2d(input_front, self.conv_filter, strides=[1, 1, self.hop_size, 1], padding="SAME", name='Conv_STFT')

		# Reshape to Btot batches of T x N images with 1 channel
		# [Btot, 1, T_pool, N] -> [Btot, T-pool, N, 1]
		self.y = tf.transpose(self.y,[0, 2, 3, 1], name='output')

		tf.summary.image('front/output', self.y, max_outputs=3)
		y_shape = tf.shape(self.y)
		y = tf.reshape(self.y, [self.B_tot, y_shape[1]*y_shape[2]])<commit_msg>fix hop<commit_after># -*- coding: utf-8 -*-
from utils.ops import unpool, variable_summaries, get_scope_variable, scope, kl_div
import os
import config
import tensorflow as tf
from itertools import combinations, permutations
from tensorflow.python.saved_model import builder as saved_model_builder
import numpy as np
from network import Network

#############################################
#     Adaptive Front and Back End Model     #
#############################################
		
class Adapt(Network):
	def __init__(self, *args, **kwargs):
		##
		## Model Configuration 
		##
		super(Adapt, self).__init__(*args, **kwargs)

		if kwargs is not None:
			self.N = kwargs['filters']
			self.max_pool_value = kwargs['max_pool']
			self.l = kwargs['regularization']
			self.beta = kwargs['beta']
			self.p = kwargs['sparsity']
			self.window = kwargs['window_size']
			self.pretraining = kwargs['pretraining']
			self.overlap_coef = kwargs['overlap_coef']
			self.overlap_value = kwargs['overlap_value']
			self.loss = kwargs['loss']
			self.separation = kwargs['separation']
			self.with_max_pool = kwargs['with_max_pool']
			self.with_average_pool = kwargs['with_average_pool']
			self.hop_size = kwargs['hop_size']

		with tf.get_default_graph().as_default():

			with tf.name_scope('preprocessing'):
				if self.pretraining:
					self.x = tf.concat([self.x_mix,  tf.reshape(self.x_non_mix, [self.B*self.S, self.L])], axis=0)
					self.B_tot = tf.shape(self.x)[0]
				else:

					self.x =tf.concat([self.x_mix,  tf.reshape(self.x_non_mix, [self.B*self.S, self.L])], axis=0)
					self.B_tot = self.B*(self.S+1)
					
			if self.pretraining:
				self.front
				self.separator
				self.back
				self.cost_model = self.cost
				self.finish_construction()
				self.optimize
			else:
				# self.sepNet = kwargs['separator']
				self.front

	def create_centroids_saver(self):
		self.centroids_saver = tf.train.Saver([self.sepNet.speaker_vectors], max_to_keep=10000000)

	def savedModel(self):
		path = os.path.join(config.log_dir,self.folder ,self.runID, 'SavedModel')
		builder = saved_model_builder.SavedModelBuilder(path)

		# Build signatures
		input_tensor_info = tf.saved_model.utils.build_tensor_info(self.x_mix)
		output_tensor_info = tf.saved_model.utils.build_tensor_info(self.back)

		signature = tf.saved_model.signature_def_utils.build_signature_def(
			inputs={
				'mixed_audio':
				input_tensor_info
			},
			outputs={
				'unmixed_audio':
				output_tensor_info
			}
			)


		builder.add_meta_graph_and_variables(
			self.sess, ['validating'],
			signature_def_map={'separate_audio':signature}
			)

		builder.save()
		print 'Successfully exported model to %s' % path

	##
	## Front End creating STFT like data
	##
	@scope
	def front(self):
		# Front-End

		# x : [ Btot , 1, L , 1]
		# Equivalent to B_tot batches of image of height = 1, width = L and 1 channel -> for Conv1D with Conv2D
		input_front =  tf.reshape(self.x, [self.B_tot, 1, self.L, 1])

		# Filter [filter_height, filter_width, input_channels, output_channels] = [1, W, 1, N]
		self.window_filter = get_scope_variable('window', 'w', shape=[self.window], initializer=tf.contrib.layers.xavier_initializer_conv2d())
		self.bases = get_scope_variable('bases', 'bases', shape=[self.window, self.N], initializer=tf.contrib.layers.xavier_initializer_conv2d())
		self.conv_filter = tf.reshape(tf.abs(tf.expand_dims(self.window_filter,1))*self.bases , [1, self.window, 1, self.N])
		# self.conv_filter = get_scope_variable('filters_front','filters_front', shape=[1, self.window, 1, self.N])
		variable_summaries(self.conv_filter)

		# 1 Dimensional convolution along T axis with a window length = self.window
		# And N = 256 filters -> Create a [Btot, 1, T, N]
		self.T = tf.shape(input_front)[2]

		if self.with_max_pool:
			self.X = tf.nn.conv2d(input_front, self.conv_filter, strides=[1, 1, 1, 1], padding="SAME", name='Conv_STFT')
			self.y, self.argmax = tf.nn.max_pool_with_argmax(self.X, [1, 1, self.max_pool_value, 1], 
										strides=[1, 1, self.hop_size, 1], padding="VALID", name='output')
			print self.argmax
		elif self.with_average_pool:
			self.X = tf.nn.conv2d(input_front, self.conv_filter, strides=[1, 1, 1, 1], padding="SAME", name='Conv_STFT')
			self.y = tf.layers.average_pooling2d(self.X, (1, self.max_pool_value), strides=(1, self.hop_size), name='output')		
		else:
			self.y = tf.nn.conv2d(input_front, self.conv_filter, strides=[1, 1, self.hop_size, 1], padding="SAME", name='Conv_STFT')

		# Reshape to Btot batches of T x N images with 1 channel
		# [Btot, 1, T_pool, N] -> [Btot, T-pool, N, 1]
		self.y = tf.transpose(self.y,[0, 2, 3, 1], name='output')

		tf.summary.image('front/output', self.y, max_outputs=3)
		y_shape = tf.shape(self.y)
		y = tf.reshape(self.y, [self.B_tot, y_shape[1]*y_shape[2]])
{'fraction_non_alphanumeric': 0.1588125389246419, 'fraction_numerical': 0.014324268216732406, 'mean_word_length': 4.394736842105263, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '865222', 'n_tokens_mistral': 3964, 'n_tokens_neox': 3483, 'n_words': 694}