多输出模型的一个图像输入:InvalidArgumentError

如何解决多输出模型的一个图像输入:InvalidArgumentError

我有一个项目可以从图像中预测 8 个输出(4 个坐标),当我运行 model.fit() 时发生此错误。
我的 .csv 文件有 768 个数据,例如:

      name           x1   y1   x2   y2    x3   y3   x4   y4
  0   frame220.png  254  720  619  432  1189  720  641  432
  1   frame570.png  230  720  641  432  1154  720  686  432
  ....

df = pd.read_csv('./dataFile/data.csv')


我的模型(VGG16):

def build_model():
  input = Input(shape = (224,224,3),batch_size=32)
  x = Conv2D(64,(3,padding= 'same',activation= 'relu')(input)
  x = Conv2D(64,activation= 'relu')(x)
  x = MaxPooling2D(pool_size=(2,2),strides=(2,2))(x)
  x = Conv2D(128,activation= 'relu')(x)
  x = Conv2D(128,2))(x)
  x = Conv2D(256,activation= 'relu')(x)
  x = Conv2D(256,2))(x)
  x = Conv2D(512,activation= 'relu')(x)
  x = Conv2D(512,2))(x)
  x = Flatten()(x)
  x = Dense(256,activation= 'relu')(x)
  x = Dropout(0.5)(x)

  x1_dence = Dense(128,activation='softmax')(x)
  x1 = Dense(1,name = 'x1')(x)
  y1_dence = Dense(128,activation='softmax')(x)
  y1 = Dense(1,name = 'y1')(x)

  x2_dence = Dense(128,activation='softmax')(x)
  x2 = Dense(1,name = 'x2')(x)
  y2_dence = Dense(128,activation='softmax')(x)
  y2 = Dense(1,name = 'y2')(x)

  x3_dence = Dense(128,activation='softmax')(x)
  x3 = Dense(1,name = 'x3')(x)
  y3_dence = Dense(128,activation='softmax')(x)
  y3 = Dense(1,name = 'y3')(x)

  x4_dence = Dense(128,activation='softmax')(x)
  x4 = Dense(1,name = 'x4')(x)
  y4_dence = Dense(128,activation='softmax')(x)
  y4 = Dense(1,name = 'y4')(x)

  model = Model( inputs = input,outputs = [x1,y1,x2,y2,x3,y3,x4,y4])

  return model


模型总结

Model: "model_6"
__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_7 (InputLayer)            [(32,3)]  0                                            
__________________________________________________________________________________________________
conv2d_78 (Conv2D)              (32,64)   1792        input_7[0][0]                    
__________________________________________________________________________________________________
conv2d_79 (Conv2D)              (32,64)   36928       conv2d_78[0][0]                  
__________________________________________________________________________________________________
max_pooling2d_30 (MaxPooling2D) (32,112,64)   0           conv2d_79[0][0]                  
__________________________________________________________________________________________________
conv2d_80 (Conv2D)              (32,128)  73856       max_pooling2d_30[0][0]           
__________________________________________________________________________________________________
conv2d_81 (Conv2D)              (32,128)  147584      conv2d_80[0][0]                  
__________________________________________________________________________________________________
max_pooling2d_31 (MaxPooling2D) (32,56,128)    0           conv2d_81[0][0]                  
__________________________________________________________________________________________________
conv2d_82 (Conv2D)              (32,256)    295168      max_pooling2d_31[0][0]           
__________________________________________________________________________________________________
conv2d_83 (Conv2D)              (32,256)    590080      conv2d_82[0][0]                  
__________________________________________________________________________________________________
conv2d_84 (Conv2D)              (32,256)    590080      conv2d_83[0][0]                  
__________________________________________________________________________________________________
max_pooling2d_32 (MaxPooling2D) (32,28,256)    0           conv2d_84[0][0]                  
__________________________________________________________________________________________________
conv2d_85 (Conv2D)              (32,512)    1180160     max_pooling2d_32[0][0]           
__________________________________________________________________________________________________
conv2d_86 (Conv2D)              (32,512)    2359808     conv2d_85[0][0]                  
__________________________________________________________________________________________________
conv2d_87 (Conv2D)              (32,512)    2359808     conv2d_86[0][0]                  
__________________________________________________________________________________________________
max_pooling2d_33 (MaxPooling2D) (32,14,512)    0           conv2d_87[0][0]                  
__________________________________________________________________________________________________
conv2d_88 (Conv2D)              (32,512)    2359808     max_pooling2d_33[0][0]           
__________________________________________________________________________________________________
conv2d_89 (Conv2D)              (32,512)    2359808     conv2d_88[0][0]                  
__________________________________________________________________________________________________
conv2d_90 (Conv2D)              (32,512)    2359808     conv2d_89[0][0]                  
__________________________________________________________________________________________________
max_pooling2d_34 (MaxPooling2D) (32,7,512)      0           conv2d_90[0][0]                  
__________________________________________________________________________________________________
flatten_6 (Flatten)             (32,25088)          0           max_pooling2d_34[0][0]           
__________________________________________________________________________________________________
dense_54 (Dense)                (32,256)            6422784     flatten_6[0][0]                  
__________________________________________________________________________________________________
dropout_6 (Dropout)             (32,256)            0           dense_54[0][0]                   
__________________________________________________________________________________________________
x1 (Dense)                      (32,1)              257         dropout_6[0][0]                  
__________________________________________________________________________________________________
y1 (Dense)                      (32,1)              257         dropout_6[0][0]                  
__________________________________________________________________________________________________
x2 (Dense)                      (32,1)              257         dropout_6[0][0]                  
__________________________________________________________________________________________________
y2 (Dense)                      (32,1)              257         dropout_6[0][0]                  
__________________________________________________________________________________________________
x3 (Dense)                      (32,1)              257         dropout_6[0][0]                  
__________________________________________________________________________________________________
y3 (Dense)                      (32,1)              257         dropout_6[0][0]                  
__________________________________________________________________________________________________
x4 (Dense)                      (32,1)              257         dropout_6[0][0]                  
__________________________________________________________________________________________________
y4 (Dense)                      (32,1)              257         dropout_6[0][0]                  
==================================================================================================
Total params: 21,139,528
Trainable params: 21,528
Non-trainable params: 0


将图像文件夹连接到 CSV 文件

train_image = []
for i in tqdm(range(df.shape[0])):
    img = image.load_img('./dataframe/frame' + str(i)+ '.png',target_size=(224,3))
    img = image.img_to_array(img)
    train_image.append(img)
X = np.array(train_image)

print(X.shape)

y = np.array(df.drop(['name'],axis=1))
print(y.shape)

100%|██████████| 786/786 [05:07<00:00,2.55it/s](786,3)
(786,8)


拆分数据 - 我在 X_train 中展示了一个样本 > 结果是图像

X_train,X_test,y_train,y_test = train_test_split(X,y,random_state=42,test_size=0.2)
y_train[67]

array([ 279,720,643,432,1217,672,432])

model = build_model()

optimizer = tf.keras.optimizers.SGD(learning_rate=0.001)
model.compile(
    optimizer = optimizer,loss={'x1': 'mse','y1': 'mse','x2': 'mse','y2': 'mse','x3': 'mse','y3': 'mse','x4': 'mse','y4': 'mse'},metrics={
        'x1': tf.keras.metrics.RootMeanSquaredError(),'y1': tf.keras.metrics.RootMeanSquaredError(),'x2': tf.keras.metrics.RootMeanSquaredError(),'y2': tf.keras.metrics.RootMeanSquaredError(),'x3': tf.keras.metrics.RootMeanSquaredError(),'y3': tf.keras.metrics.RootMeanSquaredError(),'x4': tf.keras.metrics.RootMeanSquaredError(),'y4': tf.keras.metrics.RootMeanSquaredError()
    }
)


最后当我运行 model.fit()

history = model.fit(X_train,epochs=10,batch_size=32,validation_data=(X_test,y_test))

Epoch 1/10
19/20 [===========================>..] - ETA: 0s - loss: nan - x1_loss: nan - y1_loss: nan - x2_loss: nan - y2_loss: nan - x3_loss: nan - y3_loss: nan - x4_loss: nan - y4_loss: nan - x1_root_mean_squared_error: nan - y1_root_mean_squared_error: nan - x2_root_mean_squared_error: nan - y2_root_mean_squared_error: nan - x3_root_mean_squared_error: nan - y3_root_mean_squared_error: nan - x4_root_mean_squared_error: nan - y4_root_mean_squared_error: nan
---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-23-5984067494bc> in <module>()
----> 1 history = model.fit(X_train,y_test))

6 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/execute.py in quick_execute(op_name,num_outputs,inputs,attrs,ctx,name)
     58     ctx.ensure_initialized()
     59     tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle,device_name,op_name,---> 60                                         inputs,num_outputs)
     61   except core._NotOkStatusException as e:
     62     if name is not None:

InvalidArgumentError:  required broadcastable shapes at loc(unknown)
     [[node model_4/dropout_4/dropout/Mul_1 (defined at /usr/local/lib/python3.7/dist-packages/keras/layers/core.py:205) ]] [Op:__inference_train_function_10022]

Function call stack:
train_function


那么,我错在哪里?以及如何修复它?谢谢你

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res