Random Image Feed

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="style.css" />
    <title>Random Image Feed</title>
  </head>
  <body>
    <h1>Random Image Feed</h1>
    <div class="container"></div>

    <script src="script.js"></script>
  </body>
</html>
@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap');

* {
  box-sizing: border-box;
}

body {
  font-family: 'Roboto', sans-serif;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  margin: 0;
}

.title {
  margin: 10px 0 0;
  text-align: center;
}

.container {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  max-width: 1000px;
}

.container img {
  object-fit: cover;
  margin: 10px;
  height: 300px;
  width: 300px;
  max-width: 100%;
}
const container = document.querySelector('.container')
const unsplashURL = 'https://source.unsplash.com/random/'
const rows = 5

for(let i = 0; i < rows * 3; i++) {
    const img = document.createElement('img')
    img.src = `${unsplashURL}${getRandomSize()}`
    container.appendChild(img)
}

function getRandomSize() {
    return `${getRandomNr()}x${getRandomNr()}`
}

function getRandomNr() {
    return Math.floor(Math.random() * 10) + 300
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用TensorFlow实现风格迁移的Python代码: ```python import numpy as np import tensorflow as tf import scipy.misc import argparse import os import time import sys # 定义参数 parser = argparse.ArgumentParser() parser.add_argument('--content', type=str, default='content.jpg', help='Content image') parser.add_argument('--style', type=str, default='style.jpg', help='Style image') parser.add_argument('--output', type=str, default='output.jpg', help='Output image') parser.add_argument('--iterations', type=int, default=1000, help='Number of iterations') parser.add_argument('--content_weight', type=float, default=0.1, help='Content weight') parser.add_argument('--style_weight', type=float, default=1.0, help='Style weight') parser.add_argument('--learning_rate', type=float, default=10, help='Learning rate') args = parser.parse_args() # 加载图像 def load_image(path): image = scipy.misc.imread(path).astype(np.float) image = np.expand_dims(image, axis=0) image = vgg19.preprocess(image) return image # 保存图像 def save_image(path, image): image = image.reshape((image.shape[1], image.shape[2], 3)) image = vgg19.deprocess(image) scipy.misc.imsave(path, image) # 定义VGG19模型 class VGG19: def __init__(self, input_image): self.input_image = input_image self.mean_pixel = np.array([123.68, 116.779, 103.939]).reshape((1,1,1,3)) self.layers = [ 'conv1_1', 'relu1_1', 'conv1_2', 'relu1_2', 'pool1', 'conv2_1', 'relu2_1', 'conv2_2', 'relu2_2', 'pool2', 'conv3_1', 'relu3_1', 'conv3_2', 'relu3_2', 'conv3_3', 'relu3_3', 'conv3_4', 'relu3_4', 'pool3', 'conv4_1', 'relu4_1', 'conv4_2', 'relu4_2', 'conv4_3', 'relu4_3', 'conv4_4', 'relu4_4', 'pool4', 'conv5_1', 'relu5_1', 'conv5_2', 'relu5_2', 'conv5_3', 'relu5_3', 'conv5_4', 'relu5_4' ] self.weights = self.load_weights() # 加载VGG19模型的权重 def load_weights(self): weights = np.load('vgg19_weights.npy', encoding='latin1').item() return weights # 预处理图像 def preprocess(self, image): return image - self.mean_pixel # 反向预处理图像 def deprocess(self, image): return image + self.mean_pixel # 定义卷积层 def conv_layer(self, input, weights, bias): conv = tf.nn.conv2d(input, tf.constant(weights), strides=(1,1,1,1), padding='SAME') return tf.nn.bias_add(conv, bias) # 定义池化层 def pool_layer(self, input): return tf.nn.max_pool(input, ksize=(1,2,2,1), strides=(1,2,2,1), padding='SAME') # 定义前向传播 def forward(self): net = {} current = self.input_image for i, name in enumerate(self.layers): kind = name[:4] if kind == 'conv': kernels = self.weights[name][0][0] bias = self.weights[name][0][1] current = self.conv_layer(current, kernels, bias) elif kind == 'relu': current = tf.nn.relu(current) elif kind == 'pool': current = self.pool_layer(current) net[name] = current return net # 定义内容损失 def content_loss(content, combination): return tf.reduce_sum(tf.square(combination - content)) # 定义风格损失 def style_loss(style, combination): S = gram_matrix(style) C = gram_matrix(combination) channels = 3 size = style.shape[1] * style.shape[2] return tf.reduce_sum(tf.square(S - C)) / (4.0 * (channels ** 2) * (size ** 2)) # 定义Gram矩阵 def gram_matrix(input_tensor): channels = int(input_tensor.shape[-1]) a = tf.reshape(input_tensor, [-1, channels]) n = tf.shape(a)[0] gram = tf.matmul(a, a, transpose_a=True) return gram / tf.cast(n, tf.float32) # 定义总损失 def total_loss(content_image, style_image, combination_image): with tf.variable_scope('loss') as scope: content_layers = ['conv4_2'] style_layers = ['conv1_1', 'conv2_1', 'conv3_1', 'conv4_1', 'conv5_1'] vgg = VGG19(combination_image) net = vgg.forward() content_features = {} style_features = {} for layer in content_layers: content_features[layer] = net[layer] for layer in style_layers: style_features[layer] = net[layer] loss = 0 # 计算内容损失 for layer in content_layers: content_feature = content_features[layer] combination_feature = net[layer] loss += args.content_weight * content_loss(content_feature, combination_feature) # 计算风格损失 for layer in style_layers: style_feature = style_features[layer] combination_feature = net[layer] sl = style_loss(style_feature, combination_feature) loss += (args.style_weight / len(style_layers)) * sl # 计算总变差损失 loss += args.tv_weight * tf.image.total_variation(combination_image) return loss # 定义训练函数 def train(content_image, style_image, output_image): content_shape = content_image.shape style_shape = style_image.shape with tf.Graph().as_default(): # 定义输入 content = tf.placeholder(tf.float32, shape=content_shape, name='content') style = tf.placeholder(tf.float32, shape=style_shape, name='style') combination = tf.Variable(tf.random_normal(content_shape), name='combination') # 定义损失函数 loss = total_loss(content, style, combination) # 定义优化器 optimizer = tf.train.AdamOptimizer(args.learning_rate).minimize(loss) # 开始训练 with tf.Session() as sess: sess.run(tf.global_variables_initializer()) sess.run(tf.assign(combination, content)) for i in range(args.iterations): sess.run(optimizer, feed_dict={content: content_image, style: style_image}) if i % 100 == 0: print('Iteration %d' % i) output_image = sess.run(combination, feed_dict={content: content_image, style: style_image}) save_image(args.output, output_image) output_image = sess.run(combination, feed_dict={content: content_image, style: style_image}) save_image(args.output, output_image) # 加载图像 content_image = load_image(args.content) style_image = load_image(args.style) # 进行风格迁移 train(content_image, style_image, args.output) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值