MCPcopy Create free account
hub / github.com/OpenGVLab/DragGAN / generate_image

Function generate_image

draggan/draggan.py:143–185  ·  view source on GitHub ↗

Generates an image using a pretrained generator network. Args: W (torch.Tensor): A tensor of latent codes of shape [batch_size, latent_dim, 512]. _G (Optional[torch.nn.Module]): The generator network. If None, the network will be loaded from `network_pkl`. network_p

(
    W,
    _G: Optional[torch.nn.Module] = None,
    network_pkl: Optional[str] = None,
    class_idx=None,
    device=torch.device("cuda"),
)

Source from the content-addressed store, hash-verified

141
142
143def generate_image(
144 W,
145 _G: Optional[torch.nn.Module] = None,
146 network_pkl: Optional[str] = None,
147 class_idx=None,
148 device=torch.device("cuda"),
149) -> Tuple[PIL.Image.Image, torch.Tensor]:
150 """
151 Generates an image using a pretrained generator network.
152
153 Args:
154 W (torch.Tensor): A tensor of latent codes of shape [batch_size, latent_dim, 512].
155 _G (Optional[torch.nn.Module]): The generator network. If None, the network will be loaded from `network_pkl`.
156 network_pkl (Optional[str]): The path to the network pickle file. If None, the default network will be used.
157 class_idx (Optional[int]): The class index to use for conditional generation. If None, unconditional generation will be used.
158 device (str): The device to use for the computation.
159
160 Returns:
161 A tuple containing the generated image as a PIL Image object and the feature maps tensor of shape [batch_size, num_channels, height, width].
162 """
163 if _G is None:
164 assert network_pkl is not None
165 _G = load_model(network_pkl, device)
166 G = _G
167
168 # Labels.
169 label = torch.zeros([1, G.c_dim], device=device)
170 if G.c_dim != 0:
171 if class_idx is None:
172 raise Exception(
173 "Must specify class label with --class when using a conditional network"
174 )
175 label[:, class_idx] = 1
176 else:
177 if class_idx is not None:
178 print("warn: --class=lbl ignored when running on an unconditional network")
179
180 # Generate image
181 img, features = forward_G(G, W, device)
182
183 img = utils.tensor_to_PIL(img)
184
185 return img, features
186
187
188def drag_gan(

Callers

nothing calls this directly

Calls 2

load_modelFunction · 0.85
forward_GFunction · 0.85

Tested by

no test coverage detected