python-3.x - CNN精度y轴范围
问题描述
我已经训练了我的 CNN 模型并获得了准确度图,我使用 pickle 保存了训练时期。
当我对图表进行编码时,我得到了从 0 到 1 的 y 轴范围。如何使用已经保存的泡菜值来获得 0-100 的范围。
from keras.models import Sequential
from keras.layers import Conv2D,Activation,MaxPooling2D,Dense,Flatten,Dropout
import numpy as np
from keras.preprocessing.image import ImageDataGenerator
from IPython.display import display
import matplotlib.pyplot as plt
from PIL import Image
from sklearn.metrics import classification_report, confusion_matrix
import keras
from keras.layers import BatchNormalization
from keras.optimizers import Adam
import pickle
from keras.models import load_model
f = open('32_With_Dropout_rl_001_1_layer', 'rb')
history = pickle.load(f)
f = open('32_With_Dropout_rl_001_2_layers', 'rb')
history1 = pickle.load(f)
f = open('32_With_Dropout_rl_001_3_layers', 'rb')
history2 = pickle.load(f)
# summarize history for accuracy
plt.plot(history['val_accuracy'])
plt.plot(history1['val_accuracy'])
plt.plot(history2['val_accuracy'])
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['CNN_1', 'CNN_2', 'CNN_3'], loc='lower right')
plt.show()
# summarize history for loss
plt.plot(history['val_loss'])
plt.plot(history1['val_loss'])
plt.plot(history2['val_loss'])
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['CNN_1', 'CNN_2', 'CNN_3'], loc='upper left')
plt.show()
解决方案
您可以将列表值 .ie 'val_accuracy' 乘以 100。代码如下所示,
val_accuracy = [i * 100 for i in history.history['val_accuracy']]
plt.plot(val_accuracy)
plt.title('Model Accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['Val Accuracy'], loc='upper left')
plt.show()
模型和绘图示例 -
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Conv2D, Flatten, Dropout, MaxPooling2D
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.optimizers import Adam
import os
import numpy as np
import matplotlib.pyplot as plt
_URL = 'https://storage.googleapis.com/mledu-datasets/cats_and_dogs_filtered.zip'
path_to_zip = tf.keras.utils.get_file('cats_and_dogs.zip', origin=_URL, extract=True)
PATH = os.path.join(os.path.dirname(path_to_zip), 'cats_and_dogs_filtered')
train_dir = os.path.join(PATH, 'train')
validation_dir = os.path.join(PATH, 'validation')
train_cats_dir = os.path.join(train_dir, 'cats') # directory with our training cat pictures
train_dogs_dir = os.path.join(train_dir, 'dogs') # directory with our training dog pictures
validation_cats_dir = os.path.join(validation_dir, 'cats') # directory with our validation cat pictures
validation_dogs_dir = os.path.join(validation_dir, 'dogs') # directory with our validation dog pictures
num_cats_tr = len(os.listdir(train_cats_dir))
num_dogs_tr = len(os.listdir(train_dogs_dir))
num_cats_val = len(os.listdir(validation_cats_dir))
num_dogs_val = len(os.listdir(validation_dogs_dir))
total_train = num_cats_tr + num_dogs_tr
total_val = num_cats_val + num_dogs_val
batch_size = 128
epochs = 15
IMG_HEIGHT = 150
IMG_WIDTH = 150
train_image_generator = ImageDataGenerator(rescale=1./255,brightness_range=[0.5,1.5]) # Generator for our training data
validation_image_generator = ImageDataGenerator(rescale=1./255,brightness_range=[0.5,1.5]) # Generator for our validation data
train_data_gen = train_image_generator.flow_from_directory(batch_size=batch_size,
directory=train_dir,
shuffle=True,
target_size=(IMG_HEIGHT, IMG_WIDTH),
class_mode='binary')
val_data_gen = validation_image_generator.flow_from_directory(batch_size=batch_size,
directory=validation_dir,
target_size=(IMG_HEIGHT, IMG_WIDTH),
class_mode='binary')
model = Sequential([
Conv2D(16, 3, padding='same', activation='relu', input_shape=(IMG_HEIGHT, IMG_WIDTH ,3)),
MaxPooling2D(),
Conv2D(32, 3, padding='same', activation='relu'),
MaxPooling2D(),
Conv2D(64, 3, padding='same', activation='relu'),
MaxPooling2D(),
Flatten(),
Dense(512, activation='relu'),
Dense(1)
])
model.compile(optimizer="adam",
loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),
metrics=['accuracy'])
history = model.fit_generator(
train_data_gen,
steps_per_epoch=total_train // batch_size,
epochs=epochs,
validation_data=val_data_gen,
validation_steps=total_val // batch_size)
val_accuracy = [i * 100 for i in history.history['val_accuracy']]
plt.plot(val_accuracy)
plt.title('Model Accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['Val Accuracy'], loc='upper left')
plt.show()
输出 -
Found 2000 images belonging to 2 classes.
Found 1000 images belonging to 2 classes.
Epoch 1/15
15/15 [==============================] - 11s 763ms/step - loss: 0.8592 - accuracy: 0.5036 - val_loss: 0.6932 - val_accuracy: 0.4989
Epoch 2/15
15/15 [==============================] - 12s 767ms/step - loss: 0.6926 - accuracy: 0.5021 - val_loss: 0.6927 - val_accuracy: 0.5000
Epoch 3/15
15/15 [==============================] - 11s 740ms/step - loss: 0.6908 - accuracy: 0.4989 - val_loss: 0.6830 - val_accuracy: 0.5000
Epoch 4/15
15/15 [==============================] - 11s 746ms/step - loss: 0.6752 - accuracy: 0.5235 - val_loss: 0.6534 - val_accuracy: 0.5580
Epoch 5/15
15/15 [==============================] - 11s 748ms/step - loss: 0.6401 - accuracy: 0.5865 - val_loss: 0.6111 - val_accuracy: 0.6127
Epoch 6/15
15/15 [==============================] - 11s 747ms/step - loss: 0.5673 - accuracy: 0.6779 - val_loss: 0.5867 - val_accuracy: 0.6786
Epoch 7/15
15/15 [==============================] - 11s 747ms/step - loss: 0.5347 - accuracy: 0.7196 - val_loss: 0.5962 - val_accuracy: 0.6964
Epoch 8/15
15/15 [==============================] - 11s 748ms/step - loss: 0.4618 - accuracy: 0.7879 - val_loss: 0.6002 - val_accuracy: 0.6897
Epoch 9/15
15/15 [==============================] - 11s 745ms/step - loss: 0.4271 - accuracy: 0.7906 - val_loss: 0.5649 - val_accuracy: 0.6931
Epoch 10/15
15/15 [==============================] - 11s 753ms/step - loss: 0.3839 - accuracy: 0.8125 - val_loss: 0.5892 - val_accuracy: 0.7042
Epoch 11/15
15/15 [==============================] - 11s 750ms/step - loss: 0.3151 - accuracy: 0.8558 - val_loss: 0.6658 - val_accuracy: 0.6629
Epoch 12/15
15/15 [==============================] - 11s 751ms/step - loss: 0.2736 - accuracy: 0.8686 - val_loss: 0.6635 - val_accuracy: 0.7188
Epoch 13/15
15/15 [==============================] - 11s 748ms/step - loss: 0.2423 - accuracy: 0.8868 - val_loss: 0.7478 - val_accuracy: 0.7054
Epoch 14/15
15/15 [==============================] - 11s 749ms/step - loss: 0.2192 - accuracy: 0.9092 - val_loss: 0.8924 - val_accuracy: 0.6719
Epoch 15/15
15/15 [==============================] - 11s 751ms/step - loss: 0.1754 - accuracy: 0.9215 - val_loss: 0.7900 - val_accuracy: 0.7087
绘图输出 -
推荐阅读
- c++ - 为什么我不能在 c++20 中的 istream_view 之后使用 take()
- android - 当我只选择一个选项时如何使用按钮 kotlin
- c++ - 将定时器与 Windows 线程池一起使用
- c++ - 初始化结构时的 GCC“AddressSanitizer:堆缓冲区溢出”
- parsing - 汇编程序应该如何区分符号和全字母十六进制值?
- oracle - 如何使用命令行使用 OWB 存储库助手
- c++ - 尝试为 GoogleTest exe 运行 VSTest 时出现“没有可用的测试”
- reactjs - 如何覆盖 Material-UI 芯片 deleteIcon / cancelIcon?
- vue.js - 如何在不失去反应性的情况下展平计算对象
- javascript - 使用 SPA 功能禁用后退按钮上的滚动顶部