def decode(self, code):
        with tf.variable_scope('unpool1'):
            unpool1 = upsample(code)
            deconv1 = deconv(unpool1, [512, 512], 'deconv5_3')
            deconv2 = deconv(deconv1, [512, 512], 'deconv5_2')
            deconv3 = deconv(deconv2, [512, 512], 'deconv5_1')

        with tf.variable_scope('unpool2'):
            unpool2 = upsample(deconv3)
            deconv4 = deconv(unpool2, [512, 512], 'deconv4_3')
            deconv5 = deconv(deconv4, [512, 512], 'deconv4_2')
            deconv6 = deconv(deconv5, [256, 512], 'deconv4_1')

        with tf.variable_scope('unpool3'):
            unpool3 = upsample(deconv6, self.indices[2])
            deconv7 = deconv(unpool3, [256, 256], 'deconv3_3')
            deconv8 = deconv(deconv7, [256, 256], 'deconv3_2')
            deconv9 = deconv(deconv8, [128, 256], 'deconv3_1')

        with tf.variable_scope('unpool4'):
            unpool4 = upsample(deconv9, self.indices[1])
            deconv10 = deconv(unpool4, [128, 128], 'deconv2_2')
            deconv11 = deconv(deconv10, [64, 128], 'deconv2_1')

        with tf.variable_scope('unpool5'):
            unpool5 = upsample(deconv11, self.indices[0])
            deconv12 = deconv(unpool5, [64, 64], 'deconv1_2')
            deconv13 = deconv(deconv12, [32, 64], 'deconv1_1')

        with tf.variable_scope('fc', reuse=True):
            deconv13_flat, deconv_13_features = flatten_layer(deconv13)
            logits = fc(deconv13_flat, [deconv13_features, FLAGS.labels_count],
                        [FLAGS.labels_count])

        return logits
    def tester(n, m):
        N = global_N
        test_data = tv.sin_cos_prod(N, n, m)
        from upsample import upsample
        test_data = upsample(test_data, factor=4)
        dd = field_trace.Derivator(test_data)
        nulls = field_trace.find_null_cells_minimize(dd)
        null_locs = [(null.x0, null.y0) for null in nulls]
        saddles = [_ for _ in nulls if _.is_saddle()]
        maxs = [_ for _ in nulls if _.is_maximum()]
        mins = [_ for _ in nulls if _.is_minimum()]
        eq_(len(saddles + maxs + mins), len(nulls))

        print ("4*n*m: %d num_saddles: %d num_max+num_min: %d" %
                (4*n*m, len(saddles), len(maxs+mins)))

        if 1:
            import pylab as pl
            pl.ion()
            pl.imshow(test_data)
            X = [_.x0 for _ in saddles]
            Y = [_.y0 for _ in saddles]
            pl.scatter(Y, X, marker='x', c='k')
            X = [_.x0 for _ in maxs]
            Y = [_.y0 for _ in maxs]
            pl.scatter(Y, X, c='r', marker='d')
            X = [_.x0 for _ in mins]
            Y = [_.y0 for _ in mins]
            pl.scatter(Y, X, c='b', marker='o')
            raw_input("enter to continue")
            pl.close('all')
Example #3
0
def save_figs():
    from upsample import upsample#{{{
    for bx, by, psi_arr in izip(h5_gen('data.h5', 'bx'),
                                h5_gen('data.h5', 'by'),
                                h5_gen('data.h5', 'psi')):
        bx = upsample(bx.read(), factor=1)
        by = upsample(by.read(), factor=1)
        psi_arr = upsample(psi_arr.read(), factor=1)
        # min_regions = field_trace.detect_min_regions(psi_arr, min_size=20)
        # mask = field_trace.regions_to_mask(psi_arr.shape, min_regions)
        # all_nulls, all_regions = field_trace.nulls_and_regions(psi_arr, chatty=True)
        all_nulls = field_trace.get_nulls(psi_arr)
        mins = [null for null in all_nulls if null.is_minimum()]
        maxs = [null for null in all_nulls if null.is_maximum()]
        saddles = [null for null in all_nulls if null.is_saddle()]
        print( "-"*80)
        print( "num mins: %d unique mins: %d" % (len(mins), num_nonredundant_nulls(mins, psi_arr.shape)))
        print( "num maxs: %d unique maxs: %d" % (len(maxs), num_nonredundant_nulls(maxs, psi_arr.shape)))
        print( "num saddles: %d unique saddles: %d" % (len(saddles), num_nonredundant_nulls(saddles, psi_arr.shape)))
        print( "num nulls: %d unique nulls: %d" % (len(all_nulls), num_nonredundant_nulls(all_nulls, psi_arr.shape)))
        # print "peaks - saddles: %d" % (len(maxs)+len(mins) - len(saddles))
        # print "num maxs: %d" % (len(maxs))
        # print "num mins: %d" % (len(mins))
        # print "num saddles: %d" % (len(saddles))
        # field_trace.find_region_ncontained(psi_arr.shape, all_regions)
        # n2regions = field_trace.regions_by_n_contained(all_regions)
        # mask = field_trace.regions_to_mask(psi_arr.shape, n2regions[0])
        # mask += field_trace.regions_to_mask(psi_arr.shape, n2regions[1])
        # mask += field_trace.regions_to_mask(psi_arr.shape, n2regions[2])
        # modb = np.sqrt(bx**2 + by**2)
        # print "saving to file"
        # field_trace.save_fig(modb, 'bmag_%03d' % ctr)
        # field_trace.save_fig(mask, 'mask_%03d' % ctr)
        # overlay = modb
        # overlay[mask] = overlay.max()
        # peak_x = [peak.x0 for peak in peaks]
        # peak_y = [peak.y0 for peak in peaks]
        # field_trace.save_fig_with_scatter(overlay, (peak_x, peak_y), 'bmagmaskpeaks_%03d' % ctr)
        # field_trace.save_fig(overlay, 'bmagmask_%03d' % ctr)
        # ctr += 1
        break#}}}
Example #4
0
 def test_001_t(self):
     src_data = (2.4, -4.4, 0.1)
     expected_result = (2.4, 0.0, 0.0, 0.0, 0.0, -4.4, 0.0, 0.0, 0.0, 0.0,
                        0.1, 0.0, 0.0, 0.0, 0.0)
     src = blocks.vector_source_f(src_data)
     print "derp"
     thing = upsample(5)
     print "derp"
     snk = blocks.vector_sink_f()
     self.tb.connect(src, thing)
     self.tb.connect(thing, snk)
     self.tb.run()
     result_data = snk.data()
     self.assertFloatTuplesAlmostEqual(expected_result, result_data, 6)
Example #5
0
def test_upsample():
    upsample_factor = 4
    N = global_N
    test_data = tv.sin_cos_prod(N, N/4, N/8)
    test_data_4 = tv.sin_cos_prod(N*4, N/4, N/8)

    upsampled = upsample.upsample(test_data, factor=4)

    ok_(np.allclose(upsample_factor**2 * upsampled, test_data_4))

    if 0:
        pl.ion()
        pl.imshow(test_data, interpolation='nearest', cmap='hot')
        pl.title('original data')
        pl.figure()
        pl.imshow(upsampled, interpolation='nearest', cmap='hot')
        pl.title('upsampled by %d' % upsample_factor)
        pl.figure()
        pl.imshow(test_data_4, interpolation='nearest', cmap='hot')
        pl.title('comparison')
        raw_input('enter to continue')
    def tester(n, m):
        N = global_N
        test_data = tv.sin_cos_arr(N, n, m)
        from upsample import upsample
        test_data = upsample(test_data, factor=4)
        dd = field_trace.Derivator(test_data)
        nulls = field_trace.find_null_cells_minimize(dd)
        cell_locs = [null.loc for null in nulls]
        null_locs = [(null.x0, null.y0) for null in nulls]
        print 4*n*m, len(nulls)

        if 0:
            X, Y = zip(*null_locs)
            import pylab as pl
            pl.ion()
            pl.imshow(test_data)
            pl.scatter(Y, X)
            X, Y = zip(*cell_locs)
            pl.scatter(Y, X, c='r')
            raw_input("enter to continue")
            pl.close('all')
Example #7
0
def waveglow(spect, params):
    """
    spect:  [batch, n_mel_channels, frames],  NCW
    """
    dtype = spect.dtype
    sigma = params["sigma"]
    n_mel_channels = params["n_mel_channels"]
    wn_channels = params["wn_channels"]
    wn_layers = params["wn_layers"]
    n_flows = params["n_flows"]
    n_early_every = params["n_early_every"]
    n_early_size = params["n_early_size"]
    n_remaining_channels = params["n_group"]

    # Calculate initial audio channels for inference
    for k in range(params["n_flows"]):
        if k % params["n_early_every"] == 0 and k > 0:
            n_remaining_channels = n_remaining_channels - params["n_early_size"]

    batch = tf.shape(spect)[0]  # batch = 1
    spect = upsample(spect, n_mel_channels)
    spect, n_mel_group = regroup(spect, n_mel_channels, params["n_group"],
                                 batch)
    # spect: NCHW, [1, 640, 1, 12800]

    t_dim = tf.shape(spect)[3]  # 12800
    audio = random_normal(dtype, t_dim, n_remaining_channels, sigma)

    for k in reversed(range(n_flows)):
        with tf.variable_scope("flow_" + str(k)):
            n_half = int(n_remaining_channels / 2)
            audio_0 = audio[:, :n_half, :, :]
            audio_1 = audio[:, n_half:, :, :]

            log_s, b = wn(audio_0,
                          spect,
                          in_channels=n_half,
                          n_mel_channels=n_mel_group,
                          n_layers=wn_layers,
                          n_channels=wn_channels,
                          kernel_size=3)

            with jit_scope():
                audio_1 = (audio_1 - b) / tf.exp(log_s)
                audio = tf.concat(values=[audio_0, audio_1], axis=1)

            # Reverse W after training
            with tf.variable_scope("1x1_invertible_conv"):
                w = get_weight(
                    [1, 1, n_remaining_channels, n_remaining_channels],
                    dtype=dtype,
                    name='1x1_inv_conv_w')
                audio = tf.nn.conv2d(audio,
                                     filter=w,
                                     strides=[1, 1, 1, 1],
                                     padding='SAME',
                                     data_format='NCHW',
                                     name='1x1_inv_conv')

            if k % n_early_every == 0 and k > 0:
                z = random_normal(dtype, t_dim, n_early_size, sigma)
                audio = tf.concat(values=(z, audio), axis=1, name="append_z")
                n_remaining_channels = n_remaining_channels + params[
                    "n_early_size"]

    audio = tf.squeeze(audio, axis=2)
    audio = tf.transpose(audio, [0, 2, 1])
    audio = tf.reshape(audio, [batch, -1], name="output_audio")
    return audio
Example #8
0
if 0:
    a = 1.5**.5
    x = (np.random.uniform(-a, a, (100, 64)) + np.random.uniform(-a, a, (100, 64)) * 1j)
    outsize = x.size*5//4 * upsample_factor

    n = np.random.standard_normal(outsize) * 10.**(-30 / 20)
    trajectory = iir.stagedLowpass(3e-5)(np.random.standard_normal(outsize)) * 10
    trajectory = trajectory[:outsize]
    # trajectory is in meters
    cs = 340. # m/s
    x[:,0] = 0
    x[:,27:38] = 0
    y = np.tile(np.fft.ifft(x, axis=1), (1,3))[:,48:129]
    y[1:,0] = (y[1:,0] + y[:-1,-1]) * .5
    y = upsample.upsample(y[:,:80].flatten(), upsample_factor)
    y = (y * np.exp(1j*2*np.pi*Fc*np.arange(y.size)/Fs)).real

    pl.figure()
    _=pl.specgram(y+n, NFFT=64*upsample_factor, noverlap=-16*upsample_factor, interpolation='nearest', Fs=Fs)

    t0 = np.arange(y.size)/Fs
    t = t0 - trajectory / cs
    y2 = np.interp(t, t0, y.real)
    y2 = iir.lowpass(1./upsample_factor)(y2 * np.exp(-1j*2*np.pi*Fc*np.arange(y.size)/Fs))[::upsample_factor/4]

    pl.figure()
    _=pl.specgram(y2+n[:y2.size], NFFT=64*4, noverlap=-16*4, interpolation='nearest', Fs=Fs/(upsample_factor/4))

    noisy_y2 = y2 + n[:y2.size]