Browse Source

fix pylint problem.

tags/v0.5.0-beta
yangyongjie 5 years ago
parent
commit
4d2b9b10f9
5 changed files with 51 additions and 36 deletions
  1. +22
    -22
      model_zoo/deeplabv3/src/deeplabv3.py
  2. +1
    -1
      model_zoo/deeplabv3/src/ei_dataset.py
  3. +19
    -4
      model_zoo/deeplabv3/src/md_dataset.py
  4. +7
    -7
      model_zoo/deeplabv3/src/utils/custom_transforms.py
  5. +2
    -2
      model_zoo/deeplabv3/src/utils/file_io.py

+ 22
- 22
model_zoo/deeplabv3/src/deeplabv3.py View File

@@ -23,11 +23,11 @@ from .backbone.resnet_deeplab import _conv_bn_relu, resnet50_dl, _deep_conv_bn_r

class ASPPSampleBlock(nn.Cell):
"""ASPP sample block."""
def __init__(self, feature_shape, scale_size,output_stride):
def __init__(self, feature_shape, scale_size, output_stride):
super(ASPPSampleBlock, self).__init__()
sample_h = (feature_shape[0] * scale_size + 1) / output_stride + 1
sample_w = (feature_shape[1] * scale_size + 1) / output_stride + 1
self.sample = P.ResizeBilinear((int(sample_h),int(sample_w)),align_corners=True)
self.sample = P.ResizeBilinear((int(sample_h), int(sample_w)), align_corners=True)

def construct(self, x):
return self.sample(x)
@@ -84,25 +84,25 @@ class ASPP(nn.Cell):
aspp_scale_depth_size = np.ceil((feature_shape[0]*scale_size)/16)
if atrous_rates is None:
break
for i in range(len(atrous_rates)):
for rate in atrous_rates:
padding = 0
for j in range(100):
padded_size = atrous_rates[i] * j
if padded_size >= aspp_scale_depth_size + 2 * atrous_rates[i]:
padding = padded_size - aspp_scale_depth_size - 2 * atrous_rates[i]
padded_size = rate * j
if padded_size >= aspp_scale_depth_size + 2 * rate:
padding = padded_size - aspp_scale_depth_size - 2 * rate
break
paddings = [[atrous_rates[i], atrous_rates[i] + int(padding)],
[atrous_rates[i], atrous_rates[i] + int(padding)]]
self.aspp_depth_spacetobatch = SpaceToBatch(atrous_rates[i],paddings)
paddings = [[rate, rate + int(padding)],
[rate, rate + int(padding)]]
self.aspp_depth_spacetobatch = SpaceToBatch(rate, paddings)
self.aspp_depth_spacetobatchs.append(self.aspp_depth_spacetobatch)
crops =[[0, int(padding)], [0, int(padding)]]
self.aspp_depth_batchtospace = BatchToSpace(atrous_rates[i],crops)
crops = [[0, int(padding)], [0, int(padding)]]
self.aspp_depth_batchtospace = BatchToSpace(rate, crops)
self.aspp_depth_batchtospaces.append(self.aspp_depth_batchtospace)
self.aspp_depths = nn.CellList(self.aspp_depths)
self.aspp_depth_spacetobatchs = nn.CellList(self.aspp_depth_spacetobatchs)
self.aspp_depth_batchtospaces = nn.CellList(self.aspp_depth_batchtospaces)

self.global_pooling = nn.AvgPool2d(kernel_size=(int(feature_shape[0]),int(feature_shape[1])))
self.global_pooling = nn.AvgPool2d(kernel_size=(int(feature_shape[0]), int(feature_shape[1])))
self.global_poolings = []
for scale_size in scale_sizes:
pooling_h = np.ceil((feature_shape[0]*scale_size)/output_stride)
@@ -116,7 +116,7 @@ class ASPP(nn.Cell):
use_batch_statistics=fine_tune_batch_norm)
self.samples = []
for scale_size in scale_sizes:
self.samples.append(ASPPSampleBlock(feature_shape,scale_size,output_stride))
self.samples.append(ASPPSampleBlock(feature_shape, scale_size, output_stride))
self.samples = nn.CellList(self.samples)
self.feature_shape = feature_shape
self.concat = P.Concat(axis=1)
@@ -126,7 +126,7 @@ class ASPP(nn.Cell):
aspp1 = self.global_poolings[scale_index](x)
aspp1 = self.conv_bn(aspp1)
aspp1 = self.samples[scale_index](aspp1)
output = self.concat((aspp1,aspp0))
output = self.concat((aspp1, aspp0))

for i in range(len(self.atrous_rates)):
aspp_i = self.aspp_depth_spacetobatchs[i + scale_index * len(self.atrous_rates)](x)
@@ -135,13 +135,13 @@ class ASPP(nn.Cell):
aspp_i = self.aspp_depth_bn(aspp_i)
aspp_i = self.aspp_depth_relu(aspp_i)
aspp_i = self.aspp_pointwise(aspp_i)
output = self.concat((output,aspp_i))
output = self.concat((output, aspp_i))
return output


class DecoderSampleBlock(nn.Cell):
"""Decoder sample block."""
def __init__(self,feature_shape,scale_size=1.0,decoder_output_stride=4):
def __init__(self, feature_shape, scale_size=1.0, decoder_output_stride=4):
super(DecoderSampleBlock, self).__init__()
sample_h = (feature_shape[0] * scale_size + 1) / decoder_output_stride + 1
sample_w = (feature_shape[1] * scale_size + 1) / decoder_output_stride + 1
@@ -206,7 +206,7 @@ class Decoder(nn.Cell):
self.concat = P.Concat(axis=1)
self.samples = []
for scale_size in scale_sizes:
self.samples.append(DecoderSampleBlock(feature_shape,scale_size,decoder_output_stride))
self.samples.append(DecoderSampleBlock(feature_shape, scale_size, decoder_output_stride))
self.samples = nn.CellList(self.samples)

def construct(self, x, low_level_feature, scale_index):
@@ -359,10 +359,10 @@ class DeepLabV3(nn.Cell):

self.image_pyramid = image_pyramid
scale_sizes = []
for i in range(len(image_pyramid)):
scale_sizes.append(image_pyramid[i])
for i in range(len(infer_scale_sizes)):
scale_sizes.append(infer_scale_sizes[i])
for pyramid in image_pyramid:
scale_sizes.append(pyramid)
for scale in infer_scale_sizes:
scale_sizes.append(scale)
self.samples = []
for scale_size in scale_sizes:
self.samples.append(SampleBlock(feature_shape, scale_size))
@@ -454,4 +454,4 @@ def deeplabv3_resnet50(num_classes, feature_shape, image_pyramid,
decoder_output_stride=decoder_output_stride,
output_stride=output_stride,
fine_tune_batch_norm=fine_tune_batch_norm,
image_pyramid=image_pyramid)
image_pyramid=image_pyramid)

+ 1
- 1
model_zoo/deeplabv3/src/ei_dataset.py View File

@@ -20,7 +20,7 @@ import time
from .utils.adapter import get_raw_samples, read_image


class BaseDataset(object):
class BaseDataset:
"""
Create dataset.



+ 19
- 4
model_zoo/deeplabv3/src/md_dataset.py View File

@@ -21,7 +21,7 @@ from .ei_dataset import HwVocRawDataset
from .utils import custom_transforms as tr


class DataTransform(object):
class DataTransform:
"""Transform dataset for DeepLabV3."""

def __init__(self, args, usage):
@@ -29,12 +29,20 @@ class DataTransform(object):
self.usage = usage

def __call__(self, image, label):
if "train" == self.usage:
if self.usage == "train":
return self._train(image, label)
elif "eval" == self.usage:
if self.usage == "eval":
return self._eval(image, label)
return None

def _train(self, image, label):
"""
Process training data.

Args:
image (list): Image data.
label (list): Dataset label.
"""
image = Image.fromarray(image)
label = Image.fromarray(label)

@@ -50,6 +58,13 @@ class DataTransform(object):
return image, label

def _eval(self, image, label):
"""
Process eval data.

Args:
image (list): Image data.
label (list): Dataset label.
"""
image = Image.fromarray(image)
label = Image.fromarray(label)

@@ -93,7 +108,7 @@ def create_dataset(args, data_url, epoch_num=1, batch_size=1, usage="train"):
# 3658 steps / 183 = 20 epochs
if usage == "train":
dataset = dataset.shuffle(1464)
dataset = dataset.batch(batch_size, drop_remainder=(usage == usage))
dataset = dataset.batch(batch_size, drop_remainder=(usage == "train"))
dataset = dataset.repeat(count=epoch_num)
dataset.map_model = 4



+ 7
- 7
model_zoo/deeplabv3/src/utils/custom_transforms.py View File

@@ -19,7 +19,7 @@ import numpy as np
from PIL import Image, ImageOps, ImageFilter


class Normalize(object):
class Normalize:
"""Normalize a tensor image with mean and standard deviation.
Args:
mean (tuple): means for each channel.
@@ -37,7 +37,7 @@ class Normalize(object):
return img, mask


class RandomHorizontalFlip(object):
class RandomHorizontalFlip:
"""Randomly decide whether to horizontal flip."""
def __call__(self, img, mask):
if random.random() < 0.5:
@@ -47,7 +47,7 @@ class RandomHorizontalFlip(object):
return img, mask


class RandomRotate(object):
class RandomRotate:
"""
Randomly decide whether to rotate.

@@ -65,7 +65,7 @@ class RandomRotate(object):
return img, mask


class RandomGaussianBlur(object):
class RandomGaussianBlur:
"""Randomly decide whether to filter image with gaussian blur."""
def __call__(self, img, mask):
if random.random() < 0.5:
@@ -75,7 +75,7 @@ class RandomGaussianBlur(object):
return img, mask


class RandomScaleCrop(object):
class RandomScaleCrop:
"""Randomly decide whether to scale and crop image."""
def __init__(self, base_size, crop_size, fill=0):
self.base_size = base_size
@@ -110,7 +110,7 @@ class RandomScaleCrop(object):
return img, mask


class FixScaleCrop(object):
class FixScaleCrop:
"""Scale and crop image with fixing size."""
def __init__(self, crop_size):
self.crop_size = crop_size
@@ -135,7 +135,7 @@ class FixScaleCrop(object):
return img, mask


class FixedResize(object):
class FixedResize:
"""Resize image with fixing size."""
def __init__(self, size):
self.size = (size, size)


+ 2
- 2
model_zoo/deeplabv3/src/utils/file_io.py View File

@@ -1,4 +1,3 @@
#!/bin/bash
# Copyright 2020 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -13,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
"""File operation module."""
import os


@@ -33,4 +33,4 @@ def walk(url):
if _is_obs(url):
# TODO read cloud file.
return None
return os.walk(url)
return os.walk(url)

Loading…
Cancel
Save