コード例 #1
0
def test_guided_backprop():
    # placeholder for input image
    image = tf.placeholder(tf.float32, shape=[None, None, None, 3])
    # initialize input dataflow
    # change '.png' to other image types if other types of images are used
    input_im = ImageFromFile('.png', data_dir=IMPATH,
                             num_channel=3, shuffle=False)
    # batch size has to be one
    input_im.set_batch_size(1)

    # initialize guided back propagation class
    # use VGG19 as an example
    # images will be rescaled to smallest side = 224 is is_rescale=True
    model = GuideBackPro(vis_model=VGG19_FCN(is_load=False,
                                             is_rescale=True))

    # get op to compute guided back propagation map
    # final output respect to input image
    back_pro_op = model.get_visualization(image)

    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())

        im = input_im.next_batch()[0]
        guided_backpro, label, o_im =\
            sess.run([back_pro_op, model.pre_label,
                     model.input_im],
                     feed_dict={image: im})
        print(label)
    tf.reset_default_graph()
コード例 #2
0
def test_gradcam():

    # merge several output images in one large image
    merge_im = 1
    grid_size = np.ceil(merge_im**0.5).astype(int)

    # class label for Grad-CAM generation
    # 355 llama 543 dumbbell 605 iPod 515 hat 99 groose 283 tiger cat
    # 282 tabby cat 233 border collie 242 boxer
    # class_id = [355, 543, 605, 515]
    class_id = [283, 242]

    # initialize Grad-CAM
    # using VGG19
    gcam = ClassifyGradCAM(vis_model=VGG19_FCN(is_load=False, is_rescale=True))
    gbackprob = GuideBackPro(
        vis_model=VGG19_FCN(is_load=False, is_rescale=True))

    # placeholder for input image
    image = tf.placeholder(tf.float32, shape=[None, None, None, 3])

    # create VGG19 model
    gcam.create_model(image)
    gcam.setup_graph()

    # generate class map and prediction label ops
    map_op = gcam.get_visualization(class_id=class_id)
    label_op = gcam.pre_label

    back_pro_op = gbackprob.get_visualization(image)

    # initialize input dataflow
    # change '.png' to other image types if other types of images are used
    input_im = ImageFromFile('.png',
                             data_dir=IMPATH,
                             num_channel=3,
                             shuffle=False)
    input_im.set_batch_size(1)

    with tf.Session() as sess:

        sess.run(tf.global_variables_initializer())

        cnt = 0
        merge_cnt = 0
        o_im_list = []
        im = input_im.next_batch()[0]
        gcam_map, b_map, label, o_im =\
            sess.run([map_op, back_pro_op, label_op, gcam.input_im],
                     feed_dict={image: im})
        print(label)
        o_im_list.extend(o_im)
        for idx, cid, cmap in zip(count(), gcam_map[1], gcam_map[0]):
            overlay_im = image_overlay(cmap, o_im)
            weight_im = image_weight_mask(b_map[0], cmap)
            try:
                weight_im_list[idx].append(weight_im)
                overlay_im_list[idx].append(overlay_im)
            except NameError:
                gcam_class_id = gcam_map[1]
                weight_im_list = [[] for i in range(len(gcam_class_id))]
                overlay_im_list = [[] for i in range(len(gcam_class_id))]
                weight_im_list[idx].append(weight_im)
                overlay_im_list[idx].append(overlay_im)
    tf.reset_default_graph()
コード例 #3
0
                              num_channel=3)
    display_data(test_data, 'test_data')

    word_dict = get_word_list('../data/imageNetLabel.txt')

    model.create_model([image, 1])
    test_op = tf.nn.top_k(tf.nn.softmax(model.layer['output']),
                          k=5,
                          sorted=True)
    input_op = model.layer['input']

    writer = tf.summary.FileWriter(conf.SAVE_DIR)

    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        writer.add_graph(sess.graph)

        for k in range(0, 50):
            if test_data.epochs_completed < 1:
                batch_data = test_data.next_batch()
                im = batch_data[0]
                im = resize_image_with_smallest_side(im, 224)
                # im = center_crop_image(im, 224, 224)
                # scipy.misc.imsave('{}test_{}.png'.format(conf.SAVE_DIR, k),
                #                   np.squeeze(im))
                result = sess.run(test_op, feed_dict={image: im})
                for val, ind in zip(result.values, result.indices):
                    print(val)
                    print(ind)
                    print(word_dict[ind[0]])
コード例 #4
0
    back_pro_op[0]: list中每個值,是激活值對輸入圖片透過導向反向傳播求導值,
                    為和輸入一樣大的矩陣,分別為激活值對每個像素的導數
    back_pro_op[1]: list中每個值,是該輸入圖所對應的類別,
                    有指定class_id: 是該class_id
                    沒指定class_id: 分類的結果
    """
    back_pro_op = model.get_visualization(image)

    #writer = tf.summary.FileWriter(config.save_path)
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        #writer.add_graph(sess.graph)

        cnt = 0
        while input_im.epochs_completed < 1:
            im = input_im.next_batch()[0]
            
            guided_backpro, label, o_im =\
                sess.run([back_pro_op, model.pre_label, model.input_im],
                         feed_dict={image: im})
            print("\n第%d張影像前%d類分類結果:"%(cnt, len(label[0][0])))
            for i in range(len(label[0][0])):
                print("置信度:%5.2f%% 類別(%3d):%s"%(label[0][0][i]*100, label[1][0][i], C.class_name[label[1][0][i]]))
            
            
            for cid, guided_map in zip(guided_backpro[1], guided_backpro[0]):
                scipy.misc.imsave(
                    '{}map_{}_class_{}.png'.format(config.save_path_guided + 'guided/', cnt, cid),
                    np.squeeze(guided_map))
            scipy.misc.imsave('{}im_{}.png'.format(config.save_path_guided + 'org/', cnt),
                              np.squeeze(o_im))