Skip to content

Instantly share code, notes, and snippets.

View ArnoutDevos's full-sized avatar
🎯
Focusing

Arnout Devos ArnoutDevos

🎯
Focusing
View GitHub Profile
{% if page.mathjax %}
<script type="text/javascript" async
src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-MML-AM_CHTML">
</script>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
tex2jax: {inlineMath: [['@@','@@'], ['\\(','\\)']]}
});
</script>
{% endif %}
def step_forward(self, x, prev_h):
next_h = np.tanh(x.dot(self.params[self.wx_name]) + prev_h.dot(self.params[self.wh_name]) + self.params[self.b_name])
meta = [x, prev_h, next_h]
return next_h, meta
def step_backward(self, dnext_h, meta):
x, prev_h, next_h = meta
dtanh = dnext_h*(1.0-next_h**2)
dx = dtanh.dot(self.params[self.wx_name].T)
dprev_h = dtanh.dot(self.params[self.wh_name].T)
dWx = x.T.dot(dtanh)
dWh = prev_h.T.dot(dtanh)
db = np.sum(dtanh,axis=0)
VGG_MODEL = 'model/imagenet-vgg-verydeep-19.mat'
MEAN_VALUES = np.array([123.68, 116.779, 103.939]).reshape((1,1,3))
def load_vgg_model(path):
vgg = scipy.io.loadmat(path)
vgg_layers = vgg['layers']
def _weights(layer, expected_layer_name):
W = vgg_layers[0][layer][0][0][2][0][0]
# Output folder for the images.
OUTPUT_DIR = 'output/'
# Style image to use.
STYLE_IMAGE = 'images/muse.jpg'
# Content image to use.
CONTENT_IMAGE = 'images/trojan_shrine.jpg'
# Image dimensions constants.
IMAGE_WIDTH = 640
IMAGE_HEIGHT = 480
COLOR_CHANNELS = 3
[content_image_raw, content_image] = load_image(CONTENT_IMAGE)
[style_image_raw, style_image] = load_image(STYLE_IMAGE)
fig = plt.figure(figsize=(10,10))
ax1 = plt.subplot(221)
ax2 = plt.subplot(222)
ax3 = plt.subplot(223)
ax4 = plt.subplot(224)
def normalize(image):
[content_image_raw, content_image] = load_image(CONTENT_IMAGE)
[style_image_raw, style_image] = load_image(STYLE_IMAGE)
fig = plt.figure(figsize=(10,10))
ax1 = plt.subplot(221)
ax2 = plt.subplot(222)
ax3 = plt.subplot(223)
ax4 = plt.subplot(224)
def normalize(image):
def generate_noise_image(content_image, noise_ratio):
noise_image = np.random.randn(IMAGE_HEIGHT, IMAGE_WIDTH, COLOR_CHANNELS)
#Take a weighted average of the values
gen_image = noise_image * noise_ratio + content_image * (1.0 - noise_ratio)
return gen_image
fig = plt.figure(figsize=(10,10))
ax1 = plt.subplot(221)
ax2 = plt.subplot(222)
ax3 = plt.subplot(223)
ax4 = plt.subplot(224)
gen_image = generate_noise_image(content_image, 0.0)
ax1.imshow(gen_image)
ax1.set_title('Noise ratio: 0.0')
gen_image = generate_noise_image(content_image, 0.25)
ax2.imshow(gen_image)
CONTENT_LAYER = 'conv4_2'
def content_loss_func(sess, model):
def _content_loss(current_feat, content_feat):
loss = 0.5*tf.reduce_sum(tf.square(current_feat - content_feat))
return loss
return _content_loss(sess.run(model[CONTENT_LAYER]), model[CONTENT_LAYER])