Ejemplo n.º 1
0
def write_out_vel(particles,
                  vel,
                  d,
                  v,
                  t,
                  suffix,
                  xlim=None,
                  ylim=None,
                  s=5,
                  z=None):
    writeNumpyRaw((dst_path + suffix + "_%03d") % (d, v, t), vel)
    plot_particles(particles,
                   xlim,
                   ylim,
                   s, (dst_path + suffix + "_%03d.png") % (d, v, t),
                   src=particles,
                   vel=vel,
                   z=z)
    plot_particles(particles,
                   xlim,
                   ylim,
                   s, (dst_path + suffix + "_%03d.svg") % (d, v, t),
                   src=particles,
                   vel=vel,
                   z=z)
    write_csv((dst_path + suffix + "_%03d.csv") % (d, v, t), vel)
Ejemplo n.º 2
0
def add_images(writer,
               tag,
               src,
               ref,
               res,
               epoch,
               xlim=None,
               ylim=None,
               s=1.0,
               z=None):
    imgs = []
    buf = io.BytesIO()
    plot_particles(res,
                   path=buf,
                   src=src,
                   ref=ref,
                   xlim=xlim,
                   ylim=ylim,
                   s=s,
                   z=z)
    img = tf.Summary.Image(colorspace=4, encoded_image_string=buf.getvalue())
    imgs.append(tf.Summary.Value(tag=tag + "_comp", image=img))
    buf = io.BytesIO()
    plot_particles(res, path=buf, xlim=xlim, ylim=ylim, s=s, z=z)
    img = tf.Summary.Image(colorspace=4, encoded_image_string=buf.getvalue())
    imgs.append(tf.Summary.Value(tag=tag + "_res", image=img))
    buf = io.BytesIO()
    plot_particles(src, path=buf, xlim=xlim, ylim=ylim, s=s, z=z)
    img = tf.Summary.Image(colorspace=4, encoded_image_string=buf.getvalue())
    imgs.append(tf.Summary.Value(tag=tag + "_src", image=img))
    buf = io.BytesIO()
    plot_particles(ref, path=buf, xlim=xlim, ylim=ylim, s=s, z=z)
    img = tf.Summary.Image(colorspace=4, encoded_image_string=buf.getvalue())
    imgs.append(tf.Summary.Value(tag=tag + "_ref", image=img))

    writer.add_summary(tf.Summary(value=imgs), epoch)
    writer.flush()

    return
Ejemplo n.º 3
0
        0 if len(patch_pos) == 3 else 2,
        aux_data=par_aux,
        features=features,
        pad_val=pad_val,
        bnd=bnd,
        last_pos=positions,
        stride_hys=1.0)

    if len(patch_pos) == 3:
        idx = get_nearest_idx(patch_extractor.positions, patch_pos)
        patch = patch_extractor.get_patch(idx, False)

        plot_particles(patch_extractor.positions,
                       [0, int(out_res / factor_d[0])],
                       [0, int(out_res / factor_d[0])],
                       5,
                       tmp_path + "patch_centers_%03d.png" % i,
                       np.array([patch_extractor.positions[idx]]),
                       np.array([patch_pos]),
                       z=patch_pos[2] if dim == 3 else None)
        patch_pos = patch_extractor.positions[idx] + par_aux['v'][
            patch_extractor.pos_idx[idx]] / data_config['fps']
        ref_patch = extract_particles(ref_data[i], patch_pos * factor_d,
                                      par_cnt_dst, half_ps, pad_val)[0]
        result = eval_patch(punet, [np.array([patch])],
                            tmp_path + "result_%s" + "_%03d" % i,
                            z=None if dim == 2 else 0,
                            verbose=3 if verbose else 1)

        hdr = OrderedDict([('dim', len(result)), ('dimX', int(patch_size_ref)),
                           ('dimY', int(patch_size_ref)),
                           ('dimZ', 1 if dim == 2 else int(patch_size_ref)),
Ejemplo n.º 4
0
               1,
               name="coord_reconstruction_1",
               activation=activation)(x)

    b = np.zeros((3, ), dtype='float32')
    W = np.zeros((1, fac * 8, 3), dtype='float32')
    x = Conv1D(3, 1, name="coord_reconstruction_2", activation=activation)(x)

    x = Reshape((100, 3))(x)

    m = Model(inputs=inputs, outputs=x)
    m.compile(
        keras.optimizers.adam(),
        loss=lambda y_true, y_pred: repulsion_loss(y_pred, 20, 0.1, 0.005))

    data = np.random.random((10000, 100, 3)) * 0.1

    plot_particles(m.predict(data[:1])[0], src=data[0], s=5)

    m.fit(data, data, epochs=1)
    plot_particles(m.predict(data[:1])[0], src=data[0], s=5)
    m.fit(data, data, epochs=1)
    plot_particles(m.predict(data[:1])[0], src=data[0], s=5)
    m.fit(data, data, epochs=1)
    plot_particles(m.predict(data[:1])[0], src=data[0], s=5)
    m.fit(data, data, epochs=1)
    plot_particles(m.predict(data[:1])[0], src=data[0], s=5)
    m.fit(data, data, epochs=1)

    plot_particles(m.predict(data[:1])[0], src=data[0], s=5)
Ejemplo n.º 5
0
        src_grid.sample_sdf()
        ref_grid.sample_sdf()
        return src_grid, ref_grid


if __name__ == '__main__':

    from neuralparticles.tools.plot_helpers import plot_sdf, plot_particles
    from neuralparticles.tools.data_helpers import extract_particles

    rp = RandomParticles(50, 50, 1, 2, 3, 10, 5, 0.5, 3)

    rp.gen_random()
    src_grid, ref_grid = rp.get_grid()

    plot_particles(src_grid.particles, [0, src_grid.dimX], [0, src_grid.dimY],
                   0.1, "src.pdf")
    plot_particles(ref_grid.particles, [0, ref_grid.dimX], [0, ref_grid.dimY],
                   0.1, "ref.pdf")
    plot_particles(src_grid.particles, [0, src_grid.dimX], [0, src_grid.dimY],
                   0.1, "comp.pdf", ref_grid.particles / 3)

    plot_sdf(src_grid.cells[0, :, :, 0], [0, src_grid.dimX],
             [0, src_grid.dimY],
             "src_sdf.pdf",
             src=src_grid.particles,
             s=0.1)
    plot_sdf(ref_grid.cells[0, :, :, 0], [0, ref_grid.dimX],
             [0, ref_grid.dimY],
             "ref_sdf.pdf",
             src=ref_grid.particles,
             s=0.1)
Ejemplo n.º 6
0
def eval_frame(model,
               patch_extractor,
               factor_d,
               path="",
               src=None,
               aux=None,
               ref=None,
               hdim=0,
               z=None,
               verbose=0):
    patches = patch_extractor.get_patches()
    result = model.predict(patches)
    if patch_extractor.data.shape[0] > 0:
        if z is None:
            tmp = np.repeat(patch_extractor.data, factor_d**2, axis=0)
            displace = np.transpose(
                np.reshape(np.mgrid[:factor_d, :factor_d, :1] + 0.5,
                           (3, -1))) / factor_d - 0.5
            displace = np.concatenate(
                np.repeat(np.expand_dims(displace, axis=0),
                          patch_extractor.data.shape[0],
                          axis=0))
            patch_extractor.data = tmp + displace * np.array(
                [1., 1., 0.]
            )  # + np.random.normal(scale=1/(3*factor_d), size=displace.shape)) * np.array([1.,1.,0.])
        else:
            tmp = np.repeat(patch_extractor.data, factor_d**3, axis=0)
            displace = np.transpose(
                np.reshape(np.mgrid[:factor_d, :factor_d, :factor_d] + 0.5,
                           (3, -1))) / factor_d - 0.5
            displace = np.concatenate(
                np.repeat(np.expand_dims(displace, axis=0),
                          patch_extractor.data.shape[0],
                          axis=0))
            patch_extractor.data = tmp + displace  # + np.random.normal(scale=1/(3*factor_d), size=displace.shape)

    if type(result) is list:
        for i in range(len(patch_extractor.positions)):
            patch_extractor.set_patch(
                result[0][i, :int(result[1][i] * result[0].shape[1])], i)
    else:
        for i in range(len(patch_extractor.positions)):
            cnt = int(np.count_nonzero(patches[0][..., 1] != -2.0)) * (
                result.shape[1] // patches[0].shape[1])
            patch_extractor.set_patch(result[i, :cnt], i)

    result = patch_extractor.data * np.array(
        [factor_d, factor_d, 0 if z is None else factor_d])
    if path != "" and verbose > 0:
        vel_src = None
        '''for k in aux:
            aux_src = aux[k]
            if k == 'v':
                vel_src = aux_src/100
            if verbose > 2: write_csv(path + "_%s.csv"%k, aux_src)'''

        if verbose > 0:
            plot_particles(result,
                           xlim=[0, hdim],
                           ylim=[0, hdim],
                           s=0.1,
                           path=path % ("comp") + ".png",
                           ref=ref,
                           src=src * factor_d,
                           vel=vel_src,
                           z=z)
            plot_particles(src,
                           xlim=[0, hdim // factor_d],
                           ylim=[0, hdim // factor_d],
                           s=0.1,
                           path=path % ("src") + ".png",
                           src=src,
                           vel=vel_src,
                           z=z)
            if ref is not None:
                plot_particles(ref,
                               xlim=[0, hdim],
                               ylim=[0, hdim],
                               s=0.1,
                               path=path % ("ref") + ".png",
                               z=z)
            plot_particles(result,
                           xlim=[0, hdim],
                           ylim=[0, hdim],
                           s=0.1,
                           path=path % ("res") + ".png",
                           z=z)
            if verbose > 1:
                plot_particles(result,
                               xlim=[0, hdim],
                               ylim=[0, hdim],
                               s=0.1,
                               path=path % ("comp") + ".svg",
                               ref=ref,
                               src=src * factor_d,
                               vel=vel_src,
                               z=z)
                plot_particles(src,
                               xlim=[0, hdim // factor_d],
                               ylim=[0, hdim // factor_d],
                               s=0.1,
                               path=path % ("src") + ".svg",
                               src=src,
                               vel=vel_src,
                               z=z)
                if ref is not None:
                    plot_particles(ref,
                                   xlim=[0, hdim],
                                   ylim=[0, hdim],
                                   s=0.1,
                                   path=path % ("ref") + ".svg",
                                   z=z)
                plot_particles(result,
                               xlim=[0, hdim],
                               ylim=[0, hdim],
                               s=0.1,
                               path=path % ("res") + "svg",
                               z=z)
                if verbose > 2:
                    write_csv(path % ("res") + ".csv", result)
                    if ref is not None: write_csv(path % ("ref") + ".csv", ref)
                    write_csv(path % ("src") + ".csv", src)
    patch_extractor.reset()
    return result
Ejemplo n.º 7
0
def eval_patch(model,
               src,
               path="",
               ref=None,
               features=[],
               z=None,
               verbose=0,
               truncate=False):
    result = model.predict(src)

    if type(result) is list:
        raw_result = result[0][0]
        cnt = int(result[1][0] * result[0].shape[1])
        if verbose > 0: print("Reduce points to: " + str(cnt))
        result = result[0][0, :cnt]
    elif truncate:
        raw_result = result[0]
        cnt = int(np.count_nonzero(
            src[0][..., 1] != -2.0)) * (result.shape[1] // src[0].shape[1])
        if verbose > 0: print("Reduce points to: " + str(cnt))
        result = result[0, :cnt]
    else:
        raw_result = result[0]
        if verbose > 0: print("No Truncation!")
        result = result[0]

    if path != "" and verbose > 0:
        _i = 0
        vel_src = None
        '''for i in range(len(features)):
            if features[i] == 'v':
                aux_src = src[1][:,_i:_i+3]
                vel_src = aux_src/100
                _i += 3
            else:
                aux_src = src[1][:,_i:_i+1]
                _i += 1
            if verbose > 2: write_csv(path + "_%s.csv"%features[i], aux_src)'''

        if verbose > 0:
            permutate = True
            print(
                cluster_analysis(src[0][0][..., :3], raw_result,
                                 result.shape[0], permutate))
            """c = np.arange(src[0][0].shape[0])
            #c[0] = 0
            if permutate:
                c = np.repeat(c, raw_result.shape[0]//src[0][0].shape[0])[:result.shape[0]]
            else:
                c = np.repeat(np.expand_dims(c,0), raw_result.shape[0]//src[0][0].shape[0]).flatten()[:result.shape[0]]
            
            plot_particles(result, xlim=[-1,1], ylim=[-1,1], s=5, path=path%("detail") + ".png", src=src[0][0], vel=vel_src, z = z, c=c)"""
            fac = raw_result.shape[0] // src[0][0].shape[0]

            plot_particles(result,
                           xlim=[-1, 1],
                           ylim=[-1, 1],
                           s=5,
                           path=path % ("comp") + ".png",
                           ref=ref,
                           src=src[0][0],
                           vel=vel_src,
                           z=z)
            plot_particles(result[fac:],
                           xlim=[-1, 1],
                           ylim=[-1, 1],
                           s=5,
                           path=path % ("detail") + ".png",
                           src=src[0][0],
                           vel=vel_src,
                           z=z,
                           ref=result[:fac])
            plot_particles(src[0][0],
                           xlim=[-1, 1],
                           ylim=[-1, 1],
                           s=5,
                           path=path % ("src") + ".png",
                           src=src[0][0],
                           vel=vel_src,
                           z=z)
            if ref is not None:
                plot_particles(ref,
                               xlim=[-1, 1],
                               ylim=[-1, 1],
                               s=5,
                               path=path % ("ref") + ".png",
                               z=z)
            plot_particles(result,
                           xlim=[-1, 1],
                           ylim=[-1, 1],
                           s=5,
                           path=path % ("res") + ".png",
                           z=z)
            if verbose > 1:
                plot_particles(result,
                               xlim=[-1, 1],
                               ylim=[-1, 1],
                               s=5,
                               path=path % ("comp") + ".svg",
                               ref=ref,
                               src=src[0][0],
                               vel=vel_src,
                               z=z)
                plot_particles(result[fac:],
                               xlim=[-1, 1],
                               ylim=[-1, 1],
                               s=5,
                               path=path % ("detail") + ".svg",
                               src=src[0][0],
                               vel=vel_src,
                               z=z,
                               ref=result[:fac])
                plot_particles(src[0][0],
                               xlim=[-1, 1],
                               ylim=[-1, 1],
                               s=5,
                               path=path % ("src") + ".svg",
                               src=src[0][0],
                               vel=vel_src,
                               z=z)
                if ref is not None:
                    plot_particles(ref,
                                   xlim=[-1, 1],
                                   ylim=[-1, 1],
                                   s=5,
                                   path=path % ("ref") + ".svg",
                                   z=z)
                plot_particles(result,
                               xlim=[-1, 1],
                               ylim=[-1, 1],
                               s=5,
                               path=path % ("res") + ".svg",
                               z=z)
                if verbose > 2:
                    write_csv(path % ("res") + ".csv", result)
                    if ref is not None: write_csv(path % ("ref") + ".csv", ref)
                    write_csv(path % ("src") + ".csv", src[0][0])

    return result