code2
code2
import errno
import os
import numpy as np
# from PIL import Image
import torch
import torch.nn as nn
EPS = 1e-7
# def load_imageid(folder):
# images = load_folder(folder, 'jpg')
# img_ids = set()
# for img in images:
# img_id = int(img.split('/')[-1].split('.')[0].split('_')[-1])
# img_ids.add(img_id)
# return img_ids
# def pil_loader(path):
# with open(path, 'rb') as f:
# with Image.open(f) as img:
# return img.convert('RGB')
def weights_init(m):
"""custom weights initialization."""
cname = m.__class__
if cname == nn.Linear or cname == nn.Conv2d or cname == nn.ConvTranspose2d:
m.weight.data.normal_(0.0, 0.02)
elif cname == nn.BatchNorm2d:
m.weight.data.normal_(1.0, 0.02)
m.bias.data.fill_(0)
else:
print('%s is not initialized.' % cname)
def create_dir(path):
if not os.path.exists(path):
try:
os.makedirs(path)
except OSError as exc:
if exc.errno != errno.EEXIST:
raise
class Logger(object):
def __init__(self, output_name):
dirname = os.path.dirname(output_name)
if not os.path.exists(dirname):
os.mkdir(dirname)