{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"## Experiment - Generate Images from trained StyleGAN for covid & negative\n","metadata":{}},{"cell_type":"code","source":"import os\nos_info = os.uname()\nprint(os_info) ","metadata":{"execution":{"iopub.status.busy":"2021-08-24T20:08:09.817066Z","iopub.execute_input":"2021-08-24T20:08:09.817426Z","iopub.status.idle":"2021-08-24T20:08:09.822558Z","shell.execute_reply.started":"2021-08-24T20:08:09.817396Z","shell.execute_reply":"2021-08-24T20:08:09.821486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cd '/kaggle/working/'","metadata":{"execution":{"iopub.status.busy":"2021-08-24T20:08:12.16177Z","iopub.execute_input":"2021-08-24T20:08:12.162102Z","iopub.status.idle":"2021-08-24T20:08:12.166968Z","shell.execute_reply.started":"2021-08-24T20:08:12.162071Z","shell.execute_reply":"2021-08-24T20:08:12.166148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#!git clone https://github.com/NVlabs/stylegan2-ada.git\n#added different generators as image trained for covid & negative were 1 channel and 3 channels\n#generator to output jpg instead of png\nimport shutil\ntry:\n    shutil.rmtree('/kaggle/working/stylegan2-ada-pytorch')\nexcept:\n    pass\n\n!git clone https://github.com/aaronbcj/stylegan2-ada-pytorch.git","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-08-24T20:08:15.703707Z","iopub.execute_input":"2021-08-24T20:08:15.704142Z","iopub.status.idle":"2021-08-24T20:08:17.736467Z","shell.execute_reply.started":"2021-08-24T20:08:15.704098Z","shell.execute_reply":"2021-08-24T20:08:17.735576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#!conda update -n base conda --yes\n#!conda install -y python=3.7\n!python --version\nimport torch \nprint(torch.__version__)","metadata":{"execution":{"iopub.status.busy":"2021-08-24T20:08:22.899315Z","iopub.execute_input":"2021-08-24T20:08:22.899718Z","iopub.status.idle":"2021-08-24T20:08:23.563959Z","shell.execute_reply.started":"2021-08-24T20:08:22.899681Z","shell.execute_reply":"2021-08-24T20:08:23.563046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!nvidia-smi","metadata":{"execution":{"iopub.status.busy":"2021-08-24T18:59:45.56718Z","iopub.execute_input":"2021-08-24T18:59:45.567523Z","iopub.status.idle":"2021-08-24T18:59:46.249571Z","shell.execute_reply.started":"2021-08-24T18:59:45.567489Z","shell.execute_reply":"2021-08-24T18:59:46.248678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#%cd /kaggle/working\n#!wget https://docs.google.com/uc?export=download&id=1aZjaWs-NUeE_pv0VzM9lU3O6lwxqWF26\n#!pip install -r ../input/ljmusiimcovid19exp4/lmju-siim-covid19-exp4/requirements.txt -f https://download.pytorch.org/whl/torch_stable.html\n#!wget https://storage.googleapis.com/ljmu-thesis-project/network-snapshot-000743-tf-neg.pkl\n\n#https://docs.google.com/uc?export=download&id=1aZjaWs-NUeE_pv0VzM9lU3O6lwxqWF26\n#https://drive.google.com/drive/folders/1aZjaWs-NUeE_pv0VzM9lU3O6lwxqWF26?usp=sharing\n","metadata":{"execution":{"iopub.status.busy":"2021-08-16T14:33:53.18512Z","iopub.execute_input":"2021-08-16T14:33:53.185417Z","iopub.status.idle":"2021-08-16T14:33:53.191101Z","shell.execute_reply.started":"2021-08-16T14:33:53.185387Z","shell.execute_reply":"2021-08-16T14:33:53.190295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# TEST if network deps/settings are working before actual generation - COVID\n%cd '/kaggle/working/stylegan2-ada-pytorch'\n!python generate-negative.py --outdir='/kaggle/working/test' --trunc=.6 --seeds=85,265,297 --network='/kaggle/input/ljmusiim19stylegan2ada/py-neg-004900.pkl'","metadata":{"execution":{"iopub.status.busy":"2021-08-24T20:11:15.31134Z","iopub.execute_input":"2021-08-24T20:11:15.311686Z","iopub.status.idle":"2021-08-24T20:12:02.681582Z","shell.execute_reply.started":"2021-08-24T20:11:15.311653Z","shell.execute_reply":"2021-08-24T20:12:02.68062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd /kaggle/working/test\nfrom IPython.display import display, Image\ndisplay(Image(filename='seed0085.jpg'))\ndisplay(Image(filename='seed0265.jpg'))\ndisplay(Image(filename='seed0297.jpg'))","metadata":{"execution":{"iopub.status.busy":"2021-08-24T20:12:10.10966Z","iopub.execute_input":"2021-08-24T20:12:10.109994Z","iopub.status.idle":"2021-08-24T20:12:10.126214Z","shell.execute_reply.started":"2021-08-24T20:12:10.109959Z","shell.execute_reply":"2021-08-24T20:12:10.125386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random\nimport os\nimport shutil\n\n#generate random seeds\ndef get_seeds(num,st,en):\n    li=list()\n    for j in range(num):\n        li.append(random.randint(st, en))\n    \n    seeds=set(li)\n    while len(seeds)<num:\n        seeds.add(random.randint(st, en))\n    \n    return list(seeds)\n\n#print(get_seeds(5,10,3000))\n\ndef make_folder(path):\n    try:\n        shutil.rmtree(path)\n    except:\n        pass\n    finally:\n        os.makedirs(path)\n        print(path+\" created\")\n\n#make zip file\ndef make_zip(folder):\n\n    %cd '/kaggle/working'\n    \n    path=\"/kaggle/working/\"+folder\n    zipfile=folder+\".zip\"\n\n    if os.path.isfile(zipfile):\n        os.remove(zipfile)\n    shutil.make_archive(folder, 'zip', path)\n\n#generate images    \ndef generate_images(truncation, int_list, model_name, folder_name):\n    \n    %cd '/kaggle/working/stylegan2-ada-pytorch'\n    \n    str_list = [str(element) for element in int_list]\n    seeds=\",\".join(str_list)\n    model_path='/kaggle/input/ljmusiim19stylegan2ada/'+model_name\n    image_path='/kaggle/working/' + folder_name\n    \n    #covid/img_1_0\n    image_path = image_path + \"/img_\" + str(truncation).replace(\".\",\"_\")\n    make_folder(image_path)\n    !python generate-{folder_name}.py --outdir={image_path} --trunc={truncation} --seeds={seeds} --network={model_path}\n\n    print(\"generated ...\")","metadata":{"execution":{"iopub.status.busy":"2021-08-24T20:18:37.705236Z","iopub.execute_input":"2021-08-24T20:18:37.705566Z","iopub.status.idle":"2021-08-24T20:18:37.728247Z","shell.execute_reply.started":"2021-08-24T20:18:37.705535Z","shell.execute_reply":"2021-08-24T20:18:37.727392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#trunc=1.0 for 1000 images, 0.70 for 1000 and 0.5 for 1000\n#todo: to make sure all 3 sets/collections are mutually exclusive (100-3000, 3100-6000, 6100-9000)\nN=1000\nmake_folder('covid')\nmake_folder('negative')\n\nprint(\"Generating covid trunc=1.0 ...\")\nseeds_1_0 = get_seeds(N, 100,3000)\ngenerate_images(1.0, seeds_1_0, 'py-cov-005010.pkl', 'covid')\n\nprint(\"Generating covid trunc=0.7 ...\")\nseeds_0_7 = get_seeds(N, 3100,6000)\ngenerate_images(0.7, seeds_0_7, 'py-cov-005010.pkl', 'covid')\n\nprint(\"Generating covid trunc=0.5 ...\")\nseeds_0_5 = get_seeds(N, 6100,9000)\ngenerate_images(0.5, seeds_0_5, 'py-cov-005010.pkl', 'covid')\n\nprint(\"Generating negative trunc=1.0 ...\")\nseeds_1_0 = get_seeds(N, 100,3000)\ngenerate_images(1.0, seeds_1_0, 'py-neg-004900.pkl', 'negative')\n\nprint(\"Generating negative trunc=0.7 ...\")\nseeds_0_7 = get_seeds(N, 3100,6000)\ngenerate_images(0.7, seeds_0_7, 'py-neg-004900.pkl', 'negative')\n\nprint(\"Generating negative trunc=0.5 ...\")\nseeds_0_5 = get_seeds(N, 6100,9000)\ngenerate_images(0.5, seeds_0_5, 'py-neg-004900.pkl', 'negative')\n\nprint(\"Making zip files ...\")\nmake_zip('covid')\nmake_zip('negative')","metadata":{"execution":{"iopub.status.busy":"2021-08-24T20:18:41.686012Z","iopub.execute_input":"2021-08-24T20:18:41.68638Z","iopub.status.idle":"2021-08-24T20:19:24.288734Z","shell.execute_reply.started":"2021-08-24T20:18:41.686347Z","shell.execute_reply":"2021-08-24T20:19:24.287786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from IPython.display import FileLink, FileLinks\n\n%cd '/kaggle/working'\nFileLink('covid.zip')\n","metadata":{"execution":{"iopub.status.busy":"2021-08-24T20:00:04.78347Z","iopub.execute_input":"2021-08-24T20:00:04.783885Z","iopub.status.idle":"2021-08-24T20:00:04.795985Z","shell.execute_reply.started":"2021-08-24T20:00:04.78385Z","shell.execute_reply":"2021-08-24T20:00:04.795093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"FileLink('negative.zip')\n\n","metadata":{"execution":{"iopub.status.busy":"2021-08-24T20:00:07.626579Z","iopub.execute_input":"2021-08-24T20:00:07.626909Z","iopub.status.idle":"2021-08-24T20:00:07.631924Z","shell.execute_reply.started":"2021-08-24T20:00:07.626876Z","shell.execute_reply":"2021-08-24T20:00:07.631143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Completed ...\")","metadata":{"execution":{"iopub.status.busy":"2021-08-24T20:00:10.912779Z","iopub.execute_input":"2021-08-24T20:00:10.913157Z","iopub.status.idle":"2021-08-24T20:00:10.918617Z","shell.execute_reply.started":"2021-08-24T20:00:10.913121Z","shell.execute_reply":"2021-08-24T20:00:10.917597Z"},"trusted":true},"execution_count":null,"outputs":[]}]}