kerasで三値分類の評価関数を自作したい
ここに質問の内容を詳しく書いてください。
y_true = [0,1,0]かつy_pred = [0,1,0]が非常に多いのでIoUのようにして
このような場合を除いたときの精度を求めようとしているのですがうまく行きません。
よろしくお願いします。
該当のソースコード
Python
1model = tensorflow.keras.models.Sequential() 2dense_1 = Dense(30,input_dim=399, activation = 'relu') 3dense_2 = Dense(30, activation = 'relu') 4dense_3 = Dense(3, activation = 'sigmoid') 5model.add(dense_1) 6model.add(dense_2) 7model.add(dense_3) 8model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy', IoU]) 9model.fit(x_train, y_train, epochs=1, batch_size=1, validation_data=(x_test, y_test))
Python
1#IoU 2def iou0_0(y_true, y_pred): 3 print(y_true[0]) 4 print(y_pred[0]) 5 return K.sum(K.cast(K.equal(y_true[0] * y_pred[0], 1), K.floatx())) 6def iou0_1(y_true, y_pred): 7 return K.sum(K.cast(K.equal(y_true[0] * y_pred[1], 1), K.floatx())) 8def iou0_2(y_true, y_pred): 9 return K.sum(K.cast(K.equal(y_true[0] * y_pred[2], 1), K.floatx())) 10def iou1_0(y_true, y_pred): 11 return K.sum(K.cast(K.equal(y_true[1] * y_pred[0], 1), K.floatx())) 12def iou1_2(y_true, y_pred): 13 return K.sum(K.cast(K.equal(y_true[1] * y_pred[2], 1), K.floatx())) 14def iou2_0(y_true, y_pred): 15 return K.sum(K.cast(K.equal(y_true[2] * y_pred[0], 1), K.floatx())) 16def iou2_1(y_true, y_pred): 17 return K.sum(K.cast(K.equal(y_true[2] * y_pred[1], 1), K.floatx())) 18def iou2_2(y_true, y_pred): 19 return K.sum(K.cast(K.equal(y_true[2] * y_pred[2], 1), K.floatx())) 20 21def IoU(y_true, y_pred): 22 y_pred = K.round(y_pred) 23 return (iou0_0(y_true, y_pred) + iou2_2(y_true, y_pred))/ iou0_0(y_true, y_pred)+iou0_1(y_true, y_pred)+iou0_2(y_true, y_pred)+iou1_0(y_true, y_pred)+iou1_2(y_true, y_pred)+iou2_0(y_true, y_pred)+iou2_1(y_true, y_pred)+iou2_2(y_true, y_pred)
発生している問題・エラーメッセージ
402 model.add(dense_3) 403 model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy', IoU]) --> 404 model.fit(x_train, y_train, epochs=1, batch_size=1, validation_data=(x_test, y_test)) 406 def main(): 725 max_queue_size=max_queue_size, 726 workers=workers, --> 727 use_multiprocessing=use_multiprocessing) 728 729 def evaluate(self, 673 validation_steps=validation_steps, 674 validation_freq=validation_freq, --> 675 steps_name='steps_per_epoch') 676 677 def evaluate(self, 392 393 # Get outputs. --> 394 batch_outs = f(ins_batch) 395 if not isinstance(batch_outs, list): 396 batch_outs = [batch_outs] 3474 3475 fetched = self._callable_fn(*array_vals, -> 3476 run_metadata=self.run_metadata) 3477 self._call_fetch_callbacks(fetched[-len(self._fetches):]) 3478 output_structure = nest.pack_sequence_as( 1470 ret = tf_session.TF_SessionRunCallable(self._session._session, 1471 self._handle, args, -> 1472 run_metadata_ptr) 1473 if run_metadata: 1474 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr) InvalidArgumentError: slice index 2 of dimension 0 out of bounds. [[{{node metrics_46/IoU/strided_slice_20}}]]
あなたの回答
tips
プレビュー