Skip to content

Commit a9594fe

Browse files
committed
added ResNet block and inception module
2 parents a9c0240 + a96d4e4 commit a9594fe

27 files changed

+1539
-8762
lines changed

.ipynb_checkpoints/Model2-checkpoint.ipynb

Lines changed: 348 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 19,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"import os\n",
10+
"import cv2\n",
11+
"import glob\n",
12+
"import random\n",
13+
"import numpy as np"
14+
]
15+
},
16+
{
17+
"cell_type": "code",
18+
"execution_count": 3,
19+
"metadata": {},
20+
"outputs": [],
21+
"source": [
22+
"videos = glob.glob('../dataset/first-set/*.mp4')\n",
23+
"mos = np.loadtxt('../dataset/first-set/mos.txt')"
24+
]
25+
},
26+
{
27+
"cell_type": "code",
28+
"execution_count": 31,
29+
"metadata": {},
30+
"outputs": [],
31+
"source": [
32+
"def scaleDownVideos(videos, path):\n",
33+
" for video in videos:\n",
34+
" os.system('ffmpeg -i '+video+' -qp 1 -vf scale=120:68 '+path+'/'+video.split('/')[-1])\n",
35+
"\n",
36+
"def extractImages(videos, path):\n",
37+
" for video in videos:\n",
38+
" os.system('ffmpeg -i '+video+' -vf fps=10 '+path+'/'+video.split('/')[-1][:-4]+'_%d.png')"
39+
]
40+
},
41+
{
42+
"cell_type": "code",
43+
"execution_count": 32,
44+
"metadata": {},
45+
"outputs": [],
46+
"source": [
47+
"#scaleDownVideos(videos, '../dataset/first-set/images')\n",
48+
"videos = glob.glob('../dataset/first-set/images/*.mp4')\n",
49+
"extractImages(videos, '../dataset/first-set/images')"
50+
]
51+
},
52+
{
53+
"cell_type": "code",
54+
"execution_count": 43,
55+
"metadata": {},
56+
"outputs": [],
57+
"source": [
58+
"def makeImagesNumpy(videos, mos, path):\n",
59+
" for video in videos:\n",
60+
" images = glob.glob(path+'/'+video.split('/')[-1][:-4]+'_*.png')\n",
61+
" images = images[:200]\n",
62+
" frames = []\n",
63+
" for image in images:\n",
64+
" image = cv2.imread(image)\n",
65+
" image = np.array(image)\n",
66+
" frames.append(image)\n",
67+
" frames = np.array(frames)\n",
68+
" np.save('../dataset/first-set/numpys/'+video.split('/')[-1][:-4]+'.npy', frames)\n",
69+
" mosIndex = int(video.split('/')[-1][:-4].split('_')[1])\n",
70+
" mosNumpy = np.full(200, mos[mosIndex-1])\n",
71+
" np.save('../dataset/first-set/numpys/'+video.split('/')[-1][:-4]+'_mos.npy', mosNumpy)\n",
72+
"makeImagesNumpy(videos, mos, '../dataset/first-set/images')"
73+
]
74+
},
75+
{
76+
"cell_type": "code",
77+
"execution_count": null,
78+
"metadata": {},
79+
"outputs": [],
80+
"source": []
81+
}
82+
],
83+
"metadata": {
84+
"kernelspec": {
85+
"display_name": "Python 3",
86+
"language": "python",
87+
"name": "python3"
88+
},
89+
"language_info": {
90+
"codemirror_mode": {
91+
"name": "ipython",
92+
"version": 3
93+
},
94+
"file_extension": ".py",
95+
"mimetype": "text/x-python",
96+
"name": "python",
97+
"nbconvert_exporter": "python",
98+
"pygments_lexer": "ipython3",
99+
"version": "3.5.2"
100+
}
101+
},
102+
"nbformat": 4,
103+
"nbformat_minor": 2
104+
}

Model2.ipynb

Lines changed: 405 additions & 0 deletions
Large diffs are not rendered by default.

Model2.py

Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
2+
# coding: utf-8
3+
4+
# In[11]:
5+
6+
7+
from __future__ import absolute_import
8+
from __future__ import print_function
9+
import os
10+
import glob
11+
import random
12+
import numpy as np
13+
from keras import optimizers
14+
from keras.layers import LSTM
15+
from keras.models import Sequential, Model
16+
from keras.applications.vgg16 import VGG16
17+
from keras.layers.wrappers import TimeDistributed
18+
from keras.applications.mobilenet import MobileNet
19+
from keras.layers import Conv2D, MaxPooling2D, Dropout, Flatten, Dense, GlobalAveragePooling2D
20+
from keras.layers import Input, InputLayer
21+
from keras.layers.core import Activation, Flatten, Reshape
22+
from keras.layers.convolutional import Convolution2D, MaxPooling2D, UpSampling2D
23+
from keras.layers.normalization import BatchNormalization
24+
from keras.utils import np_utils
25+
from keras.applications import imagenet_utils
26+
27+
28+
# In[3]:
29+
30+
31+
videoFiles = glob.glob('../dataset/first-set/numpys/*.npy')
32+
mosFiles = [i for i in videoFiles if 'mos' in i]
33+
videoFiles = [i for i in videoFiles if 'mos' not in i]
34+
35+
36+
# In[4]:
37+
38+
39+
def myGenerator():
40+
while True:
41+
index_list = random.sample(range(1, 80), 2)
42+
alldata_x = []
43+
alldata_y = []
44+
for i in index_list:
45+
f = videoFiles[i]
46+
s = f[:-4]+'_mos.npy'
47+
a = np.load(f)
48+
b = np.load(s)
49+
alldata_x.append(a)
50+
alldata_y.append(b[0])
51+
alldata_x = np.array(alldata_x)
52+
#alldata_x = np.rollaxis(alldata_x, 1, 5)
53+
#alldata_x = alldata_x.reshape((32, 30, height, width, 3))
54+
#alldata_x = np.swapaxes(alldata_x, 1, 4)
55+
alldata_y = np.array(alldata_y)
56+
yield alldata_x, alldata_y
57+
#x = myGenerator()
58+
#xtrain, ytrain = next(x)
59+
#print('xtrain shape:',xtrain.shape)
60+
#print('ytrain shape:',ytrain.shape)
61+
62+
# In[5]:
63+
64+
65+
height = 68
66+
width = 120
67+
input_shape=(200, height, width, 3)
68+
69+
70+
# In[12]:
71+
72+
73+
def mySegNet(input_shape):
74+
base_model = MobileNet(input_shape=(224,224,3), include_top=False)
75+
x = base_model.output
76+
x = GlobalAveragePooling2D()(x)
77+
cnn_model = Model(inputs=base_model.input, outputs=x)
78+
79+
model = Sequential();
80+
#model.add(InputLayer(input_shape=input_shape))
81+
model.add(TimeDistributed(cnn_model, input_shape=input_shape))
82+
model.add(TimeDistributed(Flatten()))
83+
#model.add(cnn_model)
84+
#model.add(Flatten())
85+
86+
model.add(LSTM(50, return_sequences=False))
87+
model.add(Dense(5, activation='softmax'))
88+
model.compile(optimizer='adam', loss='mean_squared_error')
89+
print(model.summary())
90+
return model
91+
#mySegNet(input_shape)
92+
93+
94+
# In[9]:
95+
96+
97+
model = mySegNet(input_shape)
98+
99+
model.fit_generator(generator=myGenerator(),
100+
use_multiprocessing=True,
101+
steps_per_epoch=3, epochs=10)
102+
model.save('model1.h5')
103+
model.save_weights('model_weights1.h5')
104+
105+
106+
# In[11]:
107+
108+
109+
input_shape=(30, height, width, 3)
110+
model = mySegNet(input_shape)
111+
model.load_weights('model_weights2.h5')
112+
totalTestSamples = len(allfiles)
113+
predictions = []
114+
ytrue = []
115+
for i in range(0, totalTestSamples, batchSize):
116+
x = myTestDataGenerator()
117+
xtest, ytest = next(x)
118+
ytrue.append(ytest)
119+
pred = model.predict(xtest, batch_size=batchSize)
120+
for p in pred:
121+
predictions.append(p)
122+
print('predictions shape: ', np.array(predictions).shape)
123+
124+
125+
# In[60]:
126+
127+
128+
tileFrames = []
129+
for sample in ytrue[:1]:
130+
for frames in sample:
131+
t = []
132+
for frame in frames:
133+
f = []
134+
for i, j in enumerate(frame):
135+
if j!=0:
136+
f.append(i+1)
137+
tileFrames.append(f)
138+
print(np.array(tileFrames).shape)
139+
140+
141+
# In[59]:
142+
143+
144+
pTileFrames = []
145+
for sample in predictions[:3]:
146+
for frames in sample:
147+
f = []
148+
for i, j in enumerate(frames):
149+
if j!=0:
150+
f.append(i+1)
151+
pTileFrames.append(f)
152+
print(np.array(pTileFrames).shape)
153+
154+
155+
# In[82]:
156+
157+
158+
from PIL import Image
159+
import numpy as np
160+
from matplotlib import pyplot as plt
161+
162+
breadth = 3840
163+
width = 1920
164+
tileSize = 192
165+
tilesInColumn = width / tileSize
166+
for i, tiles in enumerate(tileFrames):
167+
frame = np.zeros(width*breadth)
168+
print(tiles)
169+
for tileNo in tiles:
170+
tileRowNumber = int((tileNo - 1) / tilesInColumn)
171+
tileColumnNumber = (tileNo - 1) % tilesInColumn
172+
firstPixel = tileRowNumber * width * tileSize + tileColumnNumber * tileSize
173+
for rowPixel in range(0, tileSize):
174+
for columnPixel in range(0, tileSize):
175+
frame[int(firstPixel + rowPixel * breadth + columnPixel)] = 255
176+
frame = frame.reshape((width, breadth))
177+
plt.imshow(frame, interpolation='nearest')
178+
plt.show()
179+
break
180+
181+
182+
# In[83]:
183+
184+
185+
for i, tiles in enumerate(pTileFrames):
186+
frame = np.zeros(width*breadth)
187+
for tileNo in tiles:
188+
tileRowNumber = int((tileNo - 1) / tilesInColumn)
189+
tileColumnNumber = (tileNo - 1) % tilesInColumn
190+
firstPixel = tileRowNumber * width * tileSize + tileColumnNumber * tileSize
191+
for rowPixel in range(0, tileSize):
192+
for columnPixel in range(0, tileSize):
193+
frame[int(firstPixel + rowPixel * breadth + columnPixel)] = 255
194+
frame = frame.reshape((width, breadth))
195+
plt.imshow(frame, interpolation='nearest')
196+
plt.show()
197+
break
198+
199+
200+
# In[ ]:
201+
202+
203+
index = 28
204+
thresh = 0.5
205+
206+
temp = predictions[0][index]
207+
temp[temp > thresh] = 1
208+
temp[temp <= thresh] = 0
209+
210+
for i, j in enumerate(ytest[0][index]):
211+
if ytest[0][index][i] != temp[i]:
212+
print('Index: ', i, 'Value: ', ytest[0][index][i], temp[i])
213+
214+
215+
# In[ ]:
216+
217+
218+
print(ytest[0][index].shape)
219+

README.md

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)