要求在训练集:MNIST 60,000张、测试集:MNIST 10,000张和验证集:SVHN 5,000张上实验。并且给出以下五个结果: ①训练集,测试集,验证集三个数据集的准确率,召回率,精确率,f1分数。 ②还有神经网络层次 ③混淆矩阵 ④训练的总时间 ⑤绘制出训练集,测试集,验证集的损失以及准确率曲线。最好可以用matplotlib画出来 这是我的实验环境: Python 版本: 3.11.11 (main, Dec 4 2024, 08:55:07) [GCC 11.4.0] TensorFlow 版本: 2.18.0 帮我写一份resnet配合gan进行手写数字图像识别的代码 svhn不用下载,直接从train_32x32.mat读取
时间: 2025-06-24 14:43:08 浏览: 13
### 实现ResNet与GAN结合的模型进行手写数字图像识别
以下是基于Python 3.11.11和TensorFlow 2.18.0实现ResNet与GAN结合的模型用于手写数字图像识别的完整代码。该代码使用MNIST作为训练集和测试集,SVHN作为验证集,并输出多个实验结果指标。
#### 数据预处理
首先加载并归一化MNIST数据集[^2]:
```python
import numpy as np
from tensorflow.keras.datasets import mnist
from scipy.io import loadmat
# 加载MNIST数据集
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
# 归一化数据到[0, 1]
train_images = train_images.reshape((60000, 28 * 28)).astype('float32') / 255
test_images = test_images.reshape((10000, 28 * 28)).astype('float32') / 255
# SVHN验证集加载
svhn_data = loadmat('train_32x32.mat')
svhn_images = svhn_data['X']
svhn_labels = svhn_data['y'].flatten()
svhn_images = np.moveaxis(svhn_images, -1, 0).reshape((-1, 32*32*3)).astype('float32') / 255
```
#### ResNet-GAN架构定义
构建ResNet基础模块并与GAN生成器和判别器相结合[^3]:
```python
from tensorflow.keras.layers import Input, Dense, Conv2D, BatchNormalization, LeakyReLU, Flatten, Reshape, Dropout, Add
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adam
def resnet_block(input_tensor, filters):
x = Conv2D(filters=filters, kernel_size=3, padding='same')(input_tensor)
x = BatchNormalization()(x)
x = LeakyReLU(alpha=0.2)(x)
x = Conv2D(filters=filters, kernel_size=3, padding='same')(x)
x = BatchNormalization()(x)
x = Add()([x, input_tensor])
x = LeakyReLU(alpha=0.2)(x)
return x
def build_generator(latent_dim):
model_input = Input(shape=(latent_dim,))
x = Dense(7 * 7 * 256)(model_input)
x = LeakyReLU(alpha=0.2)(x)
x = Reshape((7, 7, 256))(x)
x = Conv2DTranspose(128, (4, 4), strides=(2, 2), padding="same")(x)
x = LeakyReLU(alpha=0.2)(x)
x = Conv2DTranspose(1, (4, 4), strides=(2, 2), padding="same", activation="tanh")(x)
generator_model = Model(model_input, x)
return generator_model
def build_discriminator():
model_input = Input(shape=(28, 28, 1))
x = Conv2D(64, (3, 3), strides=(2, 2), padding="same")(model_input)
x = LeakyReLU(alpha=0.2)(x)
x = Dropout(0.25)(x)
x = Conv2D(128, (3, 3), strides=(2, 2), padding="same")(x)
x = ZeroPadding2D(padding=((0, 1), (0, 1)))(x)
x = BatchNormalization(momentum=0.8)(x)
x = LeakyReLU(alpha=0.2)(x)
x = Dropout(0.25)(x)
x = Flatten()(x)
x = Dense(1, activation="sigmoid")(x)
discriminator_model = Model(model_input, x)
return discriminator_model
def build_resnet_classifier():
inputs = Input(shape=(28, 28, 1))
x = Conv2D(64, (3, 3), padding='same')(inputs)
x = BatchNormalization()(x)
x = LeakyReLU(alpha=0.2)(x)
for _ in range(3): # 添加三个残差块
x = resnet_block(x, 64)
x = Flatten()(x)
outputs = Dense(10, activation='softmax')(x)
classifier_model = Model(inputs, outputs)
return classifier_model
```
#### 训练过程
设置超参数并训练模型[^3]:
```python
import time
from sklearn.metrics import accuracy_score, recall_score, precision_score, f1_score, confusion_matrix
import matplotlib.pyplot as plt
# 初始化超参数
INIT_LR = 0.0002
EPOCHS = 100
BATCH_SIZE = 64
# 构建模型
generator = build_generator(latent_dim=100)
discriminator = build_discriminator()
classifier = build_resnet_classifier()
# 编译模型
discriminator.compile(loss="binary_crossentropy", optimizer=Adam(INIT_LR), metrics=["accuracy"])
classifier.compile(optimizer=Adam(INIT_LR), loss="sparse_categorical_crossentropy", metrics=['accuracy'])
start_time = time.time()
for epoch in range(EPOCHS):
idx = np.random.randint(0, train_images.shape[0], BATCH_SIZE)
real_images = train_images[idx].reshape(-1, 28, 28, 1)
noise = np.random.normal(0, 1, (BATCH_SIZE, 100))
gen_images = generator(noise)
d_loss_real = discriminator.train_on_batch(real_images, np.ones((BATCH_SIZE, 1)))
d_loss_fake = discriminator.train_on_batch(gen_images, np.zeros((BATCH_SIZE, 1)))
c_loss = classifier.train_on_batch(real_images, train_labels[idx])
end_time = time.time()
training_time = end_time - start_time
```
#### 输出评估指标
计算准确率、召回率、精确率、F1分数等指标[^1]:
```python
# 测试集预测
predictions_test = classifier.predict(test_images.reshape(-1, 28, 28, 1))
predicted_classes_test = predictions_test.argmax(axis=-1)
acc_test = accuracy_score(test_labels, predicted_classes_test)
recall_test = recall_score(test_labels, predicted_classes_test, average='macro', zero_division=0)
precision_test = precision_score(test_labels, predicted_classes_test, average='macro', zero_division=0)
f1_test = f1_score(test_labels, predicted_classes_test, average='macro')
conf_matrix = confusion_matrix(test_labels, predicted_classes_test)
print(f"Test Accuracy: {acc_test}")
print(f"Recall Score: {recall_test}")
print(f"Precision Score: {precision_test}")
print(f"F1 Score: {f1_test}")
plt.figure(figsize=(10, 8))
plt.imshow(conf_matrix, cmap=plt.cm.Blues)
plt.title("Confusion Matrix")
plt.colorbar()
plt.show()
```
#### 绘制损失和准确率曲线
绘制训练过程中损失和准确率的变化趋势:
```python
history = classifier.history.history
epochs_range = range(len(history['loss']))
plt.plot(epochs_range, history['loss'], label='Training Loss')
plt.plot(epochs_range, history['accuracy'], label='Training Accuracy')
plt.legend(loc='upper left')
plt.xlabel('Epochs')
plt.ylabel('Loss/Accuracy')
plt.title('Training Metrics Over Epochs')
plt.show()
```
---
阅读全文
相关推荐


















