Training the model from course 1 using different classifiers

After running the following code in lesson 1, how can I use the feature matrix and try out using other classifiers like Decision Tree and Random Forest using Scikit-Learn?

data = ImageClassifierData.from_paths(PATH, tfms=tfms_from_model(arch, sz))
learn = ConvLearner.pretrained(arch, data, precompute=True), 2)

Do you mean the confusion matrix? You should be able to generate that from any list of predictions / actuals regardless of how they were generated.

If you want more information about visualizing decision trees / random forests, these topics are discussed in the machine learning course.

I meant to obtain the matrix that is being fed to the last layer containing the fully connected network. Then, instead of using a fully connected network, I wish to use a decision tree.

Oh so you want the activations at the final layer? Try this

def return_sequential(layer_num, model):
    return nn.Sequential(

class get_activation_layer(nn.Module):
    def __init__(self, model):
        self.model = model
        self.layer_models = []
        for i in range(len(self.model)):
             self.layer_models.append(return_sequential(i, self.model))
    def forward(self, x):
        self.outputs = []
        for i in range(len(self.model)):
        return self.outputs


tmp_model = get_activation_layer(learn.model)
layer_outputs = tmp_model(V(i))

Where i is your input image as a tensor. Then layer_output will have the activation tensors at each layer.


Thanks, this is what I wanted.

Hey, I am getting an error saying i is uninitialized on the last line. Please help.

In layer_outputs = tmp_model(V(i)) ?

Sounds like the model doesn’t like how you loaded the image. Here’s how I do it:

trn_tfms, val_tfms = tfms_from_model(arch, sz)
i = val_tfms (open_image(fn))
i = i[None]
i = torch.from_numpy(i).cuda()
tmp_model = get_activation_layer(model)
layer_outputs = tmp_model(V(i))

What does it take as input and return as output?

open_image is from It opens an image with OpenCV and normalizes it.

What is V() and which library does it belong to?