Visualizing layers of Resnet50 model trained on Plant Seedlings data

cool :grinning:

1 Like

Do Share the images…
It’s really cool to look at them…

I have no weights :slight_smile: . Do you have them? Or it’s better to train it by myself.
Update:
When I try to train I have this error
error: /io/opencv/modules/imgproc/src/color.cpp:10606: error: (-215) scn == 3 || scn == 4 in function cvtColor

You should be training them, It will be fun :slight_smile: you can try it on dogs and cat model.

1 Like

yes, you are right! maybe it’s better for dogs and cats, since I worked with them already and have everything ready :slight_smile:

1 Like

@alessa
This thread will solve your problem
Problem is with the image_id’s spaces and folder structure…

BTW if anyone is interested trying contributing to fastai, here’s a good opportunity - add some simple error checking to open_image in dataset.py that checks that the file exists, and that it is a file (not a directory).

1 Like

os.path.isdir() and os.path.isfile() should be sufficient ?

Yes something like that should be fine. You want to give it a go?

Will surely try tonight…

Sorry beat you @ecdrid to it: https://github.com/fastai/fastai/pull/62

Was small enough task that I was tempted to attempt a pull-request.

Pleasure to see that people are interested in coding here…

Nice - bonus points for adding docs!

Do you think these would be better as warning instead of print, so users can easily opt out of them if they want? I’ve tried to avoid using ‘print’ as much as I can in fastai.

Will this solve the problem?
print should be replaced by an exception that is thrown...

OpenCV exceptions are pretty cryptic. I think that is why we are adding the basic two error checks with explicit debug statements.

Jeremy are you suggesting a logger in that case or actually use warnings (https://docs.python.org/2/library/warnings.html)?

I was thinking actual warnings. Does these seem reasonable?

@merajat, thanks for sharing this. Using your code, I dont have any images being saved, layer_op remains empty. I would appreciate any ideas as to why, thanks.

# This writes layer outputs to file

def visulaize_layers(outputs):
    for index, layer in enumerate(outputs):
        features = layer.data
        size_plot = features.shape[1]
        if size_plot % 2 != 0:
            size_plot += 1
        original_size = np.int(np.ceil(np.sqrt(size_plot)))
        f, axarr = plt.subplots(original_size + 1, original_size + 1)
        i, j = 0,0
        counter = 1
        for blocks in features:
            for block in blocks:
                counter += 1
                x = block.cpu().numpy()
                if counter % original_size == 0:
                    i += 1
                    j = 0 

                axarr[i,j].imshow(x)
                j += 1
        counter = 0
        print(f'layer {index} done')
        f.savefig(f'layer_op/output{index}.jpg')
        print('image generated')

Are you getting any error? Can you try it for a single layer?

@merajat, the above code has no error but when I run a single layer it states there is no such file or directory: layer_op/output0.jpg.

When I check the layer_op directory after running the above code there are no images being saved

After you load the image, you should call the visulaize_layers(layer_outputs) in order to generate those layers.

img_path = f'{path}/train/dogs/dog.802.jpg'
i = image_loader(img_path, expand_dim=True)
i = i.cuda()


nr_layers = 10
tmp_model = get_activation_layer(model, nr_layers)
layer_outputs = tmp_model(Variable(i))
visulaize_layers(layer_outputs)

Also check the path from visulaize_layers function

f.savefig(f'{path}/layer_op/output{index}.jpg')

where for example my path is

path = '/home/alessa/Documents/MyGoal/data/dogscats_sample'

4 Likes