[go: nahoru, domu]

Skip to content
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

Simplifying some code #76

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
'Refactored by Sourcery'
  • Loading branch information
Sourcery AI authored and Nourollah committed Apr 3, 2022
commit 48f990b258f52d5e8880380c7a00df29bce045d8
24 changes: 12 additions & 12 deletions datasets/cityscapes.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,15 @@ def __init__(self, root, split='train', mode='fine', target_type='semantic', tra
if not os.path.isdir(self.images_dir) or not os.path.isdir(self.targets_dir):
raise RuntimeError('Dataset not found or incomplete. Please make sure all required folders for the'
' specified "split" and "mode" are inside the "root" directory')

for city in os.listdir(self.images_dir):
img_dir = os.path.join(self.images_dir, city)
target_dir = os.path.join(self.targets_dir, city)

for file_name in os.listdir(img_dir):
self.images.append(os.path.join(img_dir, file_name))
target_name = '{}_{}'.format(file_name.split('_leftImg8bit')[0],
self._get_target_suffix(self.mode, self.target_type))
target_name = f"{file_name.split('_leftImg8bit')[0]}_{self._get_target_suffix(self.mode, self.target_type)}"

self.targets.append(os.path.join(target_dir, target_name))

@classmethod
Expand Down Expand Up @@ -135,13 +135,13 @@ def _load_json(self, path):
return data

def _get_target_suffix(self, mode, target_type):
if target_type == 'instance':
return '{}_instanceIds.png'.format(mode)
elif target_type == 'semantic':
return '{}_labelIds.png'.format(mode)
elif target_type == 'color':
return '{}_color.png'.format(mode)
elif target_type == 'polygon':
return '{}_polygons.json'.format(mode)
if target_type == 'color':
return f'{mode}_color.png'
elif target_type == 'depth':
return '{}_disparity.png'.format(mode)
return f'{mode}_disparity.png'
elif target_type == 'instance':
return f'{mode}_instanceIds.png'
elif target_type == 'polygon':
return f'{mode}_polygons.json'
elif target_type == 'semantic':
return f'{mode}_labelIds.png'
8 changes: 3 additions & 5 deletions datasets/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ def makedir_exist_ok(dirpath):
try:
os.makedirs(dirpath)
except OSError as e:
if e.errno == errno.EEXIST:
pass
else:
if e.errno != errno.EEXIST:
raise


Expand All @@ -63,10 +61,10 @@ def download_url(url, root, filename=None, md5=None):

# downloads file
if os.path.isfile(fpath) and check_integrity(fpath, md5):
print('Using downloaded and verified file: ' + fpath)
print(f'Using downloaded and verified file: {fpath}')
else:
try:
print('Downloading ' + url + ' to ' + fpath)
print(f'Downloading {url} to {fpath}')
urllib.request.urlretrieve(
url, fpath,
reporthook=gen_bar_updater(tqdm(unit='B', unit_scale=True))
Expand Down
12 changes: 6 additions & 6 deletions datasets/voc.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,14 @@ def __init__(self,
if year=='2012_aug':
is_aug = True
year = '2012'

self.root = os.path.expanduser(root)
self.year = year
self.url = DATASET_YEAR_DICT[year]['url']
self.filename = DATASET_YEAR_DICT[year]['filename']
self.md5 = DATASET_YEAR_DICT[year]['md5']
self.transform = transform

self.image_set = image_set
base_dir = DATASET_YEAR_DICT[year]['base_dir']
voc_root = os.path.join(self.root, base_dir)
Expand All @@ -112,7 +112,7 @@ def __init__(self,
if not os.path.isdir(voc_root):
raise RuntimeError('Dataset not found or corrupted.' +
' You can use download=True to download it')

if is_aug and image_set=='train':
mask_dir = os.path.join(voc_root, 'SegmentationClassAug')
assert os.path.exists(mask_dir), "SegmentationClassAug not found, please refer to README.md and prepare it manually"
Expand All @@ -129,9 +129,9 @@ def __init__(self,

with open(os.path.join(split_f), "r") as f:
file_names = [x.strip() for x in f.readlines()]
self.images = [os.path.join(image_dir, x + ".jpg") for x in file_names]
self.masks = [os.path.join(mask_dir, x + ".png") for x in file_names]

self.images = [os.path.join(image_dir, f'{x}.jpg') for x in file_names]
self.masks = [os.path.join(mask_dir, f'{x}.png') for x in file_names]
assert (len(self.images) == len(self.masks))

def __getitem__(self, index):
Expand Down
15 changes: 10 additions & 5 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,15 @@ def get_argparser():
help="num classes (default: None)")

# Deeplab Options
available_models = sorted(name for name in network.modeling.__dict__ if name.islower() and \
not (name.startswith("__") or name.startswith('_')) and callable(
network.modeling.__dict__[name])
)
available_models = sorted(
name
for name in network.modeling.__dict__
if name.islower()
and not name.startswith("__")
and not name.startswith('_')
and callable(network.modeling.__dict__[name])
)

parser.add_argument("--model", type=str, default='deeplabv3plus_mobilenet',
choices=available_models, help='model name')
parser.add_argument("--separable_conv", action='store_true', default=False,
Expand Down Expand Up @@ -281,7 +286,7 @@ def save_ckpt(path):
"scheduler_state": scheduler.state_dict(),
"best_score": best_score,
}, path)
print("Model saved as %s" % path)
print(f"Model saved as {path}")

utils.mkdir('checkpoints')
# Restore
Expand Down
5 changes: 2 additions & 3 deletions metrics/stream_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,10 @@ def to_str(results):

def _fast_hist(self, label_true, label_pred):
mask = (label_true >= 0) & (label_true < self.n_classes)
hist = np.bincount(
return np.bincount(
self.n_classes * label_true[mask].astype(int) + label_pred[mask],
minlength=self.n_classes ** 2,
).reshape(self.n_classes, self.n_classes)
return hist

def get_results(self):
"""Returns accuracy score evaluation result.
Expand Down Expand Up @@ -85,7 +84,7 @@ def reset(self):
class AverageMeter(object):
"""Computes average values"""
def __init__(self):
self.book = dict()
self.book = {}

def reset_all(self):
self.book.clear()
Expand Down
16 changes: 8 additions & 8 deletions network/_deeplab.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,13 @@ class ASPP(nn.Module):
def __init__(self, in_channels, atrous_rates):
super(ASPP, self).__init__()
out_channels = 256
modules = []
modules.append(nn.Sequential(
nn.Conv2d(in_channels, out_channels, 1, bias=False),
nn.BatchNorm2d(out_channels),
nn.ReLU(inplace=True)))
modules = [
nn.Sequential(
nn.Conv2d(in_channels, out_channels, 1, bias=False),
nn.BatchNorm2d(out_channels),
nn.ReLU(inplace=True),
)
]

rate1, rate2, rate3 = tuple(atrous_rates)
modules.append(ASPPConv(in_channels, out_channels, rate1))
Expand All @@ -155,9 +157,7 @@ def __init__(self, in_channels, atrous_rates):
nn.Dropout(0.1),)

def forward(self, x):
res = []
for conv in self.convs:
res.append(conv(x))
res = [conv(x) for conv in self.convs]
res = torch.cat(res, dim=1)
return self.project(res)

Expand Down
38 changes: 26 additions & 12 deletions network/backbone/hrnetv2.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,28 @@ def __init__(self, stage, output_branches, c):
nn.Upsample(scale_factor=(2.0 ** (branch_number - branch_output_number)), mode='nearest'),
))
elif branch_number < branch_output_number:
downsampling_fusion = []
for _ in range(branch_output_number - branch_number - 1):
downsampling_fusion.append(nn.Sequential(
nn.Conv2d(c * (2 ** branch_number), c * (2 ** branch_number), kernel_size=3, stride=2,
padding=1,
bias=False),
nn.BatchNorm2d(c * (2 ** branch_number), eps=1e-05, momentum=0.1, affine=True,
track_running_stats=True),
downsampling_fusion = [
nn.Sequential(
nn.Conv2d(
c * (2 ** branch_number),
c * (2 ** branch_number),
kernel_size=3,
stride=2,
padding=1,
bias=False,
),
nn.BatchNorm2d(
c * (2 ** branch_number),
eps=1e-05,
momentum=0.1,
affine=True,
track_running_stats=True,
),
nn.ReLU(inplace=True),
))
)
for _ in range(branch_output_number - branch_number - 1)
]

downsampling_fusion.append(nn.Sequential(
nn.Conv2d(c * (2 ** branch_number), c * (2 ** branch_output_number), kernel_size=3,
stride=2, padding=1,
Expand Down Expand Up @@ -224,15 +236,17 @@ def __init__(self, c=48, num_blocks=[1, 4, 3], num_classes=1000):

# Classifier (extra module if want to use for classification):
# pool, reduce dimensionality, flatten, connect to linear layer for classification:
out_channels = sum([c * 2 ** i for i in range(len(num_blocks)+1)]) # total output channels of HRNetV2
out_channels = sum(c * 2 ** i for i in range(len(num_blocks)+1))
pool_feature_map = 8
self.bn_classifier = nn.Sequential(
nn.Conv2d(out_channels, out_channels // 4, kernel_size=1, bias=False),
nn.BatchNorm2d(out_channels // 4, eps=1e-05, affine=True, track_running_stats=True),
nn.BatchNorm2d(
out_channels // 4, eps=1e-05, affine=True, track_running_stats=True
),
nn.ReLU(inplace=True),
nn.AdaptiveAvgPool2d(pool_feature_map),
nn.Flatten(),
nn.Linear(pool_feature_map * pool_feature_map * (out_channels // 4), num_classes),
nn.Linear(pool_feature_map ** 2 * (out_channels // 4), num_classes),
)

@staticmethod
Expand Down
18 changes: 8 additions & 10 deletions network/backbone/mobilenetv2.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,7 @@ def __init__(self, inp, oup, stride, dilation, expand_ratio):

def forward(self, x):
x_pad = F.pad(x, self.input_padding)
if self.use_res_connect:
return x + self.conv(x_pad)
else:
return self.conv(x_pad)
return x + self.conv(x_pad) if self.use_res_connect else self.conv(x_pad)

class MobileNetV2(nn.Module):
def __init__(self, num_classes=1000, output_stride=8, width_mult=1.0, inverted_residual_setting=None, round_nearest=8):
Expand All @@ -95,10 +92,7 @@ def __init__(self, num_classes=1000, output_stride=8, width_mult=1.0, inverted_r
"""
super(MobileNetV2, self).__init__()
block = InvertedResidual
input_channel = 32
last_channel = 1280
self.output_stride = output_stride
current_stride = 1
if inverted_residual_setting is None:
inverted_residual_setting = [
# t, c, n, s
Expand All @@ -113,14 +107,18 @@ def __init__(self, num_classes=1000, output_stride=8, width_mult=1.0, inverted_r

# only check the first element, assuming user knows t,c,n,s are required
if len(inverted_residual_setting) == 0 or len(inverted_residual_setting[0]) != 4:
raise ValueError("inverted_residual_setting should be non-empty "
"or a 4-element list, got {}".format(inverted_residual_setting))
raise ValueError(
f"inverted_residual_setting should be non-empty or a 4-element list, got {inverted_residual_setting}"
)


input_channel = 32
# building first layer
input_channel = _make_divisible(input_channel * width_mult, round_nearest)
last_channel = 1280
self.last_channel = _make_divisible(last_channel * max(1.0, width_mult), round_nearest)
features = [ConvBNReLU(3, input_channel, stride=2)]
current_stride *= 2
current_stride = 1 * 2
dilation=1
previous_dilation = 1

Expand Down
37 changes: 28 additions & 9 deletions network/backbone/resnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,10 @@ def __init__(self, block, layers, num_classes=1000, zero_init_residual=False,
# the 2x2 stride with a dilated convolution instead
replace_stride_with_dilation = [False, False, False]
if len(replace_stride_with_dilation) != 3:
raise ValueError("replace_stride_with_dilation should be None "
"or a 3-element tuple, got {}".format(replace_stride_with_dilation))
raise ValueError(
f"replace_stride_with_dilation should be None or a 3-element tuple, got {replace_stride_with_dilation}"
)

self.groups = groups
self.base_width = width_per_group
self.conv1 = nn.Conv2d(3, self.inplanes, kernel_size=7, stride=2, padding=3,
Expand Down Expand Up @@ -184,14 +186,31 @@ def _make_layer(self, block, planes, blocks, stride=1, dilate=False):
norm_layer(planes * block.expansion),
)

layers = []
layers.append(block(self.inplanes, planes, stride, downsample, self.groups,
self.base_width, previous_dilation, norm_layer))
layers = [
block(
self.inplanes,
planes,
stride,
downsample,
self.groups,
self.base_width,
previous_dilation,
norm_layer,
)
]

self.inplanes = planes * block.expansion
for _ in range(1, blocks):
layers.append(block(self.inplanes, planes, groups=self.groups,
base_width=self.base_width, dilation=self.dilation,
norm_layer=norm_layer))
layers.extend(
block(
self.inplanes,
planes,
groups=self.groups,
base_width=self.base_width,
dilation=self.dilation,
norm_layer=norm_layer,
)
for _ in range(1, blocks)
)

return nn.Sequential(*layers)

Expand Down
Loading