Visualization of patches

Hello,
I am trying to use the functions created in the week 3 assignment to extract patches from an image in Colab.

> X, y = get_sub_volume(image, label)
>  util.visualize_patch(X[0, :, :, :], y[2])

I am getting “module ‘utils’ has no attribute ‘visualize_patch’”

I checked tensorflow.keras.utils, but did not find ‘visualize_patch’

Can you point me to the latest API for util or a better alternative to util?

Thank you,
Madhu

That is a helper function supplied in the local util.py file that should be imported at the bottom of the first code cell

import keras
import json
import numpy as np
import pandas as pd
import nibabel as nib
import matplotlib.pyplot as plt

from tensorflow.keras import backend as K 

import util
def visualize_patch(X, y):
    fig, ax = plt.subplots(1, 2, figsize=[10, 5], squeeze=False)

    ax[0][0].imshow(X[:, :, 0], cmap='Greys_r')
    ax[0][0].set_yticks([])
    ax[0][0].set_xticks([])
    ax[0][1].imshow(y[:, :, 0], cmap='Greys_r')
    ax[0][1].set_xticks([])
    ax[0][1].set_yticks([])

    fig.subplots_adjust(wspace=0, hspace=0)

notice that it is util (singular) not utils (plural)

Thank you so much for letting me know how to define the ‘visualize_patch’ function.

When I am trying to import util, it gives the following output:
No module named 'util'

Can you please let me know how to import/install the util?

Take a close look at the first cell I attached above. Notice the line


import util

That’s how. That code cell needs to be executed or ‘run’ before anything else in the notebook. That line makes the functions in the util.py file accessible to the namespace. If that import line fails it means that python file is missing from the workspace and it needs to be refreshed. There are other threads and FAQs that cover how to do that.

Oh got it! Now I am able to visualize the patches. Thank you a lot for patiently answering my questions.

1 Like

Glad it worked for you