SOURCE CODE: Mid-Level API

Hi All, This is a great initiative. I am late to the party. I just went through the top post and seen the links to the recorded videos until now. Will go through them. Is there a time schedule for the study group meetings?

hey @arora_aman, would you mind sharing the repos that you used for the walkthru?

Was doing a backtrace of ImageDataLoaders class for vision and and in the from_name_func method came across f = using_attr(label_func, ‘name’) which is then passed on to the from_path_func method as the label_func.

Could someone help me in understanding whats happening in that line: f = using_attr(label_func, ‘name’)?

  • The doc says it’s used to "Change function f to operate on attr"
  • and the docstring says the function returns “partial(_using_attr, f, attr)”…

I’m not an expert on this topic, from what I understand f = using_attr(label_func, 'name') creates a function f which does nothing but calls the attribute .name of it’s inputs. i.e Basically it takes the label_func which returns a list of paths, modifies it to call .name on every path in that list and return it. This new modified function is assigned as f, leaving the old function intact.

So label_func is applied on the .name of every path by creating a partial function of ‘_using_attr’ by passing in label_func and ‘name’ as the attribute which returns boolean values of whether the path is a cat or a dog. Which is then passed on to get_y which then sets the labels? I got that flow but couldn’t quite understand what _get_attr does, still kinda iffy on that! :sweat_smile:

From what I can gather f(file_name) is the same a label_func(file_name.name), so that’s what is happening I guess, creating a function that gets the name attribute of the file path and labe_func is applied to it.

1 Like

Yes, in simpler words us using_attr(label_func,“name”) simply calls label_func but instead of passing its original input, it calls it with each of [inputs[0].name,inputs[1].name …]

1 Like

Later joiner here, how does this study group runs?

1 Like

Hi @nok! You can read the initial wiki post and follow through the discussion to get a rough idea of how things are going to work. If you have any specific doubts feel free to ask them here.

1 Like

So it will be studying the FastGarden and blog individually?

1 Like

Yes, for first week that is the plan :slight_smile:

1 Like

Hi,

I may be asking some basic questions but would help if someone can answer the below queries.

  1. Is cls equivalent to calling self in a function?
  2. What is the purpose of decorator @classmethod being referenced in from_name_func function as there is no mention of it in DataLoaders or ImageDataLoaders? (I have referred this URL to understand decorators - https://www.geeksforgeeks.org/decorators-in-python/) . Calling doc of classmethod only returns ’ classmethod.__func__’ which means it returns the original function object.
  3. doc(using_attr) provides a link and detail of the function. The link ‘Show in docs’ navigates to https://fastcore.fast.ai/utils#using_attr. This page does not have the definition of using_attr. Am I missing something?

@classmethod
def from_name_func(cls, path, fnames, label_func, **kwargs):
"Create from the name attrs of fnames in paths with label_func"
f = using_attr(label_func, ‘name’)
return cls.from_path_func(path, fnames, f, **kwargs)

Class methods are used in OOP’s to change some properties of the class itself, in this case ImageDataLoaders.

Classmethods inherits underlying properties of the class, unlike a static method that works with the parameters provided to it on its instance. Hopefully, this tutorial will help!

3 Likes

using_attr creates a partial function of the function passed with the specific attribute instead of the default. From what I have seen its used to get the name attribute from path files as shown in the example below…

2 Likes

Thanks the video was very useful.

Yes, this is understood. Thanks.

I had a query on the usage of the doc function on using_attr function. It displays the details of the function with a link. If you click the link ‘Show in docs’, it opens a page with the URL i have provided. I cant seem to find the function on that link.

Yup! Corey Schafers tutorials is the reason i’m even remotely decent at using python! His videos are pretty damn good!

1 Like

After you type doc(function) click on source on the top right. That should get you the exact underlying code! :slight_smile:

I understand what he is talking about, it’s underlying code is only in part of one function and that is from_name_func. Give me a moment to try to trace it. You’re not going crazy @ganesh.bhat (to understand what I mean, look at the vision.data notebook, it only shows up once)

1 Like

I get the below error if I click on source on the top right of the doc function output.

Blocked by Content Security Policy

An error occurred during a connection to github.com.

Firefox prevented this page from loading in this way because the page has a content security policy that disallows it.