DeepDream,是Google在2015年推出的一種基於卷積神經網絡的視覺模型算法。其主要功能是通過構建出一個訓練好的神經網絡模型,將輸入圖像轉化為對人腦具有高智能度認識的「夢境」圖像。這種技術的誕生,給藝術創作帶來了極大的啟示,也開創了人工智能的無限探索。本文將從deepdream軟件、熱氣球deepdream、deepdream什麼意思、deep dream generator、dream網站ai生成畫等多個方面對這項技術進行詳細闡述。
一、DeepDream軟件
在談論DeepDream時,不得不提到這款由Google開源的DeepDream軟件。它可以讓使用者上傳自己的圖片,然後通過該算法分析圖片,將其轉化為「夢境」圖像的形式。用戶只需輕鬆地選擇」magnify layer」 或者 「level of zoom」 ,就能夠讓計算機開始工作,將改變後的圖片輸出為一種意想不到的藝術創作。
import numpy as np
import PIL.Image
import tensorflow as tf
import urllib.request
model_url = 'https://storage.googleapis.com/download.tensorflow.org/models/inception5h.zip'
model_fn = 'inception5h.zip'
layers = ['model/pool_3:0']
# 預載入模型,設置層數
download_model = urllib.request.urlretrieve(model_url, model_fn)
zip_ref = zipfile.ZipFile(model_fn, 'r')
zip_ref.extractall()
zip_ref.close()
graph = tf.Graph()
sess = tf.InteractiveSession(graph=graph)
# 載入整個圖像模型進緩存
with tf.gfile.FastGFile('tensorflow_inception_graph.pb', 'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
# 將緩存中的圖像模型導入計算圖中
t_input = tf.placeholder(np.float32, name='input') #定義輸入圖像
imagenet_mean = 117.0 # 在預處理中,IMAGENET數據集的平均值
t_preprocessed = tf.expand_dims(t_input-imagenet_mean, 0)
tf.import_graph_def(graph_def, {'input':t_preprocessed})
# 拿到圖像模型的某一層
def T(layer):
return graph.get_tensor_by_name("import/"+layer)
# 定義計算圖片在某一層激活值的函數
def calc_grad_tiled(img, t_grad, tile_size=512):
sz = tile_size
h, w = img.shape[:2]
sx, sy = np.random.randint(sz, size=2)
img_shift = np.roll(np.roll(img, sx, 1), sy, 0)
grad = np.zeros_like(img)
for y in range(0, max(h-sz//2, sz), sz):
for x in range(0, max(w-sz//2, sz), sz):
sub = img_shift[y:y+sz,x:x+sz]
g = sess.run(t_grad, {t_input:sub})
grad[y:y+sz,x:x+sz] = g
return np.roll(np.roll(grad, -sx, 1), -sy, 0)
# 定義算法的主體
def render_deepdream(t_obj, img0=img_noise, iter_n=10, step=1.5, octave_n=4, octave_scale=1.4):
t_score = tf.reduce_mean(t_obj)
t_grad = tf.gradients(t_score, t_input)[0]
img = img0
octaves = []
for i in range(octave_n-1):
hw = img.shape[:2]
lo = resize(img, np.int32(np.float32(hw)/octave_scale))
hi = img-resize(lo, hw)
img = lo
octaves.append(hi)
for octave in range(octave_n):
if octave > 0:
hi = octaves[-octave]
img = resize(img, hi.shape[:2])+hi
for i in range(iter_n):
g = calc_grad_tiled(img, t_grad)
img += g*(step / (np.abs(g).mean()+1e-7))
return img / 255.0
二、熱氣球 DeepDream
DeepDream 的誕生,歸功於 Google 的這位科學家 Alex Krizhevsky。一天晚上,Alex在夢中對人腦活動的模擬漫遊產生了靈感,隨後他就開始嘗試用神經網絡在計算機上相似地模擬人類視覺系統。據Alex自己介紹,他最初的試驗媒介是一張鮮紅色的熱氣球圖片,這便是DeepDream的第一個圖案。對於這張看上去蔚為壯觀的圖案,Alex自己都很難解釋清楚何為Deep Dream。
三、DeepDream的意義
DeepDream算法出現之後,對於科研領域和藝術領域都產生了很大的影響。在計算機視覺和圖像識別領域,它能夠幫助開發者探究深度神經網絡並進一步拓展識別能力。在藝術領域,使用這一技術可以讓藝術家或設計師在短時間內實現自己理想的藝術創意,幫助他們發掘潛在的創作潛力。而在人工智能領域,深度學習的技術可以為我們提供更大的編程靈活性,提升機器自學習的能力,開創機器的智能新紀元。
四、Deep Dream Generator
Deep Dream Generator是一個網站,這個網站用人工智能(AI)的方式來生成藝術形成。用戶可以選擇自己的圖片上傳,以及對 Deep Style 進行程序化的文藝修改。通過在不同的圖片之間選擇和調整不同的參數,這個訓練好的神經網絡模型可以深度探究到圖像的每個角落並轉化出具有藝術感的夢幻般的新圖像。
# A Python Script for generating Deep Dreams:
import os, numpy as np, PIL.Image, time, argparse, functools
import tensorflow as tf
import matplotlib.pyplot as plt
from io import BytesIOmodel_url = "https://storage.googleapis.com/download.tensorflow.org/models/inception5h.zip"
model_fn = 'inception5h.zip'
layers = ['mixed3a_1x1_pre_relu', 'mixed5b_3x3_pre_relu', 'mixed4a_3x3_bottleneck_pre_relu', 'mixed4e_3x3_bottleneck_pre_relu', 'mixed4e_5x5_bottleneck_pre_relu', 'mixed4a_5x5_pre_relu', 'mixed4a_3x3_bottleneck_pre_relu', 'mixed4e_pool_reduce_pre_relu', 'mixed3a_3x3_bottleneck_pre_relu', 'mixed4b_1x1_pre_relu', 'mixed4b_5x5_pre_relu', 'mixed5b_5x5_pre_relu', 'mixed5a_3x3_pre_relu', 'mixed4a_1x1_pre_relu', 'mixed3a_5x5_pre_relu', 'mixed4e_1x1_pre_relu']
num_iterations = 10
step_size = 1.5
rescale_factor = 0.7
img_noise = np.random.uniform(size=(224,224,3)) + 100.0
session = None
graph = None
model = None
def get_model():
global session, graph, model
# make sure we have a model
if model is None:
print(f"Loading model from {model_fn} ...")
start = time.monotonic()
# downloader
if not os.path.exists(model_fn):
import requests
print(f"Downloading: {model_url} ...")
response = requests.get(model_url)
open(model_fn, 'wb').write(response.content)
# unpack
import zipfile
with zipfile.ZipFile(model_fn) as zip_ref:
zip_ref.extractall('.')
model_dir = os.path.splitext(model_fn)[0]
with open(os.path.join(model_dir, 'tensorflow_inception_graph.pb'), 'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
# start session
session = tf.InteractiveSession()
graph = tf.Graph()
with graph.as_default():
tf.import_graph_def(graph_def)
session.run(tf.global_variables_initializer())
model = graph.get_tensor_by_name("import/input:0")
logits = graph.get_tensor_by_name("import/softmax2:0")
inception = lambda img : session.run(logits, feed_dict={model: img})[:, np.argmax(model_labels)]
print(f"Took {time.monotonic()-start:.3f} seconds")
else:
print("Using cached model")
return inception
def resize(img, scale):
if scale == 1:
return img
else:
size = np.array(img.shape[0:2])
new_size = (size * float(scale)).astype(int)
img1 = np.copy(img)
img1.resize((new_size[0], new_size[1], img.shape[2]))
return img1
def calc_grad_tiled(img, model, tile_size=512, fmt='png'):
if isinstance(img, str):
# load image from file
try:
img = PIL.Image.open(img)
except Exception as e:
print(e)
return None
# rescale image if required
initial_shape = img.size[::-1]
scale = float(tile_size) / np.max(initial_shape)
img = resize(img, scale)
# important variables
x = img
a = img_noise.copy()
start_x, end_x = 0, 0
start_y, end_y = 0, 0
# select random tiles to migrate
shift = np.array([tile_size/2.0, tile_size/2.0])
ox = np.random.randint(-tile_size, tile_size)
oy = np.random.randint(-tile_size, tile_size)
ox = (ox/scale) + shift[0]
oy = (oy/scale) + shift[1]
# begin migration
x1 = np.roll(np.roll(x, int(ox-shift[0]), axis=1), int(oy-shift[1]), axis=0)
a1 = np.roll(np.roll(a, int(ox-shift[0]), axis=1), int(oy-shift[1]), axis=0)
def get_tile():
# detemine the tiles for this iteration
nonlocal x, x1, a, a1, start_x, end_x, start_y, end_y
sz = tile_size
start_x += np.random.randint(sz/2, size=1)[0]
start_y += np.random.randint(sz/2, size=1)[0]
end_x = min(start_x+sz, x.shape[1])
end_y = min(start_y+sz, x.shape[0])
x_slice = x[start_y:end_y, start_x:end_x, :]
a_slice = a1[start_y:end_y, start_x:end_x, :]
return x_slice, a_slice
def update_tile(g):
nonlocal x, x1, a, a1, start_x, end_x, start_y, end_y
x_slice = x[start_y:end_y, start_x:end_x, :]
a_slice = a1[start_y:end_y, start_x:end_x, :]
x_grad = x_slice * (rescale_factor**2) * g
tile_image = PIL.Image.fromarray(np.uint8(np.clip(x_slice + x_grad, 0, 255)))
# save tile gradient
a_slice += x_grad
a1[start_y:end_y, start_x:end_x, :] = a_slice
a = np.roll(np.roll(a1, int(-(ox-shift[0])), axis=1), int(-(oy-shift[1])), axis=0)
tile_buffer = BytesIO()
tile_image.save(tile_buffer, format=fmt)
tile_buffer.seek(0)
img_tile = PIL.Image.open(tile_buffer)
x[start_y:end_y, start_x:end_x, :] = np.array(img_tile)
x1 = np.roll(np.roll(x, int(-(ox-shift[0])), axis=1), int(-(oy-shift[1])), axis=0)
model_labels = np.loadtxt("synset.txt
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/286179.html