<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/ghosts-of-animals-haunt-portlands-overpriced-apartments/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://dgarmat.github.io/Deep-Dream-On-Overpriced-Apartments/"> Dan Garmat's Blog -- R</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Apartment hunting in an expensive city is leading me to curses and exclamations. Following are some outstanding examples of insanely priced apartments in Portland, OR, run through Google Deep Dream in hopes of my understanding why people pay so much for a small box. These listings will be gone in no time (I’m sure) so including some captions for posterity.</p>
<p>Let’s start with this one. Indeed, it appears $1899 for 1 bedroom grants access to this clubhouse haunted by some floating apparition:</p>
<p><img alt="clubhousedd" title="clubhousedd" src="https://i2.wp.com/dgarmat.github.io/images/deep%20dream%20apartments/dream/00m0m_dKJEQpvJY87_1200x900_dream4.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p><span></span></p>
<p>Deep Dream InceptionV3 algorithm here is trained on ImageNet, then makes changes that increase confidence in the predicted category. Looped several times with the num_octave hyperparameter, it starts to look a good bit trippy and helps give some intuition what a neural network “sees” as prototypical examples of a predicted class. Apparently there is no “view of apartment” class as it keeps seeing ghastly animals. Perhaps it is no coincidence even before running InceptionV3 this clubhouse already looks like it could work in The Shining:</p>
<p><img alt="clubhouseorig" title="clubhouseorig" src="https://i0.wp.com/dgarmat.github.io/images/deep%20dream%20apartments/orig/00m0m_dKJEQpvJY87_1200x900.jpg?w=456&ssl=1" data-recalc-dims="1"></p>
<h3><i>$1850 / 1br – 697ft2 – BRAND NEW! Enjoy Luxury Uban [Urban?] Living at The Franklin Flats!</i></h3>
<p><i>“NEW ON THE MARKET!</i></p>
<p><i>“The Franklin Flats is the newest addition to this desirable part of town! Built with the urban adventurer in mind, our small community offers luxury appeal with a neighborhood feel. Boasting a walkability score of 86 out of 100, you can’t beat the location! [unless an 87+?] Our close proximity to Mt. Tabor, food carts, [because you won’t have anything left over for restaurants] shopping and eateries gives you the classic Northwest experience you crave. Email or call to schedule a personal tour today!”</i></p>
<p><img alt="franklin01" title="franklin01" src="https://i2.wp.com/dgarmat.github.io/images/deep%20dream%20apartments/dream/00909_3h4KHJeucOb_1200x900_dream1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>Apparently the Attack on Titan seals make this a desirable part of town.</p>
<p>Perhaps those seals are why walkability doesn’t crack 90. If you survive the seals on a midday stroll, there are titan polar bears who can walk through walls:</p>
<p><img alt="franklin02" title="franklin02" src="https://i0.wp.com/dgarmat.github.io/images/deep%20dream%20apartments/dream/00u0u_kPRRhgh84yU_1200x900_dream4.png?w=456&ssl=1" data-recalc-dims="1"></p>
<h3><i>$4250 / 2br – 1900ft2 – Condo on the Willamette</i></h3>
<p><i>“Breathtaking views of the city and the Willamette River, located in the elegant Atwater. This condo has two bedrooms, living room, dining room, gourmet kitchen, gas fireplace, small office, two balconies, utility room and underground parking. Includes concierge desk, card-accessed security.”</i></p>
<p>Something tells me this view of the Willamette River would be complete if a cocker spaniel is staring at me…</p>
<p><img alt="atwater01" title="atwater01" src="https://i2.wp.com/dgarmat.github.io/images/deep%20dream%20apartments/dream/00A0A_5YGDBoyOPgr_1200x900_dream2.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>But this view is what you really pay for: look at all the suckers in the two identical buildings who massively overpaid for – how the heck did those get up here?!</p>
<p><img alt="atwater02" title="atwater01" src="https://i0.wp.com/dgarmat.github.io/images/deep%20dream%20apartments/dream/00101_1x6lw7IBiyc_1200x900_dream1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<h3><i>$3900 / 2br – 1004ft2 – Portland’s most well-appointed two bedroom apartments now available</i></h3>
<p><i>“Portland’s premier rental community is now pre-leasing. Find everything you desire in one place: The finest dining, distinguished boutiques, and most-beloved haunts. Experience the perfect merger of luxury and livability with our timeless brick exterior, stunning marble lobby, tiled bathrooms, tall ceilings, ample light, extensive storage, concierge services, and even a dog washing station.</i></p>
<p><i>“Proudly situated in Portland’s [S]Nob Hill/23rd Ave neighborhood, 21 Astor boasts a “97” walk score and a “96” bike score. [Beat that Franklin Flats!] Life is great in the epicenter of Portland’s charm. Pick up your locally-grown produce at City Market. Grab happy hour with the gang at Seratto. Sweat it out at Corepower Yoga.</i></p>
<p><i>“Our greater than 1:1 parking ratio assures a space for your car in our garage. Imagine never having to find parking on NW 23rd again.”</i></p>
<p>I work on NW 21st, that’s almost the cost of parking alone. People walk by on Oregon ‘pot tours’ and this may be how they see the building as well:</p>
<p><img alt="well-appointed01" title="well-appointed01" src="https://i2.wp.com/dgarmat.github.io/images/deep%20dream%20apartments/dream/01313_bM7Omxe4ymh_1200x900_dream1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>Whoa! Is that a pig in the sky? Far out!</p>
<p>At least their new $3.88/sqft/month kitchens aren’t yet haunted – oh, god! Are those giant psychedelic worms!</p>
<p><img alt="well-appointed02" title="well-appointed01" src="https://i2.wp.com/dgarmat.github.io/images/deep%20dream%20apartments/dream/01717_8141Kt1477r_1200x900_dream0.png?w=456&ssl=1" data-recalc-dims="1"></p>
<h2>Code:</h2>
<p>Started with JJ Allaire, François Chollet, RStudio, and Google’s <a rel="nofollow" href="https://keras.rstudio.com/articles/examples/deep_dream.html" target="_blank">keras code</a> based on <a rel="nofollow" href="https://en.wikipedia.org/wiki/DeepDream" target="_blank">Google DeepDream</a>. Added a little looping to try different parameters over these 7 images:</p>
<div>
<div>
<pre><code><span>library</span><span>(</span><span>keras</span><span>)</span><span>
</span><span>library</span><span>(</span><span>tensorflow</span><span>)</span><span>
</span><span>library</span><span>(</span><span>purrr</span><span>)</span><span>
</span><span># Function Definitions ----------------------------------------------------</span><span>
</span><span>preprocess_image</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>image_path</span><span>){</span><span>
</span><span>image_load</span><span>(</span><span>image_path</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>image_to_array</span><span>()</span><span> </span><span>%>%</span><span>
</span><span>array_reshape</span><span>(</span><span>dim</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>1</span><span>,</span><span> </span><span>dim</span><span>(</span><span>.</span><span>)))</span><span> </span><span>%>%</span><span>
</span><span>inception_v3_preprocess_input</span><span>()</span><span>
</span><span>}</span><span>
</span><span>deprocess_image</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>x</span><span>){</span><span>
</span><span>x</span><span> </span><span>&lt;-</span><span> </span><span>x</span><span>[</span><span>1</span><span>,,,]</span><span>
</span><span># Remove zero-center by mean pixel</span><span>
</span><span>x</span><span> </span><span>&lt;-</span><span> </span><span>x</span><span>/</span><span>2</span><span>.</span><span>
</span><span>x</span><span> </span><span>&lt;-</span><span> </span><span>x</span><span> </span><span>+</span><span> </span><span>0.5</span><span>
</span><span>x</span><span> </span><span>&lt;-</span><span> </span><span>x</span><span> </span><span>*</span><span> </span><span>255</span><span>
</span><span># 'BGR'->'RGB'</span><span>
</span><span>x</span><span> </span><span>&lt;-</span><span> </span><span>x</span><span>[,,</span><span>c</span><span>(</span><span>3</span><span>,</span><span>2</span><span>,</span><span>1</span><span>)]</span><span>
</span><span># Clip to interval 0, 255</span><span>
</span><span>x</span><span>[</span><span>x</span><span> </span><span>></span><span> </span><span>255</span><span>]</span><span> </span><span>&lt;-</span><span> </span><span>255</span><span>
</span><span>x</span><span>[</span><span>x</span><span> </span><span></span><span> </span><span>0</span><span>]</span><span> </span><span>&lt;-</span><span> </span><span>0</span><span>
</span><span>x</span><span>[]</span><span> </span><span>&lt;-</span><span> </span><span>as.integer</span><span>(</span><span>x</span><span>)</span><span>/</span><span>255</span><span>
</span><span>x</span><span>
</span><span>}</span><span>
</span><span># Parameters --------------------------------------------------------</span><span>
</span><span>## list of images to process --</span><span>
</span><span>list_images</span><span> </span><span>&lt;-</span><span> </span><span>list.files</span><span>(</span><span>'images/deep dream apartments/orig/'</span><span>,</span><span> </span><span>full.names</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span>
</span><span>## list of settings to try --</span><span>
</span><span>list_settings</span><span> </span><span>&lt;-</span><span> </span><span>list</span><span>(</span><span>
</span><span>settings</span><span> </span><span>=</span><span> </span><span>list</span><span>(</span><span>
</span><span>features</span><span> </span><span>=</span><span> </span><span>list</span><span>(</span><span>
</span><span>mixed2</span><span> </span><span>=</span><span> </span><span>0.2</span><span>,</span><span>
</span><span>mixed3</span><span> </span><span>=</span><span> </span><span>0.5</span><span>,</span><span>
</span><span>mixed4</span><span> </span><span>=</span><span> </span><span>2</span><span>.</span><span>,</span><span>
</span><span>mixed5</span><span> </span><span>=</span><span> </span><span>1.5</span><span>),</span><span>
</span><span>hyperparams</span><span> </span><span>=</span><span> </span><span>list</span><span>(</span><span>
</span><span># Playing with these hyperparameters will also allow you to achieve new effects</span><span>
</span><span>step</span><span> </span><span>=</span><span> </span><span>0.01</span><span>,</span><span> </span><span># Gradient ascent step size</span><span>
</span><span>num_octave</span><span> </span><span>=</span><span> </span><span>6</span><span>,</span><span> </span><span># Number of scales at which to run gradient ascent</span><span>
</span><span>octave_scale</span><span> </span><span>=</span><span> </span><span>1.4</span><span>,</span><span> </span><span># Size ratio between scales</span><span>
</span><span>iterations</span><span> </span><span>=</span><span> </span><span>20</span><span>,</span><span> </span><span># Number of ascent steps per scale</span><span>
</span><span>max_loss</span><span> </span><span>=</span><span> </span><span>10</span><span>
</span><span>)</span><span>
</span><span>),</span><span>
</span><span>settings</span><span> </span><span>=</span><span> </span><span>list</span><span>(</span><span>
</span><span>features</span><span> </span><span>=</span><span> </span><span>list</span><span>(</span><span>
</span><span>mixed2</span><span> </span><span>=</span><span> </span><span>0.5</span><span>,</span><span>
</span><span>mixed3</span><span> </span><span>=</span><span> </span><span>0.2</span><span>,</span><span>
</span><span>mixed4</span><span> </span><span>=</span><span> </span><span>1.1</span><span>,</span><span>
</span><span>mixed5</span><span> </span><span>=</span><span> </span><span>1.5</span><span>),</span><span>
</span><span>hyperparams</span><span> </span><span>=</span><span> </span><span>list</span><span>(</span><span>
</span><span>step</span><span> </span><span>=</span><span> </span><span>0.01</span><span>,</span><span> </span><span>num_octave</span><span> </span><span>=</span><span> </span><span>9</span><span>,</span><span> </span><span>octave_scale</span><span> </span><span>=</span><span> </span><span>1.1</span><span>,</span><span> </span><span>iterations</span><span> </span><span>=</span><span> </span><span>20</span><span>,</span><span> </span><span>max_loss</span><span> </span><span>=</span><span> </span><span>5</span><span>
</span><span>)</span><span>
</span><span>),</span><span>
</span><span>settings</span><span> </span><span>=</span><span> </span><span>list</span><span>(</span><span>
</span><span>features</span><span> </span><span>=</span><span> </span><span>list</span><span>(</span><span>
</span><span>mixed2</span><span> </span><span>=</span><span> </span><span>0.02</span><span>,</span><span>
</span><span>mixed3</span><span> </span><span>=</span><span> </span><span>0.05</span><span>,</span><span>
</span><span>mixed4</span><span> </span><span>=</span><span> </span><span>0.01</span><span>,</span><span>
</span><span>mixed5</span><span> </span><span>=</span><span> </span><span>0.05</span><span>),</span><span>
</span><span>hyperparams</span><span> </span><span>=</span><span> </span><span>list</span><span>(</span><span>
</span><span>step</span><span> </span><span>=</span><span> </span><span>0.01</span><span>,</span><span> </span><span>num_octave</span><span> </span><span>=</span><span> </span><span>11</span><span>,</span><span> </span><span>octave_scale</span><span> </span><span>=</span><span> </span><span>1.1</span><span>,</span><span> </span><span>iterations</span><span> </span><span>=</span><span> </span><span>20</span><span>,</span><span> </span><span>max_loss</span><span> </span><span>=</span><span> </span><span>20</span><span>
</span><span>)</span><span>
</span><span>),</span><span>
</span><span>settings</span><span> </span><span>=</span><span> </span><span>list</span><span>(</span><span>
</span><span>features</span><span> </span><span>=</span><span> </span><span>list</span><span>(</span><span>
</span><span>mixed2</span><span> </span><span>=</span><span> </span><span>0.2</span><span>,</span><span>
</span><span>mixed3</span><span> </span><span>=</span><span> </span><span>0.5</span><span>,</span><span>
</span><span>mixed4</span><span> </span><span>=</span><span> </span><span>2</span><span>.</span><span>,</span><span>
</span><span>mixed5</span><span> </span><span>=</span><span> </span><span>1.5</span><span>),</span><span>
</span><span>hyperparams</span><span> </span><span>=</span><span> </span><span>list</span><span>(</span><span>
</span><span>step</span><span> </span><span>=</span><span> </span><span>0.01</span><span>,</span><span> </span><span>num_octave</span><span> </span><span>=</span><span> </span><span>8</span><span>,</span><span> </span><span>octave_scale</span><span> </span><span>=</span><span> </span><span>1.4</span><span>,</span><span> </span><span>iterations</span><span> </span><span>=</span><span> </span><span>20</span><span>,</span><span> </span><span>max_loss</span><span> </span><span>=</span><span> </span><span>10</span><span>
</span><span>)</span><span>
</span><span>),</span><span>
</span><span>settings</span><span> </span><span>=</span><span> </span><span>list</span><span>(</span><span>
</span><span>features</span><span> </span><span>=</span><span> </span><span>list</span><span>(</span><span>
</span><span>mixed2</span><span> </span><span>=</span><span> </span><span>0.2</span><span>,</span><span>
</span><span>mixed3</span><span> </span><span>=</span><span> </span><span>0.1</span><span>,</span><span>
</span><span>mixed4</span><span> </span><span>=</span><span> </span><span>0.4</span><span>,</span><span>
</span><span>mixed5</span><span> </span><span>=</span><span> </span><span>0.3</span><span>),</span><span>
</span><span>hyperparams</span><span> </span><span>=</span><span> </span><span>list</span><span>(</span><span>
</span><span>step</span><span> </span><span>=</span><span> </span><span>0.01</span><span>,</span><span> </span><span>num_octave</span><span> </span><span>=</span><span> </span><span>8</span><span>,</span><span> </span><span>octave_scale</span><span> </span><span>=</span><span> </span><span>1.4</span><span>,</span><span> </span><span>iterations</span><span> </span><span>=</span><span> </span><span>20</span><span>,</span><span> </span><span>max_loss</span><span> </span><span>=</span><span> </span><span>10</span><span>
</span><span>)</span><span>
</span><span>),</span><span>
</span><span>settings</span><span> </span><span>=</span><span> </span><span>list</span><span>(</span><span>
</span><span>features</span><span> </span><span>=</span><span> </span><span>list</span><span>(</span><span>
</span><span>mixed2</span><span> </span><span>=</span><span> </span><span>1.2</span><span>,</span><span>
</span><span>mixed3</span><span> </span><span>=</span><span> </span><span>1.5</span><span>,</span><span>
</span><span>mixed4</span><span> </span><span>=</span><span> </span><span>3</span><span>.</span><span>,</span><span>
</span><span>mixed5</span><span> </span><span>=</span><span> </span><span>2.5</span><span>),</span><span>
</span><span>hyperparams</span><span> </span><span>=</span><span> </span><span>list</span><span>(</span><span>
</span><span>step</span><span> </span><span>=</span><span> </span><span>0.01</span><span>,</span><span> </span><span>num_octave</span><span> </span><span>=</span><span> </span><span>8</span><span>,</span><span> </span><span>octave_scale</span><span> </span><span>=</span><span> </span><span>1.4</span><span>,</span><span> </span><span>iterations</span><span> </span><span>=</span><span> </span><span>20</span><span>,</span><span> </span><span>max_loss</span><span> </span><span>=</span><span> </span><span>25</span><span>
</span><span>)</span><span>
</span><span>),</span><span>
</span><span>settings</span><span> </span><span>=</span><span> </span><span>list</span><span>(</span><span>
</span><span>features</span><span> </span><span>=</span><span> </span><span>list</span><span>(</span><span>
</span><span>mixed2</span><span> </span><span>=</span><span> </span><span>0.2</span><span>,</span><span>
</span><span>mixed3</span><span> </span><span>=</span><span> </span><span>2.5</span><span>,</span><span>
</span><span>mixed4</span><span> </span><span>=</span><span> </span><span>2</span><span>.</span><span>,</span><span>
</span><span>mixed5</span><span> </span><span>=</span><span> </span><span>3.5</span><span>),</span><span>
</span><span>hyperparams</span><span> </span><span>=</span><span> </span><span>list</span><span>(</span><span>
</span><span>step</span><span> </span><span>=</span><span> </span><span>0.05</span><span>,</span><span> </span><span>num_octave</span><span> </span><span>=</span><span> </span><span>6</span><span>,</span><span> </span><span>octave_scale</span><span> </span><span>=</span><span> </span><span>1.4</span><span>,</span><span> </span><span>iterations</span><span> </span><span>=</span><span> </span><span>20</span><span>,</span><span> </span><span>max_loss</span><span> </span><span>=</span><span> </span><span>13</span><span>
</span><span>)</span><span>
</span><span>),</span><span>
</span><span>settings</span><span> </span><span>=</span><span> </span><span>list</span><span>(</span><span>
</span><span>features</span><span> </span><span>=</span><span> </span><span>list</span><span>(</span><span>
</span><span>mixed2</span><span> </span><span>=</span><span> </span><span>0.2</span><span>,</span><span>
</span><span>mixed3</span><span> </span><span>=</span><span> </span><span>2.5</span><span>,</span><span>
</span><span>mixed4</span><span> </span><span>=</span><span> </span><span>2</span><span>.</span><span>,</span><span>
</span><span>mixed5</span><span> </span><span>=</span><span> </span><span>3.5</span><span>),</span><span>
</span><span>hyperparams</span><span> </span><span>=</span><span> </span><span>list</span><span>(</span><span>
</span><span>step</span><span> </span><span>=</span><span> </span><span>0.05</span><span>,</span><span> </span><span>num_octave</span><span> </span><span>=</span><span> </span><span>8</span><span>,</span><span> </span><span>octave_scale</span><span> </span><span>=</span><span> </span><span>1.4</span><span>,</span><span> </span><span>iterations</span><span> </span><span>=</span><span> </span><span>20</span><span>,</span><span> </span><span>max_loss</span><span> </span><span>=</span><span> </span><span>13</span><span>
</span><span>)</span><span>
</span><span>)</span><span>
</span><span>)</span><span>
</span><span># Model Definition --------------------------------------------------------</span><span>
</span><span>k_set_learning_phase</span><span>(</span><span>0</span><span>)</span><span>
</span><span># Build the InceptionV3 network with our placeholder.</span><span>
</span><span># The model will be loaded with pre-trained ImageNet weights.</span><span>
</span><span>model</span><span> </span><span>&lt;-</span><span> </span><span>application_inception_v3</span><span>(</span><span>weights</span><span> </span><span>=</span><span> </span><span>&quot;imagenet&quot;</span><span>,</span><span> </span><span>include_top</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>)</span><span>
</span><span># This will contain our generated image</span><span>
</span><span>dream</span><span> </span><span>&lt;-</span><span> </span><span>model</span><span>$</span><span>input</span><span>
</span><span># Get the symbolic outputs of each &quot;key&quot; layer (we gave them unique names).</span><span>
</span><span>layer_dict</span><span> </span><span>&lt;-</span><span> </span><span>model</span><span>$</span><span>layers</span><span>
</span><span>names</span><span>(</span><span>layer_dict</span><span>)</span><span> </span><span>&lt;-</span><span> </span><span>map_chr</span><span>(</span><span>layer_dict</span><span> </span><span>,</span><span>~</span><span>.x</span><span>$</span><span>name</span><span>)</span><span>
</span><span>## just loop on images - bottleneck is training anyway --</span><span>
</span><span>for</span><span>(</span><span>image_path</span><span> </span><span>in</span><span> </span><span>list_images</span><span>){</span><span>
</span><span>image</span><span> </span><span>&lt;-</span><span> </span><span>preprocess_image</span><span>(</span><span>image_path</span><span>)</span><span>
</span><span>#res &lt;-predict(model,image)</span><span>
</span><span>#reduce(dim(res)[-1], .f = `*`) %>% </span><span>
</span><span># as.numeric() %>% </span><span>
</span><span>#array_reshape(res, c(2, 51200)) %>% </span><span>
</span><span># imagenet_decode_predictions()</span><span>
</span><span>## get model prediction:</span><span>
</span><span>#x &lt;-image_to_array(image)</span><span>
</span><span>#x &lt;-array_reshape(image, c(1, dim(image)))</span><span>
</span><span>#x &lt;-imagenet_preprocess_input(image)</span><span>
</span><span>#preds = predict(model, x)</span><span>
</span><span>#imagenet_decode_predictions(preds, top = 3)</span><span>
</span><span>#predict_classes(model, image)</span><span>
</span><span># need to try again later</span><span>
</span><span>setting_counter</span><span> </span><span>&lt;-</span><span> </span><span>0</span><span>
</span><span>## then nested loop on settings --</span><span>
</span><span>for</span><span>(</span><span>settings</span><span> </span><span>in</span><span> </span><span>list_settings</span><span>){</span><span>
</span><span>setting_counter</span><span> </span><span>&lt;-</span><span> </span><span>setting_counter</span><span> </span><span>+</span><span> </span><span>1</span><span>
</span><span>print</span><span>(</span><span>paste0</span><span>(</span><span>'starting '</span><span>,</span><span> </span><span>image_path</span><span>,</span><span> </span><span>', setting # '</span><span>,</span><span> </span><span>setting_counter</span><span>))</span><span>
</span><span># reload image each time</span><span>
</span><span>image</span><span> </span><span>&lt;-</span><span> </span><span>preprocess_image</span><span>(</span><span>image_path</span><span>)</span><span>
</span><span># Define the loss</span><span>
</span><span>loss</span><span> </span><span>&lt;-</span><span> </span><span>k_variable</span><span>(</span><span>0.0</span><span>)</span><span>
</span><span>for</span><span>(</span><span>layer_name</span><span> </span><span>in</span><span> </span><span>names</span><span>(</span><span>settings</span><span>$</span><span>features</span><span>)){</span><span>
</span><span># Add the L2 norm of the features of a layer to the loss</span><span>
</span><span>coeff</span><span> </span><span>&lt;-</span><span> </span><span>settings</span><span>$</span><span>features</span><span>[[</span><span>layer_name</span><span>]]</span><span>
</span><span>x</span><span> </span><span>&lt;-</span><span> </span><span>layer_dict</span><span>[[</span><span>layer_name</span><span>]]</span><span>$</span><span>output</span><span>
</span><span>scaling</span><span> </span><span>&lt;-</span><span> </span><span>k_prod</span><span>(</span><span>k_cast</span><span>(</span><span>k_shape</span><span>(</span><span>x</span><span>),</span><span> </span><span>'float32'</span><span>))</span><span>
</span><span># Avoid border artifacts by only involving non-border pixels in the loss</span><span>
</span><span>loss</span><span> </span><span>&lt;-</span><span> </span><span>loss</span><span> </span><span>+</span><span> </span><span>coeff</span><span>*</span><span>k_sum</span><span>(</span><span>k_square</span><span>(</span><span>x</span><span>))</span><span> </span><span>/</span><span> </span><span>scaling</span><span>
</span><span>}</span><span>
</span><span># Compute the gradients of the dream wrt the loss</span><span>
</span><span>grads</span><span> </span><span>&lt;-</span><span> </span><span>k_gradients</span><span>(</span><span>loss</span><span>,</span><span> </span><span>dream</span><span>)[[</span><span>1</span><span>]]</span><span> </span><span># Normalize gradients.</span><span>
</span><span>grads</span><span> </span><span>&lt;-</span><span> </span><span>grads</span><span> </span><span>/</span><span> </span><span>k_maximum</span><span>(</span><span>k_mean</span><span>(</span><span>k_abs</span><span>(</span><span>grads</span><span>)),</span><span> </span><span>k_epsilon</span><span>())</span><span>
</span><span># Set up function to retrieve the value</span><span>
</span><span># of the loss and gradients given an input image.</span><span>
</span><span>fetch_loss_and_grads</span><span> </span><span>&lt;-</span><span> </span><span>k_function</span><span>(</span><span>list</span><span>(</span><span>dream</span><span>),</span><span> </span><span>list</span><span>(</span><span>loss</span><span>,</span><span>grads</span><span>))</span><span>
</span><span># this function will crash the R session if too many octaves on too small an image</span><span>
</span><span>eval_loss_and_grads</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>image</span><span>){</span><span>
</span><span>outs</span><span> </span><span>&lt;-</span><span> </span><span>fetch_loss_and_grads</span><span>(</span><span>list</span><span>(</span><span>image</span><span>))</span><span>
</span><span>list</span><span>(</span><span>
</span><span>loss_value</span><span> </span><span>=</span><span> </span><span>outs</span><span>[[</span><span>1</span><span>]],</span><span>
</span><span>grad_values</span><span> </span><span>=</span><span> </span><span>outs</span><span>[[</span><span>2</span><span>]]</span><span>
</span><span>)</span><span>
</span><span>}</span><span>
</span><span>gradient_ascent</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>x</span><span>,</span><span> </span><span>iterations</span><span>,</span><span> </span><span>step</span><span>,</span><span> </span><span>max_loss</span><span> </span><span>=</span><span> </span><span>NULL</span><span>)</span><span> </span><span>{</span><span>
</span><span>for</span><span> </span><span>(</span><span>i</span><span> </span><span>in</span><span> </span><span>1</span><span>:</span><span>iterations</span><span>)</span><span> </span><span>{</span><span>
</span><span>out</span><span> </span><span>&lt;-</span><span> </span><span>tryCatch</span><span>(</span><span>eval_loss_and_grads</span><span>(</span><span>x</span><span>),</span><span> </span><span>error</span><span> </span><span>=</span><span> </span><span>function</span><span>(</span><span>e</span><span>)</span><span> </span><span>NA</span><span>)</span><span> </span><span># need to add this for negative gradients</span><span>
</span><span>if</span><span>(</span><span>is.na</span><span>(</span><span>out</span><span>$</span><span>loss_value</span><span>)){</span><span>
</span><span>print</span><span>(</span><span>paste0</span><span>(</span><span>'NA loss_value on setting # '</span><span>,</span><span> </span><span>setting_counter</span><span>))</span><span>
</span><span>break</span><span>
</span><span>}</span><span> </span><span>else</span><span> </span><span>if</span><span> </span><span>(</span><span>!</span><span>is.null</span><span>(</span><span>max_loss</span><span>)</span><span> </span><span>&amp;</span><span> </span><span>out</span><span>$</span><span>loss_value</span><span> </span><span>></span><span> </span><span>max_loss</span><span>)</span><span> </span><span>{</span><span>
</span><span>break</span><span>
</span><span>}</span><span> </span><span>print</span><span>(</span><span>paste</span><span>(</span><span>&quot;Loss value at&quot;</span><span>,</span><span> </span><span>i</span><span>,</span><span> </span><span>':'</span><span>,</span><span> </span><span>out</span><span>$</span><span>loss_value</span><span>))</span><span>
</span><span>x</span><span> </span><span>&lt;-</span><span> </span><span>x</span><span> </span><span>+</span><span> </span><span>step</span><span> </span><span>*</span><span> </span><span>out</span><span>$</span><span>grad_values</span><span>
</span><span>}</span><span>
</span><span>x</span><span>
</span><span>}</span><span>
</span><span>original_shape</span><span> </span><span>&lt;-</span><span> </span><span>dim</span><span>(</span><span>image</span><span>)[</span><span>-</span><span>c</span><span>(</span><span>1</span><span>,</span><span> </span><span>4</span><span>)]</span><span>
</span><span>successive_shapes</span><span> </span><span>&lt;-</span><span> </span><span>list</span><span>(</span><span>original_shape</span><span>)</span><span>
</span><span>for</span><span> </span><span>(</span><span>i</span><span> </span><span>in</span><span> </span><span>1</span><span>:</span><span>settings</span><span>$</span><span>hyperparams</span><span>$</span><span>num_octave</span><span>)</span><span> </span><span>{</span><span>
</span><span>successive_shapes</span><span>[[</span><span>i</span><span>+1</span><span>]]</span><span> </span><span>&lt;-</span><span> </span><span>as.integer</span><span>(</span><span>original_shape</span><span>/</span><span>settings</span><span>$</span><span>hyperparams</span><span>$</span><span>octave_scale</span><span>^</span><span>i</span><span>)</span><span>
</span><span>}</span><span>
</span><span>successive_shapes</span><span> </span><span>&lt;-</span><span> </span><span>rev</span><span>(</span><span>successive_shapes</span><span>)</span><span>
</span><span>original_image</span><span> </span><span>&lt;-</span><span> </span><span>image</span><span>
</span><span>shrunk_original_img</span><span> </span><span>&lt;-</span><span> </span><span>image_array_resize</span><span>(</span><span>
</span><span>image</span><span>,</span><span> </span><span>successive_shapes</span><span>[[</span><span>1</span><span>]][</span><span>1</span><span>],</span><span> </span><span>successive_shapes</span><span>[[</span><span>1</span><span>]][</span><span>2</span><span>]</span><span>
</span><span>)</span><span>
</span><span>shpnum</span><span> </span><span>&lt;-</span><span> </span><span>0</span><span> </span><span># for debugging</span><span>
</span><span>for</span><span> </span><span>(</span><span>shp</span><span> </span><span>in</span><span> </span><span>successive_shapes</span><span>)</span><span> </span><span>{</span><span>
</span><span>shpnum</span><span> </span><span>&lt;-</span><span> </span><span>shpnum</span><span> </span><span>+</span><span> </span><span>1</span><span> </span><span># for debugging</span><span>
</span><span>image</span><span> </span><span>&lt;-</span><span> </span><span>image_array_resize</span><span>(</span><span>image</span><span>,</span><span> </span><span>shp</span><span>[</span><span>1</span><span>],</span><span> </span><span>shp</span><span>[</span><span>2</span><span>])</span><span>
</span><span>print</span><span>(</span><span>paste0</span><span>(</span><span>'running octave '</span><span>,</span><span> </span><span>shpnum</span><span>))</span><span># for debugging</span><span>
</span><span>image</span><span> </span><span>&lt;-</span><span> </span><span>gradient_ascent</span><span>(</span><span>image</span><span>,</span><span> </span><span>settings</span><span>$</span><span>hyperparams</span><span>$</span><span>iterations</span><span>,</span><span> </span><span>settings</span><span>$</span><span>hyperparams</span><span>$</span><span>`step`</span><span>,</span><span> </span><span>settings</span><span>$</span><span>hyperparams</span><span>$</span><span>max_loss</span><span>)</span><span>
</span><span>print</span><span>(</span><span>paste0</span><span>(</span><span>'finished octave '</span><span>,</span><span> </span><span>shpnum</span><span>))</span><span># for debugging</span><span>
</span><span>upscaled_shrunk_original_img</span><span> </span><span>&lt;-</span><span> </span><span>image_array_resize</span><span>(</span><span>shrunk_original_img</span><span>,</span><span> </span><span>shp</span><span>[</span><span>1</span><span>],</span><span> </span><span>shp</span><span>[</span><span>2</span><span>])</span><span>
</span><span>same_size_original</span><span> </span><span>&lt;-</span><span> </span><span>image_array_resize</span><span>(</span><span>original_image</span><span>,</span><span> </span><span>shp</span><span>[</span><span>1</span><span>],</span><span> </span><span>shp</span><span>[</span><span>2</span><span>])</span><span>
</span><span>lost_detail</span><span> </span><span>&lt;-</span><span> </span><span>same_size_original</span><span> </span><span>-</span><span> </span><span>upscaled_shrunk_original_img</span><span>
</span><span>image</span><span> </span><span>&lt;-</span><span> </span><span>image</span><span> </span><span>+</span><span> </span><span>lost_detail</span><span>
</span><span>shrunk_original_img</span><span> </span><span>&lt;-</span><span> </span><span>image_array_resize</span><span>(</span><span>original_image</span><span>,</span><span> </span><span>shp</span><span>[</span><span>1</span><span>],</span><span> </span><span>shp</span><span>[</span><span>2</span><span>])</span><span>
</span><span>}</span><span>
</span><span>image_path</span><span> </span><span>%>%</span><span> </span><span>gsub</span><span>(</span><span>'/orig/'</span><span>,</span><span> </span><span>'/dream/'</span><span>,</span><span> </span><span>.</span><span>)</span><span> </span><span>%>%</span><span> </span><span>gsub</span><span>(</span><span>'.jpg'</span><span>,</span><span> </span><span>paste0</span><span>(</span><span>'_dream'</span><span>,</span><span> </span><span>setting_counter</span><span>,</span><span> </span><span>'.png'</span><span>),</span><span> </span><span>.</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>png</span><span>(</span><span>filename</span><span> </span><span>=</span><span> </span><span>.</span><span>)</span><span>
</span><span>plot</span><span>(</span><span>as.raster</span><span>(</span><span>deprocess_image</span><span>(</span><span>image</span><span>)))</span><span>
</span><span>dev.off</span><span>()</span><span>
</span><span>print</span><span>(</span><span>paste0</span><span>(</span><span>'finished '</span><span>,</span><span> </span><span>image_path</span><span>,</span><span> </span><span>', setting # '</span><span>,</span><span> </span><span>setting_counter</span><span>))</span><span>
</span><span>}</span><span>
</span><span>}</span><span>
</span></code></pre>
</div>
</div>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://dgarmat.github.io/Deep-Dream-On-Overpriced-Apartments/"> Dan Garmat's Blog -- R</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/DpbxdWMVKMA" height="1"></a></div>