boostcampaitech3 / level2-semantic-segmentation-level2-cv-17

[2022.04.25 ~ 2022.05.12] Recycle Trash Semantic Segmentation Competition - 부스트캠프 AI Tech 3기
4 stars 2 forks source link

[refactor] image_logging & [fix] transform.py #33

Closed Dongwoo-Im closed 2 years ago

Dongwoo-Im commented 2 years ago

What is this PR?

train.py image logging & transform (Normalize) refactor를 위한 PR입니다.

Changes

train.py

transform.py

utils.py

To reviewers

Dongwoo-Im commented 2 years ago

train_all.json + test.json : mean=[106.6728, 112.1372, 117.6358] std=[55.3728, 53.408, 54.3513] train_all.json + test.json + leak.json : mean=[107.719, 113.1121, 118.4997] std=[54.4267, 52.2639, 52.8573]

import cv2
import os
import json
import numpy as np

def normalize_img(data_path, json_path, mode):
    with open(json_path, 'r') as f:
        train_json = json.loads(f.read())
    images = train_json['images']
    image_paths = [os.path.join(data_path, image['file_name']) for image in images]

    mean, std = [0,0,0], [0,0,0]
    for image_path in image_paths:
        img = cv2.imread(image_path)
        if mode == 'torch': img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        img = img.astype(np.float32)
        if mode == 'torch': img /= 255.0
        for i in range(3):
            mean[i] += np.mean(img[:,:,i])
            std[i] += np.std(img[:,:,i])

    mean = np.array(mean) / len(image_paths)
    std = np.array(std) / len(image_paths)

    return mean.tolist(), std.tolist()

# leak_mean, leak_std = normalize_img(data_path='/opt/ml/input/data', json_path='/opt/ml/input/data/leak.json', mode='torch')