Skip to content

Commit

Permalink
Content reconstruction that saves output and intermediate frames.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexjc committed Mar 6, 2016
1 parent 9719680 commit 57d36c5
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions doodle.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,12 @@ def __init__(self):
grad = T.grad(self.content_loss, self.model.tensor_img)
self.compute_loss_and_grad = theano.function([self.model.tensor_img], [self.content_loss, grad])

def prepare_image(self, image):
image = np.swapaxes(np.swapaxes(image, 1, 2), 0, 1)[::-1, :, :]
image = image.astype(np.float32) - self.model.pixel_mean
return image[np.newaxis]

def evaluate(self, Xn):
current_img = Xn.reshape(self.content_image.shape) - self.model.pixel_mean
loss, grads = self.compute_loss_and_grad(current_img)

scipy.misc.toimage(self.finalize_image(Xn), cmin=0, cmax=255).save('frames/test%04d.png'%self.iteration)

print(self.iteration, 'loss', loss, 'gradients', grads.min(), grads.max())

self.iteration += 1
Expand All @@ -104,9 +101,19 @@ def run(self):
bounds=data_bounds,
factr=0.0, pgtol=0.0, # Disable automatic termination by setting low threshold.
m=16, # Maximum correlations kept in memory by algorithm.
maxfun=5, # Limit number of calls to evaluate().
maxfun=100, # Limit number of calls to evaluate().
iprint=-1) # Handle our own logging of information.

def prepare_image(self, image):
image = np.swapaxes(np.swapaxes(image, 1, 2), 0, 1)[::-1, :, :]
image = image.astype(np.float32) - self.model.pixel_mean
return image[np.newaxis]

def finalize_image(self, x):
x = x.reshape(self.content_image.shape[1:])[::-1]
x = np.swapaxes(np.swapaxes(x, 0, 1), 1, 2)
return np.clip(x, 0, 255).astype('uint8')


if __name__ == "__main__":
generator = NeuralGenerator()
Expand Down

0 comments on commit 57d36c5

Please sign in to comment.