muneebnadeem1870 commited on
Commit
b2b650f
·
verified ·
1 Parent(s): 70611af

Upload README.md

Browse files
Files changed (1) hide show
  1. README.md +157 -0
README.md ADDED
@@ -0,0 +1,157 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # StyleGAN2-ADA Pipeline for Image Projection
2
+
3
+ This guide provides a step-by-step explanation of how to align a face image, project it into the latent space of StyleGAN2-ADA, and visualize the results.
4
+
5
+ ## Requirements
6
+
7
+ ### Dependencies
8
+ - Python 3.7+
9
+ - PyTorch
10
+ - Required libraries installed via `requirements.txt` in the repository
11
+ - Kaggle environment with internet enabled
12
+
13
+ ### Models and Methods Used
14
+ - **Face Alignment:** `align_images.py` uses the `shape_predictor_68_face_landmarks.dat` model from DLib for precise facial alignment.
15
+ - **Image Projection:** `projector.py` projects an aligned image into the latent space of StyleGAN2 using a pre-trained model (`ffhq.pkl` from NVIDIA Labs).
16
+ - **Pre-trained Models:**
17
+ - Face landmark model: `shape_predictor_68_face_landmarks.dat`
18
+ - StyleGAN2-ADA pre-trained weights: `ffhq.pkl`
19
+
20
+ ---
21
+
22
+ ## Step-by-Step Execution
23
+
24
+ ### 1. Clone the Repository
25
+ Clone the repository for StyleGAN2-ADA:
26
+ ```bash
27
+ !git clone https://github.com/rkuo2000/stylegan2-ada-pytorch.git
28
+ %cd stylegan2-ada-pytorch
29
+ ```
30
+
31
+ ### 2. Prepare the Raw Images
32
+ Create a directory for raw images and copy the desired file:
33
+ ```bash
34
+ !mkdir -p raw
35
+ !cp /kaggle/input/test-notebook-images/profile-image.jpg raw/example.jpg
36
+ ```
37
+ Verify the file:
38
+ ```bash
39
+ !ls raw
40
+ ```
41
+
42
+ ### 3. Align the Face Image
43
+ Run the face alignment script:
44
+ ```bash
45
+ !python align_images.py raw aligned
46
+ ```
47
+ - **Input:** `raw/example.jpg`
48
+ - **Output:** Aligned image saved as `aligned/example_01.png`
49
+
50
+ ### 4. Verify Alignment
51
+ List the aligned directory to confirm output:
52
+ ```bash
53
+ !ls aligned
54
+ ```
55
+
56
+ ### 5. Project the Image into Latent Space
57
+ Run the projection script:
58
+ ```bash
59
+ !python projector.py --outdir=out --target=aligned/example_01.png \
60
+ --network=https://nvlabs-fi-cdn.nvidia.com/stylegan2-ada-pytorch/pretrained/ffhq.pkl
61
+ ```
62
+ - **Output:**
63
+ - Latent space projection results saved in the `out/` directory
64
+ - A video (`proj.mp4`) showing optimization progress
65
+
66
+ ---
67
+
68
+ ## Viewing Results
69
+
70
+ ### 1. Inline Video Playback
71
+ Use the following command to view the progress video inline:
72
+ ```python
73
+ from IPython.display import Video
74
+ Video('out/proj.mp4', embed=True)
75
+ ```
76
+
77
+ ### 2. Download the Video
78
+ To download the video file, use:
79
+ ```python
80
+ from IPython.display import FileLink
81
+ FileLink('out/proj.mp4')
82
+ ```
83
+ Click the generated link to download `proj.mp4` to your local machine.
84
+
85
+ ---
86
+
87
+ ## Adding Gradio for Runtime Image Upload
88
+ You can integrate Gradio to allow users to upload a photo and generate the GAN output (image and video) on runtime. Here is how to modify the pipeline:
89
+
90
+ ### Install Gradio
91
+ ```bash
92
+ !pip install gradio
93
+ ```
94
+
95
+ ### Update the Code
96
+ Add the following Python script to create a Gradio interface:
97
+
98
+ ```python
99
+ import gradio as gr
100
+ import subprocess
101
+ from PIL import Image
102
+
103
+ def process_image(input_image):
104
+ # Save the input image to raw directory
105
+ input_path = "raw/input_image.jpg"
106
+ input_image.save(input_path)
107
+
108
+ # Align the face
109
+ subprocess.run(["python", "align_images.py", "raw", "aligned"])
110
+
111
+ # Run projection
112
+ subprocess.run([
113
+ "python", "projector.py", "--outdir=out", "--target=aligned/input_image_01.png", \
114
+ "--network=https://nvlabs-fi-cdn.nvidia.com/stylegan2-ada-pytorch/pretrained/ffhq.pkl"
115
+ ])
116
+
117
+ # Load generated image and video
118
+ output_image_path = "out/proj.png" # Adjust if necessary
119
+ output_video_path = "out/proj.mp4"
120
+ output_image = Image.open(output_image_path)
121
+
122
+ return output_image, output_video_path
123
+
124
+ # Gradio Interface
125
+ demo = gr.Interface(
126
+ fn=process_image,
127
+ inputs=[gr.Image(type="pil", label="Upload an Image")],
128
+ outputs=[
129
+ gr.Image(type="pil", label="Generated Image"),
130
+ gr.Video(label="Projection Video")
131
+ ],
132
+ title="StyleGAN2-ADA Image Projection",
133
+ description="Upload a face image to generate GAN output and projection video."
134
+ )
135
+
136
+ demo.launch()
137
+ ```
138
+
139
+ ### Running the Gradio Interface
140
+ Save the above script and run it in your environment. A Gradio web interface will open, allowing users to upload images and see the generated results in real time.
141
+
142
+ ---
143
+
144
+ ## Notes
145
+ 1. Ensure the internet is enabled in your Kaggle notebook for downloading required models.
146
+ 2. Verify input paths to match your dataset and file structure.
147
+ 3. Outputs are saved in the following structure:
148
+ - `raw/`: Original images
149
+ - `aligned/`: Aligned face images
150
+ - `out/`: Projection results and video
151
+
152
+ ---
153
+
154
+ ## Acknowledgments
155
+ - StyleGAN2-ADA by NVIDIA Labs: [GitHub Repository](https://github.com/NVlabs/stylegan2-ada-pytorch)
156
+ - DLib for face alignment: [DLib Library](http://dlib.net/)
157
+