下のpythonファイル(dice_coefficient_loss.py
)からclassのDiceconf_Loss
を呼び出そうとしたのですが、エラーが出て呼び出せません。ちなみにclassのコードはこちらのサイトを参考にしました。
呼び出し
python
1from dice_coefficient_loss import Diceconf_Loss 2 3model = unet() 4# model.load_weights("10_unet.hdf5") 5model.compile(loss=Diceconf_Loss.dice_coef_loss, optimizer=Adam(lr=1e-4), 6 metrics=[Diceconf_Loss.dice_coef]) 7 8model.summary()
エラー
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-6-af8760623381> in <module>() 21 # model.load_weights("10_unet.hdf5") 22 model.compile(loss=Diceconf_Loss.dice_coef_loss, optimizer=Adam(lr=1e-4), ---> 23 metrics=[Diceconf_Loss.dice_coef]) 24 25 model.summary() 1 frames /usr/local/lib/python3.6/dist-packages/keras/engine/training_utils.py in weighted(y_true, y_pred, weights, mask) 402 """ 403 # score_array has ndim >= 2 --> 404 score_array = fn(y_true, y_pred) 405 if mask is not None: 406 # Cast the mask to floatX to avoid float64 upcasting in Theano TypeError: dice_coef_loss() missing 1 required positional argument: 'y_pred'
原因がわからないので、解決策を教えていだけないでしょうか?
python
1# dice_coefficient_loss.py 2import keras.backend as K 3import tensorflow as tf 4import numpy as np 5 6class Diceconf_Loss(): 7 # ロス関数 8 def dice_coef_loss(self, y_true, y_pred): 9 return 1.0 - self.dice_coef(y_true, y_pred) 10 11 12 def dice_coef(self, y_true, y_pred): 13 y_true = K.flatten(y_true) 14 y_pred = K.flatten(y_pred) 15 intersection = K.sum(y_true * y_pred) 16 return 2.0 * intersection / (K.sum(y_true) + K.sum(y_pred) + 1) 17 18 19 20class DiceLossByClass(): 21 def __init__(self, input_shape, class_num): 22 self.__input_h = input_shape[0] 23 self.__input_w = input_shape[1] 24 self.__class_num = class_num 25 26 27 def dice_coef(self, y_true, y_pred): 28 y_true = K.flatten(y_true) 29 y_pred = K.flatten(y_pred) 30 intersection = K.sum(y_true * y_pred) 31 denominator = K.sum(y_true) + K.sum(y_pred) 32 if denominator == 0: 33 return 1 34 if intersection == 0: 35 return 1 / (denominator + 1) 36 return (2.0 * intersection) / denominator 37 #return (2.0 * intersection + 1) / (KB.sum(y_true) + KB.sum(y_pred) + 1) 38 39 40 def dice_coef_loss(self, y_true, y_pred): 41 # (N, h, w, ch) 42 y_true_res = tf.reshape(y_true, (-1, self.__input_h, self.__input_w, self.__class_num)) 43 y_pred_res = tf.reshape(y_pred, (-1, self.__input_h, self.__input_w, self.__class_num)) 44 y_trues = tf.unstack(y_true_res, axis=3) 45 y_preds = tf.unstack(y_pred_res, axis=3) 46 47 losses = [] 48 for y_t, y_p in zip(y_trues, y_preds): 49 losses.append((1 - self.dice_coef(y_t, y_p))*3) 50 51 # return tf.reduce_mean(tf.stack(losses)) 52 return tf.reduce_sum(tf.stack(losses)) 53 #return 1 - self.dice_coef(y_true, y_pred)
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/06/30 20:02