Commit b6f0a5ed authored by Gihan Jayatilaka's avatar Gihan Jayatilaka

Uploading files

parent fe21b897
This diff is collapsed.
#!/usr/bin/env python
# coding: utf-8
# In[1]:
'''
gihanchanaka@gmail.com
11-03-2019
1)This is to learn from BW and predict random
2) learn from random and predict movie
'''
# In[2]:
DEBUG=False
PC=False
# In[3]:
# import keras
# from keras.models import Sequential
# from keras.layers import Conv2D,Flatten,Dense,Dropout
import tensorflow as tf
from tensorflow.contrib.learn.python.learn.estimators._sklearn import train_test_split
# import keras.backend as K
import numpy as np
import cv2
import sys
import os
import pandas as pd
# In[4]:
currentTotalIterations=0
#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]:
def inputLayer(inputTensor,outputShape=None):
if outputShape==None:
outputTensor= inputTensor
else:
#x2D=tf.reshape(xFlat,[-1,imgSize,imgSize,noColorChannels])
inputShape=inputTensor.get_shape()
outputTensor=tf.reshape(inputTensor,[-1,outputShape[0],outputShape[1],1])
if DEBUG: print("DEBUG: Conv layer added with output tensor shape {}".format(outputTensor.shape))
return outputTensor
# In[6]:
def convLayer2D(inputTensor,kernelShape,noKernels,poolingSize=None,activation=None):
filterLength=kernelShape[0]
filterHeight=kernelShape[1]
noFilters=noKernels
inputShape=inputTensor.get_shape()
if DEBUG: print("DEBUG: {}".format(inputShape))
noChannels=int(inputShape[-1])
shape=[filterLength,filterHeight,noChannels,noFilters]
if DEBUG: print("DEBUG: Shape of weights {}".format(shape))
weights=tf.Variable(tf.truncated_normal(shape,stddev=0.5))
biases=tf.Variable(tf.constant(0.05,shape=[noFilters]))
outputTensor=tf.nn.conv2d(input=inputTensor,filter=weights,strides=[1,1,1,1],padding='SAME')
#strides=[img,x,y,colourChannel]
outputTensor=outputTensor+biases
if poolingSize!=None:
outputTensor=maxPoolingLayer(outputTensor,poolingSize)
if activation=='relu':
outputTensor=relu(outputTensor)
if DEBUG: print("DEBUG: Conv layer added with output tensor shape {}".format(outputTensor.shape))
return outputTensor
# In[7]:
def maxPoolingLayer(inputTensor,kernelSize):
return tf.nn.max_pool(value=inputTensor,ksize=[1,kernelSize[0],kernelSize[1],1],strides=[1,kernelSize[0],kernelSize[1],1],padding='SAME')
# In[8]:
def relu(inputTensor):
return tf.nn.relu(inputTensor)
# In[9]:
def flatten(inputTensor):
shape=inputTensor.get_shape() # layer_shape == [num_images, img_height, img_width, num_channels]
noFeatures=int(shape[1]*shape[2]*shape[3])
outputTensor=tf.reshape(inputTensor,[-1,noFeatures])
return outputTensor
# In[10]:
def fullyConnectedLayer(inputTensor,outputSize):
inputShape=inputTensor.get_shape()
shape=[int(inputShape[1]),outputSize]
weights=tf.Variable(tf.truncated_normal(shape,stddev=0.5))
biases=tf.Variable(tf.constant(0.05,shape=[outputSize]))
outputTensor=tf.matmul(inputTensor,weights)+biases
if DEBUG: print("DEBUG: Added fully connected layer with output shape {}.".format(outputTensor.shape))
return outputTensor
# In[11]:
def buildNetwork(outputTensor,targetTensor):
if DEBUG: print("DEBUG: Building network target tensor {} out tensor {}".format(targetTensor.shape,outputTensor.shape))
sqError=tf.squared_difference(outputTensor,tf.cast(targetTensor,tf.float32))
if DEBUG: print("DEBUG: sqError shape= {}".format(sqError.shape))
cost=tf.reduce_mean(sqError)
if DEBUG: print("DEBUG: cost shape= {}".format(cost.shape))
optimizer=tf.train.AdamOptimizer(learning_rate=0.01).minimize(cost)
yPred=tf.cast(tf.round(outputTensor),tf.int64)
if DEBUG: print("DEBUG: yPred shape= {}".format(yPred.shape))
correctPrediction=tf.equal(yPred,targetTensor)
if DEBUG: print("DEBUG: correctPrediction shape= {}".format(correctPrediction.shape))
accuracy=tf.reduce_mean(tf.cast(correctPrediction,tf.float32))
if DEBUG: print("DEBUG: accuracy shape= {}".format(accuracy.shape))
print("yPred",yPred.shape,"Correct pred",correctPrediction.shape,"Accuracy",accuracy.shape,"Sq error",sqError.shape,"Cost",cost.shape)
sess.run(tf.global_variables_initializer())
if DEBUG: print("DEBUG: Completed building network")
return outputTensor,optimizer,accuracy,cost
# In[12]:
def train(optimizer,noIterations,dataset,acc,cst):
DISPLAY_PROGRESS_EVERY=50
global currentTotalIterations
for it in range(currentTotalIterations,currentTotalIterations+noIterations):
initializer=iterr.make_initializer(dataset)
sess.run(initializer)
while True:
try:
sess.run(optimizer)
except:
break
if (it>0) and (it%DISPLAY_PROGRESS_EVERY==0):
print("Iter {} training accuracy:".format(it+1))
printAccuracy(iterr,dataset,acc,"Train",cst)
currentTotalIterations+=noIterations
print("DEBUG: train() function completed")
# In[13]:
def makeY(fileNameCsv,noFrames,skipFrames):
df=pd.read_csv(fileNameCsv, sep=',',header=None)
df=np.array(df)
return df[skipFrames:noFrames,:]
# In[14]:
def makeX(fileNameVideo,noFrames,skipFrames):
cap = cv2.VideoCapture(fileNameVideo)
if (cap.isOpened()== False):
print("Error opening video stream or file")
ret,frame=cap.read()
X=np.zeros((noFrames,FRAME_HEIGHT,FRAME_WIDTH,COLOR_CHANNELS),dtype=np.float32)
for f in range(NO_FRAMES):
X[f,:,:,:]=frame
ret,frame=cap.read()
return X[skipFrames:,:,:,:]
#maxeX(cap)
# In[15]:
def maxDiff(y_true, y_pred):
return K.max(K.abs(y_true-y_pred))
# In[16]:
def printAccuracy(iterr,dataset,acc,trainOrTest,cost):
initializer=iterr.make_initializer(dataset)
sess.run(initializer)
accCountProductSum=0.0
costCountProductSum=0.0
countSum=0.0
while True:
try:
#Please note: These two lines are not 100% correct. This is just guesswork!
a,c=sess.run([acc,cost])
accCountProductSum+=a
costCountProductSum+=c
if DEBUG: print("DEBUG: Batch accuracy= {}, batch cost= {}".format(a,c))
countSum+=1
except:
if countSum==0: break
print("{} accuracy= {}, cost= {}".format(trainOrTest,accCountProductSum/countSum,costCountProductSum/countSum))
break
# In[17]:
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=4
CUDA1=0
CUDA2=1
os.environ["CUDA_VISIBLE_DEVICES"]="{},{}".format(CUDA1,CUDA2)
sess = tf.Session()
# In[18]:
'''
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
dataX=makeX(FILE_NAME_VIDEO,NO_FRAMES,FRAMES_TO_SKIP)
dataY=makeY(FILE_NAME_CSV,NO_FRAMES,FRAMES_TO_SKIP)
xTrain, xTest, yTrain, yTest= train_test_split(dataX, dataY, test_size=0.5)
trainData=(xTrain,yTrain)
testData=(xTest,yTest)
trainDataset=tf.data.Dataset.from_tensor_slices(trainData)
testDataset=tf.data.Dataset.from_tensor_slices(testData)
iterr=tf.data.Iterator.from_structure(output_types=trainDataset.output_types,output_shapes=trainDataset.output_shapes)
# In[ ]:
'''xColour=tf.placeholder(tf.float32,shape=[None,INPUT_DIM[0],INPUT_DIM[1],COLOR_CHANNELS],name='xColour')
yTrue=tf.placeholder(tf.float32,shape=[None,CELLS_PER_FRAME],name='yTrue')'''
xColour,yTrue=iterr.get_next()
xGrey=tf.reduce_mean(xColour,axis=-1)/255.0
if DEBUG: print("Shapes xColour {} yTure {} xGrey {}".format(xColour.shape,yTrue.shape,xGrey.shape))
l1=inputLayer(xGrey,[INPUT_DIM[0],INPUT_DIM[1]])
l2=flatten(l1)
l3=fullyConnectedLayer(l2,OUTPUT_DIM)
yPred,opt,acc,cst=buildNetwork(l3,yTrue)
if DEBUG: print("layer shapes 1:{} 2:{} 3:{} ".format(l1.shape,l2.shape,l3.shape))
# In[ ]:
printAccuracy(iterr,trainDataset,acc,"Training ",cst)
train(opt,1000,trainDataset,acc,cst)
printAccuracy(iterr,trainDataset,acc,"Training ",cst)
# 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[ ]:
dataX=makeX(FILE_NAME_VIDEO,NO_FRAMES,FRAMES_TO_SKIP)
dataY=makeY(FILE_NAME_CSV,NO_FRAMES,FRAMES_TO_SKIP)
# In[ ]:
xTrain, xTest, yTrain, yTest= train_test_split(dataX, dataY, test_size=0.5)
printAccuracy(acc,{xColour:xTest,yTrue:yTest},cst)
train(opt,100,xTrain,yTrain)
printAccuracy(acc,{xColour:xTest,yTrue:yTest},cst)
# In[ ]:
'''
MOVIE VIDEO
'''
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[ ]:
dataX=makeX(FILE_NAME_VIDEO,NO_FRAMES,FRAMES_TO_SKIP)
dataY=makeY(FILE_NAME_CSV,NO_FRAMES,FRAMES_TO_SKIP)
# In[ ]:
xTrain, xTest, yTrain, yTest= train_test_split(dataX, dataY, test_size=0.5)
printAccuracy(acc,{xColour:xTest,yTrue:yTest},cst)
train(opt,100,xTrain,yTrain)
printAccuracy(acc,{xColour:xTest,yTrue:yTest},cst)
# In[ ]:
# In[ ]:
# In[ ]:
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