Skip to content

Commit

Permalink
Merge pull request #129 from cinemascience/MLBatchUpdates
Browse files Browse the repository at this point in the history
adding batch ML functionality
  • Loading branch information
dhrogers authored Aug 21, 2024
2 parents 4a22766 + 9b95b74 commit 385fdcc
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 32 deletions.
23 changes: 14 additions & 9 deletions pycinema/filters/MLTFPredictor.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,17 @@ def __init__(self):

def _update(self):
images = self.inputs.images.get()
model = self.inputs.trainedModel.get()
# get required input properties from model
modelList = self.inputs.trainedModel.get()
# get required input properties from first model
model = modelList[0]
width = model.layers[0].input_shape[1]
height = model.layers[0].input_shape[2]
channels = model.layers[0].input_shape[3]
if channels == 1:
gray_req = True
else: #channels == 3 or 4
gray_req = False

result = []
# iterate over all the images in the input images
for image in self.inputs.images.get():
Expand All @@ -45,13 +46,17 @@ def _update(self):
data = np.array(data) / 255
data = data.reshape((1, width, height, channels))

# at the moment, assuming only one predicted output
# from the network
one_hot = model.predict(data, verbose = 0)
predictedValue = np.argmax(one_hot)
img.meta['PredictedValue'] = float(predictedValue)
pv_iter = 0
for model in modelList:
# at the moment, assuming only one predicted output
# from the network
one_hot = model.predict(data, verbose = 0)
predictedValue = np.argmax(one_hot)
img.meta['PredictedValue_' + str(pv_iter)] = float(predictedValue)
pv_iter += 1

result.append(img)

self.outputs.images.set(result)

return 1;
return 1;
51 changes: 39 additions & 12 deletions pycinema/filters/MLTFReader.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
import logging as log

from os.path import exists
import os
import tensorflow as tf
import csv

class MLTFReader(Filter):

Expand All @@ -12,35 +14,60 @@ def __init__(self):
'path': ''
},
outputs={
'model': None
'model': []
}
)

def _update(self):

modelPath = self.inputs.path.get()
#modelDirectoryPath = modelDirectoryPath + "/" #in case missing

if not modelPath: #if the path is empty
log.error('ML Model path empty')
self.outputs.model.set(None)
log.error('ML Model or data.csv path empty')
self.outputs.models.set([])
return 0

if not exists(modelPath):
log.error('[ERROR] ML Model not found:', modelPath)
self.outputs.model.set(None)
log.error('[ERROR] ML Model or data.csv not found:', modelPath)
self.outputs.models.set([])
return 0

# load_model supports two formats that a tensorflow
# model can be saved as: .keras and HDF5
try:
# load_model supports the three formats that a tensorflow
# model can be saved as: .keras, HDF5 and SavedModel
model = tf.keras.models.load_model(modelPath)
models = []
# if the path directly points to a TF model
if modelPath.endswith(".h5") or modelPath.endswith(".keras"):
model = tf.keras.models.load_model(modelPath)
models.append(model)

else:
# if the path points to a data.csv file
# the csv file must have the first column as numerical order of files
# the second column is the path to each model
# parse through models directory and load a list of models
table = []
with open(modelPath, 'r+') as csvfile:
rows = csv.reader(csvfile, delimiter=',')
for row in rows:
table.append(row)
table = table[1:]
numModels = len(table)

models = [None] * numModels
for row in table:
parent = os.path.dirname(modelPath) + "/"
filePath = os.path.join(parent, row[1])
model = tf.keras.models.load_model(filePath)
models[int(row[0])] = model

#check if training configuration exists, if not give error
except:
log.error('[ERROR] Unable to open ML Model')
self.outputs.model.set(None)
log.error('[ERROR] Unable to open ML Model Directory')
self.outputs.models.set([])
return 0

self.outputs.model.set(model)
self.outputs.model.set(models)

return 1
return 1
22 changes: 11 additions & 11 deletions testing/gold/MLOutputTest.csv
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
TrueValue,FILE,id,PredictedValue
0,data/mnist.cdb/image/0.png,0,0.0
1,data/mnist.cdb/image/1.png,1,1.0
2,data/mnist.cdb/image/2.png,2,2.0
3,data/mnist.cdb/image/3.png,3,3.0
4,data/mnist.cdb/image/4.png,4,4.0
5,data/mnist.cdb/image/5.png,5,5.0
6,data/mnist.cdb/image/6.png,6,6.0
7,data/mnist.cdb/image/7.png,7,7.0
8,data/mnist.cdb/image/8.png,8,7.0
9,data/mnist.cdb/image/9.png,9,9.0
id,TrueValue,FILE,PredictedValue_0
0,0,data/mnist.cdb/image/0.png,0.0
1,1,data/mnist.cdb/image/1.png,1.0
2,2,data/mnist.cdb/image/2.png,2.0
3,3,data/mnist.cdb/image/3.png,3.0
4,4,data/mnist.cdb/image/4.png,4.0
5,5,data/mnist.cdb/image/5.png,5.0
6,6,data/mnist.cdb/image/6.png,6.0
7,7,data/mnist.cdb/image/7.png,7.0
8,8,data/mnist.cdb/image/8.png,7.0
9,9,data/mnist.cdb/image/9.png,9.0

0 comments on commit 385fdcc

Please sign in to comment.