-
Notifications
You must be signed in to change notification settings - Fork 12
/
Model.py
254 lines (195 loc) · 9.45 KB
/
Model.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# -*- coding: utf-8 -*-
"""
Created on Sat Nov 3 14:31:33 2018
@author: lenovo
"""
from keras.layers.normalization import BatchNormalization
from keras.preprocessing.image import load_img,img_to_array
from sklearn.metrics import mean_squared_error
from keras.initializers import RandomNormal
from keras.applications.vgg16 import VGG16
from keras.optimizers import SGD
from keras.models import Model,Sequential
from keras.layers import *
from keras import backend as K
from keras.models import model_from_json
from matplotlib import cm as CM
import matplotlib.pyplot as plt
import tensorflow as tf
from tqdm import tqdm
import scipy.io as io
from PIL import Image
import PIL
import h5py
import os
import glob
import cv2
import random
import math
import sys
import numpy as np
K.clear_session()
root = 'data'
part_A_train = os.path.join(root,'part_A_final/train_data','images')
part_A_test = os.path.join(root,'part_A_final/test_data','images')
part_B_train = os.path.join(root,'part_B_final/train_data','images')
part_B_test = os.path.join(root,'part_B_final/test_data','images')
temp = 'test_images'
path_sets = [part_A_train]
img_paths = []
for path in path_sets:
for img_path in glob.glob(os.path.join(path, '*.jpg')):
img_paths.append(str(img_path))
print("Total images : ",len(img_paths))
def create_img(path):
#Function to load,normalize and return image
im = Image.open(path).convert('RGB')
im = np.array(im)
im = im/255.0
im[:,:,0]=(im[:,:,0]-0.485)/0.229
im[:,:,1]=(im[:,:,1]-0.456)/0.224
im[:,:,2]=(im[:,:,2]-0.406)/0.225
#print(im.shape)
#im = np.expand_dims(im,axis = 0)
return im
def get_input(path):
path = path[0]
img = create_img(path)
return(img)
def get_output(path):
#import target
#resize target
gt_file = h5py.File(path,'r')
target = np.asarray(gt_file['density'])
img = cv2.resize(target,(int(target.shape[1]/8),int(target.shape[0]/8)),interpolation = cv2.INTER_CUBIC)*64
img = np.expand_dims(img,axis = 3)
#print(img.shape)
return img
def preprocess_input(image,target):
#crop image
#crop target
#resize target
crop_size = (int(image.shape[0]/2),int(image.shape[1]/2))
if random.randint(0,9)<= -1:
dx = int(random.randint(0,1)*image.shape[0]*1./2)
dy = int(random.randint(0,1)*image.shape[1]*1./2)
else:
dx = int(random.random()*image.shape[0]*1./2)
dy = int(random.random()*image.shape[1]*1./2)
#print(crop_size , dx , dy)
img = image[dx : crop_size[0]+dx , dy:crop_size[1]+dy]
target_aug = target[dx:crop_size[0]+dx,dy:crop_size[1]+dy]
#print(img.shape)
return(img,target_aug)
#Image data generator
def image_generator(files, batch_size = 64):
while True:
input_path = np.random.choice(a = files, size = batch_size)
batch_input = []
batch_output = []
#for input_path in batch_paths:
inputt = get_input(input_path )
output = get_output(input_path[0].replace('.jpg','.h5').replace('images','ground') )
batch_input += [inputt]
batch_output += [output]
batch_x = np.array( batch_input )
batch_y = np.array( batch_output )
yield( batch_x, batch_y )
def save_mod(model , str1 , str2):
model.save_weights(str1)
model_json = model.to_json()
with open(str2, "w") as json_file:
json_file.write(model_json)
def init_weights_vgg(model):
#vgg = VGG16(weights='imagenet', include_top=False)
json_file = open('models/VGG_16.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
loaded_model.load_weights("weights/VGG_16.h5")
vgg = loaded_model
vgg_weights=[]
for layer in vgg.layers:
if('conv' in layer.name):
vgg_weights.append(layer.get_weights())
offset=0
i=0
while(i<10):
if('conv' in model.layers[i+offset].name):
model.layers[i+offset].set_weights(vgg_weights[i])
i=i+1
#print('h')
else:
offset=offset+1
return (model)
def euclidean_distance_loss(y_true, y_pred):
# Euclidean distance as a measure of loss (Loss function)
return K.sqrt(K.sum(K.square(y_pred - y_true), axis=-1))
# Neural network model : VGG + Conv
def CrowdNet():
#Variable Input Size
rows = None
cols = None
#Batch Normalisation option
batch_norm = 0
kernel = (3, 3)
init = RandomNormal(stddev=0.01)
model = Sequential()
#custom VGG:
if(batch_norm):
model.add(Conv2D(64, kernel_size = kernel, input_shape = (rows,cols,3),activation = 'relu', padding='same'))
model.add(BatchNormalization())
model.add(Conv2D(64, kernel_size = kernel,activation = 'relu', padding='same'))
model.add(BatchNormalization())
model.add(MaxPooling2D(strides=2))
model.add(Conv2D(128,kernel_size = kernel, activation = 'relu', padding='same'))
model.add(BatchNormalization())
model.add(Conv2D(128,kernel_size = kernel, activation = 'relu', padding='same'))
model.add(BatchNormalization())
model.add(MaxPooling2D(strides=2))
model.add(Conv2D(256,kernel_size = kernel, activation = 'relu', padding='same'))
model.add(BatchNormalization())
model.add(Conv2D(256,kernel_size = kernel, activation = 'relu', padding='same'))
model.add(BatchNormalization())
model.add(Conv2D(256,kernel_size = kernel, activation = 'relu', padding='same'))
model.add(BatchNormalization())
model.add(MaxPooling2D(strides=2))
model.add(Conv2D(512, kernel_size = kernel,activation = 'relu', padding='same'))
model.add(BatchNormalization())
model.add(Conv2D(512, kernel_size = kernel,activation = 'relu', padding='same'))
model.add(BatchNormalization())
model.add(Conv2D(512, kernel_size = kernel,activation = 'relu', padding='same'))
model.add(BatchNormalization())
else:
model.add(Conv2D(64, kernel_size = kernel,activation = 'relu', padding='same',input_shape = (rows, cols, 3), kernel_initializer = init))
model.add(Conv2D(64, kernel_size = kernel,activation = 'relu', padding='same', kernel_initializer = init))
model.add(MaxPooling2D(strides=2))
model.add(Conv2D(128,kernel_size = kernel, activation = 'relu', padding='same', kernel_initializer = init))
model.add(Conv2D(128,kernel_size = kernel, activation = 'relu', padding='same', kernel_initializer = init))
model.add(MaxPooling2D(strides=2))
model.add(Conv2D(256,kernel_size = kernel, activation = 'relu', padding='same', kernel_initializer = init))
model.add(Conv2D(256,kernel_size = kernel, activation = 'relu', padding='same', kernel_initializer = init))
model.add(Conv2D(256,kernel_size = kernel, activation = 'relu', padding='same', kernel_initializer = init))
model.add(MaxPooling2D(strides=2))
model.add(Conv2D(512, kernel_size = kernel,activation = 'relu', padding='same', kernel_initializer = init))
model.add(Conv2D(512, kernel_size = kernel,activation = 'relu', padding='same', kernel_initializer = init))
model.add(Conv2D(512, kernel_size = kernel,activation = 'relu', padding='same', kernel_initializer = init))
#Conv2D
model.add(Conv2D(512, (3, 3), activation='relu', dilation_rate = 2, kernel_initializer = init, padding = 'same'))
model.add(Conv2D(512, (3, 3), activation='relu', dilation_rate = 2, kernel_initializer = init, padding = 'same'))
model.add(Conv2D(512, (3, 3), activation='relu', dilation_rate = 2, kernel_initializer = init, padding = 'same'))
model.add(Conv2D(256, (3, 3), activation='relu', dilation_rate = 2, kernel_initializer = init, padding = 'same'))
model.add(Conv2D(128, (3, 3), activation='relu', dilation_rate = 2, kernel_initializer = init, padding = 'same'))
model.add(Conv2D(64, (3, 3), activation='relu', dilation_rate = 2, kernel_initializer = init, padding = 'same'))
model.add(Conv2D(1, (1, 1), activation='relu', dilation_rate = 1, kernel_initializer = init, padding = 'same'))
sgd = SGD(lr = 1e-7, decay = (5*1e-4), momentum = 0.95)
model.compile(optimizer=sgd, loss=euclidean_distance_loss, metrics=['mse'])
model = init_weights_vgg(model)
return model
model = CrowdNet()
model.summary()
train_gen = image_generator(img_paths,1)
sgd = SGD(lr = 1e-7, decay = (5*1e-4), momentum = 0.95)
model.compile(optimizer=sgd, loss=euclidean_distance_loss, metrics=['mse'])
model.fit_generator(train_gen,epochs=1,steps_per_epoch= 700 , verbose=1)#训练网络
save_mod(model,"weights/model_A_weights.h5","models/Model.json")