File size: 2,582 Bytes
0d04ccc
 
 
 
 
 
be48827
e2cf2b0
 
 
 
0d04ccc
 
 
 
 
e2cf2b0
ff2c2a9
 
 
e9ceefd
e2cf2b0
 
0d04ccc
 
 
 
 
 
 
 
 
 
 
 
c1a6745
e2cf2b0
9a66c24
e2cf2b0
 
fafc0c2
8721178
 
dad6ea4
0d04ccc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e2cf2b0
 
 
0d04ccc
 
 
 
 
 
e2cf2b0
 
0d04ccc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# -*- coding: utf-8 -*-
"""
Created on Tue Apr 26 21:02:31 2022

@author: pc
"""

import pickle
import numpy as np
import torch
import gradio as gr 
import sys
import subprocess
import os
from typing import Tuple
import PIL.Image

os.system("git clone https://github.com/NVlabs/stylegan3")

sys.path.append("stylegan3")



def make_transform(translate: Tuple[float,float], angle: float):
    m = np.eye(3)
    s = np.sin(angle/360.0*np.pi*2)
    c = np.cos(angle/360.0*np.pi*2)
    m[0][0] = c
    m[0][1] = s
    m[0][2] = translate[0]
    m[1][0] = -s
    m[1][1] = c
    m[1][2] = translate[1]
    return m



network_pkl='braingan-400.pkl'
with open(network_pkl, 'rb') as f:
    G = pickle.load(f)['G_ema'] 
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
G.eval()
G.to(device)

def predict(Seed,noise_mode,truncation_psi,trans_x,trans_y,angle):

  # Generate images.
    z = torch.from_numpy(np.random.RandomState(Seed).randn(1, G.z_dim)).to(device)
    label = torch.zeros([1, G.c_dim], device=device)
    # Construct an inverse rotation/translation matrix and pass to the generator.  The
    # generator expects this matrix as an inverse to avoid potentially failing numerical
    # operations in the network.
    if hasattr(G.synthesis, 'input'):
        m = make_transform((trans_x,trans_y), angle)
        m = np.linalg.inv(m)
        G.synthesis.input.transform.copy_(torch.from_numpy(m))

    img = G(z, label, truncation_psi=truncation_psi, noise_mode=noise_mode)
    img = (img.permute(0, 2, 3, 1) * 127.5 + 128).clamp(0, 255).to(torch.uint8)
      
    return PIL.Image.fromarray(img[0].cpu().numpy()[:,:,0])



noises=['const', 'random', 'none']
interface=gr.Interface(fn=predict, title="Brain MR Image Generation with StyleGAN-2",
                       description = "",
                       article = "Author: S.Serdar Helli",
                       inputs=[gr.inputs.Slider( minimum=0, maximum=2**10,label='Seed'),gr.inputs.Radio( choices=noises,  default='const',label='Noise Mods'),
                                           gr.inputs.Slider(0, 2, step=0.05, default=1, label='Truncation psi'),
                                           gr.inputs.Slider(-1, 1, step=0.05, default=0, label='Translate X'),
                                           gr.inputs.Slider(-1, 1, step=0.05, default=0, label='Translate Y'),
                                           gr.inputs.Slider(-180, 180, step=5, default=0, label='Angle'),],
                       outputs=gr.outputs.Image( type="numpy", label="Output"))


interface.launch(debug=True)