Exemplo n.º 1
0
def plot_saliency():
    with CustomObjectScope({
            'Dilation2D': Dilation2D,
            'Erosion2D': Erosion2D,
            'attach_attention_module': attach_attention_module
    }):
        model = load_model(
            '/home/CVL1/Shaobo/PneSeg/model2/RSNA_seg_Shaobo.h5')
    model.summary()
    gb = get_batch(valid_filenames, 250)
    abatch = next(gb)
    imgs = abatch[0]['image_in']
    msks = abatch[1]['segmentation']
    labels = abatch[1]['classification']
    plt.figure(figsize=(40, 20))
    n = 3
    for i in range(n):
        img = imgs[i]

        msk = msks[i]
        class_label = labels[i]
        img1 = np.reshape(img, [height, width])
        msk = np.reshape(msk, [height, width])
        comp = msk > 0.5 / 1.
        comp1 = measure.label(comp)

        predImg = np.reshape(imgs[i], [1, height, width, 1])
        seg_pre, class_pre = model.predict(predImg)
        pred = np.reshape(seg_pre, [height, width])
        pred = (pred > 0.5) / 1.
        pred1 = measure.label(pred)

        class_idx = np.where(class_label == 1.)[0]

        ax1 = plt.subplot(3, n, i + 1)
        ax1.imshow(img1)
        ax1.set_title('Seg_Result ' + str(class_idx))
        #ax1.get_xaxis().set_visible(False)
        #ax1.get_yaxis().set_visible(False)
        for region in measure.regionprops(comp1):
            # retrieve x, y, height and width
            y11, x11, y12, x12 = region.bbox
            height1 = y12 - y11
            width1 = x12 - x11
            ax1.add_patch(
                patches.Rectangle((x11, y11),
                                  width1,
                                  height1,
                                  linewidth=1,
                                  edgecolor='b',
                                  facecolor='none'))
        for region in measure.regionprops(pred1):
            # retrieve x, y, height and width
            y21, x21, y22, x22 = region.bbox
            height2 = y22 - y21
            width2 = x22 - x21
            ax1.add_patch(
                patches.Rectangle((x21, y21),
                                  width2,
                                  height2,
                                  linewidth=1,
                                  edgecolor='r',
                                  facecolor='none'))

    #class_idx = np.where(class_label == 1.)[0]
        print(class_label)
        print(class_idx[0])
        layer_idx = utils.find_layer_idx(model, 'classification')
        print(layer_idx)
        model.layers[layer_idx].activation = activations.linear
        model = utils.apply_modifications(model,
                                          custom_objects={
                                              'Dilation2D': Dilation2D,
                                              'Erosion2D': Erosion2D
                                          })
        img2 = resize(imgs[i], (height, width))

        grads = visualize_saliency(model,
                                   layer_idx,
                                   filter_indices=class_idx[0],
                                   seed_input=img2)
        #grads = visualize_cam(model1, layer_idx, filter_indices= class_idx[0], seed_input = img2, backprop_modifier='guided')
        ax2 = plt.subplot(3, n, i + 1 + n)
        ax2.imshow(grads, cmap='jet')
        ax2.set_title('Saliency Map')
        #ax4.get_xaxis().set_visible(False)
        # ax4.get_yaxis().set_visible(False)
        for region in measure.regionprops(comp1):
            y11, x11, y12, x12 = region.bbox
            height1 = y12 - y11
            width1 = x12 - x11
            ax2.add_patch(
                patches.Rectangle((x11, y11),
                                  width1,
                                  height1,
                                  linewidth=1,
                                  edgecolor='g',
                                  facecolor='none'))
        for region in measure.regionprops(pred1):
            # retrieve x, y, height and width
            y21, x21, y22, x22 = region.bbox
            height2 = y22 - y21
            width2 = x22 - x21
            ax2.add_patch(
                patches.Rectangle((x21, y21),
                                  width2,
                                  height2,
                                  linewidth=1,
                                  edgecolor='r',
                                  facecolor='none'))

        model.layers[layer_idx].activation = keras.activations.linear
        model = utils.apply_modifications(model,
                                          custom_objects={
                                              'Dilation2D': Dilation2D,
                                              'Erosion2D': Erosion2D
                                          })
        #print(layer_idx)
        #model.layers[layer_idx].activation = activations.linear
        penultimate_layer = utils.find_layer_idx(model, 'down4')
        print(penultimate_layer)
        #model = utils.apply_modifications(model)
        img2 = resize(imgs[i], (height, width))

        grads = visualize_cam(model,
                              layer_idx,
                              filter_indices=class_idx[0],
                              seed_input=img2,
                              penultimate_layer_idx=penultimate_layer)
        ax3 = plt.subplot(3, n, i + 1 + 2 * n)
        ax3.imshow(grads, cmap='jet', alpha=0.8)

        #ax4.set_title('vanilla')
        #ax4.get_xaxis().set_visible(False)
        #ax4.get_yaxis().set_visible(False)
        for region in measure.regionprops(comp1):
            y11, x11, y12, x12 = region.bbox
            height1 = y12 - y11
            width1 = x12 - x11
            ax3.add_patch(
                patches.Rectangle((x11, y11),
                                  width1,
                                  height1,
                                  linewidth=1,
                                  edgecolor='g',
                                  facecolor='none'))
        for region in measure.regionprops(pred1):
            # retrieve x, y, height and width
            y21, x21, y22, x22 = region.bbox
            height2 = y22 - y21
            width2 = x22 - x21
            ax3.add_patch(
                patches.Rectangle((x21, y21),
                                  width2,
                                  height2,
                                  linewidth=1,
                                  edgecolor='r',
                                  facecolor='none'))

    plt.show()
    plt.savefig('/home/CVL1/Shaobo/PneSeg/model2/test.jpg')
Exemplo n.º 2
0
def plot_saliency():
    with CustomObjectScope(
        {'attach_attention_module': attach_attention_module}):
        model = load_model('/home/CVL1/Shaobo/mri/h5File/mri.h5')
    model.summary()
    gb = get_batch(valid_filenames, 5)
    abatch = next(gb)
    imgs = abatch[0]['image_in']
    msks = abatch[1]['segmentation']
    labels = abatch[1]['classification']
    plt.figure(figsize=(40, 20))
    n = 3
    for i in range(n):
        img = imgs[i]

        msk = msks[i]
        class_label = labels[i]
        img1 = np.reshape(img, [height, width])
        msk = np.reshape(msk, [height, width])
        comp = msk > 0.65 / 1.
        comp1 = measure.label(comp)

        predImg = np.reshape(imgs[i], [1, height, width, 1])
        seg_pre, class_pre = model.predict(predImg)
        pred = np.reshape(seg_pre, [height, width])
        pred = (pred > 0.65) / 1.
        pred1 = measure.label(pred)
        class_idx = np.where(class_label == 1.)[0]

        ax1 = plt.subplot(4, n, i + 1)
        ax1.imshow(img1)
        ax1.set_title('Seg_Result ' + str(class_idx))
        #ax1.get_xaxis().set_visible(False)
        #ax1.get_yaxis().set_visible(False)

        print(class_label)
        print(class_idx[0])
        layer_idx = utils.find_layer_idx(model, 'classification')
        print(layer_idx)
        model.layers[layer_idx].activation = activations.linear
        model = utils.apply_modifications(model,
                                          custom_objects={
                                              'attach_attention_module':
                                              attach_attention_module
                                          })
        img2 = resize(imgs[i], (height, width))

        grads = visualize_saliency(model,
                                   layer_idx,
                                   filter_indices=class_idx[0],
                                   seed_input=img2)
        #grads = visualize_cam(model1, layer_idx, filter_indices= class_idx[0], seed_input = img2, backprop_modifier='guided')
        ax2 = plt.subplot(4, n, i + 1 + n)
        ax2.imshow(grads, cmap='jet')
        ax2.set_title('Saliency Map')
        #ax4.get_xaxis().set_visible(False)
        # ax4.get_yaxis().set_visible(False)

        model.layers[layer_idx].activation = keras.activations.linear
        model = utils.apply_modifications(model,
                                          custom_objects={
                                              'attach_attention_module':
                                              attach_attention_module
                                          })
        #print(layer_idx)
        #model.layers[layer_idx].activation = activations.linear
        penultimate_layer = utils.find_layer_idx(model, 'down3')
        print(penultimate_layer)
        #model = utils.apply_modifications(model)
        img2 = resize(imgs[i], (height, width))

        grads = visualize_cam(model,
                              layer_idx,
                              filter_indices=class_idx[0],
                              seed_input=img2,
                              penultimate_layer_idx=penultimate_layer)
        ax3 = plt.subplot(4, n, i + 1 + 2 * n)
        ax3.imshow(grads, cmap='jet', alpha=0.8)

        ax4 = plt.subplot(4, n, i + 1 + 3 * n)
        plt.imshow(msk, cmap='gray')
        ax4.get_xaxis().set_visible(False)
        ax4.get_yaxis().set_visible(False)
        ax4.set_title("Mask")

    plt.show()
    plt.savefig('/home/CVL1/Shaobo/mri/outputs/test1.jpg')