其他分享
首页 > 其他分享> > keras模型会影响输入数据的大小吗?

keras模型会影响输入数据的大小吗?

作者:互联网

我的意思是,如果将convnet模型输入n个模型,它将给出n个输出,对吗?
但是,当我尝试使用瓶颈模型(使用VGG16 convnet构建)时,VGG16 convnet返回的输出比输入数少16.

这是控制台输出:

import numpy as np

train_data = np.load(open('bottleneck_features_train.npy'))
train_data.shape
(8384, 7, 7, 512)

validation_data = np.load(open('bottleneck_features_validation.npy'))
validation_data.shape
(3584, 7, 7, 512)

可以找到生成此输出的脚本here.

上面脚本的堆栈跟踪.

Using Theano backend.
Downloading data from 07001
Found 8400 images belonging to 120 classes. Saving train features…
Found 3600 images belonging to 120 classes. Saving test features…
Training top layers…
Compiling bottleneck model…
Training bottleneck model…
Traceback (most recent call last):

File”pretrained_network.py”, line 87, in
train_top_model()

File “pretrained_network.py”, line 82, in train_top_model
validation_data=(validation_data, validation_labels))

File “/home/ashish/ml-projects/venv/local/lib/python2.7/site-packages/keras/models.py”,line 845, in fit initial_epoch=initial_epoch)

File “/home/ashish/ml-projects/venv/local/lib/python2.7/site-packages/keras/engine/training.py”,
line 1405, in fit
batch_size=batch_size)

File “/home/ashish/ml-projects/venv/local/lib/python2.7/site-packages/keras/engine/training.py”,
line 1307, in _standardize_user_data
_check_array_lengths(x, y, sample_weights)

File “/home/ashish/ml-projects/venv/local/lib/python2.7/site-packages/keras/engine/training.py”,
line 229, in _check_array_lengths
‘and ‘ + str(list(set_y)[0]) + ‘ target samples.’)

ValueError: Input arrays should have the same number of samples as target arrays.
Found 8384 input samples and 8400 target samples.

解决方法:

问题出在例如在您的脚本中:

bottleneck_features_train = model.predict_generator(
        generator, nb_train_samples // batch_size)

它应该更改为:

bottleneck_features_train = model.predict_generator(
        generator, (nb_train_samples // batch_size) + 1)

没有此功能,发生器的调用次数将不足.

标签:keras,machine-learning,neural-network,deep-learning,python
来源: https://codeday.me/bug/20191111/2020538.html