-
Notifications
You must be signed in to change notification settings - Fork 28
/
data_loader.py
77 lines (61 loc) · 2.47 KB
/
data_loader.py
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
#-*- coding: utf-8 -*-
import numpy as np
import os, h5py, sys, argparse
import codecs, json
import skimage
from skimage import io
import cv2
def get_data(input_json, input_img_h5, input_ques_h5, img_norm):
dataset = {}
train_data = {}
# load json file
print('loading json file...')
with open(input_json) as data_file:
data = json.load(data_file)
for key in data.keys():
dataset[key] = data[key]
# load image feature
print('loading image feature...')
with h5py.File(input_img_h5,'r') as hf:
tem = hf.get('images_train')
img_feature = np.array(tem)
# load h5 file
print('loading h5 file...')
with h5py.File(input_ques_h5,'r') as hf:
tem = hf.get('ques_train')
train_data['question'] = np.array(tem)
tem = hf.get('ques_length_train')
train_data['length_q'] = np.array(tem)
tem = hf.get('img_pos_train')
train_data['img_list'] = np.array(tem)-1
print('Normalizing image feature')
if img_norm:
tem = np.sqrt(np.sum(np.multiply(img_feature, img_feature), axis=1))
img_feature = np.divide(img_feature, np.transpose(np.tile(tem,(4096,1))))
return dataset, img_feature, train_data
def crop_image(x, target_height=227, target_width=227, as_float=True):
image = skimage.io.imread(x)
if as_float:
image = skimage.img_as_float(image).astype(np.float32)
if len(image.shape) == 2:
image = np.tile(image[:,:,None], 3)
elif len(image.shape) == 4:
image = image[:,:,:,0]
height, width, rgb = image.shape
if width == height:
resized_image = cv2.resize(image, (target_height,target_width))
elif height < width:
resized_image = cv2.resize(image, (int(width * float(target_height)/height), target_width))
cropping_length = int((resized_image.shape[1] - target_height) / 2)
resized_image = resized_image[:,cropping_length:resized_image.shape[1] - cropping_length]
else:
resized_image = cv2.resize(image, (target_height, int(height * float(target_width) / width)))
cropping_length = int((resized_image.shape[0] - target_width) / 2)
resized_image = resized_image[cropping_length:resized_image.shape[0] - cropping_length,:]
return cv2.resize(resized_image, (target_height, target_width))
def read_image(path):
img = crop_image(path, target_height=224, target_width=224)
if img.shape[2] == 4:
img = img[:,:,:3]
img = img[None, ...]
return img