roc_auc.py 文件源码

python
阅读 40 收藏 0 点赞 0 评论 0

项目:deep-mil-for-whole-mammogram-classification 作者: wentaozhu 项目源码 文件源码
def on_epoch_end(self, epoch, logs={}):
    if epoch % self.interval == 0:
      y_pred = self.model.predict(self.X_val, verbose=0)
      #print(y_pred.shape)
      if self.mymil:
        y_true = self.y_val.max(axis=1)
        y_score = y_pred.max(axis=1)#>0.5
      else:
        y_true = self.y_val[:,1] #np.argmax(self.y_val, axis=1)
        y_score = y_pred[:,1] #np.argmax(y_pred, axis=1)
      sortindex = np.argsort(y_score)
      y_score = y_score[sortindex]
      y_true = y_true[sortindex]
      bestacc, bestthresh = np.mean(y_true == np.ones_like(y_true)), y_score[0]-0.001
      for thresh in y_score:
        acc = np.mean(y_true == (y_score>thresh))
        if acc > bestacc:
          bestacc, bestthresh = acc, thresh
      y_score = y_score>bestthresh
      #y_score = y_score >0.5
      acc = np.mean(y_true == y_score)
      assert(acc == bestacc)
      print("interval evaluation - epoch: {:d} - acc: {:.2f}".format(epoch, acc))
      if acc > self.acc:
        self.acc = acc
        for f in os.listdir('./'):
          if f.startswith(self.filepath+'acc'):
            os.remove(f)
        self.model.save(self.filepath+'acc'+str(acc)+'ep'+str(epoch)+'.hdf5')
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号