-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_mask_1.py
174 lines (154 loc) · 5.89 KB
/
generate_mask_1.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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import os
import sys
import random
import math
import numpy as np
import skimage.io
import matplotlib
import matplotlib.pyplot as plt
import h5py
import coco
import utils
import model as modellib
import visualize
# %matplotlib inline
# Root directory of the project
ROOT_DIR = os.getcwd()
# Directory to save logs and trained model
MODEL_DIR = os.path.join(ROOT_DIR, "logs")
# Local path to trained weights file
COCO_MODEL_PATH = os.path.join(ROOT_DIR, "mask_rcnn_coco.h5")
# Download COCO trained weights from Releases if needed
if not os.path.exists(COCO_MODEL_PATH):
utils.download_trained_weights(COCO_MODEL_PATH)
# Directory of images to run detection on
IMAGE_DIR = os.path.join(ROOT_DIR, "images")
class InferenceConfig(coco.CocoConfig):
GPU_COUNT = 1
IMAGES_PER_GPU = 20
IMAGE_MAX_DIM = 512
IMAGE_MIN_DIM = 512
BATCH_SIZE = 20
config = InferenceConfig()
config.display()
model = modellib.MaskRCNN(mode="inference", model_dir=MODEL_DIR, config=config)
model.load_weights(COCO_MODEL_PATH, by_name=True)
#######-----for all video image
# video_image_path = '/hdfs/qiuyurui/DATA/MSR_VTT_2017_train_image_3/' #youtube_image/'
# video_all = {}
# all_image_path = []
# name_with_path = []
# for video_forder in os.listdir(video_image_path):
# video_name = video_forder
# hdf5_p = '/hdfs/qiuyurui/DATA/hdf5_msr/'
# hdf5_path = os.path.join(hdf5_p, video_forder)
# if not os.path.exists(hdf5_path):
# os.mkdir(hdf5_path)
# subdir = os.path.join(video_image_path, video_name)
# image_all = {}
# for image_file in os.listdir(subdir):
# image_name = image_file[:-4]
# image_path = os.path.join(subdir, image_file)
# all_image_path.append(image_path)
# image_all[image_name] = image_path
# name_with_path.append({'video':video_name,
# 'image_name':image_name,'image_path':image_path})
# video_all[video_name] = image_all
# # print(image_name)
# # for
# # print(all_image_path[100])
# # print(video_all['vid1']['0001'])
# print(len(name_with_path))
#######-----for space not enough
video_image_path = '/data/qiuyurui/MSR_VTT_2017_train_image_3/' #youtube_image/'
# video_all = {}
# all_image_path = []
name_with_path = []
for video_forder in os.listdir(video_image_path):
video_name = video_forder
hdf5_p = '/backup/qiuyurui/hdf5_msr/'
subdir = os.path.join(video_image_path, video_name)
# image_all = {}
for image_file in os.listdir(subdir):
image_name = image_file[:-4]
image_path = os.path.join(subdir, image_file)
# all_image_path.append(image_path)
if not os.path.isfile('/hdfs/qiuyurui/DATA/hdf5_msr/{}/{}.h5'.format(video_forder,image_name)):
# image_all[image_name] = image_path
name_with_path.append({'video':video_name,
'image_name':image_name,'image_path':image_path})
hdf5_path = os.path.join(hdf5_p, video_name)
if not os.path.exists(hdf5_path):
os.mkdir(hdf5_path)
# video_all[video_name] = image_all
print(len(name_with_path))
def load_image_for_batch(image_path, config):
image = skimage.io.imread(image_path)
# shape = image.shape
image, _, _, _ = utils.resize_image(
image,
min_dim=config.IMAGE_MIN_DIM,
max_dim=config.IMAGE_MAX_DIM,
padding=config.IMAGE_PADDING)
return image
#def generate_mask_batch(batch_size = config.BATCH_SIZE, config, name_with_path):
number_image = len(name_with_path)
last_number = number_image
# for n in rang(batch_num):
n = 0
batch_size = config.BATCH_SIZE
batch_num = number_image/batch_size
images_batchs = np.zeros((batch_size, config.IMAGE_MAX_DIM, config.IMAGE_MAX_DIM, 3))
m = 1
# last_number = last_number - batch_size
while last_number >= batch_size:
vid_image = []
r = []
for i in range(batch_size):
image_path = name_with_path[n]['image_path']
video = name_with_path[n]['video']
image_name = name_with_path[n]['image_name']
resize_image = load_image_for_batch(image_path, config)
images_batchs[i] = resize_image.copy()
vid_image.append({'video':video, 'image_name': image_name})
n = n+1
last_number = last_number - batch_size
print(len(images_batchs))
r = model.detect(images_batchs)
for i in range(batch_size):
f = h5py.File('/backup/qiuyurui/hdf5_msr/{}/{}.h5'.format(vid_image[i]['video'],vid_image[i]['image_name']),'w')
f['features'] = r[i]['features']
f['masks'] = r[i]['masks']
f['class_ids'] = r[i]['class_ids']
f['rois'] = r[i]['rois']
f.close()
print(m,' batch number / {}'.format(batch_num), video)
m = m+1
# break
if last_number > 0 and last_number < batch_size:
config.BATCH_SIZE = last_number
config.IMAGES_PER_GPU = last_number
batch_size = last_number
images_batchs = np.zeros((batch_size, config.IMAGE_MAX_DIM, config.IMAGE_MAX_DIM, 3))
vid_image = []
r = []
for i in range(batch_size):
image_path = name_with_path[n]['image_path']
video = name_with_path[n]['video']
image_name = name_with_path[n]['image_name']
resize_image = load_image_for_batch(image_path, config)
images_batchs[i] = resize_image.copy()
vid_image.append({'video':video, 'image_name': image_name})
n = n+1
model = modellib.MaskRCNN(mode="inference", model_dir=MODEL_DIR, config=config)
model.load_weights(COCO_MODEL_PATH, by_name=True)
r = model.detect(images_batchs)
for i in range(batch_size):
f = h5py.File('/backup/qiuyurui/hdf5_msr/{}/{}.h5'.format(vid_image[i]['video'],vid_image[i]['image_name']),'w')
f['features'] = r[i]['features']
f['masks'] = r[i]['masks']
f['class_ids'] = r[i]['class_ids']
f['rois'] = r[i]['rois']
f.close()
print(m,' batch number / {}'.format(batch_num), video)
# print(len(r))