Commit fe21b897 authored by Gihan Jayatilaka's avatar Gihan Jayatilaka

Upload

parent 76917197
This diff is collapsed.
#!/usr/bin/env python
# coding: utf-8
# In[14]:
# In[ ]:
'''
......@@ -12,22 +12,14 @@ gihanchanaka@gmail.com
'''
# In[1]:
# In[ ]:
DEBUG=False
PC=False
# In[2]:
FILE_NAME='./video/ran'
FILE_NAME_VIDEO=FILE_NAME+'.avi'
FILE_NAME_CSV=FILE_NAME+'.csv'
# In[3]:
# In[ ]:
import keras
......@@ -43,13 +35,33 @@ import os
import pandas as pd
# In[4]:
# In[ ]:
keras.callbacks.TensorBoard(log_dir='./logs/cnnModel04/', histogram_freq=0, batch_size=32, write_graph=True, write_grads=False, write_images=False, embeddings_freq=0, embeddings_layer_names=None, embeddings_metadata=None, embeddings_data=None,)
# In[5]:
# In[ ]:
FRAME_HEIGHT=100
FRAME_WIDTH=200
COLOR_CHANNELS=3
CELLS_PER_FRAME=25
INPUT_DIM=(FRAME_HEIGHT,FRAME_WIDTH)
OUTPUT_DIM=CELLS_PER_FRAME
EPOCHS=20
BATCH_SIZE=64
CUDA1=0
CUDA2=1
os.environ["CUDA_VISIBLE_DEVICES"]="{},{}".format(CUDA1,CUDA2)
# In[ ]:
def cnn(inptuDim,outputDim):
......@@ -69,40 +81,18 @@ def cnn(inptuDim,outputDim):
return model
# In[6]:
NO_FRAMES=10000
if PC: NO_FRAMES=100
FRAMES_TO_SKIP=0
'''NO_OF_FRAMES=int(sys.argv[1])
'''
FRAME_HEIGHT=108
FRAME_WIDTH=192
COLOR_CHANNELS=3
CELLS_PER_FRAME=4
EPOCHS=20
BATCH_SIZE=64
CUDA1=0
CUDA2=1
os.environ["CUDA_VISIBLE_DEVICES"]="{},{}".format(CUDA1,CUDA2)
# In[7]:
# In[ ]:
def makeX():
cap = cv2.VideoCapture(FILE_NAME_VIDEO)
def makeX(fileNameVideo,noFrames,skipFrames):
cap = cv2.VideoCapture(fileNameVideo)
if (cap.isOpened()== False):
print("Error opening video stream or file")
ret,frame=cap.read()
'''if (len(sys.argv)>1):
NO_OF_FRAMES=int(sys.argv[1])'''
X=np.zeros((NO_FRAMES,FRAME_HEIGHT,FRAME_WIDTH,COLOR_CHANNELS),dtype=np.float32)
X=np.zeros((noFrames,FRAME_HEIGHT,FRAME_WIDTH,COLOR_CHANNELS),dtype=np.float32)
for f in range(NO_FRAMES):
X[f,:,:,:]=frame
......@@ -111,17 +101,17 @@ def makeX():
'''if f%1 == 0:
print(len(X[f,:,4,1]))'''
return X
return X[skipFrames:,:,:,:]
#maxeX(cap)
# In[8]:
# In[ ]:
def makeY():
df=pd.read_csv(FILE_NAME_CSV, sep=',',header=None)
def makeY(fileNameCsv,noFrames,skipFrames):
df=pd.read_csv(fileNameCsv, sep=',',header=None)
df=np.array(df)
return df[:NO_FRAMES,:]
return df[skipFrames:noFrames,:]
'''df=df[:NO_OF_FRAMES,1]
Y=np.zeros((NO_OF_FRAMES,CELLS_PER_FRAME),dtype=np.int32)
......@@ -139,20 +129,97 @@ def makeY():
# In[9]:
# In[ ]:
X=makeX()
Y=makeY()
X=X[FRAMES_TO_SKIP:]
Y=Y[FRAMES_TO_SKIP:]
def maxDiff(y_true, y_pred):
return K.max(K.abs(y_true-y_pred))
# In[ ]:
model=cnn(INPUT_DIM,OUTPUT_DIM)
model.compile(loss='mean_squared_error',optimizer='adam',metrics=['binary_accuracy','accuracy','mean_squared_error','binary_crossentropy',maxDiff])
#model.predict(xTest,yTest)
#print(model.summary())
# In[ ]:
if DEBUG: print("X= ",X)
'''
BLACK AND WHITE VIDEO
'''
FILE_NAME='./video/bw'
FILE_NAME_VIDEO=FILE_NAME+'.avi'
FILE_NAME_CSV=FILE_NAME+'.csv'
NO_FRAMES=10000
if PC: NO_FRAMES=100
FRAMES_TO_SKIP=0
# In[ ]:
X=makeX(FILE_NAME_VIDEO,NO_FRAMES,FRAMES_TO_SKIP)
Y=makeY(FILE_NAME_CSV,NO_FRAMES,FRAMES_TO_SKIP)
if DEBUG: print("X= ",X)
if DEBUG: print("NAN in X",np.argwhere(np.isnan(X)))
if DEBUG: print("NAN in Y",np.argwhere(np.isnan(Y)))
if DEBUG: print("X shape: ",np.shape(X))
if DEBUG: print("Y shape: ",np.shape(Y))
# In[ ]:
'''Preparing the input and output vectors'''
X=np.mean(X,axis=3)
X=np.reshape(X,(X.shape[0],X.shape[1],X.shape[2],1))
X=(X-128.0)/256.0
print("X shape {}, Y shape {}.".format(X.shape,Y.shape))
xTrain, xTest, yTrain, yTest= train_test_split(X, Y, test_size=0.01)
# In[ ]:
model.fit(xTrain,yTrain,validation_split=0.1,epochs=EPOCHS,verbose=1,batch_size=BATCH_SIZE,shuffle=True)
print("Test set evaluation for BW video",'\n',model.metrics_names,'\n', model.evaluate(xTest,yTest))
# In[ ]:
'''
RANDOM COLOUR VIDEO
'''
FILE_NAME='./video/ran'
FILE_NAME_VIDEO=FILE_NAME+'.avi'
FILE_NAME_CSV=FILE_NAME+'.csv'
NO_FRAMES=10000
if PC: NO_FRAMES=100
FRAMES_TO_SKIP=0
# In[ ]:
X=makeX(FILE_NAME_VIDEO,NO_FRAMES,FRAMES_TO_SKIP)
Y=makeY(FILE_NAME_CSV,NO_FRAMES,FRAMES_TO_SKIP)
if DEBUG: print("X= ",X)
if DEBUG: print("NAN in X",np.argwhere(np.isnan(X)))
if DEBUG: print("NAN in Y",np.argwhere(np.isnan(Y)))
if DEBUG: print("X shape: ",np.shape(X))
if DEBUG: print("Y shape: ",np.shape(Y))
......@@ -160,49 +227,81 @@ INPUT_DIM=X[0].shape
OUTPUT_DIM=CELLS_PER_FRAME
# In[10]:
# In[ ]:
'''Preparing the input and output vectors'''
X=np.mean(X,axis=3)
X=np.reshape(X,(X.shape[0],X.shape[1],X.shape[2],1))
X=(X-128.0)/256.0
print("X shape {}, Y shape {}.".format(X.shape,Y.shape))
xTrain, xTest, yTrain, yTest= train_test_split(X, Y, test_size=0.10)
xTrain, xTest, yTrain, yTest= train_test_split(X, Y, test_size=0.01)
# In[ ]:
# In[11]:
model.fit(xTrain,yTrain,validation_split=0.1,epochs=EPOCHS,verbose=1,batch_size=BATCH_SIZE,shuffle=True)
print("Test set evaluation for random video",'\n',model.metrics_names,'\n', model.evaluate(xTest,yTest))
if PC:
cv2.imshow('Example frame',X[10,:,:,:])
cv2.waitKey(100)
print(Y[10])
# In[ ]:
# In[12]:
'''
MOVIE VIDEO
def maxDiff(y_true, y_pred):
return K.max(K.abs(y_true-y_pred))
'''
FILE_NAME='./video/multipleVideos'
FILE_NAME_VIDEO=FILE_NAME+'.mp4'
FILE_NAME_CSV=FILE_NAME+'.csv'
NO_FRAMES=10000
if PC: NO_FRAMES=100
FRAMES_TO_SKIP=0
# In[13]:
# In[ ]:
model=cnn(INPUT_DIM,OUTPUT_DIM)
model.build()
model.compile(loss='mean_squared_error',optimizer='adam',metrics=['binary_accuracy','accuracy','mean_squared_error','binary_crossentropy',maxDiff])
model.fit(xTrain,yTrain,validation_split=0.1,epochs=EPOCHS,verbose=1,batch_size=BATCH_SIZE,shuffle=False)
print(model.summary())
X=makeX(FILE_NAME_VIDEO,NO_FRAMES,FRAMES_TO_SKIP)
Y=makeY(FILE_NAME_CSV,NO_FRAMES,FRAMES_TO_SKIP)
if DEBUG: print("X= ",X)
if DEBUG: print("NAN in X",np.argwhere(np.isnan(X)))
if DEBUG: print("NAN in Y",np.argwhere(np.isnan(Y)))
if DEBUG: print("X shape: ",np.shape(X))
if DEBUG: print("Y shape: ",np.shape(Y))
INPUT_DIM=X[0].shape
OUTPUT_DIM=CELLS_PER_FRAME
print("Test set evaluation",model.evaluate(xTest,yTest))
print(np.round(model.predict(xTest)))
# In[ ]:
'''Preparing the input and output vectors'''
X=np.mean(X,axis=3)
X=np.reshape(X,(X.shape[0],X.shape[1],X.shape[2],1))
X=(X-128.0)/256.0
print("X shape {}, Y shape {}.".format(X.shape,Y.shape))
xTrain, xTest, yTrain, yTest= train_test_split(X, Y, test_size=0.5)
# In[ ]:
print("Test set evaluation for Movie video (Without training for movie video): ",model.metrics_names,' ', model.evaluate(xTest,yTest))
model.fit(xTrain,yTrain,validation_split=0.1,epochs=EPOCHS,verbose=1,batch_size=BATCH_SIZE,shuffle=True)
print("Test set evaluation for Movie video (After training for movie video):",model.metrics_names,' ', model.evaluate(xTest,yTest))
# In[ ]:
print(model.summary())
# In[ ]:
......
......@@ -1060,8 +1060,8 @@
"FRAME_WIDTH=192\n",
"COLOR_CHANNELS=3\n",
"\n",
"CELLS_VERTICLE=2\n",
"CELLS_HORIZONTAL=1\n",
"CELLS_VERTICLE=5\n",
"CELLS_HORIZONTAL=5\n",
"CELLS_PER_FRAME=CELLS_VERTICLE*CELLS_HORIZONTAL\n",
"\n",
"\n",
......
......@@ -25,8 +25,8 @@ FRAME_HEIGHT=108
FRAME_WIDTH=192
COLOR_CHANNELS=3
CELLS_VERTICLE=2
CELLS_HORIZONTAL=1
CELLS_VERTICLE=5
CELLS_HORIZONTAL=5
CELLS_PER_FRAME=CELLS_VERTICLE*CELLS_HORIZONTAL
......
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"MIN=50\n",
"MAX=200\n",
"OFFSET=20"
]
},
{
"cell_type": "code",
"execution_count": 4,
......@@ -1057,12 +1068,12 @@
"source": [
"NO_FRAMES=10000\n",
"if PC: NO_FRAMES=100\n",
"FRAME_HEIGHT=108\n",
"FRAME_WIDTH=192\n",
"FRAME_HEIGHT=100\n",
"FRAME_WIDTH=200\n",
"COLOR_CHANNELS=3\n",
"\n",
"CELLS_VERTICLE=2\n",
"CELLS_HORIZONTAL=2\n",
"CELLS_VERTICLE=5\n",
"CELLS_HORIZONTAL=5\n",
"CELLS_PER_FRAME=CELLS_VERTICLE*CELLS_HORIZONTAL\n",
"\n",
"\n",
......@@ -1089,7 +1100,7 @@
" msg=np.random.random_integers(0,1,CELLS_PER_FRAME)\n",
" \n",
" \n",
" frame=np.random.random_integers(low=120, high=140, size=(FRAME_HEIGHT,FRAME_WIDTH,COLOR_CHANNELS))\n",
" frame=np.random.random_integers(low=MIN, high=MAX, size=(FRAME_HEIGHT,FRAME_WIDTH,COLOR_CHANNELS))\n",
" frame=frame.astype(np.uint8)\n",
" \n",
" \n",
......@@ -1098,9 +1109,9 @@
" \n",
" if msg[cell]==1:\n",
" #if DEBUG: print(\"yIdx,xIdx\",yIdx,xIdx)\n",
" frame[yIdx*CELL_HEIGHT:(yIdx+1)*CELL_HEIGHT,xIdx*CELL_WIDTH:(xIdx+1)*CELL_WIDTH,:]+=50\n",
" frame[yIdx*CELL_HEIGHT:(yIdx+1)*CELL_HEIGHT,xIdx*CELL_WIDTH:(xIdx+1)*CELL_WIDTH,:]+=OFFSET\n",
" else:\n",
" frame[yIdx*CELL_HEIGHT:(yIdx+1)*CELL_HEIGHT,xIdx*CELL_WIDTH:(xIdx+1)*CELL_WIDTH,:]-=50\n",
" frame[yIdx*CELL_HEIGHT:(yIdx+1)*CELL_HEIGHT,xIdx*CELL_WIDTH:(xIdx+1)*CELL_WIDTH,:]-=OFFSET\n",
" \n",
" \n",
" csvToSave[f,:]=msg[:]\n",
......
#!/usr/bin/env python
# coding: utf-8
# In[ ]:
MIN=50
MAX=200
OFFSET=20
# In[4]:
......@@ -21,12 +29,12 @@ import pandas as pd
NO_FRAMES=10000
if PC: NO_FRAMES=100
FRAME_HEIGHT=108
FRAME_WIDTH=192
FRAME_HEIGHT=100
FRAME_WIDTH=200
COLOR_CHANNELS=3
CELLS_VERTICLE=2
CELLS_HORIZONTAL=2
CELLS_VERTICLE=5
CELLS_HORIZONTAL=5
CELLS_PER_FRAME=CELLS_VERTICLE*CELLS_HORIZONTAL
......@@ -53,7 +61,7 @@ for f in range(NO_FRAMES):
msg=np.random.random_integers(0,1,CELLS_PER_FRAME)
frame=np.random.random_integers(low=120, high=140, size=(FRAME_HEIGHT,FRAME_WIDTH,COLOR_CHANNELS))
frame=np.random.random_integers(low=MIN, high=MAX, size=(FRAME_HEIGHT,FRAME_WIDTH,COLOR_CHANNELS))
frame=frame.astype(np.uint8)
......@@ -62,9 +70,9 @@ for f in range(NO_FRAMES):
if msg[cell]==1:
#if DEBUG: print("yIdx,xIdx",yIdx,xIdx)
frame[yIdx*CELL_HEIGHT:(yIdx+1)*CELL_HEIGHT,xIdx*CELL_WIDTH:(xIdx+1)*CELL_WIDTH,:]+=50
frame[yIdx*CELL_HEIGHT:(yIdx+1)*CELL_HEIGHT,xIdx*CELL_WIDTH:(xIdx+1)*CELL_WIDTH,:]+=OFFSET
else:
frame[yIdx*CELL_HEIGHT:(yIdx+1)*CELL_HEIGHT,xIdx*CELL_WIDTH:(xIdx+1)*CELL_WIDTH,:]-=50
frame[yIdx*CELL_HEIGHT:(yIdx+1)*CELL_HEIGHT,xIdx*CELL_WIDTH:(xIdx+1)*CELL_WIDTH,:]-=OFFSET
csvToSave[f,:]=msg[:]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment