Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
zydjohnHotmail opened this issue Apr 2, 2022 · 0 comments
Open

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

zydjohnHotmail opened this issue Apr 2, 2022 · 0 comments

Comments

@zydjohnHotmail
Copy link

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,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant