import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
def load_model():
model = tf.keras.applications.InceptionV3(weights='imagenet', include_top=False)
model.trainable = False
return model
def deep_dream(image, model, iterations=10, step_size=0.01):
image = tf.convert_to_tensor(image)
image = tf.expand_dims(image, axis=0)
for _ in range(iterations):
with tf.GradientTape() as tape:
tape.watch(image)
features = model(image)
loss = tf.reduce_mean(features)
gradients = tape.gradient(loss, image)
gradients /= tf.math.reduce_std(gradients) + 1e-8
image = image + gradients * step_size
image = tf.clip_by_value(image, -1.0, 1.0)
return image.numpy().squeeze()
# Load and preprocess image
img_path = 'input_image.jpg' # Replace with your image path
img = tf.keras.preprocessing.image.load_img(img_path, target_size=(224, 224))
img = tf.keras.preprocessing.image.img_to_array(img)
img = (img / 255.0) * 2.0 - 1.0
# Load model and run DeepDream
model = load_model()
dream_img = deep_dream(img, model, iterations=50, step_size=0.01)
# Save and display the result
dream_img = (dream_img + 1.0) / 2.0
plt.imshow(dream_img)
plt.axis('off')
plt.savefig('deepdream_image.png')
plt.show()
4. 使用DeepDream生成图像
原创mb64cc5144d532c ©著作权
文章标签 tensorflow 文章分类 软件研发
©著作权归作者所有:来自51CTO博客作者mb64cc5144d532c的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
4.带头节点的双链表的实现(C语言)
双链表基本运算的代码实现,前插、后插操作
双链表 头结点 插入操作 删除操作 -
DeepDream(一)
小白也看得懂的DeepDream
DeepDream 深度学习 卷积神经网络 图像处理 d3 -
4.数组
数组
数组 -
4. Triangled3 3d #define #pragma desktop