我正在运行Keras神经网络模型以对图像进行二进制分类。
我使用了预训练的VGG16模型的第一层,并从教程中创建了最后一个完全连接的层:
https://blog.keras.io/building-powerful-image-classification-models-using-very-little-data.html
使用Tensorflow后端2.3.1,Python 3.6,Keras 2.4.3
当我使用ImageDataGenerator训练模型(使用预先保存的权重)时,会发生以下异常:
AttributeError: 'NoneType' object has no attribute 'shape'
那是我的代码
top_model_weight_path = 'feat_extr_model.h5'
train_data_dir = 'data/train'
validation_data_dir = 'data/validation'
nb_train_samples = 54
nb_validation_samples = 6
epochs = 10
batch_size = 16
base_model = VGG16(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
top_model = Sequential()
top_model.add(Flatten(input_shape=base_model.output_shape[1:]))
top_model.add(Dense(256, activation='relu'))
top_model.add(Dropout(0.5))
top_model.add(Dense(1, activation='sigmoid'))
# note that it is necessary to start with a fully-trained
# classifier, including the top classifier,
# in order to successfully do fine-tuning
top_model.load_weights(top_model_weight_path)
model = Model(inputs=base_model.input, outputs=top_model(base_model.output))
for l in model.layers[:15]:
l.trainable = False
model.compile(loss='binary_crossentropy',
optimizer=optimizers.SGD(lr=1e-4, momentum=0.9),
metrics=['accuracy'])
train_datagen = ImageDataGenerator(rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)
test_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory(train_data_dir,
target_size=(224, 224),
batch_size=batch_size,
class_mode=None)
validation_generator = test_datagen.flow_from_directory(validation_data_dir,
target_size=(224,224),
batch_size=batch_size,
class_mode=None)
model.summary()
# fine-tune the model
model.fit_generator(
train_generator,
epochs=epochs,
validation_data=validation_generator,
verbose=2)
完整的错误消息如下:
Traceback (most recent call last):
File "C:/Users/Luca Mancini/Desktop/Python Project/tutorialPretrained/tutorial.py", line 226, in <module>
verbose=2)
File "C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\util\deprecation.py", line 324, in new_func
return func(*args, **kwargs)
File "C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1829, in fit_generator
initial_epoch=initial_epoch)
File "C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\keras\engine\training.py", line 108, in _method_wrapper
return method(self, *args, **kwargs)
File "C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1098, in fit
tmp_logs = train_function(iterator)
File "C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\eager\def_function.py", line 780, in __call__
result = self._call(*args, **kwds)
File "C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\eager\def_function.py", line 823, in _call
self._initialize(args, kwds, add_initializers_to=initializers)
File "C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\eager\def_function.py", line 697, in _initialize
*args, **kwds))
File "C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\eager\function.py", line 2855, in _get_concrete_function_internal_garbage_collected
graph_function, _, _ = self._maybe_define_function(args, kwargs)
File "C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\eager\function.py", line 3213, in _maybe_define_function
graph_function = self._create_graph_function(args, kwargs)
File "C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\eager\function.py", line 3075, in _create_graph_function
capture_by_value=self._capture_by_value),
File "C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\framework\func_graph.py", line 986, in func_graph_from_py_func
func_outputs = python_func(*func_args, **func_kwargs)
File "C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\eager\def_function.py", line 600, in wrapped_fn
return weak_wrapped_fn().__wrapped__(*args, **kwds)
File "C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\framework\func_graph.py", line 973, in wrapper
raise e.ag_error_metadata.to_exception(e)
AttributeError: in user code:
C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\keras\engine\training.py:806 train_function *
return step_function(self, iterator)
C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\keras\engine\training.py:796 step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:1211 run
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2585 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2945 _call_for_each_replica
return fn(*args, **kwargs)
C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\keras\engine\training.py:789 run_step **
outputs = model.train_step(data)
C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\keras\engine\training.py:759 train_step
self.compiled_metrics.update_state(y, y_pred, sample_weight)
C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\keras\engine\compile_utils.py:388 update_state
self.build(y_pred, y_true)
C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\keras\engine\compile_utils.py:319 build
self._metrics, y_true, y_pred)
C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\util\nest.py:1139 map_structure_up_to
**kwargs)
C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\util\nest.py:1235 map_structure_with_tuple_paths_up_to
*flat_value_lists)]
C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\util\nest.py:1234 <listcomp>
results = [func(*args, **kwargs) for args in zip(flat_path_list,
C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\util\nest.py:1137 <lambda>
lambda _, *values: func(*values), # Discards the path arg.
C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\keras\engine\compile_utils.py:419 _get_metric_objects
return [self._get_metric_object(m, y_t, y_p) for m in metrics]
C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\keras\engine\compile_utils.py:419 <listcomp>
return [self._get_metric_object(m, y_t, y_p) for m in metrics]
C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\keras\engine\compile_utils.py:440 _get_metric_object
y_t_rank = len(y_t.shape.as_list())
AttributeError: 'NoneType' object has no attribute 'shape'
有人可以向我解释问题出在哪里,我该如何解决? 谢谢
答案 0 :(得分:0)
错误在这里:
testdb=# select 'public.t_toto3' LIKE 't?_%' ESCAPE '?';
?column?
----------
f
(1 row)
testdb=# select parse_ident('public.t_toto3');
parse_ident
------------------
{public,t_toto3}
(1 row)
testdb=# select (parse_ident('public.t_toto3'))[2] LIKE 't?_%' ESCAPE '?';
?column?
----------
t
(1 row)
来自docs:
class_mode :“分类”,“二进制”,“稀疏”,“输入”或“无”之一。默认值:“类别”。确定要返回的标签数组的类型:-“类别”将是2D一热编码标签,-“二进制”将是1D二进制标签,“稀疏”将是1D整数标签,-“输入”将是图像相同输入图像(主要用于自动编码器)。 -如果为None,则不返回任何标签(生成器将仅生成一批图像数据,这对于与model.predict()结合使用很有用)。请注意,在class_mode为None的情况下,数据仍需要驻留在目录的子目录中才能正常工作。
您没有给模型添加任何标签。您似乎有2个班级,所以应该是:
class_mode=None