def create_model(self, n_dim, r): # load inputs X, _, _ = self.inputs K.set_session(self.sess) with tf.name_scope('generator'): x = X L = self.layers n_filters = [ 128, 256, 512, 512, 512, 512, 512, 512] n_filtersizes = [65, 33, 17, 9, 9, 9, 9, 9, 9] downsampling_l = [] print 'building model...' # downsampling layers for l, nf, fs in zip(range(L), n_filters, n_filtersizes): with tf.name_scope('downsc_conv%d' % l): conv1d = Conv1D(filters=nf, kernel_size=fs, padding='same', kernel_initializer='orthogonal', strides=2) x = conv1d(x) x = LeakyReLU(0.2)(x) print 'D-Block: ', x.get_shape() downsampling_l.append(x) # bottleneck layer with tf.name_scope('bottleneck_conv'): conv1d = Conv1D(filters=n_filters[-1], kernel_size=n_filtersizes[-1], padding='same', kernel_initializer='orthogonal', strides=2) x = conv1d(x) x = Dropout(rate=0.5)(x) x = LeakyReLU(0.2)(x) # upsampling layers for l, nf, fs, l_in in reversed(zip(range(L), n_filters, n_filtersizes, downsampling_l)): with tf.name_scope('upsc_conv%d' % l): conv1d = Conv1D(filters=2*nf, kernel_size=fs, padding='same', kernel_initializer='orthogonal') x = (conv1d)(x) x = Dropout(rate=0.5)(x) x = Activation('relu')(x) x = SubPixel1D(x, r=2) x = Concatenate()([x, l_in]) print 'U-Block: ', x.get_shape() # final conv layer with tf.name_scope('lastconv'): x = Conv1D(filters=2, kernel_size=9, padding='same', kernel_initializer='random_normal')(x) x = SubPixel1D(x, r=2) print x.get_shape() g = Add()([x, X]) return g
def __init__(self, in_channels, channel_sizes, bottleneck_channels, use_bottleneck, general_args): """ Initializes the class UpBlock that inherits its main properties from BaseBlock. UpBlock is the main ingredient of the decoding part of both the generator and the auto-encoder. :param in_channels: number of channels of the input tensor (scalar int). :param channel_sizes: number of filters for each scale of the multi-scale convolution (list of scalar int). :param bottleneck_channels: number of filters for each of the multi-scale bottleneck convolution. :param use_bottleneck: boolean indicating whether to use the bottleneck channels or not. :param general_args: argument parser that contains the arguments that are independent to the script being executed. """ super(UpBlock, self).__init__(in_channels, general_args.kernel_sizes, channel_sizes, bottleneck_channels, use_bottleneck) self.subpixel = SubPixel1D(in_channels=sum(channel_sizes), out_channels=sum(channel_sizes) // general_args.upscale_factor, upscale_factor=general_args.upscale_factor) self.dropout = nn.Dropout(general_args.dropout_probability) self.activation = nn.PReLU(sum(channel_sizes))
def audiounet_processing(x_inp, params): n_dim = int(x_inp.get_shape()[1]) x = x_inp L = params['layers'] r = params['r'] additive_skip_connection = params['additive_skip_connection'] n_filtersizes = params['n_filtersizes'] # dim/layer è sempre : n_dim/2 , n_dim/4, n_dim/8, ... #Il numero di canali dipende invece da n_filters. if n_dim == 4096: print('n_dim = 4096') n_filters = params['n_filters_spectral_branch'] elif n_dim == 8192: print('n_dim = 8192') n_filters = params['n_filters_time_branch'] else: print('I need other n_dim') return None downsampling_l = [] # downsampling layers for l, nf, fs in zip(range(L), n_filters, n_filtersizes): with tf.name_scope('downsc_conv%d' % l): x = (Convolution1D( nb_filter=nf, filter_length=fs, activation=None, border_mode='same', input_dim=(n_dim, 1), subsample_length=2, kernel_initializer=tf.orthogonal_initializer()))( x) #init=orthogonal_init, # if l > 0: x = BatchNormalization(mode=2)(x) x = LeakyReLU(0.2)(x) print('D-Block: {}'.format(x.get_shape())) downsampling_l.append(x) #print(x) # bottleneck layer with tf.name_scope('bottleneck_conv'): x = (Convolution1D(nb_filter=n_filters[-1], filter_length=n_filtersizes[-1], activation=None, border_mode='same', subsample_length=2, kernel_initializer=tf.orthogonal_initializer()))( x) #init=orthogonal_init, x = Dropout(p=0.5)(x) x = LeakyReLU(0.2)(x) print('B-Block: {}'.format(x.get_shape())) # upsampling layers for l, nf, fs, l_in in list( zip(range(L), n_filters, n_filtersizes, downsampling_l))[::-1]: #print(l, nf, fs, l_in) with tf.name_scope('upsc_conv%d' % l): # (-1, n/2, 2f) x = (Convolution1D( nb_filter=2 * nf, filter_length=fs, activation=None, border_mode='same', kernel_initializer=tf.orthogonal_initializer()))( x) #init=orthogonal_init, x = Dropout(p=0.5)(x) x = Activation('relu')(x) # (-1, n, f) x = SubPixel1D(x, r=2) # (-1, n, 2f) x = K.concatenate(tensors=[x, l_in], axis=2) print('U-Block: {}'.format(x.get_shape())) # final conv layer with tf.name_scope('lastconv'): x = Convolution1D(nb_filter=2, filter_length=9, activation=None, border_mode='same', kernel_initializer=tf.orthogonal_initializer())( x) #init=orthogonal_init, x = SubPixel1D(x, r=2) if additive_skip_connection == True: x = tf.add(x, x_inp) return x #x è (?, 8192). // con tf.reshape(x, [-1, n_dim]) aggiungo un canale alla fine -> diventerebbe (?, 8192, 1)
def tfilm_processing(x_inp, params): n_dim = int(x_inp.get_shape()[1]) DRATE = 2 # load inputs x = x_inp L = params['layers'] r = params['r'] additive_skip_connection = params['additive_skip_connection'] n_filtersizes = params['n_filtersizes'] # dim/layer è sempre : n_dim/2 , n_dim/4, n_dim/8, ... #Il numero di canali dipende invece da n_filters. if n_dim == 4096: print('n_dim = 4096') n_filters = params['n_filters_spectral_branch'] elif n_dim == 8192: print('n_dim = 8192') n_filters = params['n_filters_time_branch'] else: print('I need other n_dim') return None downsampling_l = [] # downsampling layers for l, nf, fs in zip(range(L), n_filters, n_filtersizes): with tf.name_scope('downsc_conv%d' % l): x = (AtrousConvolution1D( nb_filter=nf, filter_length=fs, atrous_rate=DRATE, activation=None, border_mode='same', subsample_length=1, kernel_initializer=tf.orthogonal_initializer()))( x) #init=orthogonal_init x = (MaxPooling1D(pool_length=2, border_mode='valid'))(x) x = LeakyReLU(0.2)(x) # create and apply the normalizer nb = 128 / (2**l) params_before = np.sum([ np.prod(v.get_shape().as_list()) for v in tf.trainable_variables() ]) x_norm = _make_normalizer(x, nf, nb) params_after = np.sum([ np.prod(v.get_shape().as_list()) for v in tf.trainable_variables() ]) x = _apply_normalizer(x, x_norm, nf, nb) print('D-Block: {}'.format(x.get_shape())) downsampling_l.append(x) # bottleneck layer with tf.name_scope('bottleneck_conv'): x = (AtrousConvolution1D( nb_filter=n_filters[-1], filter_length=n_filtersizes[-1], atrous_rate=DRATE, activation=None, border_mode='same', subsample_length=1, kernel_initializer=tf.orthogonal_initializer()))( x) #init=orthogonal_init x = (MaxPooling1D(pool_length=2, border_mode='valid'))(x) x = Dropout(p=0.5)(x) x = LeakyReLU(0.2)(x) # create and apply the normalizer nb = 128 / (2**L) x_norm = _make_normalizer(x, n_filters[-1], nb) x = _apply_normalizer(x, x_norm, n_filters[-1], nb) print('B-Block: {}'.format(x.get_shape())) # upsampling layers for l, nf, fs, l_in in list( zip(range(L), n_filters, n_filtersizes, downsampling_l))[::-1]: with tf.name_scope('upsc_conv%d' % l): x = (AtrousConvolution1D( nb_filter=2 * nf, filter_length=fs, atrous_rate=DRATE, activation=None, border_mode='same', kernel_initializer=tf.orthogonal_initializer()))( x) #init=orthogonal_init x = Dropout(p=0.5)(x) x = Activation('relu')(x) x = SubPixel1D(x, r=2) # create and apply the normalizer x_norm = _make_normalizer(x, nf, nb) x = _apply_normalizer(x, x_norm, nf, nb) x = K.concatenate(tensors=[x, l_in], axis=2) print('U-Block: {}'.format(x.get_shape())) with tf.name_scope('lastconv'): x = Convolution1D(nb_filter=2, filter_length=9, activation=None, border_mode='same', kernel_initializer=tf.keras.initializers.normal())( x) #, init=normal_init x = SubPixel1D(x, r=2) if additive_skip_connection == True: x = tf.add(x, x_inp) return x #x è (?, 8192). // con tf.reshape(x, [-1, n_dim]) aggiungo un canale alla fine -> diventerebbe (?, 8192, 1)
def create_model(self, n_dim, r): # load inputs X, _, _ = self.inputs K.set_session(self.sess) with tf.name_scope('generator'): x = X L = self.layers n_filters = [128, 256, 512, 512, 512, 512, 512, 512] n_blocks = [128, 64, 32, 16, 8] n_filtersizes = [65, 33, 17, 9, 9, 9, 9, 9, 9] downsampling_l = [] print 'building model...' def _make_normalizer(x_in, n_filters, n_block): """applies an lstm layer on top of x_in""" x_shape = tf.shape(x_in) n_steps = x_shape[1] / n_block # will be 32 at training # first, apply standard conv layer to reduce the dimension # input of (-1, 4096, 128) becomes (-1, 32, 128) # input of (-1, 512, 512) becomes (-1, 32, 512) x_in_down = (MaxPooling1D(pool_length=n_block, border_mode='valid'))(x_in) # pooling to reduce dimension x_shape = tf.shape(x_in_down) x_rnn = LSTM(output_dim=n_filters, return_sequences=True)(x_in_down) # output: (-1, n_steps, n_filters) return x_rnn def _apply_normalizer(x_in, x_norm, n_filters, n_block): x_shape = tf.shape(x_in) n_steps = x_shape[1] / n_block # will be 32 at training # reshape input into blocks x_in = tf.reshape(x_in, shape=(-1, n_steps, n_block, n_filters)) x_norm = tf.reshape(x_norm, shape=(-1, n_steps, 1, n_filters)) # multiply x_out = x_norm * x_in # return to original shape x_out = tf.reshape(x_out, shape=x_shape) return x_out # downsampling layers for l, nf, fs in zip(range(L), n_filters, n_filtersizes): with tf.name_scope('downsc_conv%d' % l): x = (AtrousConvolution1D(nb_filter=nf, filter_length=fs, atrous_rate=DRATE, activation=None, border_mode='same', init=orthogonal_init, subsample_length=1))(x) x = (MaxPooling1D(pool_length=2, border_mode='valid'))(x) x = LeakyReLU(0.2)(x) # create and apply the normalizer nb = 128 / (2**l) params_before = np.sum([ np.prod(v.get_shape().as_list()) for v in tf.trainable_variables() ]) x_norm = _make_normalizer(x, nf, nb) params_after = np.sum([ np.prod(v.get_shape().as_list()) for v in tf.trainable_variables() ]) x = _apply_normalizer(x, x_norm, nf, nb) print 'D-Block: ', x.get_shape() downsampling_l.append(x) # bottleneck layer with tf.name_scope('bottleneck_conv'): x = (AtrousConvolution1D(nb_filter=n_filters[-1], filter_length=n_filtersizes[-1], atrous_rate=DRATE, activation=None, border_mode='same', init=orthogonal_init, subsample_length=1))(x) x = (MaxPooling1D(pool_length=2, border_mode='valid'))(x) x = Dropout(p=0.5)(x) x = LeakyReLU(0.2)(x) # create and apply the normalizer nb = 128 / (2**L) x_norm = _make_normalizer(x, n_filters[-1], nb) x = _apply_normalizer(x, x_norm, n_filters[-1], nb) # upsampling layers for l, nf, fs, l_in in reversed( zip(range(L), n_filters, n_filtersizes, downsampling_l)): with tf.name_scope('upsc_conv%d' % l): # (-1, n/2, 2f) x = (AtrousConvolution1D(nb_filter=2 * nf, filter_length=fs, atrous_rate=DRATE, activation=None, border_mode='same', init=orthogonal_init))(x) x = Dropout(p=0.5)(x) x = Activation('relu')(x) # (-1, n, f) x = SubPixel1D(x, r=2) # create and apply the normalizer x_norm = _make_normalizer(x, nf, nb) x = _apply_normalizer(x, x_norm, nf, nb) # (-1, n, 2f) x = merge([x, l_in], mode='concat', concat_axis=-1) print 'U-Block: ', x.get_shape() # final conv layer with tf.name_scope('lastconv'): x = Convolution1D(nb_filter=2, filter_length=9, activation=None, border_mode='same', init=normal_init)(x) x = SubPixel1D(x, r=2) g = merge([x, X], mode='sum') return g
def create_model(self, n_dim, r): # load inputs X, _, _ = self.inputs K.set_session(self.sess) with tf.name_scope('generator'): x = X L = self.layers # dim/layer: 4096, 2048, 1024, 512, 256, 128, 64, 32, # n_filters = [ 64, 128, 256, 384, 384, 384, 384, 384] n_filters = [ 128, 256, 512, 512, 512, 512, 512, 512] # n_filters = [ 256, 512, 512, 512, 512, 1024, 1024, 1024] # n_filtersizes = [129, 65, 33, 17, 9, 9, 9, 9] # n_filtersizes = [31, 31, 31, 31, 31, 31, 31, 31] n_filtersizes = [65, 33, 17, 9, 9, 9, 9, 9, 9] downsampling_l = [] print 'building model...' # downsampling layers for l, nf, fs in zip(range(L), n_filters, n_filtersizes): with tf.name_scope('downsc_conv%d' % l): '''x = (Convolution1D(nb_filter=nf, filter_length=fs, activation=None, border_mode='same', init=orthogonal_init, subsample_length=2))(x)''' x = Conv1D(filters=nf, kernel_size=fs, activation=None, padding='same', kernel_initializer='Orthogonal', strides=2)(x) # if l > 0: x = BatchNormalization(mode=2)(x) x = LeakyReLU(0.2)(x) print 'D-Block: ', x.get_shape() downsampling_l.append(x) # bottleneck layer with tf.name_scope('bottleneck_conv'): '''x = (Convolution1D(nb_filter=n_filters[-1], filter_length=n_filtersizes[-1], activation=None, border_mode='same', init=orthogonal_init, subsample_length=2))(x)''' x = Conv1D(filters=n_filters[-1], kernel_size=n_filtersizes[-1], activation=None, padding='same', kernel_initializer='Orthogonal', strides=2)(x) #x = Dropout(p=0.5)(x) x = Dropout(rate=0.5)(x) # x = BatchNormalization(mode=2)(x) x = LeakyReLU(0.2)(x) # upsampling layers for l, nf, fs, l_in in reversed(zip(range(L), n_filters, n_filtersizes, downsampling_l)): with tf.name_scope('upsc_conv%d' % l): # (-1, n/2, 2f) '''x = (Convolution1D(nb_filter=2*nf, filter_length=fs, activation=None, border_mode='same', init=orthogonal_init))(x)''' x = Conv1D(filters=2*nf, kernel_size=fs, activation=None, padding='same', kernel_initializer='Orthogonal')(x) # x = BatchNormalization(mode=2)(x) #x = Dropout(p=0.5)(x) x = Dropout(rate=0.5)(x) x = Activation('relu')(x) # (-1, n, f) x = SubPixel1D(x, r=2) # (-1, n, 2f) '''changed merge to new keras.layers.concact''' x = concatenate([x, l_in], axis=-1) #x = merge([x, l_in], mode='concat', concat_axis=-1) print 'U-Block: ', x.get_shape() #print 'U-Block: ', x.output_shape # final conv layer with tf.name_scope('lastconv'): '''x = Convolution1D(nb_filter=2, filter_length=9, activation=None, border_mode='same', init=normal_init)(x)''' x = Conv1D(filters=2, kernel_size=9, activation=None, padding='same', kernel_initializer='RandomNormal')(x) x = SubPixel1D(x, r=2) print x.get_shape() #g = merge([x, X], mode='sum') g = add([x, X]) return g
def create_model(self, n_dim, r): # load inputs X, _, _ = self.inputs K.set_session(self.sess) with tf.name_scope('generator'): x = X L = self.layers # dim/layer: 4096, 2048, 1024, 512, 256, 128, 64, 32, n_filters = [128, 384, 512, 512, 512, 512, 512, 512] n_filtersizes = [65, 33, 17, 9, 9, 9, 9, 9, 9] downsampling_l = [] print 'building model...' # downsampling layers for l, nf, fs in zip(range(L), n_filters, n_filtersizes): with tf.name_scope('downsc_conv%d' % l): x = (Convolution1D(nb_filter=nf, filter_length=fs, activation=None, border_mode='same', init=orthogonal_init, subsample_length=2))(x) # if l > 0: x = BatchNormalization(mode=2)(x) x = LeakyReLU(0.2)(x) print 'D-Block: ', x.get_shape() downsampling_l.append(x) # bottleneck layer with tf.name_scope('bottleneck_conv'): x = (Convolution1D(nb_filter=n_filters[-1], filter_length=n_filtersizes[-1], activation=None, border_mode='same', init=orthogonal_init, subsample_length=2))(x) x = Dropout(p=0.5)(x) x = LeakyReLU(0.2)(x) # upsampling layers for l, nf, fs, l_in in reversed( zip(range(L), n_filters, n_filtersizes, downsampling_l)): with tf.name_scope('upsc_conv%d' % l): # (-1, n/2, 2f) x = (Convolution1D(nb_filter=2 * nf, filter_length=fs, activation=None, border_mode='same', init=orthogonal_init))(x) x = Dropout(p=0.5)(x) x = Activation('relu')(x) # (-1, n, f) x = SubPixel1D(x, r=2) # (-1, n, 2f) x = K.concatenate(tensors=[x, l_in], axis=2) print 'U-Block: ', x.get_shape() # final conv layer with tf.name_scope('lastconv'): x = Convolution1D(nb_filter=2, filter_length=9, activation=None, border_mode='same', init=normal_init)(x) x = SubPixel1D(x, r=2) print x.get_shape() g = merge([x, X], mode='sum') return g