-
Notifications
You must be signed in to change notification settings - Fork 791
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sourcery Starbot ⭐ refactored meetps/pytorch-semseg #264
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -157,13 +157,8 @@ def __call__(self, img, mask): | |
x_offset = int(2 * (random.random() - 0.5) * self.offset[0]) | ||
y_offset = int(2 * (random.random() - 0.5) * self.offset[1]) | ||
|
||
x_crop_offset = x_offset | ||
y_crop_offset = y_offset | ||
if x_offset < 0: | ||
x_crop_offset = 0 | ||
if y_offset < 0: | ||
y_crop_offset = 0 | ||
|
||
x_crop_offset = max(x_offset, 0) | ||
y_crop_offset = max(y_offset, 0) | ||
Comment on lines
-160
to
+161
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
cropped_img = tf.crop( | ||
img, | ||
y_crop_offset, | ||
|
@@ -175,13 +170,13 @@ def __call__(self, img, mask): | |
if x_offset >= 0 and y_offset >= 0: | ||
padding_tuple = (0, 0, x_offset, y_offset) | ||
|
||
elif x_offset >= 0 and y_offset < 0: | ||
elif x_offset >= 0: | ||
padding_tuple = (0, abs(y_offset), x_offset, 0) | ||
|
||
elif x_offset < 0 and y_offset >= 0: | ||
elif y_offset >= 0: | ||
padding_tuple = (abs(x_offset), 0, 0, y_offset) | ||
|
||
elif x_offset < 0 and y_offset < 0: | ||
else: | ||
padding_tuple = (abs(x_offset), abs(y_offset), 0, 0) | ||
|
||
return ( | ||
|
@@ -237,11 +232,11 @@ def __call__(self, img, mask): | |
if w > h: | ||
ow = self.size | ||
oh = int(self.size * h / w) | ||
return (img.resize((ow, oh), Image.BILINEAR), mask.resize((ow, oh), Image.NEAREST)) | ||
else: | ||
oh = self.size | ||
ow = int(self.size * w / h) | ||
return (img.resize((ow, oh), Image.BILINEAR), mask.resize((ow, oh), Image.NEAREST)) | ||
|
||
return (img.resize((ow, oh), Image.BILINEAR), mask.resize((ow, oh), Image.NEAREST)) | ||
Comment on lines
-240
to
+239
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
|
||
class RandomSizedCrop(object): | ||
|
@@ -250,7 +245,7 @@ def __init__(self, size): | |
|
||
def __call__(self, img, mask): | ||
assert img.size == mask.size | ||
for attempt in range(10): | ||
for _ in range(10): | ||
Comment on lines
-253
to
+248
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
area = img.size[0] * img.size[1] | ||
target_area = random.uniform(0.45, 1.0) * area | ||
aspect_ratio = random.uniform(0.5, 2) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,7 +35,7 @@ def __init__( | |
if not self.test_mode: | ||
for split in ["training", "validation"]: | ||
file_list = recursive_glob( | ||
rootdir=self.root + "images/" + self.split + "/", suffix=".jpg" | ||
rootdir=f"{self.root}images/{self.split}/", suffix=".jpg" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
) | ||
self.files[split] = file_list | ||
|
||
|
@@ -44,7 +44,7 @@ def __len__(self): | |
|
||
def __getitem__(self, index): | ||
img_path = self.files[self.split][index].rstrip() | ||
lbl_path = img_path[:-4] + "_seg.png" | ||
lbl_path = f"{img_path[:-4]}_seg.png" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
img = m.imread(img_path) | ||
img = np.array(img, dtype=np.uint8) | ||
|
@@ -96,7 +96,7 @@ def decode_segmap(self, temp, plot=False): | |
r = temp.copy() | ||
g = temp.copy() | ||
b = temp.copy() | ||
for l in range(0, self.n_classes): | ||
for l in range(self.n_classes): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
r[temp == l] = 10 * (l % 10) | ||
g[temp == l] = l | ||
b[temp == l] = 0 | ||
|
@@ -105,11 +105,10 @@ def decode_segmap(self, temp, plot=False): | |
rgb[:, :, 0] = r / 255.0 | ||
rgb[:, :, 1] = g / 255.0 | ||
rgb[:, :, 2] = b / 255.0 | ||
if plot: | ||
plt.imshow(rgb) | ||
plt.show() | ||
else: | ||
if not plot: | ||
return rgb | ||
plt.imshow(rgb) | ||
plt.show() | ||
|
||
|
||
if __name__ == "__main__": | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,16 +33,16 @@ def __init__( | |
|
||
if not self.test_mode: | ||
for split in ["train", "test", "val"]: | ||
file_list = os.listdir(root + "/" + split) | ||
file_list = os.listdir(f"{root}/{split}") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
self.files[split] = file_list | ||
|
||
def __len__(self): | ||
return len(self.files[self.split]) | ||
|
||
def __getitem__(self, index): | ||
img_name = self.files[self.split][index] | ||
img_path = self.root + "/" + self.split + "/" + img_name | ||
lbl_path = self.root + "/" + self.split + "annot/" + img_name | ||
img_path = f"{self.root}/{self.split}/{img_name}" | ||
lbl_path = f"{self.root}/{self.split}annot/{img_name}" | ||
Comment on lines
-44
to
+45
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
img = m.imread(img_path) | ||
img = np.array(img, dtype=np.uint8) | ||
|
@@ -107,7 +107,7 @@ def decode_segmap(self, temp, plot=False): | |
r = temp.copy() | ||
g = temp.copy() | ||
b = temp.copy() | ||
for l in range(0, self.n_classes): | ||
for l in range(self.n_classes): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
r[temp == l] = label_colours[l, 0] | ||
g[temp == l] = label_colours[l, 1] | ||
b[temp == l] = label_colours[l, 2] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -133,7 +133,7 @@ def __init__( | |
self.class_map = dict(zip(self.valid_classes, range(19))) | ||
|
||
if not self.files[split]: | ||
raise Exception("No files for split=[%s] found in %s" % (split, self.images_base)) | ||
raise Exception(f"No files for split=[{split}] found in {self.images_base}") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
print("Found %d %s images" % (len(self.files[split]), split)) | ||
|
||
|
@@ -150,7 +150,7 @@ def __getitem__(self, index): | |
lbl_path = os.path.join( | ||
self.annotations_base, | ||
img_path.split(os.sep)[-2], | ||
os.path.basename(img_path)[:-15] + "gtFine_labelIds.png", | ||
f"{os.path.basename(img_path)[:-15]}gtFine_labelIds.png", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
) | ||
|
||
img = m.imread(img_path) | ||
|
@@ -205,7 +205,7 @@ def decode_segmap(self, temp): | |
r = temp.copy() | ||
g = temp.copy() | ||
b = temp.copy() | ||
for l in range(0, self.n_classes): | ||
for l in range(self.n_classes): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
r[temp == l] = self.label_colours[l][0] | ||
g[temp == l] = self.label_colours[l][1] | ||
b[temp == l] = self.label_colours[l][2] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,7 +40,7 @@ def __init__( | |
self.ignore_id = 250 | ||
|
||
if not self.files[split]: | ||
raise Exception("No files for split=[%s] found in %s" % (split, self.images_base)) | ||
raise Exception(f"No files for split=[{split}] found in {self.images_base}") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
print("Found %d %s images" % (len(self.files[split]), split)) | ||
|
||
|
@@ -53,7 +53,7 @@ def parse_config(self): | |
class_names = [] | ||
class_ids = [] | ||
class_colors = [] | ||
print("There are {} labels in the config file".format(len(labels))) | ||
print(f"There are {len(labels)} labels in the config file") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
for label_id, label in enumerate(labels): | ||
class_names.append(label["readable"]) | ||
class_ids.append(label_id) | ||
|
@@ -86,9 +86,7 @@ def __getitem__(self, index): | |
return img, lbl | ||
|
||
def transform(self, img, lbl): | ||
if self.img_size == ("same", "same"): | ||
pass | ||
else: | ||
if self.img_size != ("same", "same"): | ||
img = img.resize( | ||
(self.img_size[0], self.img_size[1]), resample=Image.LANCZOS | ||
) # uint8 with RGB mode | ||
|
@@ -103,7 +101,7 @@ def decode_segmap(self, temp): | |
r = temp.copy() | ||
g = temp.copy() | ||
b = temp.copy() | ||
for l in range(0, self.n_classes): | ||
for l in range(self.n_classes): | ||
r[temp == l] = self.class_colors[l][0] | ||
g[temp == l] = self.class_colors[l][1] | ||
b[temp == l] = self.class_colors[l][2] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function
get_composed_augmentations
refactored with the following changes:use-fstring-for-formatting
)