Skip to content

Instantly share code, notes, and snippets.

@emuccino
Created September 10, 2019 18:38
Show Gist options
  • Save emuccino/05318e3b76a68dcab416a450fa5ec062 to your computer and use it in GitHub Desktop.
Save emuccino/05318e3b76a68dcab416a450fa5ec062 to your computer and use it in GitHub Desktop.
DeepDream
from keras.models import Model
from keras.layers import Dense, Input, Reshape
from keras.regularizers import Regularizer
from keras.utils.generic_utils import get_custom_objects
from keras import backend as K
from keras.applications.inception_v3 import InceptionV3
from keras.initializers import RandomUniform
from keras.preprocessing.image import array_to_img, save_img
import numpy as np
#custom regularizer that maximizes specified filter
class Reg(Regularizer):
def __init__(self, filt=0):
self.filt = filt
def __call__(self, x):
if len(x.shape) == 4:
return - K.sqrt(K.mean(K.square(x[:, :, :, self.filt])))
else:
return - K.sqrt(K.mean(K.square(x[:, self.filt])))
def get_config(self):
return {'filt': self.filt}
#custom loss function that always returns 0 loss
def customLoss(yTrue,yPred):
return K.variable(0.0)
get_custom_objects().update({'customLoss':customLoss})
#generate dream given layer name and filter index
def get_dream(conv_layer,filt):
print(conv_layer,filt)
K.clear_session()
#load pretrained inception model
model = InceptionV3(include_top=True, weights='imagenet', input_tensor=None,
input_shape=None, pooling=None, classes=1000)
#get layer index
print([layer.name for layer in model.layers])
layer = [i for i,name in enumerate([layer.name for layer in model.layers]) if name == conv_layer][0]
#apply custom regularizaation to layer
model.layers[layer].activity_regularizer = Reg(filt=filt)
model.compile(optimizer='nadam', loss='customLoss')
#unit input for adversarial noise
unity = Input(shape=(1,),name='unity')
#layer for learning adversarial noise to apply to image
noise = Dense(299*299*3,activation = 'tanh',use_bias=True,
kernel_initializer=RandomUniform(minval=-0.01,maxval=0.01, seed=None),
name='adversarial_noise')(unity)
#reshape noise in shape of image
noise = Reshape((299,299,3),name='reshape')(noise)
#compile model for producing dreams
output_model = Model(inputs=unity, outputs=noise)
output_model.compile(optimizer='nadam', loss='customLoss')
#feed dream to trained MNIST classifier
outputs = model(noise)
#compile model for training dreams
dream_model = Model(inputs=unity, outputs=outputs)
dream_model.layers[-1].trainable = False
dream_model.compile(optimizer='nadam', loss='customLoss')
#fit image
dream_model.fit(x=np.ones(shape=(1,1)),y=np.array([[1]+[0 for _ in range(999)]]),epochs=50000,verbose=0)
#produce image
dream = output_model.predict(np.ones(shape=(1,1))).reshape((299,299,3))
#save image
save_img('./dream_'+conv_layer+'_'+str(filt)+'_'+'_original.jpeg',array_to_img((dream+1)/2),scale=False)
#generate dream for first filter in the firt convolutional layer
dream = get_dream('activation_1',0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment