Chapter 28
Other Computer Vision Problems
NotebookPython 3 (ipykernel)72 cells
In [ ]python · cell 1
python
#hide
! [ -e /content ] && pip install -Uqq fastbook
import fastbook
fastbook.setup_book()In [ ]python · cell 2
python
#hide
from fastbook import *Other Computer Vision Problems
Multi-Label Classification
The Data
In [ ]python · cell 6
python
from fastai.vision.all import *
path = untar_data(URLs.PASCAL_2007)In [ ]python · cell 7
python
df = pd.read_csv(path/'train.csv')
df.head()Sidebar: Pandas and DataFrames
In [ ]python · cell 9
python
df.iloc[:,0]In [ ]python · cell 10
python
df.iloc[0,:]
# Trailing :s are always optional (in numpy, pytorch, pandas, etc.),
# so this is equivalent:
df.iloc[0]In [ ]python · cell 11
python
df['fname']In [ ]python · cell 12
python
tmp_df = pd.DataFrame({'a':[1,2], 'b':[3,4]})
tmp_dfIn [ ]python · cell 13
python
tmp_df['c'] = tmp_df['a']+tmp_df['b']
tmp_dfEnd sidebar
Constructing a DataBlock
In [ ]python · cell 16
python
dblock = DataBlock()In [ ]python · cell 17
python
dsets = dblock.datasets(df)In [ ]python · cell 18
python
len(dsets.train),len(dsets.valid)In [ ]python · cell 19
python
x,y = dsets.train[0]
x,yIn [ ]python · cell 20
python
x['fname']In [ ]python · cell 21
python
dblock = DataBlock(get_x = lambda r: r['fname'], get_y = lambda r: r['labels'])
dsets = dblock.datasets(df)
dsets.train[0]In [ ]python · cell 22
python
def get_x(r): return r['fname']
def get_y(r): return r['labels']
dblock = DataBlock(get_x = get_x, get_y = get_y)
dsets = dblock.datasets(df)
dsets.train[0]In [ ]python · cell 23
python
def get_x(r): return path/'train'/r['fname']
def get_y(r): return r['labels'].split(' ')
dblock = DataBlock(get_x = get_x, get_y = get_y)
dsets = dblock.datasets(df)
dsets.train[0]In [ ]python · cell 24
python
dblock = DataBlock(blocks=(ImageBlock, MultiCategoryBlock),
get_x = get_x, get_y = get_y)
dsets = dblock.datasets(df)
dsets.train[0]In [ ]python · cell 25
python
idxs = torch.where(dsets.train[0][1]==1.)[0]
dsets.train.vocab[idxs]In [ ]python · cell 26
python
def splitter(df):
train = df.index[~df['is_valid']].tolist()
valid = df.index[df['is_valid']].tolist()
return train,valid
dblock = DataBlock(blocks=(ImageBlock, MultiCategoryBlock),
splitter=splitter,
get_x=get_x,
get_y=get_y)
dsets = dblock.datasets(df)
dsets.train[0]In [ ]python · cell 27
python
dblock = DataBlock(blocks=(ImageBlock, MultiCategoryBlock),
splitter=splitter,
get_x=get_x,
get_y=get_y,
item_tfms = RandomResizedCrop(128, min_scale=0.35))
dls = dblock.dataloaders(df)In [ ]python · cell 28
python
dls.show_batch(nrows=1, ncols=3)Binary Cross-Entropy
In [ ]python · cell 30
python
learn = vision_learner(dls, resnet18)In [ ]python · cell 31
python
x,y = to_cpu(dls.train.one_batch())
activs = learn.model(x)
activs.shapeIn [ ]python · cell 32
python
activs[0]In [ ]python · cell 33
python
def binary_cross_entropy(inputs, targets):
inputs = inputs.sigmoid()
return -torch.where(targets==1, inputs, 1-inputs).log().mean()In [ ]python · cell 34
python
loss_func = nn.BCEWithLogitsLoss()
loss = loss_func(activs, y)
lossIn [ ]python · cell 35
python
def say_hello(name, say_what="Hello"): return f"{say_what} {name}."
say_hello('Jeremy'),say_hello('Jeremy', 'Ahoy!')In [ ]python · cell 36
python
f = partial(say_hello, say_what="Bonjour")
f("Jeremy"),f("Sylvain")In [ ]python · cell 37
python
learn = vision_learner(dls, resnet50, metrics=partial(accuracy_multi, thresh=0.2))
learn.fine_tune(3, base_lr=3e-3, freeze_epochs=4)In [ ]python · cell 38
python
learn.metrics = partial(accuracy_multi, thresh=0.1)
learn.validate()In [ ]python · cell 39
python
learn.metrics = partial(accuracy_multi, thresh=0.99)
learn.validate()In [ ]python · cell 40
python
preds,targs = learn.get_preds()In [ ]python · cell 41
python
accuracy_multi(preds, targs, thresh=0.9, sigmoid=False)In [ ]python · cell 42
python
xs = torch.linspace(0.05,0.95,29)
accs = [accuracy_multi(preds, targs, thresh=i, sigmoid=False) for i in xs]
plt.plot(xs,accs);Regression
Assemble the Data
In [ ]python · cell 45
python
path = untar_data(URLs.BIWI_HEAD_POSE)In [ ]python · cell 46
python
#hide
Path.BASE_PATH = pathIn [ ]python · cell 47
python
path.ls().sorted()In [ ]python · cell 48
python
(path/'01').ls().sorted()In [ ]python · cell 49
python
img_files = get_image_files(path)
def img2pose(x): return Path(f'{str(x)[:-7]}pose.txt')
img2pose(img_files[0])In [ ]python · cell 50
python
im = PILImage.create(img_files[0])
im.shapeIn [ ]python · cell 51
python
im.to_thumb(160)In [ ]python · cell 52
python
cal = np.genfromtxt(path/'01'/'rgb.cal', skip_footer=6)
def get_ctr(f):
ctr = np.genfromtxt(img2pose(f), skip_header=3)
c1 = ctr[0] * cal[0][0]/ctr[2] + cal[0][2]
c2 = ctr[1] * cal[1][1]/ctr[2] + cal[1][2]
return tensor([c1,c2])In [ ]python · cell 53
python
get_ctr(img_files[0])In [ ]python · cell 54
python
biwi = DataBlock(
blocks=(ImageBlock, PointBlock),
get_items=get_image_files,
get_y=get_ctr,
splitter=FuncSplitter(lambda o: o.parent.name=='13'),
batch_tfms=aug_transforms(size=(240,320)),
)In [ ]python · cell 55
python
dls = biwi.dataloaders(path)
dls.show_batch(max_n=9, figsize=(8,6))In [ ]python · cell 56
python
xb,yb = dls.one_batch()
xb.shape,yb.shapeIn [ ]python · cell 57
python
yb[0]Training a Model
In [ ]python · cell 59
python
learn = vision_learner(dls, resnet18, y_range=(-1,1))In [ ]python · cell 60
python
def sigmoid_range(x, lo, hi): return torch.sigmoid(x) * (hi-lo) + loIn [ ]python · cell 61
python
plot_function(partial(sigmoid_range,lo=-1,hi=1), min=-4, max=4)In [ ]python · cell 62
python
dls.loss_funcIn [ ]python · cell 63
python
learn.lr_find()In [ ]python · cell 64
python
lr = 1e-2
learn.fine_tune(3, lr)In [ ]python · cell 65
python
math.sqrt(0.0001)In [ ]python · cell 66
python
learn.show_results(ds_idx=1, nrows=3, figsize=(6,8))Conclusion
Questionnaire
- How could multi-label classification improve the usability of the bear classifier?
- How do we encode the dependent variable in a multi-label classification problem?
- How do you access the rows and columns of a DataFrame as if it was a matrix?
- How do you get a column by name from a DataFrame?
- What is the difference between a
DatasetandDataLoader? - What does a
Datasetsobject normally contain? - What does a
DataLoadersobject normally contain? - What does
lambdado in Python? - What are the methods to customize how the independent and dependent variables are created with the data block API?
- Why is softmax not an appropriate output activation function when using a one hot encoded target?
- Why is
nll_lossnot an appropriate loss function when using a one-hot-encoded target? - What is the difference between
nn.BCELossandnn.BCEWithLogitsLoss? - Why can't we use regular accuracy in a multi-label problem?
- When is it okay to tune a hyperparameter on the validation set?
- How is
y_rangeimplemented in fastai? (See if you can implement it yourself and test it without peeking!) - What is a regression problem? What loss function should you use for such a problem?
- What do you need to do to make sure the fastai library applies the same data augmentation to your input images and your target point coordinates?
Further Research
- Read a tutorial about Pandas DataFrames and experiment with a few methods that look interesting to you. See the book's website for recommended tutorials.
- Retrain the bear classifier using multi-label classification. See if you can make it work effectively with images that don't contain any bears, including showing that information in the web application. Try an image with two different kinds of bears. Check whether the accuracy on the single-label dataset is impacted using multi-label classification.
In [ ]python · cell 72
python
