steganoGAN代码
时间: 2024-02-11 14:34:26 浏览: 93
基于卷积神经网络的隐写分析 Matlab SRM、SCA隐写分析+源代码+文档说明
SteganoGAN 是一个基于生成对抗网络 (GAN) 的图像隐写工具,可以将秘密信息嵌入到图像中,同时保持图像的视觉感知。以下是 SteganoGAN 的 Python 代码实现:
```python
import tensorflow as tf
import numpy as np
from PIL import Image
class SteganoGAN:
def __init__(self, img_size=256, batch_size=16, alpha=0.2, beta=1.0, gamma=100.0):
self.img_size = img_size
self.batch_size = batch_size
self.alpha = alpha
self.beta = beta
self.gamma = gamma
self._build_model()
def _build_model(self):
self.generator = self._generator()
self.discriminator = self._discriminator()
self.stegano = self._stegano()
self.generator_optimizer = tf.keras.optimizers.Adam(1e-4)
self.discriminator_optimizer = tf.keras.optimizers.Adam(1e-4)
def _generator(self):
inputs = tf.keras.layers.Input(shape=(self.img_size, self.img_size, 3))
x = tf.keras.layers.Conv2D(64, 4, strides=2, padding='same')(inputs)
x = tf.keras.layers.LeakyReLU(alpha=self.alpha)(x)
x = tf.keras.layers.Conv2D(128, 4, strides=2, padding='same')(x)
x = tf.keras.layers.LeakyReLU(alpha=self.alpha)(x)
x = tf.keras.layers.Conv2D(256, 4, strides=2, padding='same')(x)
x = tf.keras.layers.LeakyReLU(alpha=self.alpha)(x)
x = tf.keras.layers.Conv2D(512, 4, strides=2, padding='same')(x)
x = tf.keras.layers.LeakyReLU(alpha=self.alpha)(x)
x = tf.keras.layers.Conv2DTranspose(256, 4, strides=2, padding='same')(x)
x = tf.keras.layers.LeakyReLU(alpha=self.alpha)(x)
x = tf.keras.layers.Conv2DTranspose(128, 4, strides=2, padding='same')(x)
x = tf.keras.layers.LeakyReLU(alpha=self.alpha)(x)
x = tf.keras.layers.Conv2DTranspose(64, 4, strides=2, padding='same')(x)
x = tf.keras.layers.LeakyReLU(alpha=self.alpha)(x)
outputs = tf.keras.layers.Conv2DTranspose(3, 4, strides=2, padding='same', activation='tanh')(x)
return tf.keras.Model(inputs, outputs)
def _discriminator(self):
inputs = tf.keras.layers.Input(shape=(self.img_size, self.img_size, 3))
x = tf.keras.layers.Conv2D(64, 4, strides=2, padding='same')(inputs)
x = tf.keras.layers.LeakyReLU(alpha=self.alpha)(x)
x = tf.keras.layers.Conv2D(128, 4, strides=2, padding='same')(x)
x = tf.keras.layers.LeakyReLU(alpha=self.alpha)(x)
x = tf.keras.layers.Conv2D(256, 4, strides=2, padding='same')(x)
x = tf.keras.layers.LeakyReLU(alpha=self.alpha)(x)
x = tf.keras.layers.Conv2D(512, 4, strides=2, padding='same')(x)
x = tf.keras.layers.LeakyReLU(alpha=self.alpha)(x)
outputs = tf.keras.layers.Conv2D(1, 4, strides=1, padding='valid')(x)
return tf.keras.Model(inputs, outputs)
def _stegano(self):
cover = tf.keras.layers.Input(shape=(self.img_size, self.img_size, 3))
secret = tf.keras.layers.Input(shape=(self.img_size, self.img_size, 3))
stego = self.generator(cover)
stego = stego + tf.keras.backend.stop_gradient(secret - stego)
return tf.keras.Model([cover, secret], stego)
def _loss(self, y_true, y_pred):
loss = tf.reduce_mean(y_true * y_pred)
return loss
def train_step(self, cover_image, secret_image):
with tf.GradientTape() as gen_tape, tf.GradientTape() as disc_tape:
# Generate stego image
stego_image = self.stegano([cover_image, secret_image], training=True)
# Discriminator loss
real_output = self.discriminator(cover_image, training=True)
fake_output = self.discriminator(stego_image, training=True)
disc_real_loss = self._loss(tf.ones_like(real_output), real_output)
disc_fake_loss = self._loss(tf.zeros_like(fake_output), fake_output)
disc_loss = self.beta * (disc_real_loss + disc_fake_loss)
# Generator loss
gen_gan_loss = self._loss(tf.ones_like(fake_output), fake_output)
gen_l1_loss = self.gamma * tf.reduce_mean(tf.abs(secret_image - stego_image))
gen_loss = gen_gan_loss + gen_l1_loss
# Update generator and discriminator
generator_gradients = gen_tape.gradient(gen_loss, self.generator.trainable_variables)
discriminator_gradients = disc_tape.gradient(disc_loss, self.discriminator.trainable_variables)
self.generator_optimizer.apply_gradients(zip(generator_gradients, self.generator.trainable_variables))
self.discriminator_optimizer.apply_gradients(zip(discriminator_gradients, self.discriminator.trainable_variables))
return {'gen_loss': gen_loss, 'disc_loss': disc_loss}
def encode(self, cover_path, secret_path, stego_path):
cover_image = np.array(Image.open(cover_path).resize((self.img_size, self.img_size)))
secret_image = np.array(Image.open(secret_path).resize((self.img_size, self.img_size)))
stego_image = self.stegano.predict([np.expand_dims(cover_image, axis=0), np.expand_dims(secret_image, axis=0)])
stego_image = np.squeeze(stego_image)
Image.fromarray(((stego_image + 1) * 127.5).astype(np.uint8)).save(stego_path)
def decode(self, stego_path, secret_path):
stego_image = np.array(Image.open(stego_path).resize((self.img_size, self.img_size)))
secret_image = stego_image * 2 - 1
secret_image = np.expand_dims(secret_image, axis=0)
cover_image = stego_image - tf.keras.backend.stop_gradient(secret_image - 0.5)
cover_image = np.expand_dims(cover_image, axis=0)
secret_image = self.generator.predict(cover_image)
secret_image = np.squeeze(secret_image)
Image.fromarray(((secret_image + 1) * 127.5).astype(np.uint8)).save(secret_path)
```
这里的 `SteganoGAN` 类包含了模型的构建、训练和使用等方法。其中,`_generator` 和 `_discriminator` 方法分别构建了生成器和判别器模型,`_stegano` 方法构建了嵌入模型。
`train_step` 方法用于模型的训练,`encode` 和 `decode` 方法分别用于嵌入和提取秘密信息。在 `encode` 方法中,输入的 `cover_path` 和 `secret_path` 分别为载体图像和秘密图像的路径,`stego_path` 为嵌入秘密信息后的图像保存路径;在 `decode` 方法中,输入的 `stego_path` 和 `secret_path` 分别为嵌入秘密信息后的图像路径和提取出的秘密信息的保存路径。
阅读全文