No account yet ?
Deep Dream is an algorithm that uses a convolutional neural network to create a psychedelic experience in images.
Tested in Anaconda and Python 3.7
import torch from torchvision import models, transforms import numpy as np from matplotlib import pyplot from PIL import Image, ImageFilter, ImageChops IMAGE_PATH = 'wave.jpg' CUDA_ENABLED = False # Deep dream configs LAYER_ID = 28 # The layer to maximize the activations through NUM_ITERATIONS = 5 # Number of iterations to update the input image with the layer's gradient LR = 0.2 # We downscale the image recursively, apply the deep dream computation, scale up, and then blend with the original image # to achieve better result. NUM_DOWNSCALES = 20 BLEND_ALPHA = 0.6 class DeepDream: def __init__(self, image): self.image = image self.model = models.vgg16(pretrained=True) if CUDA_ENABLED: self.model = self.model.cuda() self.modules = list(self.model.features.modules()) # vgg16 use 224x224 images imgSize = 224 self.transformMean = [0.485, 0.456, 0.406] self.transformStd = [0.229, 0.224, 0.225] self.transformNormalise = transforms.Normalize( mean=self.transformMean, std=self.transformStd ) self.transformPreprocess = transforms.Compose([ transforms.Resize((imgSize, imgSize)), transforms.ToTensor(), self.transformNormalise ]) self.tensorMean = torch.Tensor(self.transformMean) if CUDA_ENABLED: self.tensorMean = self.tensorMean.cuda() self.tensorStd = torch.Tensor(self.transformStd) if CUDA_ENABLED: self.tensorStd = self.tensorStd.cuda() def toImage(self, input): return input * self.tensorStd + self.tensorMean class DeepDream(DeepDream): def deepDream(self, image, layer, iterations, lr): transformed = self.transformPreprocess(image).unsqueeze(0) if CUDA_ENABLED: transformed = transformed.cuda() input = torch.autograd.Variable(transformed, requires_grad=True) self.model.zero_grad() for _ in range(iterations): out = input for layerId in range(layer): out = self.modules[layerId + 1](out) loss = out.norm() loss.backward() input.data = input.data + lr * input.grad.data input = input.data.squeeze() input.transpose_(0,1) input.transpose_(1,2) input = np.clip(self.toImage(input), 0, 1) return Image.fromarray(np.uint8(input*255)) class DeepDream(DeepDream): def deepDreamRecursive(self, image, layer, iterations, lr, num_downscales): if num_downscales > 0: # scale down the image image_small = image.filter(ImageFilter.GaussianBlur(2)) small_size = (int(image.size[0]/2), int(image.size[1]/2)) if (small_size[0] == 0 or small_size[1] == 0): small_size = image.size image_small = image_small.resize(small_size, Image.ANTIALIAS) # run deepDreamRecursive on the scaled down image image_small = self.deepDreamRecursive(image_small, layer, iterations, lr, num_downscales-1) # Scale up the result image to the original size image_large = image_small.resize(image.size, Image.ANTIALIAS) # Blend the two image image = ImageChops.blend(image, image_large, BLEND_ALPHA) img_result = self.deepDream(image, layer, iterations, lr) img_result = img_result.resize(image.size) return img_result def deepDreamProcess(self): return self.deepDreamRecursive(self.image, LAYER_ID, NUM_ITERATIONS, LR, NUM_DOWNSCALES) img = Image.open(IMAGE_PATH) pyplot.imshow(img) pyplot.title("Image loaded from " + IMAGE_PATH) img_deep_dream = DeepDream(img).deepDreamProcess() pyplot.imshow(img_deep_dream) pyplot.title("Deep dream image") img_deep_dream.save('deepdream_' + IMAGE_PATH)
deep-dream-in-pytorch
Copyright (c) 2018 Duc Ngo
Tested in Anaconda and Python 3.7
import torch from torch.autograd import Variable from torchvision import models from torchvision import transforms import numpy as np import matplotlib.pyplot as plt from PIL import Image, ImageFilter, ImageChops def load_image(path): image = Image.open(path) plt.imshow(image) plt.title("Original image") plt.show() return image normalise = transforms.Normalize( mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225] ) preprocess = transforms.Compose([ transforms.Resize((224,224)), transforms.ToTensor(), normalise ]) def deprocess(image): return image * torch.Tensor([0.229, 0.224, 0.225]) + torch.Tensor([0.485, 0.456, 0.406]) vgg = models.vgg16(pretrained=True) print(vgg) modulelist = list(vgg.features.modules()) def dd_helper(image, layer, iterations, lr): input = Variable(preprocess(image).unsqueeze(0), requires_grad=True) vgg.zero_grad() for i in range(iterations): out = input for j in range(layer): out = modulelist[j+1](out) loss = out.norm() loss.backward() input.data = input.data + lr * input.grad.data input = input.data.squeeze() input.transpose_(0,1) input.transpose_(1,2) input = np.clip(deprocess(input), 0, 1) im = Image.fromarray(np.uint8(input*255)) return im def deep_dream_vgg(image, layer, iterations, lr, octave_scale, num_octaves): if num_octaves > 0: image1 = image.filter(ImageFilter.GaussianBlur(2)) if(image1.size[0] / octave_scale < 1 or image1.size[1] / octave_scale < 1): size = image1.size else: size = (int(image1.size[0] / octave_scale), int(image1.size[1] / octave_scale)) image1 = image1.resize(size,Image.ANTIALIAS) image1 = deep_dream_vgg(image1, layer, iterations, lr, octave_scale, num_octaves-1) size = (image.size[0], image.size[1]) image1 = image1.resize(size,Image.ANTIALIAS) image = ImageChops.blend(image, image1, 0.6) # print("-------------- Recursive level: ", num_octaves, '--------------') img_result = dd_helper(image, layer, iterations, lr) img_result = img_result.resize(image.size) plt.axis('off') plt.imshow(img_result) return img_result img = load_image('wave-01.jpg') img_5 = deep_dream_vgg(img, 5, 5, 0.3, 2, 20) plt.show() img_5.save('Deep-dream-5.jpg') img_7 = deep_dream_vgg(img, 7, 4, 0.3, 2, 20) plt.show() img_7.save('Deep-dream-7.jpg') img_10 = deep_dream_vgg(img, 10, 3, 0.3, 2, 20) plt.show() img_10.save('Deep-dream-10.jpg') img_12 = deep_dream_vgg(img, 12, 2, 0.3, 2, 20) plt.show() img_12.save('Deep-dream-12.jpg') img_14 = deep_dream_vgg(img, 14, 3, 0.3, 2, 20) plt.show() img_14.save('Deep-dream-14.jpg') img_17 = deep_dream_vgg(img, 17, 3, 0.3, 2, 20) plt.show() img_17.save('Deep-dream-17.jpg') img_19 = deep_dream_vgg(img, 19, 3, 0.3, 2, 20) plt.show() img_19.save('Deep-dream-19.jpg') img_21 = deep_dream_vgg(img, 21, 3, 0.3, 2, 20) plt.show() img_21.save('Deep-dream-21.jpg') img_24 = deep_dream_vgg(img, 24, 5, 0.2, 2, 20) plt.show() img_24.save('Deep-dream-24.jpg') img_26 = deep_dream_vgg(img, 26, 5, 0.2, 2, 20) plt.show() img_26.save('Deep-dream-26.jpg') img_28 = deep_dream_vgg(img, 28, 5, 0.2, 2, 20) plt.show() img_28.save('Deep-dream-28.jpg')
deep-dream-in-pytorch
Copyright (c) 2018 Sarthak Gupta
Welcome, my name is Eric Soupet and I am the administrator of the site elodees.com. elodees.com is a state of the art of Artificial Intelligence and aims to be collaborative, you can now offer content such as articles, events, tutorials, ... so don't hesitate !
Platform images credit : Pixabay - Pixabay License | Pexels - Pexels License