Skip to content

Commit

Permalink
Add support for regression problems (#1667)
Browse files Browse the repository at this point in the history
* add regression

* modify evaluate

* .

* remove accidentally commited file

* works for regression and binary problems now

* refactor codes

* clean up codes

* set default problem_type

* modify error info

* free base_estimator

* fix

* move problem_type arg to end

* fix negative rmse & predict_proba

* fix predict_proba

* add ImagePrediction class

* add newline to eof & set default fromlist to None

* clean up

* add ImagePredictionDataset

* modify dataset class & add test case

* clean up code

* add space

* free model_zoo

* fix import

Co-authored-by: Ubuntu <[email protected]>
Co-authored-by: Joshua Z. Zhang <[email protected]>
  • Loading branch information
3 people authored Jun 10, 2021
1 parent 978d460 commit a169654
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 68 deletions.
35 changes: 21 additions & 14 deletions gluoncv/auto/data/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,13 @@ class ImageClassificationDataset(pd.DataFrame):

def __init__(self, data, classes=None, image_column='image', label_column='label', **kwargs):
root = kwargs.pop('root', None)
no_class = kwargs.pop('no_class', False)
if isinstance(data, str) and data.endswith('csv'):
data = self.from_csv(data, root=root, image_column=image_column, label_column=label_column)
self.classes = classes
data = self.from_csv(data, root=root, image_column=image_column, label_column=label_column, no_class=no_class)
if no_class:
self.classes = []
else:
self.classes = classes
self.IMG_COL = image_column
self.LABEL_COL = label_column
super().__init__(data, **kwargs)
Expand Down Expand Up @@ -138,8 +142,11 @@ def show_images(self, indices=None, nsample=16, ncol=4, shuffle=True, resize=224
interpolation=cv2.INTER_AREA), cv2.COLOR_BGR2RGB) for idx in indices if idx < len(self)]
titles = None
if self.LABEL_COL in self.columns:
titles = [self.classes[int(self.at[idx, self.LABEL_COL])] + ': ' + str(self.at[idx, self.LABEL_COL]) \
for idx in indices if idx < len(self)]
if self.classes:
titles = [self.classes[int(self.at[idx, self.LABEL_COL])] + ': ' + str(self.at[idx, self.LABEL_COL]) \
for idx in indices if idx < len(self)]
else:
titles = [str(self.at[idx, self.LABEL_COL]) for idx in indices if idx < len(self)]
_show_images(images, cols=ncol, titles=titles, fontsize=fontsize)

def to_mxnet(self):
Expand All @@ -148,7 +155,7 @@ def to_mxnet(self):
return _MXImageClassificationDataset(df)

@classmethod
def from_csv(cls, csv_file, root=None, image_column='image', label_column='label'):
def from_csv(cls, csv_file, root=None, image_column='image', label_column='label', no_class=False):
r"""Create from csv file.
Parameters
Expand All @@ -173,10 +180,10 @@ def from_csv(cls, csv_file, root=None, image_column='image', label_column='label
else:
classes = df[label_column].unique().tolist()
df = _absolute_pathify(df, root=root, column=image_column)
return cls(df, classes=classes, image_column=image_column, label_column=label_column)
return cls(df, classes=classes, image_column=image_column, label_column=label_column, no_class=no_class)

@classmethod
def from_folder(cls, root, exts=('.jpg', '.jpeg', '.png')):
def from_folder(cls, root, exts=('.jpg', '.jpeg', '.png'), no_class=False):
r"""A dataset for loading image files stored in a folder structure.
like::
root/car/0001.jpg
Expand Down Expand Up @@ -219,10 +226,10 @@ def from_folder(cls, root, exts=('.jpg', '.jpeg', '.png')):
continue
items['image'].append(filename)
items['label'].append(label)
return cls(items, classes=synsets)
return cls(items, classes=synsets, no_class=no_class)

@classmethod
def from_folders(cls, root, train='train', val='val', test='test', exts=('.jpg', '.jpeg', '.png')):
def from_folders(cls, root, train='train', val='val', test='test', exts=('.jpg', '.jpeg', '.png'), no_class=False):
"""Method for loading (already) splited datasets under root.
like::
root/train/car/0001.jpg
Expand Down Expand Up @@ -275,15 +282,15 @@ def from_folders(cls, root, train='train', val='val', test='test', exts=('.jpg',
train_data, val_data, test_data = empty, empty, empty
# train
if os.path.isdir(train_root):
train_data = cls.from_folder(train_root, exts=exts)
train_data = cls.from_folder(train_root, exts=exts, no_class=no_class)
else:
raise ValueError('Train split does not exist: {}'.format(train))
# val
if os.path.isdir(val_root):
val_data = cls.from_folder(val_root, exts=exts)
val_data = cls.from_folder(val_root, exts=exts, no_class=no_class)
# test
if os.path.isdir(test_root):
test_data = cls.from_folder(test_root, exts=exts)
test_data = cls.from_folder(test_root, exts=exts, no_class=no_class)

# check synsets, val/test synsets can be subsets(order matters!) or exact matches of train synset
if len(val_data) and not _check_synsets(train_data.classes, val_data.classes):
Expand All @@ -294,7 +301,7 @@ def from_folders(cls, root, train='train', val='val', test='test', exts=('.jpg',
return train_data, val_data, test_data

@classmethod
def from_name_func(cls, im_list, fn, root=None):
def from_name_func(cls, im_list, fn, root=None, no_class=False):
"""Short summary.
Parameters
Expand Down Expand Up @@ -333,7 +340,7 @@ def from_name_func(cls, im_list, fn, root=None):
if label not in synsets:
synsets.append(label)
items['label'].append(synsets.index(label)) # int label id
return cls(items, classes=synsets)
return cls(items, classes=synsets, no_class=no_class)

@classmethod
def from_name_re(cls, im_list, fn, root=None):
Expand Down
Loading

0 comments on commit a169654

Please sign in to comment.