-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscene_input.py
More file actions
103 lines (93 loc) · 3.61 KB
/
scene_input.py
File metadata and controls
103 lines (93 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# -*- coding: UTF-8 -*-
import numpy as np
import cv2
import logging
import PIL.Image as Image
import json
import os
class scene_data_fn(object):
def __init__(self, image_path, label_path):
# get all the image name and its label
self.data_dict = {}
with open(label_path, 'r') as f:
label_list = json.load(f)
for image in label_list:
self.data_dict[image['image_id']] = int(image['label_id'])
self.start = 0
self.end = 0
self.Length = len(self.data_dict)
self.img_name = list(self.data_dict.keys())
self.image_path = image_path
def img_resize(self, imgpath, img_size):
img = Image.open(imgpath)
width = img.width
height = img.height
if (width > height):
scale = float(img_size) / float(width)
img = np.array(cv2.resize(np.array(img), (
img_size, int(height * scale)))).astype(np.float32)
else:
scale = float(img_size) / float(height)
img = np.array(cv2.resize(np.array(img), (
int(width * scale), img_size))).astype(np.float32)
# crop the proper size and scale to [-1, 1]
width = img.shape[1]
height = img.shape[0]
padx = (img_size-height) //2
pady = (img_size-width) //2
img = (np.array(img).astype(np.float32))*1.0/255 - 0.5
res = np.array(cv2.copyMakeBorder(img, padx, img_size-height-padx, pady, img_size-width-pady, cv2.BORDER_CONSTANT,
value = [0,0,0])).astype(np.float32)
return res
def next_batch(self, batch_size, img_size=32):
# fetch a batch of images
self.start = self.end
if self.start >= self.Length:
self.start = 0
img_data = []
img_label = []
index = self.start
while len(img_data) < batch_size:
if index >= self.Length:
index = 0
img_data.append(self.img_resize(os.path.join(self.image_path, self.img_name[index]), img_size))
img_label.append(self.data_dict[self.img_name[index]])
index += 1
self.end = index
return np.array(img_data), np.array(img_label)
def img_resize(imgpath, img_size):
img = Image.open(imgpath)
if (img.width > img.height):
scale = float(img_size) / float(img.height)
img = np.array(cv2.resize(np.array(img), (
int(img.width * scale + 1), img_size))).astype(np.float32)
else:
scale = float(img_size) / float(img.width)
img = np.array(cv2.resize(np.array(img), (
img_size, int(img.height * scale + 1)))).astype(np.float32)
img = (img[
(img.shape[0] - img_size) // 2:
(img.shape[0] - img_size) // 2 + img_size,
(img.shape[1] - img_size) // 2:
(img.shape[1] - img_size) // 2 + img_size,
:])*1.0/255-0.5
return img
def train_log(filename='logfile'):
# create logger
logger_name = "filename"
logger = logging.getLogger(logger_name)
logger.setLevel(logging.DEBUG)
# create file handler
log_path = './' + filename + '.log'
fh = logging.FileHandler(log_path)
ch = logging.StreamHandler()
# create formatter
fmt = "%(asctime)-15s %(levelname)s %(filename)s %(lineno)d %(process)d %(message)s"
datefmt = "%a %d %b %Y %H:%M:%S"
formatter = logging.Formatter(fmt, datefmt)
# add handler and formatter to logger
fh.setFormatter(formatter)
ch.setFormatter(formatter)
logger.addHandler(fh)
logger.addHandler(ch)
return logger