Python/Tensorflow-需要可广播的形状



我在训练模型时遇到错误。

required broadcastable shapes
[[{{node binary_crossentropy/logistic_loss/mul}}]] [Op:__inference_train_function_5643]

我试着遵循这个。

https://towardsdatascience.com/ai-for-textiles-convolutional-neural-network-based-fabric-structure-classifier-c0db5433501d

我正在使用(256*256(个图像。我正在谷歌colab中阅读来自驱动器的图像。有人能告诉我如何消除这个错误吗?

我的colab代码:

我使用Tensorflow 2.5.0,因为它适用于我的树莓派

!pip install tensorflow==2.5.0
!pip install keras==2.5.0rc0

进口下流

import numpy as np;
import keras;
import tensorflow as tf
from keras.layers import AveragePooling2D;
from keras. layers.core import Dense, Flatten;
from tensorflow.keras.optimizers import Adam
from keras.metrics import binary_crossentropy;
from keras.preprocessing.image import ImageDataGenerator;
from keras.models import Model;
from keras.applications import imagenet_utils;
from keras.callbacks import ModelCheckpoint;
from tensorflow.keras import applications
from tensorflow.keras.applications.vgg16 import VGG16
train_data_path = '/content/drive/MyDrive/dataset_two/train';
test_data_path = '/content/drive/MyDrive/dataset_two/test';
train_data = ImageDataGenerator(rescale = 1.0/255, 
rotation_range = 180, 
vertical_flip = True, 
horizontal_flip = True, 
brightness_range = [0.5, 1.5], 
zoom_range = [1, 1.5]);
train_generator = train_data.flow_from_directory(directory = train_data_path, 
target_size = (224,224), 
classes = ['Pure_SET_A','Pure_MachineConnected_SET_C','Mixed_Connected_SET_D'], 
batch_size = 70, 
shuffle = True);
test_data = ImageDataGenerator(rescale = 1.0/255);
test_generator = test_data.flow_from_directory(directory = test_data_path, target_size = (224,224), classes = ['Pure_SET_A','Pure_MachineConnected_SET_C','Mixed_Connected_SET_D'], batch_size = 50, shuffle = False);
vgg16_model = keras.applications.VGG16();
x = vgg16_model.layers[-9].output;
x = AveragePooling2D(pool_size = (2,2))(x);
x = Flatten(name="flatten")(x);
x = Dense(128, activation = 'relu')(x);
x = Dense(2, activation = 'softmax')(x);
model = Model(inputs = vgg16_model.input, outputs = x);
model.compile(optimizer = Adam(lr=0.00001, clipvalue = 0.5, clipnorm = 1), loss = 'binary_crossentropy', metrics = ['accuracy']);
print("nTraining.....");
checkpoint = ModelCheckpoint(filepath = '/content/drive/MyDrive/dataset_two/data/model', monitor='val_accuracy', verbose=1, save_best_only=True, mode='max');
history = model.fit_generator(generator = train_generator, 
steps_per_epoch = 60, 
validation_data = test_generator, 
validation_steps = 2, 
epochs = 250, 
verbose = 1, 
callbacks = [checkpoint]);

错误消息:

Found 240 images belonging to 3 classes.
Found 60 images belonging to 3 classes.
Training.....
Epoch 1/250
---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-3-13058d525332> in <module>
30                               epochs = 250,
31                               verbose = 1,
---> 32                               callbacks = [checkpoint]);
InvalidArgumentError: Graph execution error:
Detected at node 'binary_crossentropy/logistic_loss/mul' defined at (most recent call last):
File "/usr/lib/python3.7/runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "/usr/lib/python3.7/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py", line 16, in <module>
app.launch_new_instance()
File "/usr/local/lib/python3.7/dist-packages/traitlets/config/application.py", line 846, in launch_instance
app.start()
File "/usr/local/lib/python3.7/dist-packages/ipykernel/kernelapp.py", line 612, in start
self.io_loop.start()
File "/usr/local/lib/python3.7/dist-packages/tornado/platform/asyncio.py", line 132, in start
self.asyncio_loop.run_forever()
File "/usr/lib/python3.7/asyncio/base_events.py", line 541, in run_forever
self._run_once()
File "/usr/lib/python3.7/asyncio/base_events.py", line 1786, in _run_once
handle._run()
File "/usr/lib/python3.7/asyncio/events.py", line 88, in _run
self._context.run(self._callback, *self._args)
File "/usr/local/lib/python3.7/dist-packages/tornado/ioloop.py", line 758, in _run_callback
ret = callback()
File "/usr/local/lib/python3.7/dist-packages/IPython/core/interactiveshell.py", line 3058, in run_cell_async
interactivity=interactivity, compiler=compiler, result=result)
File "/usr/local/lib/python3.7/dist-packages/IPython/core/interactiveshell.py", line 3249, in run_ast_nodes
if (await self.run_code(code, result,  async_=asy)):
File "/usr/local/lib/python3.7/dist-packages/IPython/core/interactiveshell.py", line 3326, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-3-13058d525332>", line 32, in <module>
callbacks = [checkpoint]);
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 2274, in fit_generator
initial_epoch=initial_epoch)
File "/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py", line 64, in error_handler
return fn(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1409, in fit
tmp_logs = self.train_function(iterator)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1051, in train_function
return step_function(self, iterator)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1040, in step_function
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1030, in run_step
outputs = model.train_step(data)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 890, in train_step
loss = self.compute_loss(x, y, y_pred, sample_weight)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 949, in compute_loss
y, y_pred, sample_weight, regularization_losses=self.losses)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/compile_utils.py", line 201, in __call__
loss_value = loss_obj(y_t, y_p, sample_weight=sw)
return tf.nn.sigmoid_cross_entropy_with_logits(labels=target, logits=output)
Node: 'binary_crossentropy/logistic_loss/mul'
required broadcastable shapes
[[{{node binary_crossentropy/logistic_loss/mul}}]] [Op:__inference_train_function_5643]

好的,我明白了我在的进口库更改

from keras.layers import AveragePooling2D;
from keras. layers.core import Dense, Flatten;
from keras.models import Model;

from tensorflow.keras.models import *
from tensorflow.keras.layers import *

并更改

x = Dense(2, activation = 'softmax')(x);

x = Dense(3, activation = 'softmax')(x);

因为我有三个班来训练那个模特它对我有效:(

相关内容

  • 没有找到相关文章