From 44f66a45a641bd1cb24f569eaca84dcf23615cba Mon Sep 17 00:00:00 2001 From: minqiyang Date: Mon, 27 Aug 2018 13:39:57 +0800 Subject: [PATCH] Fix the random difference between python2 and python3 --- fluid/image_classification/reader.py | 27 +++++++-------- fluid/image_classification/train.py | 1 + fluid/object_detection/image_util.py | 50 ++++++++++++++-------------- fluid/object_detection/reader.py | 9 +++-- 4 files changed, 44 insertions(+), 43 deletions(-) diff --git a/fluid/image_classification/reader.py b/fluid/image_classification/reader.py index 3ea26ccb..b0df4fe2 100644 --- a/fluid/image_classification/reader.py +++ b/fluid/image_classification/reader.py @@ -7,6 +7,7 @@ import paddle from PIL import Image, ImageEnhance random.seed(0) +np.random.seed(0) DATA_DIM = 224 @@ -36,8 +37,8 @@ def crop_image(img, target_size, center): w_start = (width - size) / 2 h_start = (height - size) / 2 else: - w_start = random.randint(0, width - size) - h_start = random.randint(0, height - size) + w_start = np.random.randint(0, width - size) + h_start = np.random.randint(0, height - size) w_end = w_start + size h_end = h_start + size img = img.crop((w_start, h_start, w_end, h_end)) @@ -45,7 +46,7 @@ def crop_image(img, target_size, center): def random_crop(img, size, scale=[0.08, 1.0], ratio=[3. / 4., 4. / 3.]): - aspect_ratio = math.sqrt(random.uniform(*ratio)) + aspect_ratio = math.sqrt(np.random.uniform(*ratio)) w = 1. * aspect_ratio h = 1. / aspect_ratio @@ -54,14 +55,14 @@ def random_crop(img, size, scale=[0.08, 1.0], ratio=[3. / 4., 4. / 3.]): scale_max = min(scale[1], bound) scale_min = min(scale[0], bound) - target_area = img.size[0] * img.size[1] * random.uniform(scale_min, + target_area = img.size[0] * img.size[1] * np.random.uniform(scale_min, scale_max) target_size = math.sqrt(target_area) w = int(target_size * w) h = int(target_size * h) - i = random.randint(0, img.size[0] - w) - j = random.randint(0, img.size[1] - h) + i = np.random.randint(0, img.size[0] - w) + j = np.random.randint(0, img.size[1] - h) img = img.crop((i, j, i + w, j + h)) img = img.resize((size, size), Image.LANCZOS) @@ -69,26 +70,26 @@ def random_crop(img, size, scale=[0.08, 1.0], ratio=[3. / 4., 4. / 3.]): def rotate_image(img): - angle = random.randint(-10, 10) + angle = np.random.randint(-10, 10) img = img.rotate(angle) return img def distort_color(img): def random_brightness(img, lower=0.5, upper=1.5): - e = random.uniform(lower, upper) + e = np.random.uniform(lower, upper) return ImageEnhance.Brightness(img).enhance(e) def random_contrast(img, lower=0.5, upper=1.5): - e = random.uniform(lower, upper) + e = np.random.uniform(lower, upper) return ImageEnhance.Contrast(img).enhance(e) def random_color(img, lower=0.5, upper=1.5): - e = random.uniform(lower, upper) + e = np.random.uniform(lower, upper) return ImageEnhance.Color(img).enhance(e) ops = [random_brightness, random_contrast, random_color] - random.shuffle(ops) + np.random.shuffle(ops) img = ops[0](img) img = ops[1](img) @@ -110,7 +111,7 @@ def process_image(sample, mode, color_jitter, rotate): if mode == 'train': if color_jitter: img = distort_color(img) - if random.randint(0, 1) == 1: + if np.random.randint(0, 1) == 1: img = img.transpose(Image.FLIP_LEFT_RIGHT) if img.mode != 'RGB': @@ -135,7 +136,7 @@ def _reader_creator(file_list, with open(file_list) as flist: lines = [line.strip() for line in flist] if shuffle: - random.shuffle(lines) + np.random.shuffle(lines) for line in lines: if mode == 'train' or mode == 'val': img_path, label = line.split() diff --git a/fluid/image_classification/train.py b/fluid/image_classification/train.py index aed5802c..a2ec0e25 100644 --- a/fluid/image_classification/train.py +++ b/fluid/image_classification/train.py @@ -172,6 +172,7 @@ def train(args): # but it is time consuming. For faster speed, need another dataset. import random random.seed(0) + np.random.seed(0) train_reader = paddle.batch( flowers.train(use_xmap=False), batch_size=train_batch_size) test_reader = paddle.batch( diff --git a/fluid/object_detection/image_util.py b/fluid/object_detection/image_util.py index 63efeea9..b89610ac 100644 --- a/fluid/object_detection/image_util.py +++ b/fluid/object_detection/image_util.py @@ -36,8 +36,8 @@ def bbox_area(src_bbox): def generate_sample(sampler): - scale = random.uniform(sampler.min_scale, sampler.max_scale) - aspect_ratio = random.uniform(sampler.min_aspect_ratio, + scale = np.random.uniform(sampler.min_scale, sampler.max_scale) + aspect_ratio = np.random.uniform(sampler.min_aspect_ratio, sampler.max_aspect_ratio) aspect_ratio = max(aspect_ratio, (scale**2.0)) aspect_ratio = min(aspect_ratio, 1 / (scale**2.0)) @@ -46,8 +46,8 @@ def generate_sample(sampler): bbox_height = scale / (aspect_ratio**0.5) xmin_bound = 1 - bbox_width ymin_bound = 1 - bbox_height - xmin = random.uniform(0, xmin_bound) - ymin = random.uniform(0, ymin_bound) + xmin = np.random.uniform(0, xmin_bound) + ymin = np.random.uniform(0, ymin_bound) xmax = xmin + bbox_width ymax = ymin + bbox_height sampled_bbox = bbox(xmin, ymin, xmax, ymax) @@ -167,36 +167,36 @@ def crop_image(img, bbox_labels, sample_bbox, image_width, image_height): def random_brightness(img, settings): - prob = random.uniform(0, 1) + prob = np.random.uniform(0, 1) if prob < settings._brightness_prob: - delta = random.uniform(-settings._brightness_delta, + delta = np.random.uniform(-settings._brightness_delta, settings._brightness_delta) + 1 img = ImageEnhance.Brightness(img).enhance(delta) return img def random_contrast(img, settings): - prob = random.uniform(0, 1) + prob = np.random.uniform(0, 1) if prob < settings._contrast_prob: - delta = random.uniform(-settings._contrast_delta, + delta = np.random.uniform(-settings._contrast_delta, settings._contrast_delta) + 1 img = ImageEnhance.Contrast(img).enhance(delta) return img def random_saturation(img, settings): - prob = random.uniform(0, 1) + prob = np.random.uniform(0, 1) if prob < settings._saturation_prob: - delta = random.uniform(-settings._saturation_delta, + delta = np.random.uniform(-settings._saturation_delta, settings._saturation_delta) + 1 img = ImageEnhance.Color(img).enhance(delta) return img def random_hue(img, settings): - prob = random.uniform(0, 1) + prob = np.random.uniform(0, 1) if prob < settings._hue_prob: - delta = random.uniform(-settings._hue_delta, settings._hue_delta) + delta = np.random.uniform(-settings._hue_delta, settings._hue_delta) img_hsv = np.array(img.convert('HSV')) img_hsv[:, :, 0] = img_hsv[:, :, 0] + delta img = Image.fromarray(img_hsv, mode='HSV').convert('RGB') @@ -204,30 +204,30 @@ def random_hue(img, settings): def distort_image(img, settings): - prob = random.uniform(0, 1) + prob = np.random.uniform(0, 1) # Apply different distort order if prob > 0.5: - img = random_brightness(img, settings) - img = random_contrast(img, settings) - img = random_saturation(img, settings) - img = random_hue(img, settings) + img = np.random_brightness(img, settings) + img = np.random_contrast(img, settings) + img = np.random_saturation(img, settings) + img = np.random_hue(img, settings) else: - img = random_brightness(img, settings) - img = random_saturation(img, settings) - img = random_hue(img, settings) - img = random_contrast(img, settings) + img = np.random_brightness(img, settings) + img = np.random_saturation(img, settings) + img = np.random_hue(img, settings) + img = np.random_contrast(img, settings) return img def expand_image(img, bbox_labels, img_width, img_height, settings): - prob = random.uniform(0, 1) + prob = np.random.uniform(0, 1) if prob < settings._expand_prob: if settings._expand_max_ratio - 1 >= 0.01: - expand_ratio = random.uniform(1, settings._expand_max_ratio) + expand_ratio = np.random.uniform(1, settings._expand_max_ratio) height = int(img_height * expand_ratio) width = int(img_width * expand_ratio) - h_off = math.floor(random.uniform(0, height - img_height)) - w_off = math.floor(random.uniform(0, width - img_width)) + h_off = math.floor(np.random.uniform(0, height - img_height)) + w_off = math.floor(np.random.uniform(0, width - img_width)) expand_bbox = bbox(-w_off / img_width, -h_off / img_height, (width - w_off) / img_width, (height - h_off) / img_height) diff --git a/fluid/object_detection/reader.py b/fluid/object_detection/reader.py index da0aa788..82dad83f 100644 --- a/fluid/object_detection/reader.py +++ b/fluid/object_detection/reader.py @@ -14,7 +14,6 @@ import image_util from paddle.utils.image_util import * -import random from PIL import Image from PIL import ImageDraw import numpy as np @@ -140,7 +139,7 @@ def preprocess(img, bbox_labels, mode, settings): img = np.array(img) if len(sampled_bbox) > 0: - idx = int(random.uniform(0, len(sampled_bbox))) + idx = int(np.random.uniform(0, len(sampled_bbox))) img, sampled_labels = image_util.crop_image( img, bbox_labels, sampled_bbox[idx], img_width, img_height) @@ -149,7 +148,7 @@ def preprocess(img, bbox_labels, mode, settings): img = np.array(img) if mode == 'train': - mirror = int(random.uniform(0, 2)) + mirror = int(np.random.uniform(0, 2)) if mirror == 1: img = img[:, ::-1, :] for i in six.moves.xrange(len(sampled_labels)): @@ -185,7 +184,7 @@ def coco(settings, file_list, mode, shuffle): def reader(): if mode == 'train' and shuffle: - random.shuffle(images) + np.random.shuffle(images) for image in images: image_name = image['file_name'] image_path = os.path.join(settings.data_dir, image_name) @@ -240,7 +239,7 @@ def pascalvoc(settings, file_list, mode, shuffle): def reader(): if mode == 'train' and shuffle: - random.shuffle(images) + np.random.shuffle(images) for image in images: image_path, label_path = image.split() image_path = os.path.join(settings.data_dir, image_path) -- GitLab