-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprediction.py
102 lines (83 loc) · 3.3 KB
/
prediction.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
from __future__ import absolute_import
from __future__ import print_function
import os
import glob
import random
import numpy as np
from keras import optimizers
from keras.layers import LSTM
from keras.models import Sequential, Model
from keras.applications.vgg16 import VGG16
from keras.layers.wrappers import TimeDistributed
from keras.applications.mobilenet import MobileNet
from keras.layers import Conv2D, MaxPooling2D, Dropout, Flatten, Dense, GlobalAveragePooling2D
from keras.layers import Input
from keras.layers.core import Activation, Flatten, Reshape
from keras.layers.convolutional import Convolution2D, MaxPooling2D, UpSampling2D
from keras.layers.normalization import BatchNormalization
from keras.utils import np_utils
from keras.applications import imagenet_utils
# In[3]:
height = 120
width = 240
path = '../datasets/'
allfiles = glob.glob(path+'sensory/tile/*.npy')
sali = glob.glob(path+'content/saliencyImages/*.npy')
motion = glob.glob(path+'content/motionImages/*.npy')
prob = glob.glob(path+'sensory/tileProb/*.npy')
# In[4]:
def myGenerator():
while True:
index_list = random.sample(range(1, 30000), 3)
alldata_x = []
alldata_y = []
for i in index_list:
f = allfiles[i]
s = f.split('_')
saliFile = '../datasets/content/saliencyImages/'+s[0][25:]+'_saliency_'+s[2].split('.')[0]+'.npy'
motionFile = '../datasets/content/motionImages/'+s[0][25:]+'_motion_'+s[2].split('.')[0]+'.npy'
probFile = '../datasets/sensory/tileProb/'+s[0][25:]+'_user'+s[1][4:]+'_'+s[2].split('.')[0]+'.npy'
a = np.load(f)
b = np.load(saliFile)
c = np.load(motionFile)
d = [a, b, c]
alldata_x.append(d)
alldata_y.append(np.load(probFile))
alldata_x = np.array(alldata_x)
alldata_x = np.rollaxis(alldata_x, 1, 5)
#alldata_x = alldata_x.reshape((32, 30, height, width, 3))
#alldata_x = np.swapaxes(alldata_x, 1, 4)
alldata_y = np.array(alldata_y)
yield alldata_x, alldata_y
# x = myGenerator()
# xtrain, ytrain = next(x)
# print('xtrain shape:',xtrain.shape)
# print('ytrain shape:',ytrain.shape)
# load the numpy arrays from saliency, motion maps and sensor data
sali = glob.glob(path+'content/saliencyImages/*.npy')
motion = glob.glob(path+'content/motionImages/*.npy')
sensory = glob.glob(path+'sensory/tile/*.npy')
# In[6]:
input_shape=(30, height, width, 3)
def mySegNet(input_shape):
base_model = MobileNet(input_shape=(224,224,3), include_top=False)
x = base_model.output
x = GlobalAveragePooling2D()(x)
cnn_model = Model(inputs=base_model.input, outputs=x)
model = Sequential();
model.add(TimeDistributed(cnn_model, input_shape=input_shape))
model.add(TimeDistributed(Flatten()))
model.add(LSTM(200, return_sequences=True))
model.compile(optimizer='adam', loss='mean_squared_error')
#print(model.summary())
return model
input_shape=(30, height, width, 3)
model = mySegNet(input_shape)
model.load_weights('model_weights2.h5')
xtest =
model.predict(xtest, batch_size=3)
#model.fit_generator(generator=myGenerator(),
# use_multiprocessing=True,
# steps_per_epoch=300, epochs=100)
#model.save('model3.h5')
#model.save_weights('model_weights3.h5')