从Udacity深度学习课程运行代码时发生异常错误?

时间:2018-11-07 02:34:58

标签: numpy tensorflow deep-learning computer-vision mnist

我正在尝试执行针对Udacity深度学习课程的问题1给出的代码,我不断获得Exception: Many few images that expected: 0 < 45000

这是我尝试执行的代码:

image_size = 28 #28x28
pixel_depth = 255.0 #pixel intensity

def load_letter(folder, min_num_images):
    """Load the data for a single letter label"""
    image_files = os.listdir(folder)
    dataset = np.ndarray(shape=(len(image_files), image_size, image_size), dtype=np.float32)
    print(folder)
    num_images = 0
    for image in image_files:
        image_file = os.path.join(folder, image)
        try:
            image_data = (imageio.imread(image_file).astype(float) - pixel_depth / 2) / pixel_depth
            if image_data.shape != (image_size, image_size):
                raise Exception('Unexpected image shapes: %s' % str(image_data.shape))
                dataset[num_images, :, :] = image_data
                num_images = num_images + 1
        except (IOError, ValueError) as e:
            print('Could not read:', image_file, ':', e, ' -it\s ok, skipping.')


    dataset = dataset[0:num_images, :, :]
    if num_images < min_num_images:
        raise Exception('Many few images that expected: %d < %d' %(num_images, min_num_images))

    print('Full dataset tensor:', dataset.shape)
    print('Mean: ', np.mean(dataset))
    print('Standard deviation: ', np.std(dataset))
    return dataset


def maybe_pickle(data_folders, min_num_images_per_class, force=False):
    dataset_names = []
    for folder in data_folders:
        set_filename = folder + '.pickle'
        dataset_names.append(set_filename)
        if os.path.exists(set_filename) and not force==True:
            print('% already present - Skipping pickling.' % set_filename)
        else:
            print('Pickling %s.' % set_filename)
            dataset = load_letter(folder, min_num_images_per_class)
            try:
                with open(set_filename, 'wb') as f:
                    pickle.dump(dataset, f, pickle.HIGHEST_PROTOCOL)
            except Exception as e:
                print('Unable to save data to', set_filename, ':', e)

    return dataset_names


train_datasets = maybe_pickle(train_folders, 45000)
test_datasets = maybe_pickle(test_folders, 1800)

这是我不断得到的错误:

Pickling ./notMNIST_large/A.pickle.
./notMNIST_large/A
Could not read: ./notMNIST_large/A/Um9tYW5hIEJvbGQucGZi.png : Could not find a format to read the specified file in mode 'i'  -it\s ok, skipping.
Could not read: ./notMNIST_large/A/RnJlaWdodERpc3BCb29rSXRhbGljLnR0Zg==.png : Could not find a format to read the specified file in mode 'i'  -it\s ok, skipping.
Could not read: ./notMNIST_large/A/SG90IE11c3RhcmQgQlROIFBvc3Rlci50dGY=.png : Could not find a format to read the specified file in mode 'i'  -it\s ok, skipping.
---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
<ipython-input-52-7fc6ba87fcaf> in <module>
     49 
     50 
---> 51 train_datasets = maybe_pickle(train_folders, 45000)
     52 test_datasets = maybe_pickle(test_folders, 1800)
     53 

<ipython-input-52-7fc6ba87fcaf> in maybe_pickle(data_folders, min_num_images_per_class, force)
     39         else:
     40             print('Pickling %s.' % set_filename)
---> 41             dataset = load_letter(folder, min_num_images_per_class)
     42             try:
     43                 with open(set_filename, 'wb') as f:

<ipython-input-52-7fc6ba87fcaf> in load_letter(folder, min_num_images)
     22     dataset = dataset[0:num_images, :, :]
     23     if num_images < min_num_images:
---> 24         raise Exception('Many few images that expected: %d < %d' %(num_images, min_num_images))
     25 
     26     print('Full dataset tensor:', dataset.shape)

Exception: Many few images that expected: 0 < 45000

关于这些代码的代码可以正确执行,没有任何错误。

这段代码有什么问题。我没有调整过我刚才写的任何想法。

0 个答案:

没有答案