Important: This notebook will only work with fastai-0.7.x. Do not try to run any fastai-1.x code from this path in the repository because it will load fastai-0.7.x

Testing transforms.py


In [ ]:
%reload_ext autoreload
%autoreload 2
%matplotlib inline

# This file contains all the main external libs we'll use
from fastai.imports import *
from fastai.transforms import *
from fastai.plots import *
from fastai.dataset import *

In [ ]:
PATH = "data/fish/"
PATH = "/data2/yinterian/fisheries-kaggle/"

Fish with bounding box


In [ ]:
fnames,corner_labels,_,_ = parse_csv_labels(f'{PATH}trn_bb_corners_labels', skip_header=False)

In [ ]:
def get_x(f):
    return open_image(f'{PATH}/images/{f}')

In [ ]:
f = 'img_02642.jpg'
x = get_x(f)
y = np.array(corner_labels[f], dtype=np.float32)
y


Out[ ]:
array([  699.39899,   954.69702,   895.32599,  1275.30005], dtype=float32)

In [ ]:
x.shape


Out[ ]:
(974, 1280, 3)

In [ ]:
rows = np.rint([y[0], y[0], y[2], y[2]]).astype(int)
rows


Out[ ]:
array([699, 699, 895, 895])

In [ ]:
cols = np.rint([y[1], y[3], y[1], y[3]]).astype(int)
cols


Out[ ]:
array([ 955, 1275,  955, 1275])

In [ ]:
corner_labels["img_02642.jpg"]


Out[ ]:
['699.399', '954.697', '895.326', '1275.3']

In [ ]:
def create_corner_rect(bb, color='red'):
    bb = np.array(bb, dtype=np.float32)
    return plt.Rectangle((bb[1], bb[0]), bb[3]-bb[1], bb[2]-bb[0], color=color, fill=False, lw=3)

def show_corner_bb(f='img_04908.jpg'):
    file_path = f'{PATH}images/{f}'
    bb = corner_labels[f]
    plots_from_files([file_path])
    plt.gca().add_patch(create_corner_rect(bb))

In [ ]:
show_corner_bb(f = 'img_02642.jpg')



In [ ]:
def create_rect(bb, color='red'):
    return plt.Rectangle((bb[1], bb[0]), bb[3]-bb[1], bb[2]-bb[0], color=color, fill=False, lw=3)

def plotXY(x,y):
    plots([x])
    plt.gca().add_patch(create_rect(y))

In [ ]:
plotXY(x,y)


Scale


In [ ]:
xx, yy = Scale(sz=350, tfm_y=TfmType.COORD)(x, y)

In [ ]:
plotXY(xx,yy)



In [ ]:
xx, yy = Scale(sz=350, tfm_y=TfmType.PIXEL)(x, x)
plots([xx, yy])


RandomScale


In [ ]:
xx, yy = RandomScale(sz=350, max_zoom=1.1, tfm_y=TfmType.COORD)(x, y)
plotXY(xx,yy)
print(yy)
print(y)


[271 370 347 494]
[  699.39899   954.69702   895.32599  1275.30005]

In [ ]:
xx, yy = RandomScale(sz=350, max_zoom=1.1, tfm_y=TfmType.PIXEL)(x, x)
plots([xx, yy])


RandomCrop


In [ ]:
xx, yy = RandomCrop(targ=350, tfm_y=TfmType.COORD)(x, y)


loss my fish

In [ ]:
plotXY(xx,yy)



In [ ]:
xx, yy = RandomCrop(350, tfm_y=TfmType.PIXEL)(x, x)
plots([xx, yy])


No Cropping


In [ ]:
xx, yy = NoCrop(350, tfm_y=TfmType.COORD)(x, y)

In [ ]:
print(yy)
plotXY(xx,yy)


[251 261 321 348]

In [ ]:
xx, yy = NoCrop(350, tfm_y=TfmType.PIXEL)(x, x)
plots([xx, yy])


CenterCrop


In [ ]:
xx, yy = CenterCrop(350, tfm_y=TfmType.COORD)(x, y)


loss my fish

In [ ]:
plotXY(xx,yy)



In [ ]:
xx, yy = CenterCrop(350, tfm_y=TfmType.PIXEL)(x, x)
plots([xx, yy])


Random Dihedral


In [ ]:
xx, yy = RandomDihedral(TfmType.COORD)(x, y)

In [ ]:
print(yy)


[  4  78 324 274]

In [ ]:
plotXY(xx,yy)



In [ ]:
xx, yy = RandomDihedral(tfm_y=TfmType.PIXEL)(x, x)

In [ ]:
plots([xx,yy])


RandomFlipXY


In [ ]:
xx, yy = RandomFlip(TfmType.COORD)(x, y)
print(yy)
plotXY(xx,yy)


[ 699  955  895 1275]

In [ ]:
xx, yy = RandomFlip(TfmType.PIXEL)(x, x)
plots([xx,yy])


RandomLightingXY (talk to Jeremy about this)


In [ ]:
xx, yy = RandomLighting(0.5, 0.5)(x, y)
plotXY(xx,yy)



In [ ]:
# talk to Jeremy about this
xx, yy = RandomLighting(0.5, 0.5, TfmType.PIXEL)(x, x)
plots([xx,yy])


RandomRotate


In [ ]:
xx, yy = RandomRotate(deg=30, p=1, tfm_y=TfmType.COORD)(x, y)
plotXY(xx,yy)
print(yy)


[ 547  995  808 1277]

In [ ]:


In [ ]:
xx, yy = RandomRotate(130,p=1.0, tfm_y=TfmType.COORD)(x, y)
plotXY(xx,yy)



In [ ]:
xx, yy = RandomRotate(0.5, 0.5, TfmType.PIXEL)(x, x)
plots([xx,yy])