U-2-Net
U-2-Net copied to clipboard
Give image in place of list
HI
I want to provide image as input in place of list i tried changing the code in dataloader.py but was not working
class SalObjDataset(Dataset): def init(self,img_name_list,lbl_name_list,transform=None): # self.root_dir = root_dir # self.image_name_list = glob.glob(image_dir+'.png') # self.label_name_list = glob.glob(label_dir+'.png') self.image_name_list = img_name_list self.label_name_list = lbl_name_list self.transform = transform self.list1 = ['1']
def __getitem__(self,idx):
# image = Image.open(self.image_name_list[idx])#io.imread(self.image_name_list[idx])
# label = Image.open(self.label_name_list[idx])#io.imread(self.label_name_list[idx])
#image = self.image_name_list[idx]
#imname = self.image_name_list[idx]
#imidx = np.array([1])
#print([idx])
#
if(0==len(self.label_name_list)):
label_3 = np.zeros(image.shape)
else:
label_3 = io.imread(self.label_name_list[idx])
label = np.zeros(label_3.shape[0:2])
if(3==len(label_3.shape)):
label = label_3[:,:,0]
elif(2==len(label_3.shape)):
label = label_3
if(3==len(image.shape) and 2==len(label.shape)):
label = label[:,:,np.newaxis]
elif(2==len(image.shape) and 2==len(label.shape)):
image = image[:,:,np.newaxis]
label = label[:,:,np.newaxis]
image = [self.image_name_list]
#imname = self.image_name_list[idx]
imidx = [(np.array([self.image_name_list]))]
#label_3 = io.imread(self.label_name_list[idx])
#label = [np.zeros(label_3.shape[0:2])]
sample = {'imidx':imidx, 'image':image, 'label':label}
if self.transform:
sample = self.transform(sample)
return sample
please help me on this
it's better to provide debug error info. otherwise, it is hard to localize the problem.
On Thu, May 27, 2021 at 10:51 AM sathish4ds @.***> wrote:
HI
I want to provide image as input in place of list i tried changing the code in dataloader.py but was not working
class SalObjDataset(Dataset): def init(self,img_name_list,lbl_name_list,transform=None):
self.root_dir = root_dir
self.image_name_list = glob.glob(image_dir+'
.png') # self.label_name_list = glob.glob(label_dir+'.png') self.image_name_list = img_name_list self.label_name_list = lbl_name_list self.transform = transform self.list1 = ['1']
def getitem(self,idx):
image = Image.open(self.image_name_list[idx])#io.imread(self.image_name_list[idx])
label = Image.open(self.label_name_list[idx])#io.imread(self.label_name_list[idx])
#image = self.image_name_list[idx] #imname = self.image_name_list[idx] #imidx = np.array([1]) #print([idx]) # if(0==len(self.label_name_list)): label_3 = np.zeros(image.shape) else: label_3 = io.imread(self.label_name_list[idx])
label = np.zeros(label_3.shape[0:2]) if(3==len(label_3.shape)): label = label_3[:,:,0] elif(2==len(label_3.shape)): label = label_3
if(3==len(image.shape) and 2==len(label.shape)): label = label[:,:,np.newaxis] elif(2==len(image.shape) and 2==len(label.shape)): image = image[:,:,np.newaxis] label = label[:,:,np.newaxis]
image = [self.image_name_list] #imname = self.image_name_list[idx] imidx = [(np.array([self.image_name_list]))]
#label_3 = io.imread(self.label_name_list[idx]) #label = [np.zeros(label_3.shape[0:2])]
sample = {'imidx':imidx, 'image':image, 'label':label}
if self.transform: sample = self.transform(sample)
return sample
please help me on this
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/xuebinqin/U-2-Net/issues/210, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADSGORKWEIZT5MCT2EEZAILTPXTV5ANCNFSM45TSHYQQ .
-- Xuebin Qin PhD Department of Computing Science University of Alberta, Edmonton, AB, Canada Homepage:https://webdocs.cs.ualberta.ca/~xuebin/
This is the error
TypeError Traceback (most recent call last)
C:\ProgramData\anaconda3\lib\site-packages\torch\utils\data\dataloader.py in iter(self) 289 return _SingleProcessDataLoaderIter(self) 290 else: --> 291 return _MultiProcessingDataLoaderIter(self) 292 293 @property
C:\ProgramData\anaconda3\lib\site-packages\torch\utils\data\dataloader.py in init(self, loader) 762 # prime the prefetch loop 763 for _ in range(2 * self._num_workers): --> 764 self._try_put_index() 765 766 def _try_get_data(self, timeout=_utils.MP_STATUS_CHECK_INTERVAL):
C:\ProgramData\anaconda3\lib\site-packages\torch\utils\data\dataloader.py in _try_put_index(self) 992 assert self._tasks_outstanding < 2 * self._num_workers 993 try: --> 994 index = self._next_index() 995 except StopIteration: 996 return
C:\ProgramData\anaconda3\lib\site-packages\torch\utils\data\dataloader.py in _next_index(self) 355 356 def _next_index(self): --> 357 return next(self._sampler_iter) # may raise StopIteration 358 359 def _next_data(self):
C:\ProgramData\anaconda3\lib\site-packages\torch\utils\data\sampler.py in iter(self) 206 def iter(self): 207 batch = [] --> 208 for idx in self.sampler: 209 batch.append(idx) 210 if len(batch) == self.batch_size:
C:\ProgramData\anaconda3\lib\site-packages\torch\utils\data\sampler.py in iter(self) 60 61 def iter(self): ---> 62 return iter(range(len(self.data_source))) 63 64 def len(self):
TypeError: object of type 'SalObjDataset' has no len()
It seems that your input is incorrect. Before feeding to the dataloader, you can check the exact contents of your image or image name list as well as the variables in getitem function step by step.
On Thu, May 27, 2021 at 12:05 PM sathish4ds @.***> wrote:
This is the error
TypeError Traceback (most recent call last) in 3 #filename = "7364867-0-Copy1.jpg" 4 old_start = time.time() ----> 5 process_image(image_dir,Final_Img_outpath) 6 print('Time taken for bg removal', time.time() - old_start)
in process_image(img_name_list, Final_Img_outpath) 40 41 # --------- 4. inference for each image --------- ---> 42 for i_test, data_test in enumerate(test_salobj_dataloader): 43 44 print("inferencing:",img_name_list[i_test].split(os.sep)[-1])
C:\ProgramData\anaconda3\lib\site-packages\torch\utils\data\dataloader.py in iter(self) 289 return _SingleProcessDataLoaderIter(self) 290 else: --> 291 return _MultiProcessingDataLoaderIter(self) 292 293 @Property https://github.com/Property
C:\ProgramData\anaconda3\lib\site-packages\torch\utils\data\dataloader.py in init(self, loader) 762 # prime the prefetch loop 763 for _ in range(2 * self._num_workers): --> 764 self._try_put_index() 765 766 def _try_get_data(self, timeout=_utils.MP_STATUS_CHECK_INTERVAL):
C:\ProgramData\anaconda3\lib\site-packages\torch\utils\data\dataloader.py in _try_put_index(self) 992 assert self._tasks_outstanding < 2 * self._num_workers 993 try: --> 994 index = self._next_index() 995 except StopIteration: 996 return
C:\ProgramData\anaconda3\lib\site-packages\torch\utils\data\dataloader.py in _next_index(self) 355 356 def _next_index(self): --> 357 return next(self._sampler_iter) # may raise StopIteration 358 359 def _next_data(self):
C:\ProgramData\anaconda3\lib\site-packages\torch\utils\data\sampler.py in iter(self) 206 def iter(self): 207 batch = [] --> 208 for idx in self.sampler: 209 batch.append(idx) 210 if len(batch) == self.batch_size:
C:\ProgramData\anaconda3\lib\site-packages\torch\utils\data\sampler.py in iter(self) 60 61 def iter(self): ---> 62 return iter(range(len(self.data_source))) 63 64 def len(self):
TypeError: object of type 'SalObjDataset' has no len()
— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/xuebinqin/U-2-Net/issues/210#issuecomment-849427692, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADSGORJO4YUKX5DKPBILXT3TPX4LNANCNFSM45TSHYQQ .
-- Xuebin Qin PhD Department of Computing Science University of Alberta, Edmonton, AB, Canada Homepage:https://webdocs.cs.ualberta.ca/~xuebin/
I am trying to give image as input to the dataloader in place of list of image path can you help me on this
@sathish4ds
Hello. I have modified the SalObjDataset in the dataloader.py to accept images as input.
- On lines 240 and 247 of
data_loader.py, I removed io.imread. - Similarly, in
u2net_test.py, I removed io.imread on line 41. - on line 61, put the path of the image
- on line 65, I changed img_name_list = image_dir.
This may not be the answer you want.