以下是使用Keras完成飞机车鸟数据集的代码:

导入必要的库

import numpy as np import pandas as pd import tensorflow as tf from tensorflow import keras from tensorflow.keras import layers from tensorflow.keras.preprocessing.image import ImageDataGenerator

(1) 读取data3数据集

train_df = pd.read_csv('data3/train.csv') test_df = pd.read_csv('data3/test.csv')

(2) 进行随机裁剪,翻转等操作,使数据量翻倍

train_datagen = ImageDataGenerator( rescale=1./255, rotation_range=20, width_shift_range=0.2, height_shift_range=0.2, shear_range=0.2, zoom_range=0.2, horizontal_flip=True, fill_mode='nearest' )

(3) 设置读取函数

def read_image(filename, label): image_string = tf.io.read_file(filename) image = tf.image.decode_jpeg(image_string, channels=3) image = tf.image.resize(image, [224, 224]) image = tf.cast(image, tf.float32) / 255.0 return image, label

(4) 建立管道读取

train_ds = tf.data.Dataset.from_tensor_slices((train_df['filename'], train_df['label'])) train_ds = train_ds.map(read_image) train_ds = train_ds.shuffle(buffer_size=len(train_df)) train_ds = train_ds.batch(batch_size=32)

(5) 洗牌,设定合理batch_size

test_ds = tf.data.Dataset.from_tensor_slices((test_df['filename'], test_df['label'])) test_ds = test_ds.map(read_image) test_ds = test_ds.batch(batch_size=32)

(6) 预读取数据

for image, label in train_ds.take(1): print(image.shape) print(label.shape)

(7) 合理设定超参数

learning_rate = 0.001 epochs = 20

(8) 创建vgg16模型类

class VGG16(keras.Model): def init(self): super(VGG16, self).init() self.conv1_1 = layers.Conv2D(64, (3, 3), activation='relu', padding='same') self.conv1_2 = layers.Conv2D(64, (3, 3), activation='relu', padding='same') self.pool1 = layers.MaxPooling2D((2, 2)) self.conv2_1 = layers.Conv2D(128, (3, 3), activation='relu', padding='same') self.conv2_2 = layers.Conv2D(128, (3, 3), activation='relu', padding='same') self.pool2 = layers.MaxPooling2D((2, 2)) self.conv3_1 = layers.Conv2D(256, (3, 3), activation='relu', padding='same') self.conv3_2 = layers.Conv2D(256, (3, 3), activation='relu', padding='same') self.conv3_3 = layers.Conv2D(256, (3, 3), activation='relu', padding='same') self.pool3 = layers.MaxPooling2D((2, 2)) self.conv4_1 = layers.Conv2D(512, (3, 3), activation='relu', padding='same') self.conv4_2 = layers.Conv2D(512, (3, 3), activation='relu', padding='same') self.conv4_3 = layers.Conv2D(512, (3, 3), activation='relu', padding='same') self.pool4 = layers.MaxPooling2D((2, 2)) self.conv5_1 = layers.Conv2D(512, (3, 3), activation='relu', padding='same') self.conv5_2 = layers.Conv2D(512, (3, 3), activation='relu', padding='same') self.conv5_3 = layers.Conv2D(512, (3, 3), activation='relu', padding='same') self.pool5 = layers.MaxPooling2D((2, 2)) self.flatten = layers.Flatten() self.fc1 = layers.Dense(4096, activation='relu') self.fc2 = layers.Dense(4096, activation='relu') self.fc3 = layers.Dense(3, activation='softmax')

def call(self, inputs):
    x = self.conv1_1(inputs)
    x = self.conv1_2(x)
    x = self.pool1(x)
    x = self.conv2_1(x)
    x = self.conv2_2(x)
    x = self.pool2(x)
    x = self.conv3_1(x)
    x = self.conv3_2(x)
    x = self.conv3_3(x)
    x = self.pool3(x)
    x = self.conv4_1(x)
    x = self.conv4_2(x)
    x = self.conv4_3(x)
    x = self.pool4(x)
    x = self.conv5_1(x)
    x = self.conv5_2(x)
    x = self.conv5_3(x)
    x = self.pool5(x)
    x = self.flatten(x)
    x = self.fc1(x)
    x = self.fc2(x)
    x = self.fc3(x)
    return x

(9) 设置正向传播方法

model = VGG16() loss_fn = tf.keras.losses.SparseCategoricalCrossentropy() optimizer = tf.keras.optimizers.Adam(learning_rate=learning_rate)

train_acc_metric = tf.keras.metrics.SparseCategoricalAccuracy() test_acc_metric = tf.keras.metrics.SparseCategoricalAccuracy()

for epoch in range(epochs): print('Epoch {}/{}'.format(epoch+1, epochs)) print('-' * 30) for step, (x_batch_train, y_batch_train) in enumerate(train_ds): with tf.GradientTape() as tape: # 计算模型输出 logits = model(x_batch_train, training=True) # 计算损失值 loss_value = loss_fn(y_batch_train, logits) gradients = tape.gradient(loss_value, model.trainable_weights) optimizer.apply_gradients(zip(gradients, model.trainable_weights)) # 更新训练准确率 train_acc_metric.update_state(y_batch_train, logits) if step % 10 == 0: print('Training loss (for one batch) at step {}: {}'.format(step, float(loss_value))) print('Seen so far: {} samples'.format((step + 1) * 32)) # 计算训练集准确率 train_acc = train_acc_metric.result() print('Training acc over epoch: {}'.format(float(train_acc))) # 重置训练准确率 train_acc_metric.reset_states()

# 计算测试集准确率
for x_batch_test, y_batch_test in test_ds:
    test_logits = model(x_batch_test)
    test_acc_metric.update_state(y_batch_test, test_logits)
test_acc = test_acc_metric.result()
test_acc_metric.reset_states()
print('Test acc: {}'.format(float(test_acc)))

保存最终模型

model.save('vgg16.h5')

可视化代价与准确率图

import matplotlib.pyplot as plt

history = model.history.history

train_loss = history['loss'] train_acc = history['sparse_categorical_accuracy'] test_loss = history['val_loss'] test_acc = history['val_sparse_categorical_accuracy']

plt.figure(figsize=(10, 5)) plt.subplot(1, 2, 1) plt.plot(train_loss, label='train_loss') plt.plot(test_loss, label='test_loss') plt.title('Loss') plt.legend()

plt.subplot(1, 2, 2) plt.plot(train_acc, label='train_acc') plt.plot(test_acc, label='test_acc') plt.title('Accuracy') plt.legend()

plt.show()


原文地址: http://www.cveoy.top/t/topic/XYL 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录