mnist可视化时的FileNotFoundError错误 80C

import keras
import numpy as np
from keras.datasets import mnist
from keras.models import load_model
from matplotlib import pyplot as plt

from keras.models import Sequential,Model
from keras.layers import Dense,Dropout,Flatten,Activation,Input
from keras.layers import Conv2D,MaxPooling2D

from vis.visualization import visualize_saliency
from vis.utils import utils
from keras import activations

#加载数据及定义格式
batch_size = 128
num_classes = 10
epochs = 5

img_rows, img_cols = 28, 28

(x_train, y_train), (x_test, y_test) = mnist.load_data()

x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
input_shape = (img_rows, img_cols, 1)

x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')

y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)

#建立DNN模型
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),
activation='relu',
input_shape=input_shape))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax', name='preds'))

model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adam(),
metrics=['accuracy'])

model.fit(x_train, y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(x_test, y_test))

#开始显著图的可视化(saliency visualization)
#找出第一张手写体0的下标
class_idx=0
indices=np.where(y_test[:,class_idx]==1.)[0]
idx=indices[0]
#找出名字叫preds的layer,并返回它的下标
layer_idx=utils.find_layer_idx(model,'preds')

#将找到对应下标的layer的activation从softmax变成linear
model.layers[layer_idx].activation=activations.linear
model = utils.apply_modifications(model)

#求出x_test中属于某类的某个特定图像在某个layer的heatmap
for modifier in ['guided','relu']:
grads=visualize_saliency(model,layer_idx,filter_indices=class_idx,seed_input=x_test[idx],backprop_modifier=modifier)
plt.figure()
plt.title(modifier)
#以'jet'colormap的方式可视化一张heatmap
plt.imshow(grads, cmap='jet')

    报错:
    执行到model = utils.apply_modifications(model)时报错
    错误:FileNotFoundError: [WinError 3] 系统找不到指定的路径。: '/tmp/cv86obbj.h5'

1个回答

你的运行平台是Linux吗?如果是是否Authorize root去执行的程序?如果没有那肯定会包那个错,因为apply_modifications接口会自动创建一个临时文件去保存修改,但是通常这个临时文件的路径都是需要管理员权限才可访问的地方,所以解决方法有两个:
1. 以管理员权限执行程序
2. 设置tempfile.tempdir的值为一个不用管理员权限才可访问的路径,或者设置TMPDIR,TEMP,TMP其中任何一个环境变量即可。

pdl123fgh
pdl123fgh 回复weixin_38420945: 我也是这个错误,请问你解决了吗?
大约 2 个月之前 回复
yxhlfx
双林子木 还是报错?还是报文件不存在?
2 年多之前 回复
weixin_38420945
weixin_38420945 我是win10的系统,然后按照您的意见改变了路径,把路径转到了D盘下,但还是报错
2 年多之前 回复
weixin_38420945
weixin_38420945 def apply_mod(model,custom_objects=None): #导入包 import os import tempfile from keras.models import load_model #正式执行文件 print(tempfile._get_candidate_names()) model_path=os.path.join('D:/软件集合/Keras Code/T技术类文件/显著图saliency map/temp',next(tempfile._get_candidate_names())+'.h5') print(model_path) try: model.save(model_path) print(model_path) return load_model(model_path,custom_objects=custom_objects) finally: os.remove(model_path)
2 年多之前 回复
weixin_38420945
weixin_38420945 from keras.models import load_model from vis.utils import utils from keras import activations model=load_model('model.h5') layer_idx=utils.find_layer_idx(model,'preds') model.layers[layer_idx].activation=activations.linear #model = utils.apply_modifications(model) from apply_mod import apply_mod model=apply_mod(model)
2 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问