简体   繁体   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?

using benchmark image dataset like cifar10 , cifar100 in convolutional NN is standard practice, and I am wondering is there any way we can both use cifar10 , cifar100 on same configuration of convolutional NN, so we could see training accuracy/loss of CNN model on two different dataset.在卷积神经网络中使用像cifar10cifar100这样的基准图像数据集是标准做法,我想知道有什么方法可以在卷积神经网络的相同配置上同时使用cifar10cifar100 ,所以我们可以在两个上看到 CNN model 的训练精度/损失不同的数据集。 Because it is time consuming run exactly same model on two different benchmark dataset separately.因为分别在两个不同的基准数据集上运行完全相同的 model 非常耗时。 I am not sure how to achieve this in keras.我不确定如何在 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. 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. I tried few different way of achieving this, I always get stuck with parallel run of CNN on cifar10 , and cifar100 .我尝试了几种不同的方法来实现这一点,我总是卡在cifar10cifar100上并行运行 CNN。 I used nivdia GPU to run experiment, but not sure to run parallel of CNN on feeding two different dataset?我使用 nivdia GPU 来运行实验,但不确定在馈送两个不同数据集时并行运行 CNN? Is there any way to make this happen easily in tensorflow/keras?有什么方法可以在 tensorflow/keras 中轻松实现这一点? Is that doable?那可行吗? Any possible thoughts?有什么可能的想法吗?

my current attempt我目前的尝试

here is my current attempt where I haven't completely figured out how to run CNN on cifar10 and cifar100 in parallel and collect all training output in dictionary for making accuracy/loss plot.这是我目前的尝试,我还没有完全弄清楚如何在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 for accuracy, one for loss). 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准确性,一个损失)。

How to achieve this easily?如何轻松实现这一目标? Is there any efficient way to achieve this in tensorflow?在 tensorflow 中是否有任何有效的方法来实现这一点? Any possible thoughts to make this happen?有什么可能的想法来实现这一点? Thanks谢谢

update更新

I was thinking we could do this in for loop but not sure it is good idea to do this.我在想我们可以在 for 循环中执行此操作,但不确定这样做是否是个好主意。 I bet there must be efficient and elegant way of doing this.我敢打赌,必须有一种高效而优雅的方式来做到这一点。 Can anyone point me out how to achieve this as I expected?谁能指出我如何按预期实现这一目标? Any idea?任何想法?

You cannot run the models in parallel, unless you would:您不能并行运行模型,除非您愿意:

  1. Have one trained on CPU and the other on GPU.让一个在 CPU 上训练,另一个在 GPU 上训练。 This will still represent a problem since some pre-processing operations are still done on CPU and you may very well run into a bottleneck.这仍然是一个问题,因为一些预处理操作仍然在 CPU 上完成,您很可能会遇到瓶颈。
  2. You have two GPUs and you train one on the first GPU and the second model on the second GPU.你有两个 GPU,你在第一个 GPU 上训练一个,第二个 model 在第二个 GPU 上训练一个。

The solution to your problem is to train sequentially, serialize the history objects(use pickle), and then load both history objects and plot on the same plot.您的问题的解决方案是按顺序训练,序列化历史对象(使用 pickle),然后在同一个 plot 上加载历史对象和 plot。

PS: You must use softmax and categorical_crossentropy for multi-class classification, instead of sigmoid and binary_crossentropy . PS:您必须使用softmaxcategorical_crossentropy进行多类分类,而不是sigmoidbinary_crossentropy

(1) For using pickle: (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) For enabling specific GPUs to be "seen" by the python process. (2) 使 python 进程能够“看到”特定的 GPU。

Eg: In train_cifar_100.py例如:在 train_cifar_100.py

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

Eg In train_cifar_10.py例如在 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