Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 尝试为模型生成数据输入时发生StopIteration错误_Python_Tensorflow_Stopiteration - Fatal编程技术网

Python 尝试为模型生成数据输入时发生StopIteration错误

Python 尝试为模型生成数据输入时发生StopIteration错误,python,tensorflow,stopiteration,Python,Tensorflow,Stopiteration,给出一个错误 from __future__ import print_function import tensorflow as tf import os #Dataset Parameters - CHANGE HERE MODE = 'folder' # or 'file', if you choose a plain text file (see above). DATASET_PATH = "D:\\Downloads\\Work\\" # the datase

给出一个错误

from __future__ import print_function

import tensorflow as tf
import os

#Dataset Parameters - CHANGE HERE
MODE = 'folder' # or 'file', if you choose a plain text file (see above).
DATASET_PATH = "D:\\Downloads\\Work\\" # the dataset file or root folder path.

# Image Parameters
N_CLASSES = 7 # CHANGE HERE, total number of classes
IMG_HEIGHT = 64 # CHANGE HERE, the image height to be resized to
IMG_WIDTH = 64 # CHANGE HERE, the image width to be resized to
CHANNELS = 3 # The 3 color channels, change to 1 if grayscale

# Reading the dataset
# 2 modes: 'file' or 'folder'
def read_images(dataset_path, mode, batch_size):
    imagepaths, labels = list(), list()
    if mode == 'file':
        # Read dataset file
        data = open(dataset_path, 'r').read().splitlines()
        for d in data:
            imagepaths.append(d.split(' ')[0])
            labels.append(int(d.split(' ')[1]))
    elif mode == 'folder':
        # An ID will be affected to each sub-folders by alphabetical order
        label = 0
        # List the directory
        #try:  # Python 2
        classes = next(os.walk(dataset_path))[1]
        #except Exception:  # Python 3
        #    classes = sorted(os.walk(dataset_path).__next__()[1])
        # List each sub-directory (the classes)
        for c in classes:
            c_dir = os.path.join(dataset_path, c)
            try:  # Python 2
                walk = os.walk(c_dir).next()
            except Exception:  # Python 3
                walk = os.walk(c_dir).__next__()
            # Add each image to the training set
            for sample in walk[2]:
                # Only keeps jpeg images
                if sample.endswith('.bmp'):
                    imagepaths.append(os.path.join(c_dir, sample))
                    labels.append(label)
            label += 1
    else:
        raise Exception("Unknown mode.")

    # Convert to Tensor
    imagepaths = tf.convert_to_tensor(imagepaths, dtype=tf.string)
    labels = tf.convert_to_tensor(labels, dtype=tf.int32)
    # Build a TF Queue, shuffle data
    image, label = tf.train.slice_input_producer([imagepaths, labels],
                                                 shuffle=True)

    # Read images from disk
    image = tf.read_file(image)
    image = tf.image.decode_jpeg(image, channels=CHANNELS)

    # Resize images to a common size
    image = tf.image.resize_images(image, [IMG_HEIGHT, IMG_WIDTH])

    # Normalize
    image = image * 1.0/127.5 - 1.0

    # Create batches
    X, Y = tf.train.batch([image, label], batch_size=batch_size,
                          capacity=batch_size * 8,
                          num_threads=4)

    return X, Y

# Parameters
learning_rate = 0.001
num_steps = 10000
batch_size = 32
display_step = 100

# Network Parameters
dropout = 0.75 # Dropout, probability to keep units

# Build the data input
X, Y = read_images(DATASET_PATH, MODE, batch_size)
StopIteration回溯(最近一次调用)
在()
9
10#建立数据输入
--->11 X,Y=读取图像(数据集路径、模式、批量大小)
在读取图像中(数据集路径、模式、批次大小)
14#列出目录
15#try:#Python 2
--->16类=下一个(os.walk(数据集路径))[1]
17#例外情况除外:#Python 3
18#classes=已排序(os.walk(数据集路径)。uuuu next_uuu()[1])
停止迭代:
我查看了next()的文档,发现您不能再使用as.next,但经过更正后,它仍然给了我StopIteration错误
我在本地Python上检查了类的值,它给了我一个列表['Class0','Class1','Class2','Class3','Class4','Class5','Class6']

StopIteration
意味着iterable是空的,在这样的情况下也会得到它:

下一步(iter([])) 回溯(最近一次呼叫最后一次): 文件“”,第1行,在 停止迭代
很可能您提供的路径不存在。

os.walk返回一个迭代器,还有一个棘手的细节:python本身使用异常,特别是
StopIteration
来表示迭代器已被完全使用

在您的示例中,我猜您尝试遍历的目录可能是空的

我认为您可能应该使用
os.listdir
来列出目录的内容


我使用本指南来帮助我欢迎Stackoverflow。您可能想检查一下:并相应地编辑您的问题,因为它似乎包含很多代码,乍一看,这些代码可能并不都与您的问题相关。这将帮助别人解决你的问题。
StopIteration                             Traceback (most recent call last)
<ipython-input-27-510f945ab86c> in <module>()
      9 
     10 # Build the data input
---> 11 X, Y = read_images(DATASET_PATH, MODE, batch_size)

<ipython-input-26-c715e653cf59> in read_images(dataset_path, mode, batch_size)
     14         # List the directory
     15         #try:  # Python 2
---> 16         classes = next(os.walk(dataset_path))[1]
     17         #except Exception:  # Python 3
     18         #    classes = sorted(os.walk(dataset_path).__next__()[1])

StopIteration: