bankholdup commited on
Commit
2b1dfa7
1 Parent(s): aa616da

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -5
app.py CHANGED
@@ -14,6 +14,7 @@ from torch import nn, autograd, optim
14
  from torch.nn import functional as F
15
  from tqdm import tqdm
16
  import lpips
 
17
 
18
 
19
  #from e4e_projection import projection as e4e_projection
@@ -35,6 +36,9 @@ from e4e.models.stylegan2.model import Generator
35
  from util import *
36
  from huggingface_hub import hf_hub_download
37
 
 
 
 
38
  transform = transforms.Compose([
39
  transforms.Resize((256, 256)),
40
  transforms.ToTensor(),
@@ -102,12 +106,11 @@ cat_decoder.load_state_dict(cat_d_filt, strict=True)
102
  cat_decoder.eval()
103
  cat_decoder.to(device)
104
 
 
 
 
105
 
106
  def run_alignment(image_path):
107
- import dlib
108
- from e4e.utils.alignment import align_face
109
- dlib_path = hf_hub_download(repo_id="bankholdup/stylegan_petbreeder", filename="shape_predictor_68_face_landmarks.dat")
110
- predictor = dlib.shape_predictor(dlib_path)
111
  aligned_image = align_face(filepath=image_path, predictor=predictor)
112
  print("Aligned image has shape: {}".format(aligned_image.size))
113
  return aligned_image
@@ -124,10 +127,16 @@ def gen_im(ffhq_codes, dog_codes, cat_codes, model_type='ffhq'):
124
  imgs, _ = custom_decoder([custom_codes], input_is_latent=True, randomize_noise=False, return_latents=True)
125
  return tensor2im(imgs[0])
126
 
 
 
 
127
 
128
  def inference(img):
 
 
 
129
  img.save('out.jpg')
130
- #aligned_face = align_face('out.jpg')
131
  input_image = run_alignment('out.jpg')
132
  transformed_image = transform(input_image)
133
 
 
14
  from torch.nn import functional as F
15
  from tqdm import tqdm
16
  import lpips
17
+ import time
18
 
19
 
20
  #from e4e_projection import projection as e4e_projection
 
36
  from util import *
37
  from huggingface_hub import hf_hub_download
38
 
39
+ import dlib
40
+ from e4e.utils.alignment import align_face
41
+
42
  transform = transforms.Compose([
43
  transforms.Resize((256, 256)),
44
  transforms.ToTensor(),
 
106
  cat_decoder.eval()
107
  cat_decoder.to(device)
108
 
109
+ dlib_path = hf_hub_download(repo_id="bankholdup/stylegan_petbreeder", filename="shape_predictor_68_face_landmarks.dat")
110
+ predictor = dlib.shape_predictor(dlib_path)
111
+
112
 
113
  def run_alignment(image_path):
 
 
 
 
114
  aligned_image = align_face(filepath=image_path, predictor=predictor)
115
  print("Aligned image has shape: {}".format(aligned_image.size))
116
  return aligned_image
 
127
  imgs, _ = custom_decoder([custom_codes], input_is_latent=True, randomize_noise=False, return_latents=True)
128
  return tensor2im(imgs[0])
129
 
130
+ def set_seed(rd):
131
+ np.random.seed(rd)
132
+ torch.manual_seed(rd)
133
 
134
  def inference(img):
135
+ random_seed = round(time.time() * 1000)
136
+ set_seed(random_seed)
137
+
138
  img.save('out.jpg')
139
+
140
  input_image = run_alignment('out.jpg')
141
  transformed_image = transform(input_image)
142