-
Notifications
You must be signed in to change notification settings - Fork 6
/
resnet_crowd.py
133 lines (102 loc) · 4.53 KB
/
resnet_crowd.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
# Estimating the crowd count and density map using shanghai dataset
import tensorflow as tf
from tensorflow import keras
from keras.preprocessing.image import ImageDataGenerator
from keras.applications import ResNet50
from keras.models import Model , Sequential
from keras import layers
from keras.layers import Conv2D,MaxPool2D,ReLU,Dense,AveragePooling2D,Flatten,Concatenate
from keras.activations import sigmoid
import cv2
from tensorflow import image
import h5py
import scipy.io as io
import PIL.Image as Image
import numpy as np
import os
from glob import glob
from matplotlib import pyplot as plt
from scipy.ndimage.filters import gaussian_filter
import scipy
import json
from matplotlib import cm as CM
from tqdm import tqdm
from numba import cuda
# from image import *
# %matplotlib inline
# input_layer = keras.Input((None,None,3))
def crowd(input_images):
base_model = ResNet50(input_tensor = input_images,input_shape=(224,224,3),weights='imagenet',include_top=False)
# for l in base_model.layers:
# print (l.name)
model = Model(inputs = base_model.input,outputs = base_model.get_layer("activation_22").output )
for layer in model.layers:
layer.trainable = False
# For producing heatmap of the input
model1 = Conv2D(1,(3,3),padding='same',activation='relu', name="heatmap_a")(model.output)
# heatmap = sigmoid(model1)
# heatmap = Flatten()(heatmap)
# heatmap = cv2.resize(heatmap,(224,224))
# For performing counting,violence prediction,denstiy classification
model2 = AveragePooling2D(pool_size = 2,strides = None,padding = 'same' )(model.output)
# For violence classification
# model21 = Flatten()(model2)
# model21 = Dense(units = 32,activation = 'relu',input_dim = 100352 )(model21)
# model21 = Dense(units = 2,activation = 'softmax',input_dim = 32 )(model21)
# For density classification into 5 classes
# model22 = Flatten()(model2)
# model22 = Dense(units = 32,activation = 'relu',input_dim = 100352 )(model22)
# model22 = Dense(units = 5,activation = 'softmax',input_dim = 32 )(model22)
# For counting number of people
model23 = Flatten()(model2)
model23 = Dense(units = 32,activation = 'relu', name="counting_a" )(model23)
model23 = Dense(units = 1, name="counting_b")(model23) #,activation = 'softmax'
# model23 = Flatten()(model23)
# count = model23
model_final = Model(inputs = base_model.input, outputs = [model1, model23])
# print (model_final.summary())
return model_final
# return model_final,heatmap,count
def calculate_loss(model,ground_truth_heatmap,ground_truth_count):
heatmap, count = model.output
# print(count)
# tf.summary.image('predicted', heatmap) # predicted image of the predicted image
# tf.summary.image('ground_truth', ground_truth_heatmap) # ground truth image
heatmap = tf.reshape(heatmap,[-1, 28*28]) # making all the output images flat
ground_truth_heatmap = tf.reshape(ground_truth_heatmap, [-1, 28*28])
# print (ground_truth_count.get_shape())
# print (count.get_shape())
loss_heatmap = tf.losses.mean_squared_error(
ground_truth_heatmap,
heatmap,
weights=1.0,
scope=None,
loss_collection=tf.GraphKeys.LOSSES,
reduction=tf.losses.Reduction.SUM_OVER_BATCH_SIZE
)
# loss_heatmap = tf.reduce_mean(loss_heatmap, axis=1)
loss_counting = tf.losses.mean_squared_error(
ground_truth_count,
count,
weights=1.0,
scope=None,
loss_collection=tf.GraphKeys.LOSSES,
reduction=tf.losses.Reduction.SUM_OVER_BATCH_SIZE
)
# loss_counting = tf.reduce_mean(loss_counting,axis=1)
loss = loss_heatmap + loss_counting
# print (loss_counting.get_shape())
# print (loss_heatmap.get_shape())
# print (loss.get_shape())
return loss,loss_heatmap,loss_counting
'''
def non_trainable(model,layer1_name,layer2_name):
model1 = Model(inputs = model.input,outputs = model.get_layer(layer1_name).output )
for layer in model1.layers:
layer.trainable =False
model2 = Model(inputs = layer2 ,outputs = model.output )
return Model(inputs = model1.input,outputs = model2.output)
model = crowd(x)
'''
# if __name__ == '__main__':
# heatmap,count = crowd()