emgucv / emgutf

Emgu TF is a cross platform .Net wrapper for the Google Tensorflow library. Allows Tensorflow functions to be called from .NET compatible languages such as C#, VB, VC++, IronPython.
https://www.emgu.com/wiki/index.php/Emgu_TF
Other
214 stars 43 forks source link

Can I use pre-trained Tensorflow model to make predit? #72

Open zydjohnHotmail opened 2 years ago

zydjohnHotmail commented 2 years ago

Hello: I find this repo, I want to do some testing to see if I can use pre-trained Tensorflow to make some predition (generate an image). The following is Python code from https://github.com/NVlabs/stylegan2-ada. def generate_images(network_pkl, seeds, truncation_psi, outdir, class_idx, dlatents_npz): tflib.init_tf() print('Loading networks from "%s"...' % network_pkl) with dnnlib.util.open_url(network_pkl) as fp: _G, _D, Gs = pickle.load(fp)

os.makedirs(outdir, exist_ok=True)

# Render images for a given dlatent vector.
if dlatents_npz is not None:
    print(f'Generating images from dlatents file "{dlatents_npz}"')
    dlatents = np.load(dlatents_npz)['dlatents']
    assert dlatents.shape[1:] == (18, 512) # [N, 18, 512]
    imgs = Gs.components.synthesis.run(dlatents, output_transform=dict(func=tflib.convert_images_to_uint8, nchw_to_nhwc=True))
    for i, img in enumerate(imgs):
        fname = f'{outdir}/dlatent{i:02d}.png'
        print (f'Saved {fname}')
        PIL.Image.fromarray(img, 'RGB').save(fname)
    return

# Render images for dlatents initialized from random seeds.
Gs_kwargs = {
    'output_transform': dict(func=tflib.convert_images_to_uint8, nchw_to_nhwc=True),
    'randomize_noise': False
}
if truncation_psi is not None:
    Gs_kwargs['truncation_psi'] = truncation_psi

noise_vars = [var for name, var in Gs.components.synthesis.vars.items() if name.startswith('noise')]
label = np.zeros([1] + Gs.input_shapes[1][1:])
if class_idx is not None:
    label[:, class_idx] = 1

for seed_idx, seed in enumerate(seeds):
    print('Generating image for seed %d (%d/%d) ...' % (seed, seed_idx, len(seeds)))
    rnd = np.random.RandomState(seed)
    z = rnd.randn(1, *Gs.input_shape[1:]) # [minibatch, component]
    tflib.set_vars({var: rnd.randn(*var.shape.as_list()) for var in noise_vars}) # [height, width]
    images = Gs.run(z, label, **Gs_kwargs) # [minibatch, height, width, channel]
    PIL.Image.fromarray(images[0], 'RGB').save(f'{outdir}/seed{seed:04d}.png')

The code can generate image using StyleGAN2 model. I can find some StyleGAN2 pre-trained model in ".pkl" format. I want to know if I can use the repo to load the pre-trained model to make predict just as the python code does. My PC is not powerful enough to train any big dataset, so using pre-trained model is a must, and I don't have enough experience with Python, so I want to know if I can use this repo do the same job as Python. Please advise! Thanks,