{ "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": [ "
" ], "text/plain": [ "" ] }, "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": [ "(,\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 }