def roll_features(self, feats, xy, jitter_scale=32): """Rolls an individual set of feature maps in-place.""" xy = xy * jitter_scale for layer, feat in feats.items(): scale, _ = self.layer_info(layer) roll2(feat, xy // scale) return feats
def roll(self, xy): """Rolls the optimizer's internal state.""" if (xy == 0).all(): return self.xy += xy if self.grad is not None: self.grad[:] = roll2(self.grad, xy) for s, y in zip(self.sk, self.yk): s[:] = roll2(s, xy) y[:] = roll2(y, xy)
def preprocess_images(self, pool, content_images, style_images, content_layers, style_layers, tile_size=512, roll=None): """Performs preprocessing tasks on the input images.""" # Construct list of layers to visit during the backward pass layers = [] for layer in reversed(self.layers()): if layer in content_layers or layer in style_layers: layers.append(layer) # Prepare Gram matrices from style images if roll is None: print_('Preprocessing the style image(s)...') for image in style_images: grams = {} self.set_image(image) roll2(self.img, roll) feats = self.prepare_features(pool, style_layers, tile_size, passes=1) for layer in feats: grams[layer] = gram_matrix(feats[layer]) self.styles.append(StyleData(grams)) # Prepare feature maps from content image if roll is None: print_('Preprocessing the content image(s)...') n = 10 else: n = 1 for image in content_images: self.set_image(image) roll2(self.img, roll) feats = self.prepare_features(pool, content_layers, tile_size, passes=n) self.contents.append(ContentData(feats))
def roll(self, xy): """Rolls the optimizer's internal state.""" if (xy == 0).all(): return self.xy += xy roll2(self.g1.value, xy) roll2(self.g2.value, xy) roll2(self.p1.value, xy)
def roll(self, xy): """Rolls the optimizer's internal state.""" if (xy == 0).all(): return self.xy += xy if self.grad is not None: roll2(self.grad, xy) for s, y in zip(self.sk, self.yk): roll2(s, xy) roll2(y, xy)
def update(self, opfunc): """Returns a step's parameter update given a loss/gradient evaluation function.""" # Step size decay step_size = self.step_size / self.i**self.power self.i += self.decay loss, grad = opfunc(self.params) # Adam self.g1.update(grad) self.g2.update(grad**2) step = self.g1.get() / (np.sqrt(self.g2.get()) + EPS) saxpy(-step_size, step, self.params) # Iterate averaging self.p1.update(self.params) return roll2(self.p1.get(), -self.xy), loss
def update(self, opfunc): """Returns a step's parameter update given a loss/gradient evaluation function.""" # Step size decay step_size = self.step_size / self.i**self.power self.i += self.decay loss, grad = opfunc(self.params) # Adam self.g1.update(grad) self.g2.update(grad**2) step = self.g1.get() / (np.sqrt(self.g2.get()) + EPS) axpy(-step_size, step, self.params) # Iterate averaging self.p1.update(self.params) return roll2(self.p1.get(), -self.xy), loss
def roll(self, xy, jitter_scale=32): """Rolls the image, feature maps.""" for content in self.contents: self.roll_features(content.features, xy, jitter_scale) roll2(self.img, xy * jitter_scale)
def preprocess_images(self, pool, content_images, style_images, content_layers, style_layers, tile_size=512, roll=None): """Performs preprocessing tasks on the input images.""" # Construct list of layers to visit during the backward pass layers = [] for layer in reversed(self.layers()): if layer in content_layers or layer in style_layers: layers.append(layer) # Prepare Gram matrices from style images if roll is None: print('Preprocessing the style image(s)...') sizes = [None] if ARGS.style_multiscale: vmin, vmax = ARGS.style_multiscale size = vmax sizes = [vmax] while True: size = int(round(size / np.sqrt(2))) if size < max(32, vmin): break sizes.append(size) if not self.styles: grams = {} count = 0 for i, image in enumerate(style_images): scale_too_big_cond = False for size in reversed(sizes): if scale_too_big_cond: break if size: image_scaled = resize_to_fit(image, size) if max(image_scaled.size) == max(image.size): scale_too_big_cond = True if min(image_scaled.size) < 32: continue self.set_image(image_scaled) h, w = image_scaled.size print('Processing style {} at {}x{}.'.format( i + 1, h, w)) else: self.set_image(image) roll2(self.img, roll) feats = self.prepare_features(pool, style_layers, tile_size, passes=1) for layer in feats: gram = gram_matrix(feats[layer]) if layer not in grams: grams[layer] = gram else: grams[layer] += gram count += 1 for gram in grams.values(): gram /= count self.styles.append(StyleData(grams)) # Prepare feature maps from content image if roll is None: print('Preprocessing the content image(s)...') n = 10 else: n = 1 for image in content_images: self.set_image(image) roll2(self.img, roll) feats = self.prepare_features(pool, content_layers, tile_size, passes=n) self.contents.append(ContentData(feats))