Lesson 1 - FastAI
Machine learning may seem complex at first, given the math, background understanding, and code involved. However, if you truly want to learn, the best place to start is by building and messing around with a model. FastiAI makes it super easy to create and modify models to best solve your problem! Don't worry too much if you don't understand, we will get there.
As I have said above, the best way to learn is by actually creating your first model
from fastai.vision.all import * #IMPORT
path = untar_data(URLs.PETS)/'images' #DATA SET
def is_cat(x): return x[0].isupper() #Labels for the dataset (This dataset cat labels begin w/ uppercase letter)
#Create dataset (Training data, test data) and correctly gets imgs w/ labels
dls = ImageDataLoaders.from_name_func(
path, get_image_files(path), valid_pct=0.2, seed=42,
label_func=is_cat, item_tfms=Resize(224))
learn = cnn_learner(dls, resnet34, metrics=error_rate) #Creating architecture
learn.fine_tune(1) #Training
Look at that, we created our first model and all with a few lines of code.
img = PILImage.create(image_cat())
img.to_thumb(192)
uploader = widgets.FileUpload()
uploader
img = PILImage.create(uploader.data[0])
img.to_thumb(192)
is_cat,_,probs = learn.predict(img)
print(f"Is this a cat?: {is_cat}.")
print(f"Probability it's a cat: {probs[1].item():.6f}")
Fantastic, you can now classify cats!
path = untar_data(URLs.CAMVID_TINY)
dls = SegmentationDataLoaders.from_label_func( #Segmentation
path, bs=8, fnames = get_image_files(path/"images"),
label_func = lambda o: path/'labels'/f'{o.stem}_P{o.suffix}',
codes = np.loadtxt(path/'codes.txt', dtype=str)
)
learn = unet_learner(dls, resnet34)
learn.fine_tune(8)
learn.show_results(max_n=2, figsize=(10,12))
from fastai.text.all import *
dls = TextDataLoaders.from_folder(untar_data(URLs.IMDB), valid='test')
learn = text_classifier_learner(dls, AWD_LSTM, drop_mult=0.5, metrics=accuracy)
learn.fine_tune(2, 1e-2)
learn.predict("I really liked that movie!")
from fastai.tabular.all import *
path = untar_data(URLs.ADULT_SAMPLE)
dls = TabularDataLoaders.from_csv(path/'adult.csv', path=path, y_names="salary",
cat_names = ['workclass', 'education', 'marital-status', 'occupation',
'relationship', 'race'],
cont_names = ['age', 'fnlwgt', 'education-num'],
procs = [Categorify, FillMissing, Normalize])
learn = tabular_learner(dls, metrics=accuracy)
learn.fit_one_cycle(3)
from fastai.collab import *
path = untar_data(URLs.ML_SAMPLE)
dls = CollabDataLoaders.from_csv(path/'ratings.csv')
learn = collab_learner(dls, y_range=(0.5,5.5))
learn.fine_tune(10)
learn.show_results()
Do you need these for deep learning?
- Lots of math T / F
- Lots of data T / F
- Lots of expensive computers T / F
- A PhD T / F
Name five areas where deep learning is now the best in the world.
Vision, Natural language processing, Medicine, Robotics, and Games- What was the name of the first device that was based on the principle of the artificial neuron?
Mark I Perceptron - Based on the book of the same name, what are the requirements for parallel distributed processing (PDP)?
Processing units, State of activation, Output function, Pattern of connectivity, Propagation rule, Activation rule, Learning rule, Environment - What were the two theoretical misunderstandings that held back the field of neural networks?
Single layer network unable to learn simple mathimatical functions. More layers make network too big and slow to be useful. - What is a GPU?
A graphics card is a processor that can handle 1000's of tasks at the same time. Particularly great for deep learning. - Open a notebook and execute a cell containing:
1+1
. What happens?
2 - Follow through each cell of the stripped version of the notebook for this chapter. Before executing each cell, guess what will happen.
- Complete the Jupyter Notebook online appendix.
- Why is it hard to use a traditional computer program to recognize images in a photo?
They are missing the weight assignment needed to recognize patterns within images to accomplish the task. - What did Samuel mean by "weight assignment"?
The weight is another form of input that has direct influence on the model's performance. - What term do we normally use in deep learning for what Samuel called "weights"?
Parameters - Draw a picture that summarizes Samuel's view of a machine learning model.
https://vikramriyer.github.io/assets/images/machine_learning/fastai/model.jpeg - Why is it hard to understand why a deep learning model makes a particular prediction?
There are many layers, each with numerous neurons. Therefore, it gets complex really fast what each neuron is looking for when viewing an image, and how that impacts the perediction. - What is the name of the theorem that shows that a neural network can solve any mathematical problem to any level of accuracy?
Universal approximation theorem - What do you need in order to train a model?
Data with labels - How could a feedback loop impact the rollout of a predictive policing model?
The more the model is used the more biased the data becomes, and therefore, the more bias the model becomes. - Do we always have to use 224×224-pixel images with the cat recognition model?
No. - What is the difference between classification and regression?
Classification is about categorizing/labeling objects. Regression is about predicting numerical quantities, such as temp. What is a validation set? What is a test set? Why do we need them?
The validation set measures the accuracy of the model during training.
The test set is used during the final evaluation to test the accuracy of the model.We need both of them because the validation set could cause some bias in the model as we would are fitting the model towards it during training. However, the test set removes this and evaluates the model on unseen data, thereby, giving an accurate metric of accuracy.
- What will fastai do if you don't provide a validation set?
Fastai will automatically create a validation dataset for us. - Can we always use a random sample for a validation set? Why or why not?
It is not reccomended where order is neccessary, example ordered by time. - What is overfitting? Provide an example.
This is when the model begins to fit to the training data rather than generalizing for similar unseen datasets. For example a model that does amazing on the training data, but performs poorly on test data: Good indication that model may have overfitted. - What is a metric? How does it differ from "loss"?
The loss is the value calculated by the model to determine the impact each neuron has on the end result: Therefore, the value is used by models to measure its performance. The metric gives us, humans, an overall value of how accurate the model was: Therefore, a value we use to understand the models performance. - How can pretrained models help?
A pretrained model already has the fundementals. Therefore, it can use this prior knowledge to learn faster and perform better on similer datasets. - What is the "head" of a model?
The final layers from the pretrained model that have been replaced with new layers (w/ randomized weights) to better align with our dataset. These final layers are often the only thing trained while the rest of the model is frozen. - What kinds of features do the early layers of a CNN find? How about the later layers?
The early layers often extract simple features like edges.
The later layers are more complex and can identify advanced features like faces. - Are image models only useful for photos?
No. Lots of other forms of data can be converted into images that can be used to solve such non-photo data problems. - What is an "architecture"?
This is the structure of the model we use to solve the problem. - What is segmentation?
Method of labeling all pixels within an image and masking it. - What is
y_range
used for? When do we need it?
Specifies the range of values that can be perdicted by model. For example, movie rating's 0-5. - What are "hyperparameters"?
These are the parameters that we can adjust to help the model perform better (Ex: Epochs). - What's the best way to avoid failures when using AI in an organization?
Begin with the most simplest model and then slowly building up to more complexity. This way you have something working and don't get lost as you add onto the model.
Each chapter also has a "Further Research" section that poses questions that aren't fully answered in the text, or gives more advanced assignments. Answers to these questions aren't on the book's website; you'll need to do your own research!
Why is a GPU useful for deep learning? How is a CPU different, and why is it less effective for deep learning? </br> Modern GPUs provide a far superior processing power, memory bandwidth, and efficiency over the CPU.
Try to think of three areas where feedback loops might impact the use of machine learning. See if you can find documented examples of that happening in practice. </br> I believe feedback loops are primarly great for recommendation models. This is because the feedback loops create a bias model. For example, if a viewer like a movie, he/she will like similer movies. Being bias here towards particular types of movie is the best way to keep the viewer engaged.