用theano实现绘制ResNet的ROC曲线
时间: 2024-04-25 09:15:03 浏览: 146
首先,我们需要训练一个ResNet模型并得到其预测结果。然后,我们可以使用sklearn库中的roc_curve函数来计算ROC曲线,并使用matplotlib库来绘制ROC曲线。
下面是一个使用theano库实现绘制ResNet的ROC曲线的示例代码:
```python
import numpy as np
import theano
import theano.tensor as T
import lasagne
from sklearn.metrics import roc_curve, auc
import matplotlib.pyplot as plt
# 定义ResNet模型
def build_resnet(input_var=None):
l_in = lasagne.layers.InputLayer(shape=(None, 3, 32, 32), input_var=input_var)
l_conv1 = lasagne.layers.Conv2DLayer(l_in, num_filters=16, filter_size=(3, 3), stride=(1, 1),
pad='same', nonlinearity=lasagne.nonlinearities.rectify,
W=lasagne.init.HeNormal(gain='relu'))
l_bn1 = lasagne.layers.BatchNormLayer(l_conv1)
l_conv2 = lasagne.layers.Conv2DLayer(l_bn1, num_filters=16, filter_size=(3, 3), stride=(1, 1),
pad='same', nonlinearity=lasagne.nonlinearities.rectify,
W=lasagne.init.HeNormal(gain='relu'))
l_bn2 = lasagne.layers.BatchNormLayer(l_conv2)
l_pool1 = lasagne.layers.Pool2DLayer(l_bn2, pool_size=(2, 2), mode='max')
l_res1 = lasagne.layers.ConcatLayer([l_pool1, l_pool1])
l_conv3 = lasagne.layers.Conv2DLayer(l_res1, num_filters=32, filter_size=(3, 3), stride=(1, 1),
pad='same', nonlinearity=lasagne.nonlinearities.rectify,
W=lasagne.init.HeNormal(gain='relu'))
l_bn3 = lasagne.layers.BatchNormLayer(l_conv3)
l_conv4 = lasagne.layers.Conv2DLayer(l_bn3, num_filters=32, filter_size=(3, 3), stride=(1, 1),
pad='same', nonlinearity=lasagne.nonlinearities.rectify,
W=lasagne.init.HeNormal(gain='relu'))
l_bn4 = lasagne.layers.BatchNormLayer(l_conv4)
l_pool2 = lasagne.layers.Pool2DLayer(l_bn4, pool_size=(2, 2), mode='max')
l_res2 = lasagne.layers.ConcatLayer([l_pool2, l_pool2])
l_conv5 = lasagne.layers.Conv2DLayer(l_res2, num_filters=64, filter_size=(3, 3), stride=(1, 1),
pad='same', nonlinearity=lasagne.nonlinearities.rectify,
W=lasagne.init.HeNormal(gain='relu'))
l_bn5 = lasagne.layers.BatchNormLayer(l_conv5)
l_conv6 = lasagne.layers.Conv2DLayer(l_bn5, num_filters=64, filter_size=(3, 3), stride=(1, 1),
pad='same', nonlinearity=lasagne.nonlinearities.rectify,
W=lasagne.init.HeNormal(gain='relu'))
l_bn6 = lasagne.layers.BatchNormLayer(l_conv6)
l_pool3 = lasagne.layers.Pool2DLayer(l_bn6, pool_size=(2, 2), mode='max')
l_flat = lasagne.layers.FlattenLayer(l_pool3)
l_hidden = lasagne.layers.DenseLayer(l_flat, num_units=128, nonlinearity=lasagne.nonlinearities.rectify,
W=lasagne.init.HeNormal(gain='relu'))
l_drop = lasagne.layers.DropoutLayer(l_hidden, p=0.5)
l_out = lasagne.layers.DenseLayer(l_drop, num_units=10, nonlinearity=lasagne.nonlinearities.softmax)
return l_out
# 加载数据集
def load_dataset():
from six.moves import urllib
def download(filename, source='http://yann.lecun.com/exdb/mnist/'):
print("Downloading %s" % filename)
urllib.request.urlretrieve(source + filename, filename)
import gzip
def load_mnist_images(filename):
if not os.path.exists(filename):
download(filename)
with gzip.open(filename, 'rb') as f:
data = np.frombuffer(f.read(), np.uint8, offset=16)
data = data.reshape(-1,1,28,28)
return data / np.float32(256)
def load_mnist_labels(filename):
if not os.path.exists(filename):
download(filename)
with gzip.open(filename, 'rb') as f:
data = np.frombuffer(f.read(), np.uint8, offset=8)
return data
X_train = load_mnist_images('train-images-idx3-ubyte.gz')
y_train = load_mnist_labels('train-labels-idx1-ubyte.gz')
X_test = load_mnist_images('t10k-images-idx3-ubyte.gz')
y_test = load_mnist_labels('t10k-labels-idx1-ubyte.gz')
return X_train, y_train, X_test, y_test
# 训练ResNet模型
def train_resnet():
X_train, y_train, X_test, y_test = load_dataset()
input_var = T.tensor4('inputs')
target_var = T.ivector('targets')
network = build_resnet(input_var)
prediction = lasagne.layers.get_output(network)
loss = lasagne.objectives.categorical_crossentropy(prediction, target_var)
loss = loss.mean()
params = lasagne.layers.get_all_params(network, trainable=True)
updates = lasagne.updates.nesterov_momentum(loss, params, learning_rate=0.01, momentum=0.9)
test_prediction = lasagne.layers.get_output(network, deterministic=True)
test_loss = lasagne.objectives.categorical_crossentropy(test_prediction, target_var)
test_loss = test_loss.mean()
test_acc = T.mean(T.eq(T.argmax(test_prediction, axis=1), target_var), dtype=theano.config.floatX)
train_fn = theano.function([input_var, target_var], loss, updates=updates)
val_fn = theano.function([input_var, target_var], [test_loss, test_acc])
predict_fn = theano.function([input_var], test_prediction)
print("Starting training...")
for epoch in range(10):
train_err = 0
train_batches = 0
for batch in iterate_minibatches(X_train, y_train, 500, shuffle=True):
inputs, targets = batch
train_err += train_fn(inputs, targets)
train_batches += 1
val_err = 0
val_acc = 0
val_batches = 0
for batch in iterate_minibatches(X_test, y_test, 500, shuffle=False):
inputs, targets = batch
err, acc = val_fn(inputs, targets)
val_err += err
val_acc += acc
val_batches += 1
print("Epoch {} of 10".format(epoch+1))
print(" training loss:\t\t{:.6f}".format(train_err / train_batches))
print(" validation loss:\t\t{:.6f}".format(val_err / val_batches))
print(" validation accuracy:\t\t{:.2f} %".format(val_acc / val_batches * 100))
# 得到测试集上的预测结果
y_pred = predict_fn(X_test)
return y_test, y_pred
# 计算并绘制ROC曲线
def plot_roc(y_true, y_pred):
class_names = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
n_classes = len(class_names)
# 计算ROC曲线和AUC值
fpr = dict()
tpr = dict()
roc_auc = dict()
for i in range(n_classes):
fpr[i], tpr[i], _ = roc_curve(y_true[:, i], y_pred[:, i])
roc_auc[i] = auc(fpr[i], tpr[i])
# 绘制ROC曲线
plt.figure(figsize=(8, 6))
for i in range(n_classes):
plt.plot(fpr[i], tpr[i], label='ROC curve of class %s (area = %.2f)' % (class_names[i], roc_auc[i]))
plt.plot([0, 1], [0, 1], 'k--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('ROC curves for ResNet model')
plt.legend(loc="lower right")
plt.show()
# 主函数
def main():
y_true, y_pred = train_resnet()
plot_roc(y_true, y_pred)
if __name__ == '__main__':
main()
```
需要注意的是,上面的代码仅提供了一个示例,因此可能需要根据实际情况进行修改和调整。
阅读全文