pythonは初心者なので、困っています。
以下はプログラムの一部なのですがこの部分がうまく通りません
path = '/data/0deg_30deg_0811/sleep1' # ディレクトリ毎にファイルが何個あるか調べる for root, dirs, files in os.walk(path): print 'Directory: %s' % root nsubFiles = 0 for f in files: #subdir = root.split('/') #print '%d' % int(subdir[-1]) r,e = os.path.splitext(f) if e.lower() == '.png': #print '%s' % root+'/'+f #img = cv2.imread(root+'/'+f) #cv2.imshow('result', img) #cv2.waitKey(0) nFiles += 1 nsubFiles += 1 if nsubFiles != 0: print '%d files found in %s.' % (nsubFiles, root.split('/')[-1]) print 'Total: %d files found in %s.' % (nFiles, path) # Initialization # # 訓練画像のサイズ height = 80 width = 40 # height = 60 # width = 30 # # height = 48 # width = 48 # ラベルを入れるための配列 target = np.zeros(nFiles, dtype=np.int) # 訓練画像を入れるための配列 images = np.zeros((nFiles, height, width), dtype=np.float32) # ディレクトリを辿ってラベル、画像を格納する nFiles = 0 for root, dirs, files in os.walk(path): # print 'Directory: %s' % root subdir = root.split('/') if subdir[-1] == 'sleep1': continue nsubFiles = 0 for f in files: #print '%d' % int(subdir[-1]) target[nFiles] = int(subdir[-1])-1 r,e = os.path.splitext(f) if e.lower() == '.png': # print '%s' % root+'/'+f img = imresize(imread(root+'/'+f), (height,width), interp='bilinear') image = np.asarray(img, dtype=np.float32) image /= 255.0 # scale uint8 coded colors to the [0.0, 1.0] floats images[nFiles, ...] = image nFiles += 1 nsubFiles += 1 if nsubFiles != 0: print '%d files found in %s.' % (nsubFiles, root.split('/')[-1]) print 'Total: %d files found in %s.' % (nFiles, path)
実行すると
Directory: /data/0deg_30deg_0811/sleep1
1000 files found in sleep1.
Total: 1000 files found in /data/0deg_30deg_0811/sleep1.
Total: 0 files found in /data/0deg_30deg_0811/sleep1.
Traceback (most recent call last):
File "C:\python\NN\mlp_cg.py", line 242, in <module>
data=images.reshape(len(images), -1)
ValueError: total size of new array must be unchanged
と怒られます。そもそもなぜ一つ目のtotalはフォルダの数を数えてくれているのに、次のtotalは0何でしょか。
また
target[nFiles] = int(subdir[-1])-1
は何を示しているんでしょうか
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/12/19 01:27