Spaces:
Running
Running
File size: 5,089 Bytes
71dda47 |
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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"\n",
"import sys, os\n",
"import gradio as gr\n",
"import numpy as np\n",
"sys.path.append('stylegan3')\n",
"import utils\n",
"\n",
"def to_uint8(im, ndim=2):\n",
" im -= np.min(im)\n",
" im /= np.max(im)\n",
" im *= 255.\n",
" im = np.asarray(im, dtype=np.uint8)\n",
" if ndim == 3: \n",
" if im.ndim == 2:\n",
" im = np.expand_dims(im, axis=-1)\n",
" elif im.ndim == 3:\n",
" if im.shape[0] == 1: \n",
" np.transpose(im, (1,2,0))\n",
" im = np.tile(im, (1,1,3)) #make fake RGB\n",
" return im\n",
" elif ndim ==2:\n",
" if im.ndim == 2:\n",
" return im\n",
" if im.ndim == 3:\n",
" if im.shape[0] == 1: #[1, H, W]\n",
" return im[0,...]\n",
" elif im.shape[2] == 1: #[H, W, 1]\n",
" return im[...,0]\n",
" else:\n",
" raise AssertionError(f\"Unexpected image passed to to_uint8 with shape: {np.shape(im)}.\")\n"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Running on local URL: http://127.0.0.1:7868\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"2022-10-11 17:33:10.390 INFO paramiko.transport: Connected (version 2.0, client OpenSSH_7.6p1)\n",
"2022-10-11 17:33:11.271 INFO paramiko.transport: Authentication (publickey) successful!\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Running on public URL: https://23528.gradio.app\n",
"\n",
"This share link expires in 72 hours. For free permanent hosting, check out Spaces: https://huggingface.co/spaces\n"
]
},
{
"data": {
"text/html": [
"<div><iframe src=\"https://23528.gradio.app\" width=\"900\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Tip: The inputs and outputs flagged by the users are stored in the flagging directory, specified by the flagging_dir= kwarg. You can view this data through the interface by setting the examples= kwarg to the flagging directory; for example gr.Interface(..., examples='flagged')\n"
]
},
{
"data": {
"text/plain": [
"(<gradio.routes.App at 0x7fcded9da880>,\n",
" 'http://127.0.0.1:7868/',\n",
" 'https://23528.gradio.app')"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\n",
"\n",
"in_gpu = False\n",
"num_images = 1\n",
"G = utils.load_default_gen(in_gpu=in_gpu)\n",
"sampler = utils.SampleFromGAN(G=G, z_shp=[num_images, G.z_dim], in_gpu=in_gpu)\n",
"\n",
"def sample_GAN():\n",
" im = sampler()\n",
" im = im.numpy()\n",
" im = np.transpose(im, (1,2,0))\n",
" im = np.squeeze(im) #if single channel (yes), drop it.\n",
" # print(f\"sample_linearBP: im shape: {im.shape}; min: {np.min(im)}, max: {np.max(im)}.\")\n",
" im = to_uint8(im, ndim=2)\n",
" # print(f'1. uint image shape: {im.shape}')\n",
" return im\n",
"\n",
"\n",
"title=\"Generate fake linear array images\"\n",
"description=\"Generate fake linear array images.\"\n",
"\n",
"with gr.Blocks() as demo:\n",
" gr.Markdown(description)\n",
" with gr.Row():\n",
" with gr.Column(scale=2):\n",
" button_gen = gr.Button(\"Generate fake linear image\")\n",
" with gr.Column(scale=2):\n",
" output_im = gr.Image(type=\"numpy\", shape=(256, 256), image_mode=\"L\", label=\"fake image\", interactive=False) #grayscale image\n",
" button_gen.click(sample_GAN, inputs=None, outputs=output_im)\n",
" \n",
"demo.launch(share=True, show_tips=True, enable_queue=True)\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.9.0 ('torch')",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.0"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "d9a708e2b965f26293af085a2040b513f9ca1674b4051c9a67720a0b5ff04f14"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|