Ejemplo n.º 1
0
def generate():
    latentsStr = flask.request.args.get("latents")
    latentsStrX = flask.request.args.get("xlatents")
    psi = float(flask.request.args.get("psi", 0.5))
    # use_noise = bool(flask.request.args.get('use_noise', True))
    randomize_noise = int(flask.request.args.get("randomize_noise", 0))
    fromW = int(flask.request.args.get("fromW", 0))

    global model_name
    global g_Session
    global g_dLatentsIn
    # print('g_Session.1:', g_Session)

    fetched_model_name = flask.request.args.get("model_name", "ffhq")
    if model_name != fetched_model_name:
        model_name = fetched_model_name
    gs, synthesis = loadGs()

    latent_len = gs.input_shape[1]
    if latentsStrX:
        latents = latentCode.decodeFixed16(latentsStrX, g_dLatentsIn.shape[0])
    else:
        latents = latentCode.decodeFloat32(latentsStr, latent_len)

    t0 = time.time()

    # Generate image.
    fmt = dict(func=dnnlib.tflib.convert_images_to_uint8, nchw_to_nhwc=True)
    with g_Session.as_default():
        if fromW != 0:
            # print('latentsStr:', latentsStr)
            # print('shapes:', g_dLatentsIn.shape, latents.shape)

            if latents.shape[0] < g_dLatentsIn.shape[0]:
                latents = np.tile(latents,
                                  g_dLatentsIn.shape[0] // latents.shape[0])
            images = dnnlib.tflib.run(synthesis, {g_dLatentsIn: latents})
            image = misc.convert_to_pil_image(misc.create_image_grid(images),
                                              drange=[-1, 1])
        else:
            latents = latents.reshape([1, latent_len])
            images = gs.run(
                latents,
                None,
                truncation_psi=psi,
                randomize_noise=randomize_noise != 0,
                output_transform=fmt,
            )
            image = PIL.Image.fromarray(images[0], "RGB")

    print("generation cost:", time.time() - t0)

    # encode to PNG
    fp = io.BytesIO()
    image.save(fp, PIL.Image.registered_extensions()[".png"])

    return flask.Response(fp.getvalue(), mimetype="image/png")
Ejemplo n.º 2
0
def mapZtoW():
	zStr = flask.request.args.get('z')
	psi = flask.request.args.get('psi')
	psi = psi and float(psi)

	gs, _ = loadGs()
	latent_len = gs.input_shape[1]

	z = latentCode.decodeFloat32(zStr, latent_len).reshape([1, latent_len])
	w = gs.components.mapping.run(z, None)[:, :1, :].reshape([latent_len])

	if psi is not None:
		proj = loadProjector()
		avg = proj._dlatent_avg.reshape([latent_len])

		w = avg + psi * (w - avg)

	return latentCode.encodeFloat32(w)
Ejemplo n.º 3
0
def generate():
	latentsStr = flask.request.args.get('latents')
	latentsStrX = flask.request.args.get('xlatents')
	psi = float(flask.request.args.get('psi', 0.5))
	#use_noise = bool(flask.request.args.get('use_noise', True))
	randomize_noise = int(flask.request.args.get('randomize_noise', 0))
	fromW = int(flask.request.args.get('fromW', 0))

	global g_Session
	global g_dLatentsIn
	#print('g_Session.1:', g_Session)

	gs, synthesis = loadGs()

	latent_len = gs.input_shape[1]
	if latentsStrX:
		latents = latentCode.decodeFixed16(latentsStrX, g_dLatentsIn.shape[0])
	else:
		latents = latentCode.decodeFloat32(latentsStr, latent_len)

	t0 = time.time()

	# Generate image.
	fmt = dict(func = dnnlib.tflib.convert_images_to_uint8, nchw_to_nhwc = True)
	with g_Session.as_default():
		if fromW != 0:
			#print('latentsStr:', latentsStr)
			#print('shapes:', g_dLatentsIn.shape, latents.shape)

			if latents.shape[0] < g_dLatentsIn.shape[0]:
				latents = np.tile(latents, g_dLatentsIn.shape[0] // latents.shape[0])
			images = dnnlib.tflib.run(synthesis, {g_dLatentsIn: latents})
			image = images[0].transpose(1, 2, 0)
			image = misc.adjust_dynamic_range(image, [-1,1], [0,255])
			image = np.rint(image).clip(0, 255).astype(np.uint8)
			# image = misc.convert_to_pil_image(misc.create_image_grid(images), drange = [-1,1])
		else:
			latents = latents.reshape([1, latent_len])
			images = gs.run(latents, None, truncation_psi = psi, randomize_noise = randomize_noise != 0, output_transform = fmt)
			image = images[0]
			t1 = time.time()
			# image = PIL.Image.fromarray(images[0], 'RGB')


	# encode to PNG
	# fp = io.BytesIO()
	# image.save(fp, PIL.Image.registered_extensions()['.png'])
	# data = fp.getValue()

	# Convert Numpy array to Vips image
	dtype_to_format = {
		'uint8': 'uchar',
		'int8': 'char',
		'uint16': 'ushort',
		'int16': 'short',
		'uint32': 'uint',
		'int32': 'int',
		'float32': 'float',
		'float64': 'double',
		'complex64': 'complex',
		'complex128': 'dpcomplex',
	}
	height, width, bands = image.shape
	linear = image.reshape(width * height * bands)
	vi = pyvips.Image.new_from_memory(linear.data, width, height, bands, dtype_to_format[str(image.dtype)])
	# Save to memory buffer as PNG
	# compression = 0
	# data = vi.write_to_buffer(f".png[compression={compression}]")
	# Save to memory buffer as JPG
	vi = vi.resize(2, kernel = "lanczos2")
	data = vi.write_to_buffer(".jpg")
	t2 = time.time()

	print(f'Costs:\ngenerator\t{t1 - t0}\nencoder\t\t{t2 - t1}\ntotal\t\t{t2 -t0}')
	return flask.Response(data, mimetype = 'image/png')