|
| 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 | + |
0 commit comments