Ahsen Khaliq
commited on
Commit
·
c27187e
1
Parent(s):
d343810
Update app.py
Browse files
app.py
CHANGED
|
@@ -48,28 +48,28 @@ def display_image(image, size=None, mode='nearest', unnorm=False, title=''):
|
|
| 48 |
|
| 49 |
|
| 50 |
def inferece(num, seed):
|
| 51 |
-
model_type = 'landscape'
|
| 52 |
-
num_im =
|
| 53 |
-
random_seed =
|
| 54 |
-
|
| 55 |
plt.rcParams['figure.dpi'] = 300
|
| 56 |
-
|
| 57 |
mean_latent = load_model(generator, f'{model_type}.pt')
|
| 58 |
-
|
| 59 |
# pad determines how much of an image is involve in the blending
|
| 60 |
pad = 512//4
|
| 61 |
-
|
| 62 |
all_im = []
|
| 63 |
-
|
| 64 |
random_state = np.random.RandomState(random_seed)
|
| 65 |
-
|
| 66 |
# latent smoothing
|
| 67 |
with torch.no_grad():
|
| 68 |
z = random_state.randn(num_im, 512).astype(np.float32)
|
| 69 |
z = scipy.ndimage.gaussian_filter(z, [.7, 0], mode='wrap')
|
| 70 |
z /= np.sqrt(np.mean(np.square(z)))
|
| 71 |
-
z = torch.from_numpy(z).
|
| 72 |
-
|
| 73 |
source = generator.get_latent(z, truncation=truncation, mean_latent=mean_latent)
|
| 74 |
|
| 75 |
# merge images 2 at a time
|
|
@@ -77,25 +77,25 @@ def inferece(num, seed):
|
|
| 77 |
source1 = index_layers(source, i)
|
| 78 |
source2 = index_layers(source, i+1)
|
| 79 |
all_im.append(generator.merge_extension(source1, source2))
|
| 80 |
-
|
| 81 |
# display intermediate generations
|
| 82 |
# for i in all_im:
|
| 83 |
# display_image(i)
|
| 84 |
|
| 85 |
-
|
| 86 |
b,c,h,w = all_im[0].shape
|
| 87 |
panorama_im = torch.zeros(b,c,h,512+(num_im-2)*256)
|
| 88 |
-
|
| 89 |
# We created a series of 2-blended images which we can overlay to form a large panorama
|
| 90 |
# add first image
|
| 91 |
coord = 256+pad
|
| 92 |
panorama_im[..., :coord] = all_im[0][..., :coord]
|
| 93 |
-
|
| 94 |
for im in all_im[1:]:
|
| 95 |
panorama_im[..., coord:coord+512-2*pad] = im[..., pad:-pad]
|
| 96 |
coord += 512-2*pad
|
| 97 |
panorama_im[..., coord:] = all_im[-1][..., 512-pad:]
|
| 98 |
-
|
| 99 |
img = display_image(panorama_im)
|
| 100 |
return img
|
| 101 |
|
|
|
|
| 48 |
|
| 49 |
|
| 50 |
def inferece(num, seed):
|
| 51 |
+
model_type = 'landscape' #@param ['church', 'face', 'landscape']
|
| 52 |
+
num_im = 5#@param {type:"number"}
|
| 53 |
+
random_seed = 90#@param {type:"number"}
|
| 54 |
+
|
| 55 |
plt.rcParams['figure.dpi'] = 300
|
| 56 |
+
|
| 57 |
mean_latent = load_model(generator, f'{model_type}.pt')
|
| 58 |
+
|
| 59 |
# pad determines how much of an image is involve in the blending
|
| 60 |
pad = 512//4
|
| 61 |
+
|
| 62 |
all_im = []
|
| 63 |
+
|
| 64 |
random_state = np.random.RandomState(random_seed)
|
| 65 |
+
|
| 66 |
# latent smoothing
|
| 67 |
with torch.no_grad():
|
| 68 |
z = random_state.randn(num_im, 512).astype(np.float32)
|
| 69 |
z = scipy.ndimage.gaussian_filter(z, [.7, 0], mode='wrap')
|
| 70 |
z /= np.sqrt(np.mean(np.square(z)))
|
| 71 |
+
z = torch.from_numpy(z).to(device)
|
| 72 |
+
|
| 73 |
source = generator.get_latent(z, truncation=truncation, mean_latent=mean_latent)
|
| 74 |
|
| 75 |
# merge images 2 at a time
|
|
|
|
| 77 |
source1 = index_layers(source, i)
|
| 78 |
source2 = index_layers(source, i+1)
|
| 79 |
all_im.append(generator.merge_extension(source1, source2))
|
| 80 |
+
|
| 81 |
# display intermediate generations
|
| 82 |
# for i in all_im:
|
| 83 |
# display_image(i)
|
| 84 |
|
| 85 |
+
|
| 86 |
b,c,h,w = all_im[0].shape
|
| 87 |
panorama_im = torch.zeros(b,c,h,512+(num_im-2)*256)
|
| 88 |
+
|
| 89 |
# We created a series of 2-blended images which we can overlay to form a large panorama
|
| 90 |
# add first image
|
| 91 |
coord = 256+pad
|
| 92 |
panorama_im[..., :coord] = all_im[0][..., :coord]
|
| 93 |
+
|
| 94 |
for im in all_im[1:]:
|
| 95 |
panorama_im[..., coord:coord+512-2*pad] = im[..., pad:-pad]
|
| 96 |
coord += 512-2*pad
|
| 97 |
panorama_im[..., coord:] = all_im[-1][..., 512-pad:]
|
| 98 |
+
|
| 99 |
img = display_image(panorama_im)
|
| 100 |
return img
|
| 101 |
|