Ejemplo n.º 1
0
def detect_cv_failure(source, target, transform, echo=True):
    if np.sum(np.isnan(transform)) != 0:
        score = 0
        failed = True
    else:
        if not np.isfinite(np.linalg.cond(transform)):
            score = 0
            failed = True
        else:
            u_x, u_y = utils.rigid_dot(source, np.linalg.inv(transform))
            transformed_source = utils.warp_image(source, u_x, u_y)
            ret_source, thresholded_source = threshold_calculation(source)
            ret_target, thresholded_target = threshold_calculation(target)
            ret_transformed_source, thresholded_transformed_source = threshold_calculation_with_threshold(
                transformed_source, ret_source)

            initial_dice = utils.dice(thresholded_source, thresholded_target)
            transformed_dice = utils.dice(thresholded_transformed_source,
                                          thresholded_target)
            if echo:
                print("Initial dice: ", initial_dice)
                print("Transformed dice: ", transformed_dice)

            score = transformed_dice
            if transformed_dice > initial_dice and transformed_dice > 0.75:
                failed = False
            else:
                failed = True

    success = not failed
    return score, success
Ejemplo n.º 2
0
 def damage_roll(self, attacker_id, target_id,
                 skill_name, skill_damage, bonus_ability,
                 is_critical=False, penalty=0):
     """
     skill_damage + related ability modifier
     if is_critical, twice die damage + 2 * all modifiers
     """
     attacker = self.unit_list[attacker_id]
     target = self.unit_list[target_id]
     base_damage = dice(skill_damage)
     modifier = getattr(attacker, bonus_ability.lower()+"_modifier")
     if is_critical:
         base_damage += dice(skill_damage)
         modifier += modifier
     damage = base_damage + modifier + penalty
     if damage < const.MINIMUN_DAMAGE:
         damage = MINIMUN_DAMAGE
     self.unit_list[target_id].unit_hp -= damage
     msg = ""
     is_dead = False
     if is_critical:
         msg += "(Critical Hit)"
     if target.unit_hp <= 0:
         msg += "Target died."
         is_dead = True
     target_hit_print(
         player_name=attacker.name, action_name=skill_name,
         target_name=target.name, damage=damage,
         target_hp=target.unit_hp, message=msg)
     return is_dead
    def build_fcn_net(self, inp, use_dice=False):
        with self.graph.as_default():
            self.saver = tf.train.Saver(max_to_keep=1)


            with tf.name_scope("Out"):
                bn1 = tf.layers.batch_normalization(inputs=inp, name='bn1')
                dnn1 = tf.layers.dense(bn1, 200, activation=None, name='f1')
                if use_dice:
                    dnn1 = dice(dnn1, name='dice_1')
                else:
                    dnn1 = prelu(dnn1, 'prelu1')

                dnn2 = tf.layers.dense(dnn1, 80, activation=None, name='f2')
                if use_dice:
                    dnn2 = dice(dnn2, name='dice_2')
                else:
                    dnn2 = prelu(dnn2, 'prelu2')
                dnn3 = tf.layers.dense(dnn2, 2, activation=None, name='f3')
                self.y_hat = tf.nn.softmax(dnn3) + 0.00000001

            with tf.name_scope('Metrics'):
                # Cross-entropy loss and optimizer initialization
                # 'core_type_ph': [1, 1, 0,..],

                ctr_loss = - tf.reduce_mean(tf.log(self.y_hat) * self.target_ph)
                self.loss = ctr_loss
                # tf.summary.scalar('loss', self.loss)
                self.optimizer = tf.train.AdamOptimizer(learning_rate=self.lr_ph).minimize(self.loss)
                # self.optimizer = tf.train.GradientDescentOptimizer(learning_rate=self.lr_ph).minimize(self.loss)
                # Accuracy metric
                self.accuracy = tf.reduce_mean(tf.cast(tf.equal(tf.round(self.y_hat), self.target_ph), tf.float32))
                # tf.summary.scalar('accuracy', self.accuracy)

            self.merged = tf.summary.merge_all()
Ejemplo n.º 4
0
    def build_fcn_net(self,inp,use_dice=False):
        bn1 = tf.layers.batch_normalization(inputs=inp,name='bn1')
        dnn1 = tf.layers.dense(bn1,200,activation=None,name='f1')

        if use_dice:
            dnn1 = dice(dnn1,name='dice_1')
        else:
            dnn1 = prelu(dnn1,'prelu1')

        dnn2 = tf.layers.dense(dnn1,80,activation=None,name='f2')
        if use_dice:
            dnn2 = dice(dnn2,name='dice_2')
        else:
            dnn2 = prelu(dnn2,name='prelu2')

        dnn3 = tf.layers.dense(dnn2,2,activation=None,name='f3')
        self.y_hat = tf.nn.softmax(dnn3) + 0.00000001

        with tf.name_scope('Metrics'):
            ctr_loss = -tf.reduce_mean(tf.log(self.y_hat) * self.target_ph)
            self.loss = ctr_loss
            if self.use_negsampling:
                self.loss += self.aux_loss
            tf.summary.scalar('loss',self.loss)
            self.optimizer = tf.train.AdamOptimizer(learning_rate=self.lr).minimize(self.loss)

            self.accuracy = tf.reduce_mean(tf.cast(tf.equal(tf.round(self.y_hat),self.target_ph),tf.float32))
            tf.summary.scalar('accuracy',self.accuracy)

        self.merged = tf.summary.merge_all()
Ejemplo n.º 5
0
    def build_fcn_net(self, inp, use_dice=False):
        bn1 = tf.layers.batch_normalization(inputs=inp, name='bn1')
        dnn1 = tf.layers.dense(bn1, 200, activation=None, name='f1')

        if use_dice:
            dnn1 = dice(dnn1, name='dice_1')
        else:
            dnn1 = prelu(dnn1, 'prelu1')

        dnn2 = tf.layers.dense(dnn1, 80, activation=None, name='f2')
        if use_dice:
            dnn2 = dice(dnn2, name='dice_2')
        else:
            dnn2 = prelu(dnn2, name='prelu2')

        dnn3 = tf.layers.dense(dnn2, 2, activation=None, name='f3')
        self.y_hat = tf.nn.softmax(dnn3) + 0.00000001

        with tf.name_scope('Metrics'):
            ctr_loss = -tf.reduce_mean(tf.log(self.y_hat) * self.target_ph)
            self.loss = ctr_loss
            if self.use_negsampling:
                self.loss += self.aux_loss
            tf.summary.scalar('loss', self.loss)
            self.optimizer = tf.train.AdamOptimizer(
                learning_rate=self.lr).minimize(self.loss)

            self.accuracy = tf.reduce_mean(
                tf.cast(tf.equal(tf.round(self.y_hat), self.target_ph),
                        tf.float32))
            tf.summary.scalar('accuracy', self.accuracy)

        self.merged = tf.summary.merge_all()
Ejemplo n.º 6
0
 def get_damage_dice(self, world, is_critical, *args):
     damage = 0
     if hasattr(self, 'damage') and self.damage:
         damage += self.damage
         if is_critical:
             damage += self.damage
     if hasattr(self, 'damage_dice') and self.damage_dice:
         damage += dice(self.damage_dice)
         if is_critical:
             damage += dice(self.damage_dice)
     return damage
Ejemplo n.º 7
0
 def get_damage_dice(self, world, is_critical, *args):
     damage = 0
     if hasattr(self, 'damage') and self.damage:
         damage += self.damage
         if is_critical:
             damage += self.damage
     if hasattr(self, 'damage_dice') and self.damage_dice:
         damage += dice(self.damage_dice)
         if is_critical:
             damage += dice(self.damage_dice)
     return damage
def main():
    path_test = '/home/dongnie/Desktop/myPyTorch/pytorch-SRResNet/res_1106'
    path_test = '/home/dongnie/myPytorchCodes/pytorch-SRResNet23D/resValidCha'
    ids = [1, 2, 3, 4, 6, 7, 8, 10, 11, 12, 13, 29]
    ids = range(45, 50)
    for ind in ids:
        #         mr_test_itk = sitk.ReadImage(os.path.join(path_test,'denseCrf3dSegmMap_pelvic_sub%d.nii.gz'%ind))
        mr_test_itk = sitk.ReadImage(
            os.path.join(
                path_test,
                'preCha_wce_wdice_viewExp_resEhance_fullAD_0110_iter14w_sub%02d.nii.gz'
                % ind))
        #mr_test_itk = sitk.ReadImage(os.path.join(path_test,'denseCrf3dSegmMap_probMaps_model0110_sub%02d.nii.gz'%ind))
        #mr_test_itk = sitk.ReadImage(os.path.join(path_test,'denseCrf3dSegmMap_probMaps_thresh_model0110_sub%02d.nii.gz'%ind))
        crfSeg = sitk.GetArrayFromImage(mr_test_itk)
        ct_test_itk = sitk.ReadImage(
            os.path.join(path_test, 'gt_sub%d.nii.gz' % ind))
        gtSeg = sitk.GetArrayFromImage(ct_test_itk)

        diceBladder = dice(crfSeg, gtSeg, 1)
        #diceProstate = dice(crfSeg,gtSeg,2)
        #diceRectumm = dice(crfSeg,gtSeg,3)

        iouBladder = IoU(crfSeg, gtSeg, 1)
        #iouProstate = IoU(crfSeg,gtSeg,2)
        #iouRectum = IoU(crfSeg,gtSeg,3)
        print 'sub%d' % ind, ' iou1 = ', iouBladder
        #print 'sub%d'%ind,' iou1 = ',iouBladder,' iou2= ',iouProstate,' iou3= ',iouRectum

        print 'sub%d' % ind, 'dice1 = ', diceBladder
        #print 'sub%d'%ind, 'dice1 = ',diceBladder,' dice2= ',diceProstate,' dice3= ',diceRectumm
        print '\n'
Ejemplo n.º 9
0
    def gain_level(self, class_gain):
        """docstring for gain_level"""
        # raise NotImplementedError()
        # 1. Increase HP
        c = getattr(char_classes, class_gain)()
        gain_hp = dice(c.hit_dice) + self.con_modifier
        self.unit_hp_max += gain_hp
        self.unit_hp = self.unit_hp_max

        # 2. Increate base attack bonus
        # 3. Increate save throws
        self.unit_fortitude += c.fortitude_bonus[self.unit_level]
        self.unit_reflex += c.reflex_bonus[self.unit_level]
        self.unit_will += c.will_bonus[self.unit_level]
        # 4. gain ability score
        if c.ability_bonus[self.unit_level]:
            ability = self.ask_ability_bonus()
            ability_point = getattr(self, ability) + 1
            setattr(self, ability, ability_point)
        # 5. Allicate skill points
        # 6. add feats
        # 7. classes level + 1
        self.unit_level += 1
        if class_gain in self.classes:
            self.classes[class_gain] += 1
        else:
            self.classes[class_gain] = 1
        print("%s gain a level(LV %d):" % (self.name, self.unit_level))
Ejemplo n.º 10
0
 def create(cls,
            name,
            start_class,
            str_modify=0,
            dex_modify=0,
            con_modify=0,
            int_modify=0,
            wis_modify=0,
            chr_modify=0,
            **kwargs):
     char_class = getattr(char_classes, start_class)()
     char = Character(
         name=name,
         classes={start_class: 1},
         unit_str=10 + str_modify,
         unit_dex=10 + dex_modify,
         unit_con=10 + con_modify,
         unit_int=10 + int_modify,
         unit_wis=10 + wis_modify,
         unit_chr=10 + chr_modify,
         **kwargs,
     )
     hp_max = dice(char_class.hit_dice) + char.con_modifier
     print("%s created, hit dice get %d hp" % (name, hp_max))
     if hp_max < 1:
         hp_max = 1
     char.unit_hp_max = hp_max
     char.unit_hp = hp_max
     # if char.unit_level > 1:
     #     char.gain_level(char.ask_next_level_class())
     return char
Ejemplo n.º 11
0
    def gain_level(self, class_gain):
        """docstring for gain_level"""
        # raise NotImplementedError()
        # 1. Increase HP
        c = getattr(char_classes, class_gain)()
        gain_hp = dice(c.hit_dice) + self.con_modifier
        self.unit_hp_max += gain_hp
        self.unit_hp = self.unit_hp_max

        # 2. Increate base attack bonus
        # 3. Increate save throws
        self.unit_fortitude += c.fortitude_bonus[self.unit_level]
        self.unit_reflex += c.reflex_bonus[self.unit_level]
        self.unit_will += c.will_bonus[self.unit_level]
        # 4. gain ability score
        if c.ability_bonus[self.unit_level]:
            ability = self.ask_ability_bonus()
            ability_point = getattr(self, ability) + 1
            setattr(self, ability, ability_point)
        # 5. Allicate skill points
        # 6. add feats
        # 7. classes level + 1
        self.unit_level += 1
        if class_gain in self.classes:
            self.classes[class_gain] += 1
        else:
            self.classes[class_gain] = 1
        print("%s gain a level(LV %d):" % (self.name, self.unit_level))
Ejemplo n.º 12
0
    def create_dialog(self):

        self.text = dice(len(self.dialogs) - 1)

        name = self.dialogs[self.text[0]]['npc']
        text = self.dialogs[self.text[0]]['text']
        message = {0: "{0}:".format(name)}

        max_chars = 24
        i = 1
        for j in range(0, len(text), max_chars):
            message[i] = text[j:j + max_chars]
            i += 1

        self.dialog_box = DialogBox(self.factory,
                                    font_size=32,
                                    fg_color=Colors.WHITE,
                                    bg_color=Colors.BLACK,
                                    font_name="04B_20__.TTF",
                                    text=message,
                                    position=self.position,
                                    renderer=self.renderer)

        sprites = self.dialog_box.get_sprites()
        for sprite in sprites:
            self.dialog_sprites.append(sprite)
Ejemplo n.º 13
0
 def get_heal_hp_dice(self, world, *args):
     heal_hp = 0
     if hasattr(self, 'heal_hp') and self.heal_hp:
         heal_hp += self.heal_hp
     if hasattr(self, 'heal_hp_dice') and self.heal_hp_dice:
         heal_hp += dice(self.heal_hp_dice)
     return heal_hp
Ejemplo n.º 14
0
 def get_heal_hp_dice(self, world, *args):
     heal_hp = 0
     if hasattr(self, 'heal_hp') and self.heal_hp:
         heal_hp += self.heal_hp
     if hasattr(self, 'heal_hp_dice') and self.heal_hp_dice:
         heal_hp += dice(self.heal_hp_dice)
     return heal_hp
Ejemplo n.º 15
0
 def create(cls,
            name,
            start_class,
            str_modify=0,
            dex_modify=0,
            con_modify=0,
            int_modify=0,
            wis_modify=0,
            chr_modify=0,
            **kwargs
         ):
     char_class = getattr(char_classes, start_class)()
     char = Character(
             name = name,
             classes = {
                 start_class: 1},
             unit_str = 10+str_modify,
             unit_dex = 10+dex_modify,
             unit_con = 10+con_modify,
             unit_int = 10+int_modify,
             unit_wis = 10+wis_modify,
             unit_chr = 10+chr_modify,
             **kwargs,
     )
     hp_max = dice(char_class.hit_dice) + char.con_modifier
     print("%s created, hit dice get %d hp" % (name, hp_max))
     if hp_max < 1:
         hp_max = 1
     char.unit_hp_max = hp_max
     char.unit_hp = hp_max
     # if char.unit_level > 1:
     #     char.gain_level(char.ask_next_level_class())
     return char
Ejemplo n.º 16
0
def build_deep_layers(net, params):
    # Build the hidden layers, sized according to the 'hidden_units' param.

    for layer_id, num_hidden_units in enumerate(params['hidden_units']):
        # net = tf.layers.dense(net, units=num_hidden_units, activation=tf.nn.relu, kernel_initializer=tf.glorot_uniform_initializer())
        net = tf.layers.dense(net, units=num_hidden_units, activation=None,
                              kernel_initializer=tf.glorot_uniform_initializer())
        net = dice(net, name='dice_' + str(layer_id))
    return net
Ejemplo n.º 17
0
def test(gpu, atlas_file, model_file):
    """
    model training function
    :param gpu: integer specifying the gpu to use
    :param atlas_file: atlas filename. So far we support npz file with a 'vol' variable
    :param model_file: the model directory to load from
    """

    os.environ["CUDA_VISIBLE_DEVICES"] = gpu
    device = "cuda"

    # Produce the loaded atlas with dims.:160x192x224.
    atlas = np.load(atlas_file)
    atlas_vol = atlas['vol']
    atlas_vol = atlas_vol[np.newaxis, ..., np.newaxis]
    atlas_seg = atlas['seg']

    # Test file and anatomical labels we want to evaluate
    test_file = open('../data/test_HCPChild.txt')
    test_strings = test_file.readlines()
    test_strings = [x.strip() for x in test_strings]
    good_labels = sio.loadmat('../data/labels.mat')['labels'][0]

    # Set up model
    criterion = LossFunction_mpr().cuda()
    model = MPR_net_HO(criterion)
    model.to(device)
    model.load_state_dict(
        torch.load(model_file, map_location=lambda storage, loc: storage))

    # set up atlas tensor
    input_fixed = torch.from_numpy(atlas_vol).to(device).float()
    input_fixed = input_fixed.permute(0, 4, 1, 2, 3)

    # Use this to warp segments
    trf = SpatialTransformer(atlas_vol.shape[1:-1], mode='nearest')
    trf.to(device)

    for k in range(0, len(test_strings)):

        vol_name, seg_name = test_strings[k].split(",")
        X_vol, X_seg = datagenerators.load_example_by_name(vol_name, seg_name)

        input_moving = torch.from_numpy(X_vol).to(device).float()
        input_moving = input_moving.permute(0, 4, 1, 2, 3)

        warp, flow, flow1, refine_flow1, flow2, refine_flow2 = model(
            input_moving, input_fixed)

        # Warp segment using flow
        moving_seg = torch.from_numpy(X_seg).to(device).float()
        moving_seg = moving_seg.permute(0, 4, 1, 2, 3)
        warp_seg = trf(moving_seg, flow).detach().cpu().numpy()

        vals, labels = dice(warp_seg, atlas_seg, labels=good_labels, nargout=2)
        print(np.mean(vals))
Ejemplo n.º 18
0
def detect_ng_failure(source, target, transformed_source, echo=True):
    failed = False
    ret_source, thresholded_source = threshold_calculation(source)
    ret_target, thresholded_target = threshold_calculation(target)
    ret_transformed_source, thresholded_transformed_source = threshold_calculation_with_threshold(
        transformed_source, ret_source)

    initial_dice = utils.dice(thresholded_source, thresholded_target)
    transformed_dice = utils.dice(thresholded_transformed_source,
                                  thresholded_target)
    if echo:
        print("Initial dice: ", initial_dice)
        print("Transformed dice: ", transformed_dice)
    score = transformed_dice
    if transformed_dice > initial_dice - 0.01 and transformed_dice > 0.5:
        failed = False
    else:
        failed = True
    success = not failed
    return score, success
Ejemplo n.º 19
0
def find_class_params(args, models):
    val_loader = get_train_val_loaders(args.encoder_types.split(',')[0], batch_size=args.batch_size)['valid']
    probs, masks = predict_loader(models, val_loader)
    print(probs.shape, masks.shape)

    valid_masks = []
    probabilities = np.zeros((2220, 350, 525))
    for i, (img_probs, img_masks) in enumerate(zip(probs, masks)):
        for m in img_masks:
            if m.shape != (350, 525):
                m = cv2.resize(m, dsize=(525, 350), interpolation=cv2.INTER_LINEAR)
            valid_masks.append(m)

        for j, probability in enumerate(img_probs):
            if probability.shape != (350, 525):
                probability = cv2.resize(probability, dsize=(525, 350), interpolation=cv2.INTER_LINEAR)
            probabilities[i * 4 + j, :, :] = probability

    print(len(valid_masks), len(probabilities), valid_masks[0].shape, probabilities[0].shape)
    class_params = {}
    for class_id in range(4):
        print(class_id)
        attempts = []
        for t in range(30, 90, 5):
            t /= 100
            #for ms in [0, 100, 1200, 5000, 10000]:
            for ms in [5000, 10000, 15000, 20000, 22500, 25000]:
            
                masks = []
                for i in range(class_id, len(probabilities), 4):
                    probability = probabilities[i]
                    predict, num_predict = post_process(probability, t, ms)
                    masks.append(predict)

                d = []
                for i, j in zip(masks, valid_masks[class_id::4]):
                    if (i.sum() == 0) & (j.sum() == 0):
                        d.append(1)
                    else:
                        d.append(dice(i, j))

                attempts.append((t, ms, np.mean(d)))

        attempts_df = pd.DataFrame(attempts, columns=['threshold', 'size', 'dice'])


        attempts_df = attempts_df.sort_values('dice', ascending=False)
        print(attempts_df.head())
        best_threshold = attempts_df['threshold'].values[0]
        best_size = attempts_df['size'].values[0]
        
        class_params[class_id] = (best_threshold, best_size)
    print(class_params)
    return class_params
Ejemplo n.º 20
0
 def create(cls, **kwargs):
     creature = cls(**kwargs)
     hp_max = 0
     for l in range(creature.unit_level):
         hp = dice(creature.creature_hit_dice) + creature.con_modifier
         if hp < 1:
             hp = 1
         hp_max += hp
     creature.unit_hp_max = hp_max
     creature.unit_hp = hp_max
     print("create %s with %d hp" % (creature.name, creature.unit_hp))
     return creature
Ejemplo n.º 21
0
def main():
    path_test = '/home/dongnie/Desktop/myPyTorch/pytorch-SRResNet/res_1106'
    ids = [1,2,3,4,6,7,8,10,11,12,13,29]
#     ids = [1,2,29]
    for ind in ids:    
#         mr_test_itk = sitk.ReadImage(os.path.join(path_test,'denseCrf3dSegmMap_pelvic_sub%d.nii.gz'%ind))
        mr_test_itk = sitk.ReadImage(os.path.join(path_test,'preSub_wce_wdice_adImpo_viewExp_1106_sub%d.nii.gz'%ind))
        crfSeg = sitk.GetArrayFromImage(mr_test_itk)
        ct_test_itk = sitk.ReadImage(os.path.join(path_test,'gt_sub%d.nii.gz'%ind))
        gtSeg = sitk.GetArrayFromImage(ct_test_itk)
                
        diceBladder = dice(crfSeg,gtSeg,1)
        diceProstate = dice(crfSeg,gtSeg,2)
        diceRectumm = dice(crfSeg,gtSeg,3)
     
        iouBladder = IoU(crfSeg,gtSeg,1)
        iouProstate = IoU(crfSeg,gtSeg,2)
        iouRectum = IoU(crfSeg,gtSeg,3)
        print 'sub%d'%ind,' iou1 = ',iouBladder,' iou2= ',iouProstate,' iou3= ',iouRectum
     
        print 'sub%d'%ind, 'dice1 = ',diceBladder,' dice2= ',diceProstate,' dice3= ',diceRectumm 
        print '\n'   
Ejemplo n.º 22
0
def train_step(net, imgs, labels, global_step, optimizer):
    """
    Train step for a batch
    """
    labels[labels >= 0.5] = 1.
    labels[labels < 0.5] = 0.
    labels = labels.astype('uint8')

    imgs = tf.image.convert_image_dtype(imgs, tf.float32)

    with tf.GradientTape() as tape:
        # Run image through segmentor net and get result
        seg_results = net(imgs)
        loss = tf.losses.sparse_softmax_cross_entropy(labels=tf.cast(
            labels, tf.int32),
                                                      logits=seg_results)

    grads = tape.gradient(loss, net.trainable_variables)

    optimizer.apply_gradients(zip(grads, net.trainable_variables),
                              global_step=global_step)

    batch_hds = []
    batch_IoUs = []
    batch_dices = []

    pred_np = seg_results.numpy()
    batch_size = pred_np.shape[0]
    pred_np = np.argmax(pred_np, axis=-1)
    pred_np = np.expand_dims(pred_np, -1)

    for i in range(batch_size):
        label_slice = labels[i]
        pred_slice = pred_np[i]
        pred_locations = np.argwhere(pred_slice == 1)
        label_locations = np.argwhere(label_slice == 1)

        hd = hausdorf_distance(pred_locations, label_locations)
        batch_hds.append(hd)

        IoU = iou(
            pred_slice, label_slice
        )  #np.sum(pred_slice[label_slice == 1]) / float(np.sum(pred_slice) + np.sum(label_slice) - np.sum(pred_slice[label_slice == 1]))
        Dice = dice(
            pred_slice, label_slice
        )  #np.sum(pred_slice[label_slice == 1])*2 / float(np.sum(pred_slice) + np.sum(label_slice))

        batch_IoUs.append(IoU)
        batch_dices.append(Dice)

    return loss, np.mean(batch_hds), np.mean(batch_IoUs), np.mean(batch_dices)
Ejemplo n.º 23
0
 def damage_roll(self,
                 attacker_id,
                 target_id,
                 skill_name,
                 skill_damage,
                 bonus_ability,
                 is_critical=False,
                 penalty=0):
     """
     skill_damage + related ability modifier
     if is_critical, twice die damage + 2 * all modifiers
     """
     attacker = self.unit_list[attacker_id]
     target = self.unit_list[target_id]
     base_damage = dice(skill_damage)
     modifier = getattr(attacker, bonus_ability.lower() + "_modifier")
     if is_critical:
         base_damage += dice(skill_damage)
         modifier += modifier
     damage = base_damage + modifier + penalty
     if damage < const.MINIMUN_DAMAGE:
         damage = MINIMUN_DAMAGE
     self.unit_list[target_id].unit_hp -= damage
     msg = ""
     is_dead = False
     if is_critical:
         msg += "(Critical Hit)"
     if target.unit_hp <= 0:
         msg += "Target died."
         is_dead = True
     target_hit_print(player_name=attacker.name,
                      action_name=skill_name,
                      target_name=target.name,
                      damage=damage,
                      target_hp=target.unit_hp,
                      message=msg)
     return is_dead
Ejemplo n.º 24
0
    def dialog_update(self):

        self.dialog_timer.update()

        if self.dialog_timer.check():
            self.dialog_timer.reset()
            self.close_dialog_timer.activate()
            self.msg = dice(len(self.dialogs) - 1)
            self.dialog_box = Dialog(self.window, Colors.WHITHE, 16, (10, 400),
                                     Colors.BLACK)

        self.close_dialog_timer.update()

        if self.close_dialog_timer.check():
            self.close_dialog_timer.reset()
            self.dialog_timer.activate()
            self.dialog_box = None
Ejemplo n.º 25
0
    def touch_attack_roll(self,
                    attacker_idx,
                    target_idx,
                    ability_to_attack,
                    attack_time=0,
                    critical_dice=[20],
                    penalty=0):
        """
        1d20. when get 1, always MISS.
              when get 20, maybe critical.
              when Base attack bonus + ability modifier + size modifier > target AC, HIT.
                   if also critical, then CRITICAL_HIT
              else if maybe critical, HIT.
                   else MISS
        """
        d20 = dice("1d20")
        maybe_critical = False
        if d20 == 1:
            return const.ATTACK_ROLL_FAILED
        if d20 in critical_dice:
            maybe_critical = True

        attacker = self.get_unit_by_idx(attacker_idx)
        target = self.get_unit_by_idx(target_idx)

        attack_bonus = (attacker.get_base_attack_bonus()[attack_time]
                + getattr(attacker, ability_to_attack.lower()+"_modifier")
                + attacker.size_modifier)
        target_ac = target.touch_armor_class

        attack_beat_ac = (d20 + attack_bonus + penalty) >= target_ac

        target_attack_print(player_name=attacker.name,
                            target_name=target.name)
        if attack_beat_ac:
            if maybe_critical:
                return const.ATTACK_ROLL_CRITICAL
            else:
                return const.ATTACK_ROLL_HIT
        else:
            if maybe_critical:
                return const.ATTACK_ROLL_HIT
            else:
                target_attack_failed_print(player_name=attacker.name,
                                    target_name=target.name)
                return const.ATTACK_ROLL_FAILED
Ejemplo n.º 26
0
    def touch_attack_roll(self,
                          attacker_idx,
                          target_idx,
                          ability_to_attack,
                          attack_time=0,
                          critical_dice=[20],
                          penalty=0):
        """
        1d20. when get 1, always MISS.
              when get 20, maybe critical.
              when Base attack bonus + ability modifier + size modifier > target AC, HIT.
                   if also critical, then CRITICAL_HIT
              else if maybe critical, HIT.
                   else MISS
        """
        d20 = dice("1d20")
        maybe_critical = False
        if d20 == 1:
            return const.ATTACK_ROLL_FAILED
        if d20 in critical_dice:
            maybe_critical = True

        attacker = self.get_unit_by_idx(attacker_idx)
        target = self.get_unit_by_idx(target_idx)

        attack_bonus = (attacker.get_base_attack_bonus()[attack_time] +
                        getattr(attacker,
                                ability_to_attack.lower() + "_modifier") +
                        attacker.size_modifier)
        target_ac = target.touch_armor_class

        attack_beat_ac = (d20 + attack_bonus + penalty) >= target_ac

        target_attack_print(player_name=attacker.name, target_name=target.name)
        if attack_beat_ac:
            if maybe_critical:
                return const.ATTACK_ROLL_CRITICAL
            else:
                return const.ATTACK_ROLL_HIT
        else:
            if maybe_critical:
                return const.ATTACK_ROLL_HIT
            else:
                target_attack_failed_print(player_name=attacker.name,
                                           target_name=target.name)
                return const.ATTACK_ROLL_FAILED
Ejemplo n.º 27
0
def cal_dice(img, svg, fsize, stride, im2col_fn, model, th=0.4):
    img_size = img.shape[1]
    rgbpatch = extract_rgbpatch(img, im2col_fn)
    patch_pred = model.predict(rgbpatch)
    pred_size = (img_size - fsize) / stride + 1
    patch_pred = patch_pred.reshape((pred_size, pred_size))
    region = np.zeros_like(patch_pred, np.uint8)
    ext = fsize / (8 * stride)
    for i in xrange(pred_size):
        for j in xrange(pred_size):
            bbox = vaild_bound(i - ext, j - ext, i + ext, j + ext, (pred_size, pred_size))
            neibs = patch_pred[bbox[0]:bbox[2], bbox[1]:bbox[3]]
            if np.count_nonzero(neibs > th) > np.prod(neibs.shape) / 2:
                region[i, j] = 1
    # region = dilation(region, square(5))
    # region = filter_region(region)
    svg = resize(svg, (pred_size, pred_size))
    return dice(region, svg)
Ejemplo n.º 28
0
def validate(net, data_loader):
    dices = []
    with torch.no_grad():
        for i, sample in enumerate(tqdm(data_loader)):
            mask = sample["mask"].numpy()
            imgs = sample["img"].cuda(non_blocking=True)
            cat_inp = sample["cat_inp"].cuda(non_blocking=True)
            coord_inp = sample["coord_inp"].cuda(non_blocking=True)
            nadir = sample["nadir"].cuda(non_blocking=True)
            out, nadir_pred = model(imgs, nadir, cat_inp, coord_inp)
            probs = torch.sigmoid(out)
            pred = probs.cpu().numpy() > 0.5
            for j in range(mask.shape[0]):
                dices.append(dice(mask[j, 0], pred[j, 0]))

    d = np.mean(dices)
    print("Val Dice: {0}".format(d))
    return d
Ejemplo n.º 29
0
def val(model, dataloader_val, args):

    dices = []
    for i_batch, sample_batched in enumerate(dataloader_val):
        inputs = Variable(sample_batched['image'].float()).cuda()
        labels = sample_batched['mask']
        period_names = sample_batched['period']

        with torch.no_grad():
            outputs = model(inputs)
            outputs = outputs.data.cpu().numpy()
        outputs[outputs > 0.5] = 1
        outputs[outputs <= 0.5] = 0
        result = np.squeeze(outputs)

        labels = labels.data.numpy()
        labels = np.squeeze(labels)
        dice = dice(labels, result, labels=[1.])[0]
        dices.append(dice)
       
    return np.asarray(dices).mean()
    def inference(self, train_loader, valid_loader, test_loader):
        raise NotImplementedError()

        print("Start Infernce")
        os.mkdir("%s/infer" % (self.save_path))
        os.mkdir("%s/infer/Train" % (self.save_path))
        os.mkdir("%s/infer/Valid" % (self.save_path))
        os.mkdir("%s/infer/Test" % (self.save_path))
        loaders = [("Train", train_loader), ("Valid", valid_loader),
                   ("Test", test_loader)]
        self.G.eval()
        with torch.no_grad():
            for path, loader in loaders:
                metric_avg, dice_avg = 0.0, 0.0
                for i, (input_, target_, f_name) in enumerate(loader):
                    input_, output_, target_ = self._test_foward(
                        input_, target_)

                    input_np = input_.type(
                        torch.FloatTensor).numpy()[0, self.z_idx, :, :]
                    target_np = utils.slice_threshold(target_[0, 0, :, :], 0.5)
                    output_np = utils.slice_threshold(output_[0, 0, :, :],
                                                      self.threshold)

                    jss = self.metric(output_np, target_np)
                    dice = utils.dice(output_np, target_np)

                    if dice != 1.0:
                        save_path = "%s/infer/%s/%s" % (self.save_path, path,
                                                        f_name[0][:-4])
                        input_np = (input_np - input_np.min()) / (
                            input_np.max() - input_np.min())
                        utils.image_save(save_path, input_np, target_np,
                                         output_np)

                    metric_avg += jss
                    dice_avg += dice
Ejemplo n.º 31
0
     data = np.zeros((1, 1, 512, 512))
     data[0, 0, :, :] = x1[:, :, i]
     data1 = torch.Tensor(data)
     data1 = data1.cuda()
     p = model(data1)
     p = p.cpu()
     s = p.data.numpy()
     P[i] = s
 print('P:' + str(Count))
 print(np.max(P))
 print(np.min(P))
 P[P > 0.5] = 1
 P[P <= 0.5] = 0
 T.append(P)
 pp = P.transpose(1, 2, 0)
 dice1 = dice(pp, y1)
 sen1 = sen(pp, y1)
 spe1 = spe(pp, y1)
 pre1 = pre(pp, y1)
 TolD = TolD + dice1
 TolS = TolS + sen1
 TolP = TolP + spe1
 TolR = TolR + pre1
 print(dice1)
 print(sen1)
 print(spe1)
 print(pre1)
 print(TolD / Count)
 print(TolS / Count)
 print(TolP / Count)
 print(TolR / Count)
def find_hard_sample():

    # create dir
    if not os.path.exists(args.hard_sample_dir):
        os.mkdir(args.hard_sample_dir)
    hard_sample_dir_hq = args.hard_sample_dir + '_hq'
    if not os.path.exists(hard_sample_dir_hq):
        os.mkdir(hard_sample_dir_hq)

    model = UNet(args.input_size, False)
    output_logits = model.output_mask
    output_prob = tf.sigmoid(output_logits)

    sess = tf.Session()
    saver = tf.train.Saver()
    saver.restore(sess, args.model_file)

    img_count = 0
    hard_img_count = 0
    dice_sum = 0
    gt_list = os.listdir(args.gt_dir)

    for gt_mask_name in gt_list:
        img_count += 1
        img_name = gt_mask_name.split('_mask')[0] + '.jpg'
        gt_mask_path = os.path.join(args.gt_dir, gt_mask_name)

        # read in gt mask
        gt_mask = cv2.imread(gt_mask_path)
        gt_mask = gt_mask[:, :, 0] / 255.0

        # read in test img
        img_path = os.path.join(args.input_dir, img_name)
        orig_img = cv2.imread(img_path)
        img = cv2.resize(orig_img, (args.input_size, args.input_size))
        img = img.astype(np.float32)
        img /= 255.0
        img -= 0.5
        img = np.expand_dims(img, axis=0)

        output_prob_np = sess.run([output_prob], feed_dict={model.input: img})
        output_prob_np = output_prob_np[0]

        output_prob_np = cv2.resize(output_prob_np[0], (1918, 1280))
        output_mask = (output_prob_np > args.thresh).astype(np.uint8)
        dice_acc = dice(output_mask, gt_mask)
        dice_sum += dice_acc

        if dice_acc < args.hard_thresh:
            hard_img_count += 1
            print('dice {}'.format(dice_acc))
            print('hard img ratio {} / {}'.format(hard_img_count, img_count))
            input_dir_hq = args.input_dir + '_hq'
            hard_sample_dir_hq = args.hard_sample_dir + '_hq'

            src = os.path.join(args.input_dir, img_name)
            dst = os.path.join(args.hard_sample_dir, img_name)
            src_hq = os.path.join(input_dir_hq, img_name)
            dst_hq = os.path.join(hard_sample_dir_hq, img_name)

            copyfile(src, dst)
            copyfile(src_hq, dst_hq)
    print('mean dice {}'.format(dice_sum / len(gt_list)))
Ejemplo n.º 33
0
    def update(self, position, elapsed_time):
        self.sprites.clear()
        self.position = position
        self.frame_index += 1

        if self.frame_index == (self.sprite_sheets[self.motion_type].size[0] / self.sprite_size):
            self.frame_index = 0

        if not self.moving:
            move = dice(self.move_rate)
            if move[0] == self.move_rate - 1:
                self.moving = True
                self.walk_frames = 60
                facing = dice(7)
                self.facing = facing[0]

        if self.moving:
            if self.walk_frames:

                if self.facing == 0:
                    self.movement[0] -= 2
                    self.movement[1] += 1
                elif self.facing == 1:
                    self.movement[1] += 1
                elif self.facing == 2:
                    self.movement[0] += 2
                    self.movement[1] += 1
                elif self.facing == 3:
                    self.movement[0] += 2
                elif self.facing == 4:
                    self.movement[0] += 2
                    self.movement[1] -= 1
                elif self.facing == 5:
                    self.movement[1] -= 1
                elif self.facing == 6:
                    self.movement[0] -= 2
                    self.movement[1] -= 1
                elif self.facing == 7:
                    self.movement[0] -= 2

                self.walk_frames -= 1
            else:
                self.moving = False

        x = int((int(self.start_pos[0]) + self.position[0] + self.movement[0]) - (self.sprite_size / 2))
        y = int((int(self.start_pos[1]) + self.position[1] + self.movement[1]) - (self.sprite_size / 2))

        self.position = x, y

        sprite_sheet = self.sprite_sheets[self.motion_type]

        sprite_crop = [self.frame_index * self.sprite_size,
                       self.facing * self.sprite_size,
                       self.sprite_size,
                       self.sprite_size]

        sprite = sprite_sheet.subsprite(sprite_crop)
        sprite.position = self.position
        self.sprites.append(sprite)

        """
Ejemplo n.º 34
0
def segment_val(epoch):
    global train_dataloader
    global val_dataloader
    global test_dataloader
    global model
    global loss_func
    global optimizer
    global best_metric
    model.eval()
    torch.set_grad_enabled(False)
    corrects = 0
    total = 0
    test_loss = 0
    tp = 0
    tn = 0
    fp = 0
    fn = 0
    for step, batch in enumerate(val_dataloader):
        x, y = batch
        x = x.cuda()
        y = y.cuda()
        preds = model(x)
        loss = loss_func(preds, y)
        test_loss += loss.item()
        total += y.size(0)
        total += x.size(0)
        corrects += seg_accuracy(preds, y) * x.size(0)
        tp_, tn_, fp_, fn_, _dice = dice(y, preds, supervised=True, target=1)
        tp += tp_
        tn += tn_
        fp += fp_
        fn += fn_

    acc = corrects / total
    test_loss = test_loss / total
    epsilon = 1e-7
    precision = tp / (tp + fp + epsilon)
    recall = tp / (tp + fn + epsilon)
    dice_ = (2 * (precision * recall) / (precision + recall + epsilon))

    if base_config['metric'] == 'acc':
        metric = acc
    elif base_config['metric'] == 'dice':
        metric = dice_.detach().cpu().numpy()
    elif 'loss' in base_config['metric']:
        metric = test_loss
    else:
        logging.error('{} metric is not supported now'.format(
            base_config['metric']))
    if opt(metric, best_metric):
        model_path = os.path.join(base_config['model_path'],
                                  '{}_best_pt'.format(config['model']))
        print('Saving model to {}'.format(model_path))
        state = {
            'net': model.state_dict(),
            'acc': acc,
            'dice': dice_,
            'loss': test_loss,
            'epoch': epoch,
        }
        torch.save(state, model_path)
        best_metric = metric
    return metric, best_metric
Ejemplo n.º 35
0
def find_class_params(args):
    runner = SupervisedRunner()
    model = create_model(args.encoder_type)
    valid_loader = get_train_val_loaders(args.encoder_type,
                                         batch_size=args.batch_size)['valid']

    encoded_pixels = []
    loaders = {"infer": valid_loader}
    runner.infer(
        model=model,
        loaders=loaders,
        callbacks=[CheckpointCallback(resume=args.ckp),
                   InferCallback()],
    )
    print(runner.callbacks)
    valid_masks = []
    probabilities = np.zeros((2220, 350, 525))
    for i, (batch, output) in enumerate(
            tqdm(
                zip(valid_loader.dataset,
                    runner.callbacks[0].predictions["logits"]))):
        image, mask = batch
        for m in mask:
            if m.shape != (350, 525):
                m = cv2.resize(m,
                               dsize=(525, 350),
                               interpolation=cv2.INTER_LINEAR)
            valid_masks.append(m)

        for j, probability in enumerate(output):
            if probability.shape != (350, 525):
                probability = cv2.resize(probability,
                                         dsize=(525, 350),
                                         interpolation=cv2.INTER_LINEAR)
            probabilities[i * 4 + j, :, :] = probability

    class_params = {}
    for class_id in range(4):
        print(class_id)
        attempts = []
        for t in range(0, 100, 5):
            t /= 100
            #for ms in [0, 100, 1200, 5000, 10000]:
            for ms in [5000, 10000, 15000, 20000, 22500, 25000, 30000]:

                masks = []
                for i in range(class_id, len(probabilities), 4):
                    probability = probabilities[i]
                    predict, num_predict = post_process(
                        sigmoid(probability), t, ms)
                    masks.append(predict)

                d = []
                for i, j in zip(masks, valid_masks[class_id::4]):
                    if (i.sum() == 0) & (j.sum() == 0):
                        d.append(1)
                    else:
                        d.append(dice(i, j))

                attempts.append((t, ms, np.mean(d)))

        attempts_df = pd.DataFrame(attempts,
                                   columns=['threshold', 'size', 'dice'])

        attempts_df = attempts_df.sort_values('dice', ascending=False)
        print(attempts_df.head())
        best_threshold = attempts_df['threshold'].values[0]
        best_size = attempts_df['size'].values[0]

        class_params[class_id] = (best_threshold, best_size)
    print(class_params)
    return class_params, runner
Ejemplo n.º 36
0
def main():
    in_z = 0
    volpath = os.path.join(args.datapath, 'train')
    segpath = volpath

    batch_size = args.batch_size
    orig_dim = 256
    sqr = transforms.Square()
    aff = transforms.Affine()
    crop = transforms.RandomCrop(224)
    scale = transforms.Scale(orig_dim)
    rotate = transforms.Rotate(0.5, 30)
    noise = transforms.Noise(0.02)
    flip = transforms.Flip()
    transform_plan = [sqr, scale, aff, rotate, crop, flip, noise]
    lr = 1e-4
    series_names = ['Mag']
    seg_series_names = ['AV']

    model = models.Net23(2)

    model.cuda()
    optimizer = optim.RMSprop(model.parameters(), lr=lr)

    out_z, center_crop_sz = utils.get_out_size(orig_dim, in_z,\
            transform_plan, model)

    t0 = time.time()

    counter = 0
    print_interval = args.log_interval
    model.train()
    for i in range(200000000000000000000000000000000):
        weight = torch.FloatTensor([0.2, 0.8]).cuda()
        vol, seg, inds = preprocess.get_batch(volpath, segpath, batch_size, in_z,\
                out_z, center_crop_sz, series_names, seg_series_names,\
                transform_plan, 8, nrrd=True)
        vol = torch.unsqueeze(vol, 1)
        vol = Variable(vol).cuda()
        seg = Variable(seg).cuda()

        out = model(vol).squeeze()

        loss = F.cross_entropy(out, seg, weight=weight)

        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        counter += 1

        sys.stdout.write('\r{:.2f}%'.format(counter * batch_size /
                                            print_interval))
        sys.stdout.flush()

        if counter * batch_size >= print_interval and i > 0:

            seg_hot = utils.get_hot(seg.data.cpu().numpy(), out.size()[-3])
            seg_hot = np.transpose(seg_hot, axes=[1, 0, 2, 3])
            out_hot = np.argmax(out.data.cpu().numpy(), axis=1)
            out_hot = utils.get_hot(out_hot, out.size()[-3])
            out_hot = np.transpose(out_hot, axes=[1, 0, 2, 3])

            dce2 = utils.dice(seg_hot[:, 1:], out_hot[:, 1:])

            vol_ind = utils.get_liver(seg)
            v = vol.data.cpu().numpy()[vol_ind].squeeze()
            real_seg = seg[vol_ind].data.cpu().numpy()
            out_plt = out.data.cpu().numpy()[vol_ind]
            out_plt = np.argmax(out_plt, axis=0)
            fake_seg = out_plt
            masked_real = utils.makeMask(v, real_seg, out.size()[-3], 0.5)
            masked_fake = utils.makeMask(v, fake_seg, out.size()[-3], 0.5)

            fig = plt.figure(1)
            fig.suptitle('Volume {} ; Dice = {:.2f}'.format(\
                    inds[vol_ind],dce2))
            v = fig.add_subplot(1, 2, 1)
            v.set_title('real')
            plt.imshow(masked_real)
            sreal = fig.add_subplot(1, 2, 2)
            sreal.set_title('fake')
            plt.imshow(masked_fake)
            outfile = os.path.join(args.datapath, 'out.png')
            plt.savefig(outfile, dpi=200)
            plt.clf()

            print(('\rIteration {}: Block completed in {:.2f} sec ; Loss = {:.2f}')\
                    .format(i*batch_size,time.time()-t0, loss.data.cpu()[0]))
            checkpoint_file = os.path.join(args.datapath,\
                    'model_checkpoint.pth')
            torch.save(model.state_dict(), checkpoint_file)
            counter = 0

            t0 = time.time()
Ejemplo n.º 37
0
    def update(self, position, elapsed_time):

        self.position = position

        self.frame_index += 1

        if self.frame_index == (self.sprite_sheets[self.motion_type].size[0] /
                                self.sprite_size):
            self.frame_index = 0

        if not self.moving:
            move = dice(200)
            if move[0] == 200:
                self.moving = True
                self.walk_frames = 60
                facing = dice(Facing.COUNT - 1)
                self.facing = facing[0]

        if self.moving:
            if self.walk_frames:
                self.motion_type = MotionType.WALKING

                if self.facing == 0:
                    # print("LEFT_DOWN")
                    self.movement[0] -= 2
                    self.movement[1] += 1
                    self.facing = Facing.LEFT_DOWN
                elif self.facing == 1:
                    # print("DOWN")
                    self.movement[1] += 1
                    self.facing = Facing.DOWN
                elif self.facing == 2:
                    # print("RIGHT_DOWN")
                    self.movement[0] += 2
                    self.movement[1] += 1
                    self.facing = Facing.RIGHT_DOWN
                elif self.facing == 3:
                    # print("RIGHT")
                    self.movement[0] += 2
                    self.facing = Facing.RIGHT
                elif self.facing == 4:
                    # print("RIGHT_UP")
                    self.movement[0] += 2
                    self.movement[1] -= 1
                    self.facing = Facing.RIGHT_UP
                elif self.facing == 5:
                    # print("UP")
                    self.movement[1] -= 1
                    self.facing = Facing.UP
                elif self.facing == 6:
                    # print("LEFT_UP")
                    self.movement[0] -= 2
                    self.movement[1] -= 1
                    self.facing = Facing.LEFT_UP
                elif self.facing == 7:
                    # print("LEFT")
                    self.movement[0] -= 2
                    self.facing = Facing.LEFT

                self.walk_frames -= 1
            else:
                self.moving = False
                self.motion_type = MotionType.STANDING

        self.dialog_update()