Recently I’ve started working on the face landmarks detection project. However, I can’t figure out how to prepare the dataset to use it with fastai
.
I am trying to use data_block
API instead of custom datasets I usually use. I have a folder with the following structure:
\images
- 1.jpeg
- 1.txt
- 2.jpeg
- 2.txt
...
Each .txt
file contains two columns with coordinates. (First goes Y, then goes X). The coordinates are already scaled into range [-1, 1]
. However, as I know, the coordinates rescaling is applied automatically, and I am not sure how to disable it.
I am using the following snippet to construct the data bunch:
def read_landmarks(img_path):
index = img_path.stem
pts_path = img_path.parent/f'{index}.txt'
img = imread(img_path)
h, w = img.shape[:2]
pts = np.loadtxt(pts_path, delimiter=',')
xs, ys = pts[:, 1], pts[:, 0]
xs, ys = to_absolute(xs, ys, w, h)
return np.c_[ys, xs]
p = (
PointsItemList.
from_folder(CROPPED).
split_by_idx(trn_idx, val_idx).
label_from_func(read_landmarks))
print(p)
However, the following error is raised:
[...]
~/code/fastai_v1/repo/fastai/vision/image.py in scale_flow(flow, to_unit)
417 "Scale the coords in `flow` to -1/1 or the image size depending on `to_unit`."
418 s = tensor([flow.size[0]/2,flow.size[1]/2])[None]
--> 419 if to_unit: flow.flow = flow.flow/s-1
420 else: flow.flow = (flow.flow+1)*s
421 return flow
~/anaconda3/envs/fastai/lib/python3.7/site-packages/torch/tensor.py in __rdiv__(self, other)
365 def __rdiv__(self, other):
366 if self.dtype.is_floating_point:
--> 367 return self.reciprocal() * other
368 else:
369 return (self.double().reciprocal() * other).type_as(self)
TypeError: mul(): argument 'other' (position 1) must be Tensor, not numpy.ndarray
The error is raised when I am trying to print the p
object. As I can see, it happens because __repr__
magic invokes method that tries to rescale coordinates and failes.
Could someone help with the two questions:
- how to disable coordinates re-scaling?
- is it possible to solve this issue without disabling re-scaling?
=== Software ===
python : 3.7.1
fastai : 1.0.39
fastprogress : 0.1.18
torch : 1.0.0
nvidia driver : 410.73
torch cuda : 9.0.176 / is available
torch cudnn : 7401 / is enabled
=== Hardware ===
nvidia gpus : 2
torch devices : 2
- gpu0 : 11177MB | GeForce RTX 2080
- gpu1 : 7952MB | GeForce GTX 1080 Ti
=== Environment ===
platform : Linux-4.15.0-43-generic-x86_64-with-debian-buster-sid
distro : Ubuntu 18.04 Bionic Beaver
conda env : fastai
python : /home/ck/anaconda3/envs/fastai/bin/python
sys.path : /home/ck/code/tasks/face_landmarks_detection
/home/ck/anaconda3/envs/fastai/lib/python37.zip
/home/ck/anaconda3/envs/fastai/lib/python3.7
/home/ck/anaconda3/envs/fastai/lib/python3.7/lib-dynload
/home/ck/anaconda3/envs/fastai/lib/python3.7/site-packages
/home/ck/code/fastai_v1/repo
/home/ck/code/open_source/ignite
Probably there is some link to the docs that I’ve missed.