Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

运行gap.ipynb出现错误AttributeError: 'DirectoryIterator' object has no attribute 'nb_sample' #21

Open
xiezhiepng opened this issue Jul 23, 2019 · 13 comments

Comments

@xiezhiepng
Copy link

AttributeError: 'DirectoryIterator' object has no attribute 'nb_sample'

@ypwhs
Copy link
Owner

ypwhs commented Jul 23, 2019

Keras 新版更新了API,可以直接 predict_generator,你可以去掉第二个参数 。

@xiezhiepng
Copy link
Author

xiezhiepng commented Jul 23, 2019

去掉第二个参数出现
TypeError: predict_generator() missing 1 required positional argument: 'val_samples'
我安装的版本是
Keras (1.2.2)
Keras-Applications (1.0.8)
Keras-Preprocessing (1.1.0)

代码是一样的
from keras.models import *
from keras.layers import *
from keras.applications import *
from keras.preprocessing.image import *
import h5py

def write_gap(MODEL, image_size, lambda_func=None):
width = image_size[0]
height = image_size[1]
input_tensor = Input((height, width, 3))
x = input_tensor
if lambda_func:
x = Lambda(lambda_func)(x)

base_model = MODEL(input_tensor=x, weights='imagenet', include_top=False)
model = Model(base_model.input, GlobalAveragePooling2D()(base_model.output))

gen = ImageDataGenerator()
train_generator = gen.flow_from_directory("train2", image_size, shuffle=False, 
                                          batch_size=16)
test_generator = gen.flow_from_directory("test2", image_size, shuffle=False, 
                                         batch_size=16, class_mode=None)
#修改:去掉第二个参数
train = model.predict_generator(train_generator)#, train_generator.nb_sample
test = model.predict_generator(test_generator)#, test_generator.nb_sample
with h5py.File("gap_%s.h5"%MODEL.func_name) as h:
    h.create_dataset("train", data=train)
    h.create_dataset("test", data=test)
    h.create_dataset("label", data=train_generator.classes)

write_gap(ResNet50, (224, 224))
write_gap(InceptionV3, (299, 299), inception_v3.preprocess_input)
write_gap(Xception, (299, 299), xception.preprocess_input)

@ypwhs
Copy link
Owner

ypwhs commented Jul 23, 2019

1.2.2 的话,可以传 generator.n

@ypwhs
Copy link
Owner

ypwhs commented Jul 23, 2019

这个代码的版本比较旧了,之后我找时间更新一下

@xiezhiepng
Copy link
Author

xiezhiepng commented Jul 23, 2019 via email

@raoquanquan
Copy link

AttributeError: 'DirectoryIterator' object has no attribute 'nb_sample'

我也出现你这个问题,但是我把keras版本降低为1.2.2后就没出现这个错误了。

@xiezhiepng
Copy link
Author

AttributeError: 'DirectoryIterator' object has no attribute 'nb_sample'

我也出现你这个问题,但是我把keras版本降低为1.2.2后就没出现这个错误了。

你运行都没问题吗?

@raoquanquan
Copy link

AttributeError: 'DirectoryIterator' object has no attribute 'nb_sample'

我也出现你这个问题,但是我把keras版本降

AttributeError: 'DirectoryIterator' object has no attribute 'nb_sample'

我也出现你这个问题,但是我把keras版本降低为1.2.2后就没出现这个错误了。

你运行都没问题吗?

你还出现什么问题?还有一两个地方好像有问题需要改。

@xiezhiepng
Copy link
Author

AttributeError: 'DirectoryIterator' object has no attribute 'nb_sample'

我也出现你这个问题,但是我把keras版本降

AttributeError: 'DirectoryIterator' object has no attribute 'nb_sample'

我也出现你这个问题,但是我把keras版本降低为1.2.2后就没出现这个错误了。

你运行都没问题吗?

你还出现什么问题?还有一两个地方好像有问题需要改。

我切换了1.2.2之后运行gap.ipynb文件出现Error when checking : data should be a Numpy array, or list/dict of Numpy arrays. Found: None...

@xiezhiepng
Copy link
Author

AttributeError: 'DirectoryIterator' object has no attribute 'nb_sample'

我也出现你这个问题,但是我把keras版本降

AttributeError: 'DirectoryIterator' object has no attribute 'nb_sample'

我也出现你这个问题,但是我把keras版本降低为1.2.2后就没出现这个错误了。

你运行都没问题吗?

你还出现什么问题?还有一两个地方好像有问题需要改。

我切换了1.2.2之后运行gap.ipynb文件出现Error when checking : data should be a Numpy array, or list/dict of Numpy arrays. Found: None...

是哪两个地方需要改,能帮忙说一下吗?keras新手都没弄过这个。感谢🙏

@raoquanquan
Copy link

AttributeError: 'DirectoryIterator' object has no attribute 'nb_sample'

我也出现你这个问题,但是我把keras版本降

AttributeError: 'DirectoryIterator' object has no attribute 'nb_sample'

我也出现你这个问题,但是我把keras版本降低为1.2.2后就没出现这个错误了。

你运行都没问题吗?

你还出现什么问题?还有一两个地方好像有问题需要改。

我切换了1.2.2之后运行gap.ipynb文件出现Error when checking : data should be a Numpy array, or list/dict of Numpy arrays. Found: None...

是哪两个地方需要改,能帮忙说一下吗?keras新手都没弄过这个。感谢🙏

兄弟,我也是新手,你这个问题翻译过来就是你要用 .shape得是Numpy array格式的数据,不能是list/dict(列表或者字典)。这个不是代码的问题,是作者把代码分割成几部分了,他之前用的jupyter notebook所以没问题,但是我们直接 python运行代码就会有问题。你把作者代码整合一下把特征向量提取、特征向量导入,模型搭建和可视化、训练模型(除了‘’预测结果‘’之外,因为这个部分也有些问题,是版本的问题,我还没改过来,你改好了可以告诉我)放在一个python文件里,然后运行。这样你前面的结果就都可以了出来了。

@Monkeone
Copy link

我出现Traceback (most recent call last):
File "gap.py", line 29, in
write_gap(ResNet50, (224, 224))
File "gap.py", line 23, in write_gap
train = model.predict_generator(train_generator)
File "D:\新建文件夹\python\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1298, in predict_generator
callbacks=callbacks)
File "D:\新建文件夹\python\lib\site-packages\tensorflow\python\keras\engine\training_generator.py", line 220, in model_iteration
batch_data = _get_next_batch(generator, mode)
File "D:\新建文件夹\python\lib\site-packages\tensorflow\python\keras\engine\training_generator.py", line 362, in _get_next_batch
generator_output = next(generator)
File "D:\新建文件夹\python\lib\site-packages\tensorflow\python\keras\utils\data_utils.py", line 785, in get
six.reraise(*sys.exc_info())
File "D:\新建文件夹\python\lib\site-packages\six.py", line 693, in reraise
raise value
File "D:\新建文件夹\python\lib\site-packages\tensorflow\python\keras\utils\data_utils.py", line 779, in get
inputs = self.queue.get(block=True).get()
File "D:\新建文件夹\python\lib\multiprocessing\pool.py", line 657, in get
raise self._value
File "D:\新建文件夹\python\lib\multiprocessing\pool.py", line 121, in worker
result = (True, func(*args, **kwds))
File "D:\新建文件夹\python\lib\site-packages\tensorflow\python\keras\utils\data_utils.py", line 571, in get_index
return _SHARED_SEQUENCES[uid][i]
File "D:\新建文件夹\python\lib\site-packages\keras_preprocessing\image\iterator.py", line 65, in getitem
return self._get_batches_of_transformed_samples(index_array)
File "D:\新建文件夹\python\lib\site-packages\keras_preprocessing\image\iterator.py", line 226, in _get_batches_of_transformed_samples
interpolation=self.interpolation)
File "D:\新建文件夹\python\lib\site-packages\keras_preprocessing\image\utils.py", line 104, in load_img
img = pil_image.open(path)
File "D:\新建文件夹\python\lib\site-packages\PIL\Image.py", line 2770, in open
fp = builtins.open(filename, "rb")
OSError: [Errno 22] Invalid argument: 'train2\cat\cat.1.jpg'

@ypwhs
Copy link
Owner

ypwhs commented Aug 31, 2019

我出现Traceback (most recent call last):
File "gap.py", line 29, in
write_gap(ResNet50, (224, 224))
File "gap.py", line 23, in write_gap
train = model.predict_generator(train_generator)
File "D:\新建文件夹\python\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1298, in predict_generator
callbacks=callbacks)
File "D:\新建文件夹\python\lib\site-packages\tensorflow\python\keras\engine\training_generator.py", line 220, in model_iteration
batch_data = _get_next_batch(generator, mode)
File "D:\新建文件夹\python\lib\site-packages\tensorflow\python\keras\engine\training_generator.py", line 362, in _get_next_batch
generator_output = next(generator)
File "D:\新建文件夹\python\lib\site-packages\tensorflow\python\keras\utils\data_utils.py", line 785, in get
six.reraise(*sys.exc_info())
File "D:\新建文件夹\python\lib\site-packages\six.py", line 693, in reraise
raise value
File "D:\新建文件夹\python\lib\site-packages\tensorflow\python\keras\utils\data_utils.py", line 779, in get
inputs = self.queue.get(block=True).get()
File "D:\新建文件夹\python\lib\multiprocessing\pool.py", line 657, in get
raise self._value
File "D:\新建文件夹\python\lib\multiprocessing\pool.py", line 121, in worker
result = (True, func(*args, **kwds))
File "D:\新建文件夹\python\lib\site-packages\tensorflow\python\keras\utils\data_utils.py", line 571, in get_index
return _SHARED_SEQUENCES[uid][i]
File "D:\新建文件夹\python\lib\site-packages\keras_preprocessing\image\iterator.py", line 65, in getitem
return self._get_batches_of_transformed_samples(index_array)
File "D:\新建文件夹\python\lib\site-packages\keras_preprocessing\image\iterator.py", line 226, in _get_batches_of_transformed_samples
interpolation=self.interpolation)
File "D:\新建文件夹\python\lib\site-packages\keras_preprocessing\image\utils.py", line 104, in load_img
img = pil_image.open(path)
File "D:\新建文件夹\python\lib\site-packages\PIL\Image.py", line 2770, in open
fp = builtins.open(filename, "rb")
OSError: [Errno 22] Invalid argument: 'train2\cat\cat.1.jpg'

你可以尝试使用 Ubuntu,或者你也可以尝试对 Windows 进行适配。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants