简体   繁体   English

Python:系统找不到指定的路径:“数据”

[英]Python: The system cannot find the path specified: 'data'

I'm building a CNN using Python I have a folder of pictures for classification stored in D//Files directory however an exception keeps poping 我正在使用Python构建CNN,我在D // Files目录中存储了一个用于分类的图片文件夹,但不断弹出异常

code: 码:

from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.layers.convolutional import Convolution2D, MaxPooling2D
from keras.utils import np_utils
from keras.preprocessing.image import  img_to_array



import numpy as np

# Image manipulations and arranging data
import os
from PIL import Image
import theano
theano.config.optimizer="None"

from sklearn.cross_validation import train_test_split
os.chdir("D:/File");

# input image dimensions
m,n = 50,50

path1="input";
path2="data";

classes=os.listdir(path2)

x=[]
y=[]
for fol in classes:
    print (fol)
    imgfiles=os.listdir(path2+'\\'+fol);
    for img in imgfiles:
        im=Image.open(path2+'\\'+fol+'\\'+img);
        im=im.convert(mode='RGB')
        imrs=im.resize((m,n))
        imrs=img_to_array(imrs)/255;
        imrs=imrs.transpose(2,0,1);
        imrs=imrs.reshape(3,m,n);
        x.append(imrs)
        y.append(fol)

x=np.array(x);
y=np.array(y);

batch_size=32
nb_classes=len(classes)
nb_epoch=20
nb_filters=32
nb_pool=2
nb_conv=3

x_train, x_test, y_train, y_test= train_test_split(x,y,test_size=0.2,random_state=4)

uniques, id_train=np.unique(y_train,return_inverse=True)
Y_train=np_utils.to_categorical(id_train,nb_classes)
uniques, id_test=np.unique(y_test,return_inverse=True)
Y_test=np_utils.to_categorical(id_test,nb_classes)

model= Sequential()
model.add(Convolution2D(nb_filters,nb_conv,nb_conv,border_mode='same',input_shape=x_train.shape[1:]))
model.add(Activation('relu'));
model.add(Convolution2D(nb_filters,nb_conv,nb_conv));
model.add(Activation('relu'));
model.add(MaxPooling2D(pool_size=(nb_pool,nb_pool)));
model.add(Dropout(0.5));
model.add(Flatten());
model.add(Dense(128));
model.add(Dropout(0.5));
model.add(Dense(nb_classes));
model.add(Activation('softmax'));
model.compile(loss='categorical_crossentropy',optimizer='adadelta',metrics=['accuracy'])


nb_epoch=5;
batch_size=5;
model.fit(x_train,Y_train,batch_size=batch_size,nb_epoch=nb_epoch,verbose=1,validation_data=(x_test, Y_test))


files=os.listdir(path1);
img=files[0] 
im = Image.open(path1 + '\\'+img);
imrs = im.resize((m,n))
imrs=img_to_array(imrs)/255;
imrs=imrs.transpose(2,0,1);
imrs=imrs.reshape(3,m,n);

x=[]
x.append(imrs)
x=np.array(x);
predictions = model.predict(x)

however this script does not run and gives me this in the consolem it seems that the path specified is not recognised (I'm using Windows with Python 3.6 and Spyder environment) 但是,此脚本无法运行,并且在控制台中提供了此提示,似乎无法识别指定的路径(我使用的是带有Python 3.6和Spyder环境的Windows)

runfile('C:/Users/Monirah/.spyder-py3/semi1.py', 
wdir='C:/Users/Monirah/.spyder-py3')
Traceback (most recent call last):

File "<ipython-input-13-144f8465de97>", line 1, in <module>
runfile('C:/Users/Monirah/.spyder-py3/semi1.py', 
wdir='C:/Users/Monirah/.spyder-py3')

File "C:\Users\Monirah\Anaconda3\lib\site-
packages\spyder\utils\site\sitecustomize.py", line 710, in runfile
execfile(filename, namespace)

File "C:\Users\Monirah\Anaconda3\lib\site-
packages\spyder\utils\site\sitecustomize.py", line 101, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Users/Monirah/.spyder-py3/semi1.py", line 26, in <module>
classes=os.listdir(path2)

FileNotFoundError: [WinError 3] The system cannot find the path specified: 
'data'

使用debug或先打印出程序中使用的所有路径,然后检查它们是否有效。

  1. Use "\\" in windows for paths Ex: c:\\mydir 在Windows中使用“ \\”作为路径,例如:c:\\ mydir
  2. After os.chdir("D:/File"); os.chdir("D:/File"); , do print os.getcwd() & check whether current directory is same as the path used in os.chdir() ,请print os.getcwd()并检查当前目录是否与os.chdir()使用的路径相同
  3. Now check whether print os.path.exists(path2) exists in your current directory. 现在检查当前目录中是否存在print os.path.exists(path2)

Note: Always use os.path.join() for file path manipulations. 注意:始终使用os.path.join()进行文件路径操作。 This works well irrespective of the underlying OS. 无论底层操作系统如何,此方法都可以正常工作。

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

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