Exemplo n.º 1
0
def get_bbs(sess, imgs_, labels_, return_center_crop_on_failure=True):
    global DID_INIT
    if not DID_INIT:
        #sess.run(tf.global_variables_initializer())
        tf.train.Saver(
            tf.get_collection(
                tf.GraphKeys.GLOBAL_VARIABLES, 'masker')).restore(
                    sess,
                    tf.train.get_checkpoint_state(
                        'temp_ckpts/masker1/').model_checkpoint_path)
        sess.run(tf.group(*to_init))
        sess.run(set_embedding,
                 {pretrained_embedding: np.load('temp_ckpts/clsemb.npy')})
        print 'Finished inits...'
        DID_INIT = True
    masks = sess.run(mask, {images: imgs_, labels: labels_})
    for e in xrange(len(masks)):
        r = np.concatenate((imagenet.to_bgr_img(imgs_[e]), (np.zeros(
            (224, 224, 3)) + np.expand_dims(
                (255 * masks[e]), 2)).astype(np.uint8)), 0)
        cv2.imwrite('ss%d.jpg' % e, r)
    # exit()
    bbs = []
    for m in masks:
        # for each generated mask perform density based clustering and return...
        bbs.append(
            utils.bounding_box.simple_box_from_mask(
                m,
                threshold=0.5,
                min_members=300,
                return_center_crop_on_failure=return_center_crop_on_failure))
    return bbs, masks
Exemplo n.º 2
0

def prepare_adv(sess, target):
    for n in xrange(10):
        _, p = sess.run([adv_opt, ev_probs[adv_target]], {adv_target: target})
        print p


sess = tf.Session()
sess.run(tf.global_variables_initializer())
sess.run(to_init)

print 'Initial prob', get_crop_probs(sess, dog)[203]
sess.run(set_img_var, {new_image: dog})

cv2.imshow('Initial image', imagenet.to_bgr_img(sess.run(img_var)))
cv2.waitKey(1000000)

prepare_adv(sess, 203)
best = np.argmax(sess.run(ev_probs))
print 'Highest prob class', best, imagenet.CLASS_ID_TO_NAME[best], sess.run(
    ev_probs)[best]
cv2.imshow('Adversarial image', imagenet.to_bgr_img(sess.run(img_var)))
cv2.waitKey(1000000)

b, mask = sal.get_bbs(sess, sess.run(expanded_img_var), (440, ))
print b
cv2.imshow('Mask for random (beer bottle) label', mask[0])
cv2.waitKey(1000000)

b, mask = sal.get_bbs(sess, sess.run(expanded_img_var), (203, ))
Exemplo n.º 3
0
         np.concatenate(map(imagenet.LABEL_VAL_PIPELINE, paths), 0)
     })
 sess.run(reposition_vals)
 i = 1
 while i < 301:
     _, al, sl, pl, dl, pi, di, mi = sess.run([
         opt_op, area_loss, smoothness_loss, preservation_loss,
         destroyer_loss, preserved_images, destroyed_images,
         showable_masks
     ])
     sess.run(reposition_vals)
     print i, 'A: %.2f   S: %.2f   P: %.3f    D: %.3f' % (al, sl, pl,
                                                          dl)
     if not i % 300:
         for example, path in enumerate(paths):
             pi0 = imagenet.to_bgr_img(pi[example])
             di0 = imagenet.to_bgr_img(di[example])
             box = utils.bounding_box.box_from_mask(mi[example, :, :,
                                                       0])
             gt_boxes, (
                 width,
                 height) = GT_BOXES[imagenet.img_num_from_path(path)]
             scale = 224. / min(width, height)
             ious = []
             for gt_box in gt_boxes:
                 x_offset, y_offset = (width - min(
                     width, height)) / 2., (height -
                                            min(width, height)) / 2.
                 _gt_box_in_local = int(
                     (gt_box[0] - x_offset) * scale), int(
                         (gt_box[1] - y_offset) * scale), int(
Exemplo n.º 4
0
     sess, np.array(map(lambda x: x.get_resulting_img(), imcs)),
     np.array(labels))
 k = 0
 for imc, label, gt_boxes_size, _local_box in zip(imcs, labels,
                                                  gt_boxes_sizes,
                                                  _local_boxes):
     gt_boxes, size = gt_boxes_size
     total_area = float(size[0] * size[1])
     im = imc.get_resulting_img()
     box = imc.from_local_coords(_local_box)
     # local box may be invalid (outside actual image area)
     local_box = imc.to_local_coords(box)  # this one is valid
     ious = []
     gt_scores_ = []
     gt_a_ = []
     showable = imagenet.to_bgr_img(im.copy())
     for gt_box in gt_boxes:
         p = get_crop_probs(sess, imc.original_image, gt_box)[label]
         a = utils.bounding_box.area(gt_box) / total_area
         showable = utils.bounding_box.draw_box(showable,
                                                imc.to_local_coords(gt_box),
                                                color='red',
                                                text='P: %.2f' % p)
         ious.append(utils.bounding_box.intersection_over_union(
             gt_box, box))
         gt_scores_.append(calc_score(a, p))
         gt_a_.append(math.log(max(a, 0.05)))
     gt_a.append(np.mean(gt_a_))
     iou = max(ious)
     p = get_crop_probs(sess, imc.original_image, box)[label]
     a = utils.bounding_box.area(box) / total_area
Exemplo n.º 5
0
def get_bbs(sess, im, label):
    conv = utils.fixed_image.ImageResizer(imagenet.from_bgr_normalize_only(im), (224, 224))
    xy_, mask_ = sal.get_bbs(sess, np.expand_dims(conv.get_resulting_img(), 0), [label], return_center_crop_on_failure=False)
    xy = xy_[0]
    if xy is None:
        return None
    return conv.from_local_coords(xy), mask_

sess = tf.Session()
im = cv2.imread('tests/catdog.jpg')
showable = utils.fixed_image.ImageResizer(imagenet.from_bgr_normalize_only(im), (224, 224)).get_resulting_img()

while 1:
    sel = int(raw_input('? => '))
    box, mask = get_bbs(sess, im, sel)
    mask = np.reshape(mask, (224, 224, 1))
    print 'Looking for', imagenet.CLASS_ID_TO_NAME[sel]
    if box is not None:
        print 'Found'
        cv2.imwrite('mask.png', np.concatenate((255*mask, np.zeros((224,224,2), dtype=np.float32)), 2).astype(np.uint8))
        cv2.imwrite('original.png', imagenet.to_bgr_img(showable))
        cv2.imwrite('preserved.png', imagenet.to_bgr_img(mask*showable))
        cv2.imwrite('destroyed.png', imagenet.to_bgr_img((1-mask) * showable))
        show = im.copy()/255.
        cv2.imshow('aa', utils.bounding_box.draw_box(show, box))
        cv2.waitKey(30000)
        cv2.destroyWindow('aa')
    else:
        print 'Not found'