Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
106 views
in Technique[技术] by (71.8m points)

python - How to use a custom flip function using get_transform in fastai v1?

I would like to use a custom flip. For my use case, mirror flips don't have sense:

def _dihedral_affine(k:partial(random.choice, [0, 3, 5, 6])):
    "Randomly flip `x` image based on `k`."
    x = -1 if k&1 else 1
    y = -1 if k&2 else 1
    if k&4: return [[0, x, 0.],
                    [y, 0, 0],
                    [0, 0, 1.]]
    return [[x, 0, 0.],
            [0, y, 0],
            [0, 0, 1.]]

dihedral_affine = TfmAffine(_dihedral_affine)
additional_aug = [dihedral_affine()]
tfms = get_transforms(do_flip=False, xtra_tfms=additional_aug)
data = ImageDataBunch.from_df("../pathData", df_ground, valid_pct=0.2, label_col=1, size=224, ds_tfms=tfms, bs=64)
learn = cnn_learner(data, models.resnet34, metrics=[accuracy])
learn.lr_find()

I have this error (I'm using fastai 1.0.60, torch 1.3.1+cpu torchvision 0.4.2+cpu, python 3.6.12.):

LR Finder is complete, type {learner_name}.recorder.plot() to see the graph.
---------------------------------------------------------------------------
Empty                                     Traceback (most recent call last)
~Anaconda3envsenv_fastai_v1libsite-packagesorchutilsdatadataloader.py in _try_get_data(self, timeout)
    723         try:
--> 724             data = self._data_queue.get(timeout=timeout)
    725             return (True, data)
~Anaconda3envsenv_fastai_v1libmultiprocessingqueues.py in get(self, block, timeout)
    104                     if not self._poll(timeout):
--> 105                         raise Empty
    106                 elif not self._poll():
Empty: 
During handling of the above exception, another exception occurred:
RuntimeError                              Traceback (most recent call last)
<ipython-input-26-d81c6bd29d71> in <module>
----> 1 learn.lr_find()
~Anaconda3envsenv_fastai_v1libsite-packagesfastairain.py in lr_find(learn, start_lr, end_lr, num_it, stop_div, wd)
     39     cb = LRFinder(learn, start_lr, end_lr, num_it, stop_div)
     40     epochs = int(np.ceil(num_it/len(learn.data.train_dl)))
---> 41     learn.fit(epochs, start_lr, callbacks=[cb], wd=wd)
     42 
     43 def to_fp16(learn:Learner, loss_scale:float=None, max_noskip:int=1000, dynamic:bool=True, clip:float=None,
~Anaconda3envsenv_fastai_v1libsite-packagesfastaiasic_train.py in fit(self, epochs, lr, wd, callbacks)
    198         else: self.opt.lr,self.opt.wd = lr,wd
    199         callbacks = [cb(self) for cb in self.callback_fns + listify(defaults.extra_callback_fns)] + listify(callbacks)
--> 200         fit(epochs, self, metrics=self.metrics, callbacks=self.callbacks+callbacks)
    201 
    202     def create_opt(self, lr:Floats, wd:Floats=0.)->None:
~Anaconda3envsenv_fastai_v1libsite-packagesfastaiasic_train.py in fit(epochs, learn, callbacks, metrics)
     97             cb_handler.set_dl(learn.data.train_dl)
     98             cb_handler.on_epoch_begin()
---> 99             for xb,yb in progress_bar(learn.data.train_dl, parent=pbar):
    100                 xb, yb = cb_handler.on_batch_begin(xb, yb)
    101                 loss = loss_batch(learn.model, xb, yb, learn.loss_func, learn.opt, cb_handler)
~Anaconda3envsenv_fastai_v1libsite-packagesfastprogressfastprogress.py in __iter__(self)
     45         except Exception as e:
     46             self.on_interrupt()
---> 47             raise e
     48 
     49     def update(self, val):
~Anaconda3envsenv_fastai_v1libsite-packagesfastprogressfastprogress.py in __iter__(self)
     39         if self.total != 0: self.update(0)
     40         try:
---> 41             for i,o in enumerate(self.gen):
     42                 if i >= self.total: break
     43                 yield o
~Anaconda3envsenv_fastai_v1libsite-packagesfastaiasic_data.py in __iter__(self)
     73     def __iter__(self):
     74         "Process and returns items from `DataLoader`."
---> 75         for b in self.dl: yield self.proc_batch(b)
     76 
     77     @classmethod
~Anaconda3envsenv_fastai_v1libsite-packagesorchutilsdatadataloader.py in __next__(self)
    802 
    803             assert not self._shutdown and self._tasks_outstanding > 0
--> 804             idx, data = self._get_data()
    805             self._tasks_outstanding -= 1
    806 
~Anaconda3envsenv_fastai_v1libsite-packagesorchutilsdatadataloader.py in _get_data(self)
    769         else:
    770             while True:
--> 771                 success, data = self._try_get_data()
    772                 if success:
    773                     return data
~Anaconda3envsenv_fastai_v1libsite-packagesorchutilsdatadataloader.py in _try_get_data(self, timeout)
    735             if len(failed_workers) > 0:
    736                 pids_str = ', '.join(str(w.pid) for w in failed_workers)
--> 737                 raise RuntimeError('DataLoader worker (pid(s) {}) exited unexpectedly'.format(pids_str))
    738             if isinstance(e, queue.Empty):
    739                 return (False, None)
RuntimeError: DataLoader worker (pid(s) 19652, 19132, 27668, 1436, 5696, 5340, 3828, 26140) exited unexpectedly 

I've seen this answer but it doesn't help me How to get add aditional transform to get_transform in fastai?

How to get it work?

question from:https://stackoverflow.com/questions/65847676/how-to-use-a-custom-flip-function-using-get-transform-in-fastai-v1

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...