File size: 1,964 Bytes
0d04ccc
 
 
 
 
 
be48827
e2cf2b0
 
 
 
0d04ccc
 
 
 
 
e2cf2b0
ff2c2a9
 
 
e9ceefd
e2cf2b0
 
cc96327
1cdd16d
cc96327
fb9f5c1
c1a6745
e2cf2b0
9a66c24
e2cf2b0
 
fafc0c2
8721178
 
dad6ea4
cc96327
0d04ccc
 
 
 
 
 
 
0fc4a2a
cc96327
0d04ccc
 
 
 
18fd9e7
0d04ccc
 
 
 
e2cf2b0
fb9f5c1
e2cf2b0
5de8c5c
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
# -*- 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")



DESCRIPTION = f''This model generates healthy MR Brain Images.
![Example]("https://huggingface.co/spaces/SerdarHelli/Brain-MR-Image-Generation-GAN/blob/main/ex.png")
''



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):

  # 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.
    


    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])).resize((512,512))



noises=['const', 'random', 'none']
interface=gr.Interface(fn=predict, title="Brain MR Image Generation with StyleGAN-2",
                       description = DESCRIPTION,
                       article = "Author: S.Serdar Helli",
                       inputs=[gr.inputs.Slider( minimum=0, maximum=2**16,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'),
                       outputs=gr.outputs.Image( type="numpy", label="Output"))


interface.launch(debug=True)