我正在尝试使用 InceptionV3 来训练我的图像。但仍然得到了这个ValueError: Shapes (None, 9) and (None, 13, 7, 2048) are incompatible
输入图像尺寸(RGB):480 x 270(宽x高)
输出标签:[0,0,0,0,0,0,0,0,1](9个输出)
软件包:
这是我的代码:
import numpy as np
from grabscreen import grab_screen
import cv2
import time
import pandas as pd
from random import shuffle
import tensorflow as tf
from tensorflow.keras.applications.xception import Xception
from tensorflow.keras.applications.inception_v3 import InceptionV3
from tensorflow.keras import optimizers
FILE_I_END = 201
WIDTH = 480
HEIGHT = 270
LR = 1e-3
EPOCHS = 1
MODEL_NAME = 'model.h5'
PREV_MODEL = ''
LOAD_MODEL = False
wl = 0
sl = 0
al = 0
dl = 0
wal = 0
wdl = 0
sal = 0
sdl = 0
nkl = 0
w = [1,0,0,0,0,0,0,0,0]
s = [0,1,0,0,0,0,0,0,0]
a = [0,0,1,0,0,0,0,0,0]
d = [0,0,0,1,0,0,0,0,0]
wa = [0,0,0,0,1,0,0,0,0]
wd = [0,0,0,0,0,1,0,0,0]
sa = [0,0,0,0,0,0,1,0,0]
sd = [0,0,0,0,0,0,0,1,0]
nk = [0,0,0,0,0,0,0,0,1]
model = InceptionV3(include_top=False, weights=None,input_shape=(WIDTH,HEIGHT,3), classes=9,classifier_activation='softmax')
if LOAD_MODEL:
model.load(PREV_MODEL)
print('We have loaded a previous model!!!!')
# iterates through the training files
for e in range(EPOCHS):
#data_order = [i for i in range(1,FILE_I_END+1)]
data_order = [i for i in range(1,FILE_I_END+1)]
shuffle(data_order)
for count,i in enumerate(data_order):
try:
file_name = 'training_data-{}.npy'.format(i)
# full file info
train_data = np.load(file_name,allow_pickle=True)
print('training_data-{}.npy'.format(i),len(train_data))
train = train_data[:-50]
test = train_data[-50:]
X = np.array([i[0] for i in train]).reshape(-1,WIDTH,HEIGHT,3)
Y = np.array([i[1] for i in train])
test_x = np.array([i[0] for i in test]).reshape(-1,WIDTH,HEIGHT,3)
test_y = np.array([i[1] for i in test])
learning_rate = 0.001
opt = optimizers.Adam(lr=LR, decay=1e-5)
model.compile(loss='categorical_crossentropy',
optimizer=opt,
metrics=['accuracy'])
model.fit(x=X, y=Y, epochs=1, verbose=1, validation_data=(test_x,test_y), batch_size=None)
if count%10 == 0:
print('SAVING MODEL!')
model.save(MODEL_NAME)
except Exception as e:
print(str(e))
我收到此错误:
C:\Users\codingan\Anaconda3\envs\gta5\lib\site-packages\tensorflow\python\keras\engine\training.py:806 train_function *
return step_function(self, iterator)
C:\Users\codingan\Anaconda3\envs\gta5\lib\site-packages\tensorflow\python\keras\engine\training.py:796 step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
C:\Users\codingan\Anaconda3\envs\gta5\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:1211 run
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
C:\Users\codingan\Anaconda3\envs\gta5\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2585 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
C:\Users\codingan\Anaconda3\envs\gta5\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2945 _call_for_each_replica
return fn(*args, **kwargs)
C:\Users\codingan\Anaconda3\envs\gta5\lib\site-packages\tensorflow\python\keras\engine\training.py:789 run_step **
outputs = model.train_step(data)
C:\Users\codingan\Anaconda3\envs\gta5\lib\site-packages\tensorflow\python\keras\engine\training.py:749 train_step
y, y_pred, sample_weight, regularization_losses=self.losses)
C:\Users\codingan\Anaconda3\envs\gta5\lib\site-packages\tensorflow\python\keras\engine\compile_utils.py:204 __call__
loss_value = loss_obj(y_t, y_p, sample_weight=sw)
C:\Users\codingan\Anaconda3\envs\gta5\lib\site-packages\tensorflow\python\keras\losses.py:149 __call__
losses = ag_call(y_true, y_pred)
C:\Users\codingan\Anaconda3\envs\gta5\lib\site-packages\tensorflow\python\keras\losses.py:253 call **
return ag_fn(y_true, y_pred, **self._fn_kwargs)
C:\Users\codingan\Anaconda3\envs\gta5\lib\site-packages\tensorflow\python\util\dispatch.py:201 wrapper
return target(*args, **kwargs)
C:\Users\codingan\Anaconda3\envs\gta5\lib\site-packages\tensorflow\python\keras\losses.py:1535 categorical_crossentropy
return K.categorical_crossentropy(y_true, y_pred, from_logits=from_logits)
C:\Users\codingan\Anaconda3\envs\gta5\lib\site-packages\tensorflow\python\util\dispatch.py:201 wrapper
return target(*args, **kwargs)
C:\Users\codingan\Anaconda3\envs\gta5\lib\site-packages\tensorflow\python\keras\backend.py:4687 categorical_crossentropy
target.shape.assert_is_compatible_with(output.shape)
C:\Users\codingan\Anaconda3\envs\gta5\lib\site-packages\tensorflow\python\framework\tensor_shape.py:1134 assert_is_compatible_with
raise ValueError("Shapes %s and %s are incompatible" % (self, other))
ValueError: Shapes (None, 9) and (None, 13, 7, 2048) are incompatible
你对我有什么解决办法吗?
非常感谢您。
答案 0 :(得分:0)
如果设置include_top=False
,则模型的输出将为4D(source):
池化:当include_top为False时,用于特征提取的可选池化模式。 无(默认)表示模型的输出将是最后一个卷积块的4D张量输出。 avg表示全局平均池将应用于最后一个卷积块的输出,因此模型的输出将为2D张量。 max表示将应用全局最大池。
您指定了班级数量,但是classes
仅在include_top=True
时有效:
类:分类图像的可选类数,仅当include_top为True且未指定weights参数时才指定。默认为1000。
与classifier_activation
相同:
classifier_activation :str或可调用。要在“顶层”层上使用的激活功能。除非include_top = True否则被忽略。设置classifier_activation = None返回“顶层”层的登录信息。
tl; dr ,如果您设置include_top=True