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
%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 *
PATH = "data/fish/"
PATH = "/data2/yinterian/fisheries-kaggle/"
fnames,corner_labels,_,_ = parse_csv_labels(f'{PATH}trn_bb_corners_labels', skip_header=False)
def get_x(f):
return open_image(f'{PATH}/images/{f}')
f = 'img_02642.jpg'
x = get_x(f)
y = np.array(corner_labels[f], dtype=np.float32)
y
array([ 699.39899, 954.69702, 895.32599, 1275.30005], dtype=float32)
x.shape
(974, 1280, 3)
rows = np.rint([y[0], y[0], y[2], y[2]]).astype(int)
rows
array([699, 699, 895, 895])
cols = np.rint([y[1], y[3], y[1], y[3]]).astype(int)
cols
array([ 955, 1275, 955, 1275])
corner_labels["img_02642.jpg"]
['699.399', '954.697', '895.326', '1275.3']
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))
show_corner_bb(f = 'img_02642.jpg')
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))
plotXY(x,y)
xx, yy = Scale(sz=350, tfm_y=TfmType.COORD)(x, y)
plotXY(xx,yy)
xx, yy = Scale(sz=350, tfm_y=TfmType.PIXEL)(x, x)
plots([xx, yy])
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]
xx, yy = RandomScale(sz=350, max_zoom=1.1, tfm_y=TfmType.PIXEL)(x, x)
plots([xx, yy])
xx, yy = RandomCrop(targ=350, tfm_y=TfmType.COORD)(x, y)
loss my fish
plotXY(xx,yy)
xx, yy = RandomCrop(350, tfm_y=TfmType.PIXEL)(x, x)
plots([xx, yy])
xx, yy = NoCrop(350, tfm_y=TfmType.COORD)(x, y)
print(yy)
plotXY(xx,yy)
[251 261 321 348]
xx, yy = NoCrop(350, tfm_y=TfmType.PIXEL)(x, x)
plots([xx, yy])
xx, yy = CenterCrop(350, tfm_y=TfmType.COORD)(x, y)
loss my fish
plotXY(xx,yy)
xx, yy = CenterCrop(350, tfm_y=TfmType.PIXEL)(x, x)
plots([xx, yy])
xx, yy = RandomDihedral(TfmType.COORD)(x, y)
print(yy)
[ 4 78 324 274]
plotXY(xx,yy)
xx, yy = RandomDihedral(tfm_y=TfmType.PIXEL)(x, x)
plots([xx,yy])
xx, yy = RandomFlip(TfmType.COORD)(x, y)
print(yy)
plotXY(xx,yy)
[ 699 955 895 1275]
xx, yy = RandomFlip(TfmType.PIXEL)(x, x)
plots([xx,yy])
xx, yy = RandomLighting(0.5, 0.5)(x, y)
plotXY(xx,yy)
# talk to Jeremy about this
xx, yy = RandomLighting(0.5, 0.5, TfmType.PIXEL)(x, x)
plots([xx,yy])
xx, yy = RandomRotate(deg=30, p=1, tfm_y=TfmType.COORD)(x, y)
plotXY(xx,yy)
print(yy)
[ 547 995 808 1277]
xx, yy = RandomRotate(130,p=1.0, tfm_y=TfmType.COORD)(x, y)
plotXY(xx,yy)
xx, yy = RandomRotate(0.5, 0.5, TfmType.PIXEL)(x, x)
plots([xx,yy])