def process(num):
    global params_tmp, opt_state, params, image_f, optimizer
    if (a.interpol is True):
        txt_encs = get_encs(key_txt_encs, num)
        styl_encs = get_encs(key_styl_encs, num)
        img_encs = get_encs(key_img_encs, num)
    else:
        txt_encs = (([key_txt_encs[min(num, (len(key_txt_encs) - 1))][0]] * opt_steps) if (len(key_txt_encs) > 0) else [])
        styl_encs = (([key_styl_encs[min(num, (len(key_styl_encs) - 1))][0]] * opt_steps) if (len(key_styl_encs) > 0) else [])
        img_encs = (([key_img_encs[min(num, (len(key_img_encs) - 1))][0]] * opt_steps) if (len(key_img_encs) > 0) else [])
    if (a.verbose is True):
        if (len(texts) > 0):
            print(' ref text: ', texts[min(num, (len(texts) - 1))][:80])
        if (len(styles) > 0):
            print(' ref style: ', styles[min(num, (len(styles) - 1))][:80])
        if (len(images) > 0):
            print(' ref image: ', basename(images[min(num, (len(images) - 1))])[:80])
    pbar = ProgressBar(steps)
    for ii in range(opt_steps):
        glob_step = ((num * steps) + (ii // a.opt_step))
        loss = 0
        txt_enc = (txt_encs[(ii % len(txt_encs))].unsqueeze(0) if (len(txt_encs) > 0) else None)
        styl_enc = (styl_encs[(ii % len(styl_encs))].unsqueeze(0) if (len(styl_encs) > 0) else None)
        img_enc = (img_encs[(ii % len(img_encs))].unsqueeze(0) if (len(img_encs) > 0) else None)
        if ((ii % a.opt_step) == 0):
            scale = (m_scale[glob_step] if a.anima else (1 + a.scale))
            shift = (tuple(m_shift[glob_step]) if a.anima else [0, a.shift])
            angle = (m_angle[glob_step][0] if a.anima else a.angle)
            shear = (m_shear[glob_step][0] if a.anima else a.shear)
            if (a.gen == 'RGB'):
                img_tmp = frame_transform(params_tmp, a.size, angle, shift, scale, shear)
                (params, image_f, _) = pixel_image([1, 3, *a.size], resume=img_tmp)
            else:
                if old_torch():
                    img_tmp = torch.irfft(params_tmp, 2, normalized=True, signal_sizes=a.size)
                    img_tmp = frame_transform(img_tmp, a.size, angle, shift, scale, shear)
                    params_tmp = torch.rfft(img_tmp, 2, normalized=True)
                else:
                    if (type(params_tmp) is not torch.complex64):
                        params_tmp = torch.view_as_complex(params_tmp)
                    img_tmp = torch.fft.irfftn(params_tmp, s=a.size, norm='ortho')
                    img_tmp = frame_transform(img_tmp, a.size, angle, shift, scale, shear)
                    params_tmp = torch.fft.rfftn(img_tmp, s=a.size, dim=[2, 3], norm='ortho')
                    params_tmp = torch.view_as_real(params_tmp)
                (params, image_f, _) = fft_image([1, 3, *a.size], sd=1, resume=params_tmp)
            optimizer = torch.optim.Adam(params, a.lrate)
            image_f = to_valid_rgb(image_f, colors=a.colors)
            del img_tmp
            if ((a.smooth is True) and ((num + ii) > 0)):
                optimizer.load_state_dict(opt_state)
        noise = ((a.noise * (torch.rand(1, 1, a.size[0], ((a.size[1] // 2) + 1), 1) - 0.5).cuda()) if (a.noise > 0) else 0.0)
        img_out = image_f(noise)
        img_sliced = slice_imgs([img_out], a.samples, a.modsize, trform_f, a.align, a.macro)[0]
        out_enc = model_clip.encode_image(img_sliced)
        if (a.gen == 'RGB'):
            loss += (1.66 * abs((img_out.mean((2, 3)) - 0.45)).sum())
            loss += (1.66 * abs((img_out.std((2, 3)) - 0.17)).sum())
        if (txt_enc is not None):
            loss -= (a.invert * sim_func(txt_enc, out_enc, a.sim))
        if (styl_enc is not None):
            loss -= (a.weight2 * sim_func(styl_enc, out_enc, a.sim))
        if (img_enc is not None):
            loss -= (a.weight_img * sim_func(img_enc, out_enc, a.sim))
        if (a.in_txt0 is not None):
            for anti_txt_enc in anti_txt_encs:
                loss += (0.3 * sim_func(anti_txt_enc, out_enc, a.sim))
        if (a.sharp != 0):
            loss -= (a.sharp * derivat(img_out, mode='naive'))
        if (a.enforce != 0):
            img_sliced = slice_imgs([image_f(noise)], a.samples, a.modsize, trform_f, a.align, a.macro)[0]
            out_enc2 = model_clip.encode_image(img_sliced)
            loss -= (a.enforce * sim_func(out_enc, out_enc2, a.sim))
            del out_enc2
            torch.cuda.empty_cache()
        if (a.expand > 0):
            global prev_enc
            if (ii > 0):
                loss += (a.expand * sim_func(prev_enc, out_enc, a.sim))
            prev_enc = out_enc.detach().clone()
        del img_out, img_sliced, out_enc
        torch.cuda.empty_cache()
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        if ((ii % a.opt_step) == (a.opt_step - 1)):
            params_tmp = params[0].detach().clone()
            if (a.smooth is True):
                opt_state = optimizer.state_dict()
        if ((ii % a.opt_step) == 0):
            with torch.no_grad():
                img_t = image_f(contrast=a.contrast)[0].permute(1, 2, 0)
                img = torch.clip((img_t * 255), 0, 255).cpu().numpy().astype(np.uint8)
            imsave(os.path.join(tempdir, ('%06d.jpg' % glob_step)), img, quality=95)
            if (a.verbose is True):
                cvshow(img)
            del img, img_t
            pbar.upd()
    params_tmp = params[0].detach().clone()
def process(num):
    global params_tmp, opt_state, params, image_f, optimizer
    if (a.interpol is True):
        txt_encs = get_encs(key_txt_encs, num)
        styl_encs = get_encs(key_styl_encs, num)
        img_encs = get_encs(key_img_encs, num)
    else:
        txt_encs = (([key_txt_encs[min(num, (len(key_txt_encs) - 1))][0]] * opt_steps) if (len(key_txt_encs) > 0) else [])
        styl_encs = (([key_styl_encs[min(num, (len(key_styl_encs) - 1))][0]] * opt_steps) if (len(key_styl_encs) > 0) else [])
        img_encs = (([key_img_encs[min(num, (len(key_img_encs) - 1))][0]] * opt_steps) if (len(key_img_encs) > 0) else [])
    if (a.verbose is True):
        if (len(texts) > 0):
            print(' ref text: ', texts[min(num, (len(texts) - 1))][:80])
        if (len(styles) > 0):
            print(' ref style: ', styles[min(num, (len(styles) - 1))][:80])
        if (len(images) > 0):
            print(' ref image: ', basename(images[min(num, (len(images) - 1))])[:80])
    pbar = ProgressBar(steps)
    for ii in range(opt_steps):
        glob_step = ((num * steps) + (ii // a.opt_step))
        loss = 0
        txt_enc = (txt_encs[(ii % len(txt_encs))].unsqueeze(0) if (len(txt_encs) > 0) else None)
        styl_enc = (styl_encs[(ii % len(styl_encs))].unsqueeze(0) if (len(styl_encs) > 0) else None)
        img_enc = (img_encs[(ii % len(img_encs))].unsqueeze(0) if (len(img_encs) > 0) else None)
        if ((ii % a.opt_step) == 0):
            scale = (m_scale[glob_step] if a.anima else (1 + a.scale))
            shift = (tuple(m_shift[glob_step]) if a.anima else [0, a.shift])
            angle = (m_angle[glob_step][0] if a.anima else a.angle)
            shear = (m_shear[glob_step][0] if a.anima else a.shear)
            if (a.gen == 'RGB'):
                img_tmp = frame_transform(params_tmp, a.size, angle, shift, scale, shear)
                (params, image_f, _) = pixel_image([1, 3, *a.size], resume=img_tmp)
            else:
                if old_torch():
                    img_tmp = torch.irfft(params_tmp, 2, normalized=True, signal_sizes=a.size)
                    img_tmp = frame_transform(img_tmp, a.size, angle, shift, scale, shear)
                    params_tmp = torch.rfft(img_tmp, 2, normalized=True)
                else:
                    if (type(params_tmp) is not torch.complex64):
                        params_tmp = torch.view_as_complex(params_tmp)
                    img_tmp = torch.fft.irfftn(params_tmp, s=a.size, norm='ortho')
                    img_tmp = frame_transform(img_tmp, a.size, angle, shift, scale, shear)
                    params_tmp = torch.fft.rfftn(img_tmp, s=a.size, dim=[2, 3], norm='ortho')
                    params_tmp = torch.view_as_real(params_tmp)
                (params, image_f, _) = fft_image([1, 3, *a.size], sd=1, resume=params_tmp)
            optimizer = torch.optim.Adam(params, a.lrate)
            image_f = to_valid_rgb(image_f, colors=a.colors)
            del img_tmp
            if ((a.smooth is True) and ((num + ii) > 0)):
                optimizer.load_state_dict(opt_state)
        noise = ((a.noise * (torch.rand(1, 1, a.size[0], ((a.size[1] // 2) + 1), 1) - 0.5).cuda()) if (a.noise > 0) else 0.0)
        img_out = image_f(noise)
        img_sliced = slice_imgs([img_out], a.samples, a.modsize, trform_f, a.align, a.macro)[0]
        out_enc = model_clip.encode_image(img_sliced)
        if (a.gen == 'RGB'):
            loss += (1.66 * abs((img_out.mean((2, 3)) - 0.45)).sum())
            loss += (1.66 * abs((img_out.std((2, 3)) - 0.17)).sum())
        if (txt_enc is not None):
            loss -= (a.invert * sim_func(txt_enc, out_enc, a.sim))
        if (styl_enc is not None):
            loss -= (a.weight2 * sim_func(styl_enc, out_enc, a.sim))
        if (img_enc is not None):
            loss -= (a.weight_img * sim_func(img_enc, out_enc, a.sim))
        if (a.in_txt0 is not None):
            for anti_txt_enc in anti_txt_encs:
                loss += (0.3 * sim_func(anti_txt_enc, out_enc, a.sim))
        if (a.sharp != 0):
            loss -= (a.sharp * derivat(img_out, mode='naive'))
        if (a.enforce != 0):
            img_sliced = slice_imgs([image_f(noise)], a.samples, a.modsize, trform_f, a.align, a.macro)[0]
            out_enc2 = model_clip.encode_image(img_sliced)
            loss -= (a.enforce * sim_func(out_enc, out_enc2, a.sim))
            del out_enc2
            torch.cuda.empty_cache()
        if (a.expand > 0):
            global prev_enc
            if (ii > 0):
                loss += (a.expand * sim_func(prev_enc, out_enc, a.sim))
            prev_enc = out_enc.detach().clone()
        del img_out, img_sliced, out_enc
        torch.cuda.empty_cache()
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        if ((ii % a.opt_step) == (a.opt_step - 1)):
            params_tmp = params[0].detach().clone()
            if (a.smooth is True):
                opt_state = optimizer.state_dict()
        if ((ii % a.opt_step) == 0):
            with torch.no_grad():
                img_t = image_f(contrast=a.contrast)[0].permute(1, 2, 0)
                img = torch.clip((img_t * 255), 0, 255).cpu().numpy().astype(np.uint8)
            imsave(os.path.join(tempdir, ('%06d.jpg' % glob_step)), img, quality=95)
            if (a.verbose is True):
                cvshow(img)
            del img, img_t
            pbar.upd()
    params_tmp = params[0].detach().clone()