如何在 fastai v1 中使用 get_transform 使用自定义翻转函数?

问题描述

我想使用自定义翻转。对于我的用例,镜像翻转没有意义:

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],1.]]
    return [[x,y,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()

我有这个错误(我使用的是 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)
~\Anaconda3\envs\env_fastai_v1\lib\site-packages\torch\utils\data\DataLoader.py in _try_get_data(self,timeout)
    723         try:
--> 724             data = self._data_queue.get(timeout=timeout)
    725             return (True,data)
~\Anaconda3\envs\env_fastai_v1\lib\multiprocessing\queues.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()
~\Anaconda3\envs\env_fastai_v1\lib\site-packages\fastai\train.py in lr_find(learn,start_lr,end_lr,num_it,stop_div,wd)
     39     cb = LRFinder(learn,stop_div)
     40     epochs = int(np.ceil(num_it/len(learn.data.train_dl)))
---> 41     learn.fit(epochs,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,~\Anaconda3\envs\env_fastai_v1\lib\site-packages\fastai\basic_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:
~\Anaconda3\envs\env_fastai_v1\lib\site-packages\fastai\basic_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)
~\Anaconda3\envs\env_fastai_v1\lib\site-packages\fastprogress\fastprogress.py in __iter__(self)
     45         except Exception as e:
     46             self.on_interrupt()
---> 47             raise e
     48 
     49     def update(self,val):
~\Anaconda3\envs\env_fastai_v1\lib\site-packages\fastprogress\fastprogress.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
~\Anaconda3\envs\env_fastai_v1\lib\site-packages\fastai\basic_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
~\Anaconda3\envs\env_fastai_v1\lib\site-packages\torch\utils\data\DataLoader.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 
~\Anaconda3\envs\env_fastai_v1\lib\site-packages\torch\utils\data\DataLoader.py in _get_data(self)
    769         else:
    770             while True:
--> 771                 success,data = self._try_get_data()
    772                 if success:
    773                     return data
~\Anaconda3\envs\env_fastai_v1\lib\site-packages\torch\utils\data\DataLoader.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 

我看过这个答案,但对我没有帮助How to get add aditional transform to get_transform in fastai?

如何让它发挥作用?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)