firatozdemir commited on
Commit
a326e04
1 Parent(s): 912a3d8

move to gradio

Browse files
Files changed (2) hide show
  1. app.py +57 -5
  2. app_streamlit.py +12 -0
app.py CHANGED
@@ -1,12 +1,64 @@
1
- import os, glob, sys
2
- import pickle
3
- import streamlit as st
 
 
4
  import utils
5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  in_gpu = False
7
  num_images = 1
8
  G = utils.load_default_gen(in_gpu=in_gpu)
9
  sampler = utils.SampleFromGAN(G=G, z_shp=[num_images, G.z_dim], in_gpu=in_gpu)
10
- button_on_click = utils.Plot(im_gen=sampler)
11
- button_gen_clicked = st.button(label='Generate an image', key='n', on_click=button_on_click)
12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import sys, os
3
+ import gradio as gr
4
+ import numpy as np
5
+ sys.path.append('stylegan3')
6
  import utils
7
 
8
+ def to_uint8(im, ndim=2):
9
+ im -= np.min(im)
10
+ im /= np.max(im)
11
+ im *= 255.
12
+ im = np.asarray(im, dtype=np.uint8)
13
+ if ndim == 3:
14
+ if im.ndim == 2:
15
+ im = np.expand_dims(im, axis=-1)
16
+ elif im.ndim == 3:
17
+ if im.shape[0] == 1:
18
+ np.transpose(im, (1,2,0))
19
+ im = np.tile(im, (1,1,3)) #make fake RGB
20
+ return im
21
+ elif ndim ==2:
22
+ if im.ndim == 2:
23
+ return im
24
+ if im.ndim == 3:
25
+ if im.shape[0] == 1: #[1, H, W]
26
+ return im[0,...]
27
+ elif im.shape[2] == 1: #[H, W, 1]
28
+ return im[...,0]
29
+ else:
30
+ raise AssertionError(f"Unexpected image passed to to_uint8 with shape: {np.shape(im)}.")
31
+
32
+
33
  in_gpu = False
34
  num_images = 1
35
  G = utils.load_default_gen(in_gpu=in_gpu)
36
  sampler = utils.SampleFromGAN(G=G, z_shp=[num_images, G.z_dim], in_gpu=in_gpu)
 
 
37
 
38
+ def sample_GAN():
39
+ im = sampler()
40
+ im = im.numpy()
41
+ im = np.transpose(im, (1,2,0))
42
+ im = np.squeeze(im) #if single channel (yes), drop it.
43
+ # print(f"sample_linearBP: im shape: {im.shape}; min: {np.min(im)}, max: {np.max(im)}.")
44
+ im = to_uint8(im, ndim=2)
45
+ # print(f'1. uint image shape: {im.shape}')
46
+ return im
47
+
48
+
49
+ title="Generate fake linear array images"
50
+ description="Generate fake linear array images."
51
+
52
+ with gr.Blocks() as demo:
53
+ gr.Markdown(description)
54
+ # with gr.Row(equal_height=True):
55
+ # button_gen = gr.Button("Generate fake linear image")
56
+ # with gr.Row(equal_height=True):
57
+ # output_im = gr.Image(type="numpy", shape=(256, 256), image_mode="L", label="fake image", interactive=False) #grayscale image
58
+ with gr.Column(scale=1):
59
+ button_gen = gr.Button("Generate fake linear image")
60
+ with gr.Column(scale=2):
61
+ output_im = gr.Image(type="numpy", shape=(256, 256), image_mode="L", label="fake image", interactive=False) #grayscale image
62
+ button_gen.click(sample_GAN, inputs=None, outputs=output_im)
63
+
64
+ demo.launch(share=False, show_tips=True, enable_queue=True)
app_streamlit.py ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os, glob, sys
2
+ import pickle
3
+ import streamlit as st
4
+ import utils
5
+
6
+ in_gpu = False
7
+ num_images = 1
8
+ G = utils.load_default_gen(in_gpu=in_gpu)
9
+ sampler = utils.SampleFromGAN(G=G, z_shp=[num_images, G.z_dim], in_gpu=in_gpu)
10
+ button_on_click = utils.Plot(im_gen=sampler)
11
+ button_gen_clicked = st.button(label='Generate an image', key='n', on_click=button_on_click)
12
+