繁体   English   中英

how to make accuracy/loss plot in one plot if CNN model trained both on cifar10/100 in keras?

[英]how to make accuracy/loss plot in one plot if CNN model trained both on cifar10/100 in keras?

在卷积神经网络中使用像cifar10cifar100这样的基准图像数据集是标准做法,我想知道有什么方法可以在卷积神经网络的相同配置上同时使用cifar10cifar100 ,所以我们可以在两个上看到 CNN model 的训练精度/损失不同的数据集。 因为分别在两个不同的基准数据集上运行完全相同的 model 非常耗时。 我不确定如何在 keras 中实现这一点。

I think we might run the code in parallel and encapsulate the training model output in one dictionary then make accuracy/loss plot in one plot. 我尝试了几种不同的方法来实现这一点,我总是卡在cifar10cifar100上并行运行 CNN。 我使用 nivdia GPU 来运行实验,但不确定在馈送两个不同数据集时并行运行 CNN? 有什么方法可以在 tensorflow/keras 中轻松实现这一点? 那可行吗? 有什么可能的想法吗?

我目前的尝试

这是我目前的尝试,我还没有完全弄清楚如何在cifar10cifar100上并行运行 CNN,并在字典中收集所有训练 output 以获取准确度/损失 plot。

import tensorflow as tf
import keras
from tensorflow.keras.datasets import cifar10
from tensorflow.keras.utils import to_categorical
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense

(X_train, y_train), (X_test, y_test) = cifar10.load_data()
X_train = X_train.astype('float32') / 255.0
X_test = X_test.astype('float32') / 255.0

nb_classes = 10
y_train = to_categorical(y_train, nb_classes)
y_test = to_categorical(y_test, nb_classes)

def cnn(drop_rate=0.1):
    model = Sequential()
    model.add(Conv2D(32, (3, 3), input_shape=(32, 32, 3)))
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Flatten())
    model.add(Dense(64))
    model.add(Activation('relu'))
    model.add(Dropout(drop_rate))
    model.add(Dense(10))
    model.add(Activation('sigmoid'))
    model.compile(loss='binary_crossentropy',optimizer='rmsprop',metrics=['accuracy'])
    return model

conv_model = cnn()
history = conv_model.fit(X_train, y_train, shuffle=True, callbacks=[callbacks.EarlyStopping(monitor='val_loss', min_delta=0, patience=5, verbose=0, mode='min')],epochs=10, verbose=1, batch_size=128, validation_data=(X_test, y_test),validation_split=0.2)

## example plotting
plt.figure(figsize=(20,10))
plt.plot(history.history['loss'], 'r', linewidth=3)
plt.plot(history.history['val_loss'], 'b', linewidth=3)
plt.legend(['training loss', 'validation_loss'], fontsize=12)
plt.xlabel('epochs', fontsize=12)
plt.ylabel('loss', fontsize=12)

if we could able to run CNN model by feeding cifar10 , cifar100 in parallel, and expecting all training output will be in history , then we could access history.history to make plot of corresponding accuracy/loss of cifar10 and cifar100 in two subplot (one准确性,一个损失)。

如何轻松实现这一目标? 在 tensorflow 中是否有任何有效的方法来实现这一点? 有什么可能的想法来实现这一点? 谢谢

更新

我在想我们可以在 for 循环中执行此操作,但不确定这样做是否是个好主意。 我敢打赌,必须有一种高效而优雅的方式来做到这一点。 谁能指出我如何按预期实现这一目标? 任何想法?

您不能并行运行模型,除非您愿意:

  1. 让一个在 CPU 上训练,另一个在 GPU 上训练。 这仍然是一个问题,因为一些预处理操作仍然在 CPU 上完成,您很可能会遇到瓶颈。
  2. 你有两个 GPU,你在第一个 GPU 上训练一个,第二个 model 在第二个 GPU 上训练一个。

您的问题的解决方案是按顺序训练,序列化历史对象(使用 pickle),然后在同一个 plot 上加载历史对象和 plot。

PS:您必须使用softmaxcategorical_crossentropy进行多类分类,而不是sigmoidbinary_crossentropy

(1) 使用泡菜:

with open('filename.pickle', 'wb') as handle:
    pickle.dump(a, handle, protocol=pickle.HIGHEST_PROTOCOL)

with open('filename.pickle', 'rb') as handle:
    b = pickle.load(handle)

(2) 使 python 进程能够“看到”特定的 GPU。

例如:在 train_cifar_100.py

import os
os.environ["CUDA_VISIBLE_DEVICES"]="0,1"
#train
#serialize

例如在 train_cifar_10.py

import os
os.environ["CUDA_VISIBLE_DEVICES"]="2,3"
#train
#serialize

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM