gpt4 book ai didi

python - TensorFlow 错误 : ValueError ("Shapes %s and %s are incompatible" % (self, 其他))

转载 作者:行者123 更新时间:2023-12-05 03:53:52 24 4
gpt4 key购买 nike

我正在尝试使用 分类交叉熵 作为损失函数将 PCB 图像分为两类(defectedundefected) .相同的代码如下:

import numpy as np
import matplotlib.pyplot as plt

import tensorflow
from tensorflow.keras.applications import ResNet50
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.optimizers import SGD
from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint


from keras.applications.resnet50 import preprocess_input
from keras.preprocessing.image import ImageDataGenerator

from sklearn.model_selection import train_test_split

def create_compiled_model():
model = Sequential()
model.add(ResNet50(include_top=False, weights=RESNET50_WEIGHTS, input_shape=(IMAGE_SIZE, IMAGE_SIZE, 3), pooling=RESNET50_POOLING_AVERAGE))
model.add(Dense(NUM_CLASSES, activation=DENSE_LAYER_ACTIVATION))
model.layers[0].trainable = False

sgd = SGD(lr = 0.01, decay = 1e-6, momentum = 0.9, nesterov = True)
model.compile(optimizer = sgd, loss = OBJECTIVE_FUNCTION, metrics = LOSS_METRICS)

return model

def data_splitor():
x = np.load("/content/data/xtrain.npy")
y = np.load("/content/data/ytrain.npy")

# Getting the Test and Train splits
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size= TRAIN_TEST_SPLIT, shuffle= True)

# Getting the Train and Validation splits
x__train, x__valid, y__train, y__valid = train_test_split(x_train, y_train, test_size= TRAIN_TEST_SPLIT, shuffle= True)

return x__train, x__valid, x_test, y__train, y__valid, y_test

def data_generator(x, y, batch_size, seed=None, shuffle=True):
data_generator = ImageDataGenerator(horizontal_flip=True, vertical_flip=True, rotation_range=180, brightness_range=[0.3, 1.0], preprocessing_function=preprocess_input)
generator = data_generator.flow(x_train, y_train, batch_size= batch_size, seed= seed, shuffle=shuffle)
return generator

def run_program():
x_train, x_valid, x_test, y_train, y_valid, y_test = data_splitor()
train_generator = data_generator(x_train, y_train, BATCH_SIZE_TRAINING)
validation_generator = data_generator(x_valid, y_valid, BATCH_SIZE_VALIDATION)

cb_early_stopper = EarlyStopping(monitor = 'val_loss', patience = EARLY_STOP_PATIENCE)
cb_checkpointer = ModelCheckpoint(filepath = '/content/model/best.hdf5', monitor = 'val_loss', save_best_only = True, mode = 'auto')

model = create_compiled_model()

fit_history = model.fit_generator(
train_generator,
steps_per_epoch=STEPS_PER_EPOCH_TRAINING,
epochs = NUM_EPOCHS,
validation_data=validation_generator,
validation_steps=STEPS_PER_EPOCH_VALIDATION,
callbacks=[cb_checkpointer, cb_early_stopper]
)

plt.figure(1, figsize = (15,8))

plt.subplot(221)
plt.plot(fit_history.history['acc'])
plt.plot(fit_history.history['val_acc'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'valid'])

plt.subplot(222)
plt.plot(fit_history.history['loss'])
plt.plot(fit_history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'valid'])

plt.show()


# Testing
test_generator = data_generator(x_test, y_test, BATCH_SIZE_TESTING, 123, False)
test_generator.reset()

model.load_weights("/content/model/best.hdf5")
pred = model.predict_generator(test_generator, steps = len(test_generator), verbose = 1)

predicted_class_indices = np.argmax(pred, axis = 1)


# Running the program
try:
with tensorflow.device('/device:GPU:0'):
run_program()
except RuntimeError as e:
print(e)

执行此操作后,我得到如下所示的 ValueError:

ValueError: in user code:

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:571 train_function *
outputs = self.distribute_strategy.run(
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:951 run **
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:2290 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:2649 _call_for_each_replica
return fn(*args, **kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:533 train_step **
y, y_pred, sample_weight, regularization_losses=self.losses)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/compile_utils.py:204 __call__
loss_value = loss_obj(y_t, y_p, sample_weight=sw)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/losses.py:143 __call__
losses = self.call(y_true, y_pred)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/losses.py:246 call
return self.fn(y_true, y_pred, **self._fn_kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/losses.py:1527 categorical_crossentropy
return K.categorical_crossentropy(y_true, y_pred, from_logits=from_logits)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/backend.py:4561 categorical_crossentropy
target.shape.assert_is_compatible_with(output.shape)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/tensor_shape.py:1117 assert_is_compatible_with
raise ValueError("Shapes %s and %s are incompatible" % (self, other))

ValueError: Shapes (None, 1) and (None, 2) are incompatible

我已经看过 thisthisthis ,但无法解决错误。

非常感谢您帮助解决此问题。

谢谢普拉文

这是完整的回溯...... link

最佳答案

似乎您的 y_train 数据具有形状 (None,1),而您的网络期望 (None,2)。有两种选择可以解决这个问题:

1) 将模型输出更改为 1 个单位并将损失更改为二元交叉熵

2) 将您的 y_train 数据更改为分类数据。参见 this

如果您可以在这里发布您的 model.summary() 和您的数据集形状,这将有助于我们帮助您。

关于python - TensorFlow 错误 : ValueError ("Shapes %s and %s are incompatible" % (self, 其他)),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61464888/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com