探究DeepDream:AI繪畫技術的奧秘

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 BytesIO

model_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-tw/n/286179.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-22 16:07
下一篇 2024-12-22 16:07

相關推薦

  • Python熱重載技術

    Python熱重載技術是現代編程的關鍵功能之一。它可以幫助我們在程序運行的過程中,更新代碼而無需重新啟動程序。本文將會全方位地介紹Python熱重載的實現方法和應用場景。 一、實現…

    編程 2025-04-29
  • index.m3u8+-1的奧秘

    本文將從以下多個方面對index.m3u8+-1進行詳細的闡述,解答該問題。 一、什麼是index.m3u8文件? index.m3u8是HLS (HTTP Live Stream…

    編程 2025-04-29
  • Python包絡平滑技術解析

    本文將從以下幾個方面對Python包絡平滑技術進行詳細的闡述,包括: 什麼是包絡平滑技術? Python中使用包絡平滑技術的方法有哪些? 包絡平滑技術在具體應用中的實際效果 一、包…

    編程 2025-04-29
  • parent.$.dialog是什麼技術的語法

    parent.$.dialog是一種基於jQuery插件的彈出式對話框技術,它提供了一個方便快捷的方式來創建各種類型和樣式的彈出式對話框。它是對於在網站開發中常見的彈窗、提示框等交…

    編程 2025-04-28
  • 微信小程序重構H5技術方案設計 Github

    本文旨在探討如何在微信小程序中重構H5技術方案,以及如何結合Github進行代碼存儲和版本管理。我們將從以下幾個方面進行討論: 一、小程序與H5技術對比 微信小程序與H5技術都可以…

    編程 2025-04-28
  • HTML sprite技術

    本文將從多個方面闡述HTML sprite技術,包含基本概念、使用示例、實現原理等。 一、基本概念 1、什麼是HTML sprite? HTML sprite,也稱CSS spri…

    編程 2025-04-28
  • Python工作需要掌握什麼技術

    Python是一種高級編程語言,它因其簡單易學、高效可靠、可擴展性強而成為最流行的編程語言之一。在Python開發中,需要掌握許多技術才能讓開發工作更加高效、準確。本文將從多個方面…

    編程 2025-04-28
  • Lidar避障與AI結構光避障哪個更好?

    簡單回答:Lidar避障適用於需要高精度避障的場景,而AI結構光避障更適用於需要快速響應的場景。 一、Lidar避障 Lidar,即激光雷達,通過激光束掃描環境獲取點雲數據,從而實…

    編程 2025-04-27
  • 開源腦電波技術

    本文將會探討開源腦電波技術的應用、原理和示例。 一、腦電波簡介 腦電波(Electroencephalogram,簡稱EEG),是一種用於檢測人腦電活動的無創性技術。它通過在頭皮上…

    編程 2025-04-27
  • 阿里Python技術手冊

    本文將從多個方面對阿里Python技術手冊進行詳細闡述,包括規範、大數據、Web應用、安全和調試等方面。 一、規範 Python的編寫規範對於代碼的可讀性和可維護性有很大的影響。阿…

    編程 2025-04-27

發表回復

登錄後才能評論