Skip to content
Snippets Groups Projects
Unverified Commit 0ecbe3fd authored by Nishant Kumar's avatar Nishant Kumar Committed by GitHub
Browse files

Merge pull request #7 from mpc-msri/cryptflow

CrypTFlow: An end-to-end system for secure TensorFlow inference
parents 8882f749 989c01e9
No related branches found
No related tags found
No related merge requests found
Showing
with 828 additions and 0 deletions
*.inp
*.outp
*.mtdata
*.pkl
*.out
*__temp1.ezpc
*__temp2.ezpc
__pycache__/
\ No newline at end of file
#!/bin/bash
##########################################################################################
# This is the CrypTFlow compilation script.
# Use this on a network to compile to MPC protocol.
# By default, this assumes there is a ezpc repo one level up - if you want to change it,
# please use Paths.config to override the default paths.
# Same goes for Porthos repository.
# NOTE : When overriding paths in Paths.config, assumption is there is no '/' at the end.
##########################################################################################
# Load overriden paths from config file
. Paths.config
echo -e "Loaded paths: EzPCDir - $EzPCDir, PorthosDir - $PorthosDir"
usage() {
echo -e "CrypTFlow compilation script. Options:";
echo -e "<-b|--bitlen> <bitlen> :: Bit length to compile for. Possible options: 32/64. Defaults to 64";
echo -e "<-s|--scaling-fac> <sf> :: Scaling factor to compile for. Defaults to 12.";
echo -e "<-t|--target> <target> :: Compilation target. Possible options: CPP/PORTHOS. Defaults to CPP.";
echo -e "<-f|--filename> :: Python tensorflow file to compile."
echo -e "<--disable-hlil-all-opti> :: Disable all optimizations in HLIL."
echo -e "<--disable-rmo> :: Disable Relu-Maxpool optimization."
echo -e "<--disable-liveness-opti> :: Disable Liveness Optimization."
echo -e "<--exec-python> <num of args for python script> <args for python script>... :: Execute the python script which is passed for compilation.";
echo -e "<--help> :: help options.";
exit 1;
}
BITLEN="64"
SCALINGFACTOR="12"
COMPILATIONTARGET="CPP"
EXECPYTHONARGS=""
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-b|--bitlen)
BITLEN="$2"
shift # past argument
shift # past value
;;
-s|--scaling-fac)
SCALINGFACTOR="$2"
shift # past argument
shift # past value
;;
-t|--target)
COMPILATIONTARGET="$2"
shift # past argument
shift # past value
;;
-f|--filename)
FILENAME="$2"
shift
shift
;;
--exec-python)
numargs="$2"
shift
shift
for ((curArgNum=0;curArgNum<$numargs;curArgNum++)); do
EXECPYTHONARGS="${EXECPYTHONARGS} $1"
shift #past this argument
done
EXECPYTHON=Y
;;
--help)
HELP=Y
shift # past one arg
;;
--disable-hlil-all-opti)
DisableHLILAllOpti=Y
shift # past one arg
;;
--disable-rmo)
DisableRMO=Y
shift # past one arg
;;
--disable-liveness-opti)
DisableLivenessOpti=Y
shift # past one arg
;;
*) # unknown option
usage
;;
esac
done
if [ ! -z "$HELP" ] || [ -z "$FILENAME" ] ; then
usage
fi
if [ ! -z "$EXECPYTHON" ]; then
echo -e "Exec-python parameter passed. EXECPYTHONARGS=$EXECPYTHONARGS."
fi
compilationTargetLower=$(echo "$COMPILATIONTARGET" | awk '{print tolower($0)}')
compilationTargetHigher=$(echo "$COMPILATIONTARGET" | awk '{print toupper($0)}')
givenDirPath=$(dirname "$FILENAME")
fullDirPath=$(realpath "$givenDirPath")
porthosFullDirPath=$( realpath "$PorthosDir")
baseFileName=$(basename -- "$FILENAME")
extension="${baseFileName##*.}"
actualFileName="${baseFileName%.*}" #without extension
fullFilePath=$(realpath "$FILENAME")
ezpcOutputFileName=${actualFileName}'_'${BITLEN}'_'${compilationTargetLower}
ezpcOutputFullFileName=${fullDirPath}'/'${ezpcOutputFileName}'.ezpc'
finalCodeOutputFileName=${ezpcOutputFileName}'0.cpp'
if [ "$extension" != "py" ]; then
echo -e "Error: Provide a python file to compile."
usage
fi
cd "$fullDirPath"
if [ ! -z "$EXECPYTHON" ] ; then
echo -e "********* Executing python script passed to compile. *********"
if [ "$EXECPYTHONARGS" = "" ]; then
python3 "$baseFileName"
else
python3 "$baseFileName" "$EXECPYTHONARGS"
fi
echo -e "********* Python script compilation done. *********"
fi
cd - > /dev/null
cd ./TFCompiler
python3 ProcessTFGraph.py "$fullFilePath"
cd ../SeeDot
seedotArgs="--astFile ${fullDirPath}/astOutput.pkl --consSF ${SCALINGFACTOR} --bitlen ${BITLEN} --outputFileName ${ezpcOutputFullFileName}"
if [ ! -z "$DisableHLILAllOpti" ]; then
seedotArgs="${seedotArgs} --disableAllOpti True"
fi
if [ ! -z "$DisableRMO" ]; then
seedotArgs="${seedotArgs} --disableRMO True"
fi
if [ ! -z "$DisableLivenessOpti" ]; then
seedotArgs="${seedotArgs} --disableLivenessOpti True"
fi
python3 SeeDot.py $seedotArgs
cd ..
libraryFile="$compilationTargetLower"
if [ "$compilationTargetLower" == "aby" ];then
libraryFile="cpp"
fi
cat "./TFEzPCLibrary/Library${BITLEN}_$libraryFile.ezpc" "./TFEzPCLibrary/Library${BITLEN}_common.ezpc" "$ezpcOutputFullFileName" > temp
mv temp "$ezpcOutputFullFileName"
cp "$ezpcOutputFullFileName" "$EzPCDir/EzPC"
cd "$EzPCDir/EzPC"
eval `opam config env`
./ezpc.sh "$ezpcOutputFullFileName" --bitlen "$BITLEN" --codegen "$compilationTargetHigher" --disable-tac
if [ "$compilationTargetLower" == "cpp" ]; then
cd "$fullDirPath"
g++ -O3 "$finalCodeOutputFileName" -o "$actualFileName.out"
echo -e "All compilation done."
elif [ "$compilationTargetLower" == "porthos" ]; then
cd - > /dev/null
python3 ./HelperScripts/AthosToPorthosTemp.py "$fullDirPath/$finalCodeOutputFileName"
# cd "$fullDirPath"
# cp "$finalCodeOutputFileName" "$porthosFullDirPath/src/main.cpp"
# cd "$porthosFullDirPath"
# make -j
echo -e "All compilation done."
fi
import sys,os
if (len(sys.argv)!=2):
exit(1)
filename = sys.argv[1]
with open(filename, 'r') as ff:
lines = ff.readlines()
newfunc = ['void Conv2DCSF(int32_t N, int32_t H, int32_t W, int32_t CI, int32_t FH, int32_t FW, int32_t CO, int32_t zPadHLeft, int32_t zPadHRight, int32_t zPadWLeft, int32_t zPadWRight, int32_t strideH, int32_t strideW, auto& inputArr, auto& filterArr, int32_t consSF, auto& outArr){',
'#ifdef CONV_OPTI',
' if ((FH>=5) || (FW>=5)){',
' funcConv2DCSF(N, H, W, CI, FH, FW, CO, zPadHLeft, zPadHRight, zPadWLeft, zPadWRight, strideH, strideW, inputArr, filterArr, consSF, outArr);',
' }',
' else{',
' Conv2DCSFMain(N, H, W, CI, FH, FW, CO, zPadHLeft, zPadHRight, zPadWLeft, zPadWRight, strideH, strideW, inputArr, filterArr, consSF, outArr);',
' }',
'#else',
' Conv2DCSFMain(N, H, W, CI, FH, FW, CO, zPadHLeft, zPadHRight, zPadWLeft, zPadWRight, strideH, strideW, inputArr, filterArr, consSF, outArr);',
'#endif',
'}',
'\n'
]
ii=0
while(ii < len(lines)):
curLine = lines[ii]
if curLine.startswith("void Conv2DReshapeInput("):
jj = ii+1
found = 0
while(found != 2 and jj<ii+100):
if 'funcSSCons' in lines[jj]:
found+=1
lines[jj] = lines[jj].replace('funcSSCons','')
jj+=1
elif curLine.startswith('void Pad442('):
jj = ii+1
found = 0
while(found != 1 and jj<ii+100):
if 'funcSSCons' in lines[jj]:
found+=1
lines[jj] = lines[jj].replace('funcSSCons','')
jj+=1
elif curLine.startswith('void Conv2DCSF('):
lines[ii] = lines[ii].replace('Conv2DCSF', 'Conv2DCSFMain')
jj = ii+1
found = 0
while(found != 1 and jj<ii+100):
if (lines[jj].startswith('}')):
found+=1
jj+=1
for curFuncLine in reversed(newfunc):
lines.insert(jj+1,curFuncLine+'\n')
ii = jj+len(newfunc)
ii+=1
with open(filename, 'w') as ff:
for line in lines:
ff.write(line)
*
*/
!.gitignore
\ No newline at end of file
import os,sys, functools
if (len(sys.argv)!=4):
print("Incorrect args. Error.", file=sys.stderr)
exit(1)
preProcessedImagesDir = sys.argv[1]
startImgNum = int(sys.argv[2])
endImgNum = int(sys.argv[3])
numImg = endImgNum-startImgNum+1
expectedShape = [224,224,3]
expectedNumElements = functools.reduce(lambda x,y : x*y, expectedShape)
print("ExpectedNumElements in all preprocessed images = ", expectedNumElements)
badImages = []
for i in range(startImgNum,endImgNum):
if ((i % (numImg/10))==0):
print("Reched i = {0}.".format(i))
filename = os.path.join(preProcessedImagesDir, 'ImageNum_'+str(i)+'.inp')
with open(filename, 'r') as ff:
line = ff.readline()
val = list(map(lambda x : float(x) , line.split()))
if (len(val)!=expectedNumElements):
print("Expected num of elements not found in imagenum = {0}.".format(i))
badImages.append(i)
print("Found {0} bad images.".format(len(badImages)))
with open('./ImageNet_ValData/imagenet_2012_validation_synset_labels.txt', 'r') as ff:
allLines = ff.readlines()
labels = list(map(lambda x : x.rstrip(), allLines))
sortedLabels = sorted(set(labels))
labelsMap = {}
for k,v in enumerate(sortedLabels):
labelsMap[v] = k+1
with open('./ImageNet_ValData/sortedWordnetIds.txt', 'w') as ff:
for x in sortedLabels:
ff.write(x + '\n')
with open('./ImageNet_ValData/imagenet12_val_nlabels.txt', 'w') as ff:
for curLabel in labels:
ff.write(str(labelsMap[curLabel]))
ff.write('\n')
# Use this script to find accuracy once the full exploration has been done on relevant scale factors.
# NOTE: The ground truth labels are in [1, 1000].
# Resnet outputs labels in [0,1000] -- class 0 is extraneous and is the other category.
# For DenseNet and SqueezeNet, the labels after argmax are in [0,999]:
# so either we have to do ground truth labels-1 or while outputing from the code for SqNet/DenseNet,
# add a +1. Choosing to go with the former.
# So, in summary, when running resnet, use the last parameter as 1, while for SqueezeNet/DenseNet use it as 0.
import os, sys
import numpy as np
if (len(sys.argv) < 4):
print("Usage : python3 FindAccuracy.py <groundTruthLabelsFileName> <inferenceOutputDirectory> <lowerBoundOfOutputLabels>")
exit(1)
# Change following parameters accordingly
ScalesToCheck = [10] #range(8,31)
NumProcesses = 32
numImages = 50000
topK = 5
groundTruthLabelsFileName = sys.argv[1]
inferenceOutputDirectory = sys.argv[2]
lowerBoundOfOutputLabels = int(sys.argv[3])
if (lowerBoundOfOutputLabels != 0 and lowerBoundOfOutputLabels != 1):
print("lowerBoundOfOutputLabels should be either 0 or 1. Exiting.", file=sys.stderr)
exit(1)
with open(groundTruthLabelsFileName, 'r') as ff:
groundTruthLabels = ff.readlines()
groundTruthLabels = list(map(lambda x : int(x.rstrip()), groundTruthLabels)) #For imagenet, this is in [1,1000]
if (lowerBoundOfOutputLabels==0):
groundTruthLabels = list(map(lambda x : x-1, groundTruthLabels)) #If the labels in the output start from 0,
# subtract 1 from the ground truth labels.
def parseInferenceOutputFile(predictions, outputFileName):
with open(outputFileName, 'r') as ff:
lines = ff.readlines()
lines = list(map(lambda x : x.rstrip(), lines))
lines = list(filter(lambda x : x!='', lines))
assert(len(lines)%2==0)
imgCounter = None
for line in lines:
if (line.startswith('Answer for')):
imgCounter = int(line.split('=')[1].split(':')[0]) #This is assumed to be 1-indexed
else:
assert(imgCounter is not None)
preds = line.split()
preds = np.array(list(map(lambda x : int(x), preds)))
topKPredsIdx = np.argpartition(preds, -1*topK)[-1*topK:]
topKPredsIdx = topKPredsIdx[np.argsort(preds[topKPredsIdx])]
for i,val in enumerate(topKPredsIdx):
predictions[imgCounter-1][i] = val
def calculateAccuracy(predictions):
global groundTruthLabels
top1CorrectPred = 0
topKCorrectPred = 0
for i in range(numImages):
if (groundTruthLabels[i] == predictions[i][-1]):
top1CorrectPred += 1
if (groundTruthLabels[i] in predictions[i]):
topKCorrectPred += 1
return (top1CorrectPred/(1.0*numImages), topKCorrectPred/(1.0*numImages))
for curScale in ScalesToCheck:
predictions = [[None]*topK for _ in range(numImages)]
for curProcessNum in range(NumProcesses):
curFileName = os.path.join(inferenceOutputDirectory, 'output_' + str(curScale) + '_' + str(curProcessNum) + '.outp')
parseInferenceOutputFile(predictions, curFileName)
for i in range(numImages):
for j in range(topK):
assert(predictions[i][j] is not None)
(top1Acc, topKAcc) = calculateAccuracy(predictions)
print("curScale = " + str(curScale) + ", top1Acc = " + str(top1Acc) + ", topKAcc = " + str(topKAcc))
# Use this script to find accuracy once the full exploration has been done on relevant scale factors.
# NOTE: The ground truth labels are in [1, 1000].
# Resnet outputs labels in [0,1000] -- class 0 is extraneous and is the other category.
# For DenseNet and SqueezeNet, the labels after argmax are in [0,999]:
# so either we have to do ground truth labels-1 or while outputing from the code for SqNet/DenseNet,
# add a +1. Choosing to go with the former.
# So, in summary, when running resnet, use the last parameter as 1, while for SqueezeNet/DenseNet use it as 0.
import os, sys
import numpy as np
if (len(sys.argv) < 4):
print("Usage : python3 FindAccuracy_Porthos.py <groundTruthLabelsFileName> <inferenceOutputDirectory> <lowerBoundOfOutputLabels>")
exit(1)
# Change following parameters accordingly
ScalesToCheck = [9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30]
# ScalesToCheck = [11]
numImages = 96
topK = 5
groundTruthLabelsFileName = sys.argv[1]
inferenceOutputDirectory = sys.argv[2]
lowerBoundOfOutputLabels = int(sys.argv[3])
if (lowerBoundOfOutputLabels != 0 and lowerBoundOfOutputLabels != 1):
print("lowerBoundOfOutputLabels should be either 0 or 1. Exiting.", file=sys.stderr)
exit(1)
with open(groundTruthLabelsFileName, 'r') as ff:
groundTruthLabels = ff.readlines()
groundTruthLabels = list(map(lambda x : int(x.rstrip()), groundTruthLabels)) #For imagenet, this is in [1,1000]
if (lowerBoundOfOutputLabels==0):
groundTruthLabels = list(map(lambda x : x-1, groundTruthLabels)) #If the labels in the output start from 0,
# subtract 1 from the ground truth labels.
def parseInferenceOutputFile(predictions, i, outputFileName):
with open(outputFileName, 'r') as ff:
lines = ff.readlines()
lines = list(map(lambda x : x.rstrip(), lines))
lines = list(filter(lambda x : x!='', lines))
if(len(lines)!=2):
print("Error in parsing : "+outputFileName)
assert(False)
imgCounter = None
for line in lines:
if (line.startswith('Answer for')):
imgCounter = int(line.split('=')[1].split(':')[0]) #This is assumed to be 0-indexed
else:
assert(imgCounter is not None)
preds = line.split()
# print(imgCounter,preds)
preds = np.array(list(map(lambda x : int(x), preds)))
topKPredsIdx = np.argpartition(preds, -1*topK)[-1*topK:]
topKPredsIdx = topKPredsIdx[np.argsort(preds[topKPredsIdx])]
for i, val in enumerate(topKPredsIdx):
predictions[imgCounter][i] = val
def calculateAccuracy(predictions):
global groundTruthLabels
top1CorrectPred = 0
topKCorrectPred = 0
for i in range(numImages):
if not(predictions[i][0]):
continue
if (groundTruthLabels[i] == predictions[i][-1]):
top1CorrectPred += 1
if (groundTruthLabels[i] in predictions[i]):
topKCorrectPred += 1
return (top1CorrectPred/(1.0*numImages), topKCorrectPred/(1.0*numImages))
for curScale in ScalesToCheck:
predictions = [[None]*topK for _ in range(numImages)]
for i in range(numImages):
curFileName = os.path.join(inferenceOutputDirectory, 'stderr_' + str(curScale) + '_' + str(i) +'_proc_1.outp')
parseInferenceOutputFile(predictions, i, curFileName)
for i in range(numImages):
for j in range(topK):
assert(predictions[i][j] is not None)
(top1Acc, topKAcc) = calculateAccuracy(predictions)
print("curScale = " + str(curScale) + ", top1Acc = " + str(top1Acc) + ", topKAcc = " + str(topKAcc))
# Use this script to find accuracy once the full exploration has been done on relevant scale factors.
# NOTE: The ground truth labels are in [1, 1000].
# Resnet outputs labels in [0,1000] -- class 0 is extraneous and is the other category.
# For DenseNet and SqueezeNet, the labels after argmax are in [0,999]:
# so either we have to do ground truth labels-1 or while outputing from the code for SqNet/DenseNet,
# add a +1. Choosing to go with the former.
# So, in summary, when running resnet, use the last parameter as 1, while for SqueezeNet/DenseNet use it as 0.
import os, sys
import numpy as np
if (len(sys.argv) < 5):
print("Usage : python3 FindAccuracy.py <groundTruthLabelsFileName> <tfInferenceArgmaxFileName> <tfInferenceAllOutputFileName> <lowerBoundOfOutputLabels>")
exit(1)
numImages = 50000
topK = 5
groundTruthLabelsFileName = sys.argv[1]
tfInferenceArgmaxFileName = sys.argv[2]
tfInferenceAllOutputFileName = sys.argv[3]
lowerBoundOfOutputLabels = int(sys.argv[4])
if (lowerBoundOfOutputLabels != 0 and lowerBoundOfOutputLabels != 1):
print("lowerBoundOfOutputLabels should be either 0 or 1. Exiting.", file=sys.stderr)
exit(1)
with open(groundTruthLabelsFileName, 'r') as ff:
groundTruthLabels = ff.readlines()
groundTruthLabels = list(map(lambda x : int(x.rstrip()), groundTruthLabels)) #For imagenet, this is in [1,1000]
if (lowerBoundOfOutputLabels==0):
groundTruthLabels = list(map(lambda x : x-1, groundTruthLabels)) #If the labels in the output start from 0,
# subtract 1 from the ground truth labels.
with open(tfInferenceArgmaxFileName, 'r') as ff:
tfInferenceArgmaxOutputs = ff.readlines()
tfInferenceArgmaxOutputs = list(map(lambda x : int(x.rstrip()), tfInferenceArgmaxOutputs))
def parseInferenceOutputFile(predictions, outputFileName):
with open(outputFileName, 'r') as ff:
lines = ff.readlines()
lines = list(map(lambda x : x.rstrip(), lines))
lines = list(filter(lambda x : x!='', lines))
assert(len(lines)%2==0)
imgCounter = None
for line in lines:
if (line.startswith('Answer for')):
imgCounter = int(line.split('=')[1]) #This is assumed to be 1-indexed
else:
assert(imgCounter is not None)
preds = line.split()
preds = np.array(list(map(lambda x : float(x), preds)))
topKPredsIdx = np.argpartition(preds, -1*topK)[-1*topK:]
topKPredsIdx = topKPredsIdx[np.argsort(preds[topKPredsIdx])]
for i,val in enumerate(topKPredsIdx):
predictions[imgCounter-1][i] = val
def calculateAccuracy(predictions):
global groundTruthLabels
top1CorrectPred = 0
topKCorrectPred = 0
for i in range(numImages):
if (groundTruthLabels[i] == predictions[i][-1]):
top1CorrectPred += 1
if (groundTruthLabels[i] in predictions[i]):
topKCorrectPred += 1
return (top1CorrectPred/(1.0*numImages), topKCorrectPred/(1.0*numImages))
predictions = [[None]*topK for _ in range(numImages)]
parseInferenceOutputFile(predictions, tfInferenceAllOutputFileName)
for i in range(numImages):
assert(predictions[i][-1]==tfInferenceArgmaxOutputs[i])
for j in range(topK):
assert(predictions[i][j] is not None)
(top1Acc, topKAcc) = calculateAccuracy(predictions)
print(top1Acc, topKAcc)
# with open('./ImageNet_ValData/Random_Img_Idx_25000.txt', 'r') as ff:
# lines = ff.readlines()
# i25kSelected = list(map(lambda x : int(x.rstrip())-1, lines))
# i25kSelectedCorrectTop1 = 0
# i25kSelectedCorrectTop5 = 0
# i25kNotSelectedCorrectTop1 = 0
# i25kNotSelectedCorrectTop5 = 0
# for idx in i25kSelected:
# if (groundTruthLabels[idx] == predictions[idx][-1]):
# i25kSelectedCorrectTop1 += 1
# if (groundTruthLabels[idx] in predictions[idx]):
# i25kSelectedCorrectTop5 += 1
# for idx in range(numImages):
# if idx not in i25kSelected:
# if (groundTruthLabels[idx] == predictions[idx][-1]):
# i25kNotSelectedCorrectTop1 += 1
# if (groundTruthLabels[idx] in predictions[idx]):
# i25kNotSelectedCorrectTop5 += 1
# assert(i25kSelectedCorrectTop1 + i25kNotSelectedCorrectTop1 == (top1Acc*numImages))
# assert(i25kSelectedCorrectTop5 + i25kNotSelectedCorrectTop5 == (topKAcc*numImages))
# print(i25kSelectedCorrectTop1, i25kSelectedCorrectTop5)
# print(i25kNotSelectedCorrectTop1, i25kNotSelectedCorrectTop5)
# print(i25kSelectedCorrectTop1/(1.0*25000), i25kSelectedCorrectTop5/(1.0*25000))
# print(i25kNotSelectedCorrectTop1/(1.0*25000), i25kNotSelectedCorrectTop5/(1.0*25000))
*
*/
!.gitignore
\ No newline at end of file
#Prepare imagenet validation set
ImageNetValidationSetUrl="http://www.image-net.org/challenges/LSVRC/2012/nnoupb/ILSVRC2012_img_val.tar"
ImageNetValidationSetBBoxUrl="http://www.image-net.org/challenges/LSVRC/2012/nnoupb/ILSVRC2012_bbox_val_v3.tgz"
ImageNetValidationSetSynSetLabels="https://raw.githubusercontent.com/tensorflow/models/master/research/inception/inception/data/imagenet_2012_validation_synset_labels.txt"
axel -a -n 3 -c --output ImageNet_ValData "$ImageNetValidationSetUrl"
axel -a -n 3 -c --output ImageNet_ValData "$ImageNetValidationSetBBoxUrl"
axel -a -n 3 -c --output ImageNet_ValData "$ImageNetValidationSetSynSetLabels"
cd ImageNet_ValData
mkdir img
tar -xvf ILSVRC2012_img_val.tar --directory=img
tar -xvzf ILSVRC2012_bbox_val_v3.tgz
mv val bbox
cd ..
python3 Convert_WnId_To_TrainId.py
# Choose a random subset of SELECT_IMAGES from TOTAL_IMAGES
import random
TOTAL_IMAGES = 50000 #ImageNet validation dataset size is 50k images
SELECT_IMAGES = 1000 #Choose a random subset of 1k images
with open('./ImageNet_ValData/imagenet12_val_nlabels.txt', 'r') as ff:
labels = ff.readlines()
idxSelected = random.sample(range(TOTAL_IMAGES), SELECT_IMAGES)
labelSelected = [labels[i] for i in idxSelected]
with open("./ImageNet_ValData/Random_Img_Idx.txt", 'w') as ff:
for x in idxSelected:
ff.write(str(x+1)+"\n") #make it 1-indexed as image numbers are 1 indexed
with open("./ImageNet_ValData/Random_Img_Labels.txt", "w") as ff:
for x in labelSelected:
ff.write(str(x))
#!/bin/bash
NUMIMAGES=96
NUMPROCESSES=16
PERPROCESSIMAGES=$((NUMIMAGES / NUMPROCESSES))
ScalesToTest=(18 19 20 21 22 23 24 25 26 27)
NetworkName="ResNet"
RANDOMSUBSETFILEIDX="./ImageNet_ValData/Random_Img_Idx.txt"
PreProcessedFilesDirectory="../Networks/$NetworkName/AccuracyAnalysisHelper/PreProcessedImages"
PorthosDir="../../SecureNN"
mapfile -t randomIndices < "$RANDOMSUBSETFILEIDX"
mkdir -p InferenceOutputs
mkdir -p InferenceErrors
declare -a pids
for curScale in "${ScalesToTest[@]}";
do
echo -e "******************SCALE = $curScale************************\n\n"
echo -e "Replacing this scale factor in globals.h."
sed -i -r 's/#define FLOAT_PRECISION .*/#define FLOAT_PRECISION '"${curScale}"'/' src/globals.h
echo -e "Doing make..."
make -j > /dev/null
echo -e "make done. Continuing..."
ModelFilePath="../Athos/Networks/$NetworkName/AccuracyAnalysisHelper/$NetworkName"'_'"img_input_weights_float_scaled"'_'"$curScale.inp"
echo -e "Using model path = $ModelFilePath."
imgCounter=0
while [ "${imgCounter}" -ne "${NUMIMAGES}" ];
do
echo -e "########Starting imgCounter = $imgCounter."
for ((curProcessNum=0;curProcessNum<$NUMPROCESSES;curProcessNum++)); do
echo -e "Running Porthos for imgCounter = $imgCounter, actual img idx = ${randomIndices[${imgCounter}]}."
imgFileName="$PreProcessedFilesDirectory/ImageNum"'_'"${randomIndices[${imgCounter}]}"'_'"scaled"'_'"${curScale}.inp"
stdoutFilesSuffix="./InferenceOutputs/stdout"'_'"${curScale}"'_'"${imgCounter}"'_'"proc"'_'
stderrFilesSuffix="./InferenceErrors/stderr"'_'"${curScale}"'_'"${imgCounter}"'_'"proc"'_'
./Porthos.out 3PC 0 files/parties_localhost files/keyA files/keyAB "$curProcessNum" "${imgCounter}" < "$ModelFilePath" > "${stdoutFilesSuffix}0.outp" 2> "${stderrFilesSuffix}0.outp" &
pids+=($!)
./Porthos.out 3PC 1 files/parties_localhost files/keyB files/keyAB "$curProcessNum" "${imgCounter}" < "$imgFileName" > "${stdoutFilesSuffix}1.outp" 2> "${stderrFilesSuffix}1.outp" &
pids+=($!)
./Porthos.out 3PC 2 files/parties_localhost files/keyB files/keyAB "$curProcessNum" "${imgCounter}" > "${stdoutFilesSuffix}2.outp" 2> "${stderrFilesSuffix}2.outp" &
pids+=($!)
((imgCounter=imgCounter+1))
if [ "${imgCounter}" -eq "${NUMIMAGES}" ]; then
break
fi
done
echo -e "--->>>All processes started. Now going into waiting for each process.<<<---"
for pid in ${pids[*]}; do
wait $pid
done
echo -e "--->>> Done waiting for all started processes. Unsetting Pids and starting next loop. <<<---"
unset pids
done
done
echo -e "--->>>All processes completed.<<<---"
import os, sys
numImages = 10
newScaledFileNameSuffix = '_scaled_'
scalingFactors = [12]
if (len(sys.argv)!=3):
print("Incorrect args: Run as python3 Scale_img_and_model.py <model file name> <float img directory>", file=sys.stderr)
exit(1)
modelFileName = sys.argv[1]
floatImgDir = sys.argv[2]
randomImgIdxFileName = "./ImageNet_ValData/Random_Img_Idx.txt"
def checkIfFileExists(filename):
if not(os.path.exists(filename)):
print("Expected file doesn't exist. Error. FileName = {0}.".format(filename), file=sys.stderr)
exit(1)
checkIfFileExists(randomImgIdxFileName)
with open(randomImgIdxFileName, 'r') as ff:
imgIdx = ff.readlines()
imgIdx = list(map(lambda x: int(x.rstrip()) , imgIdx))
def scaleImg(filename, scalingFac):
checkIfFileExists(filename)
with open(filename, 'r') as ff:
line = ff.readline()
val = line.split()
val = list(map(lambda x : int(float(x)*(1<<scalingFac)), val))
path = (os.path.splitext(filename)[0])
with open(path + newScaledFileNameSuffix + str(scalingFac) + '.inp', 'w') as ff:
for elem in val:
ff.write(str(elem) + ' ')
def scaleModel(modelFileName, scalingFac):
checkIfFileExists(modelFileName)
path = os.path.splitext(modelFileName)[0]
with open(path + newScaledFileNameSuffix + str(scalingFac) + '.inp', 'w') as newff:
with open(modelFileName, 'r') as ff:
line = ff.readline()
while line:
if (line != ''):
val = line.split()
val = list(map(lambda x : int(float(x)*(1<<scalingFac)), val))
for elem in val:
newff.write(str(elem) + ' ')
newff.write('\n')
line = ff.readline()
for curScale in scalingFactors:
print("Starting for scale factor = {0}.".format(curScale))
for i in range(numImages):
if (i % 100 == 0):
print("Processing img number = {0} for scale factor = {1}.".format(i, curScale))
floatFileName = os.path.join(floatImgDir, 'ImageNum_'+str(imgIdx[i])+'.inp')
scaleImg(floatFileName, curScale)
print("All images processed. Starting processing of model.")
scaleModel(modelFileName, curScale)
print("All done.")
#!/bin/bash
cifar10DownloadLink="https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz"
axel -a -n 3 -c --output CIFAR10 "$cifar10DownloadLink"
cd CIFAR10
tar -xvzf cifar-10-python.tar.gz --directory=.
import sys
filename = sys.argv[1]
scalingFac = 12
maxibits = -1
bitsdict = {}
with open(filename, 'r') as ff:
line = ff.readline()
while line:
if not(line.startswith("Matmul")):
val = line.split()
val = list(map(lambda x : int(x), val))
for elem in val:
curbits = len(bin(elem))-2
if curbits in bitsdict:
bitsdict[curbits] += 1
else:
bitsdict[curbits] = 0
if (curbits > maxibits):
maxibits = curbits
line = ff.readline()
print(maxibits)
summ = 0
for k in sorted(bitsdict.keys()):
print(k,bitsdict[k])
summ+=bitsdict[k]
print("summ = ", summ)
prob = 0
for k in sorted(bitsdict.keys()):
curprob = bitsdict[k]/(1<<(64-k-1))
print("curprob ",k,curprob)
prob += curprob
print(prob)
import cv2, numpy, sys, os, argparse, time
import tensorflow as tf
sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', 'TFCompiler'))
import DumpTFMtData
def get_preprocessed_image(filename):
resized_height = 224
resized_width = 224
test_image_path = filename
cv2_image = cv2.resize(cv2.imread(test_image_path), (resized_height, resized_width))#.astype(numpy.float32)
cv2_image = cv2_image - numpy.min(cv2_image)
cv2_image = cv2_image/numpy.ptp(cv2_image)
cv2_image = 255*cv2_image
cv2_image = cv2_image.astype('uint8')
return cv2_image
def parseArgs():
parser = argparse.ArgumentParser()
parser.add_argument("--savePreTrainedWeightsInt", type=bool, default=False, help="savePreTrainedWeightsInt")
parser.add_argument("--savePreTrainedWeightsFloat", type=bool, default=False, help="savePreTrainedWeightsFloat")
parser.add_argument("--scalingFac", type=int, default=15, help="scalingFac")
parser.add_argument("--runPrediction", type=bool, default=False, help="runPrediction")
parser.add_argument("--saveImgAndWtData", type=bool, default=False, help="saveImgAndWtData")
args = parser.parse_args()
return args
args = parseArgs()
imagesTemp = get_preprocessed_image('./SampleImages/00014251_029.png')
images = numpy.zeros(shape=(1,224,224,3))
images[0] = imagesTemp
feed_dict={'input_1:0' : images}
with tf.Session() as sess:
saver = tf.train.import_meta_graph("./PreTrainedModel/TFModel/model.meta")
sess.run(tf.global_variables_initializer())
# Find output tensor
output_tensor = None
gg = tf.get_default_graph()
for node in gg.as_graph_def().node:
if node.name == 'dense_1/Sigmoid':
output_tensor = gg.get_operation_by_name(node.name).outputs[0]
assert(output_tensor is not None)
optimized_graph_def = DumpTFMtData.save_graph_metadata(output_tensor, sess, feed_dict)
if args.savePreTrainedWeightsInt or args.savePreTrainedWeightsFloat or args.runPrediction or args.saveImgAndWtData:
saver.restore(sess, "./PreTrainedModel/TFModel/model")
if args.savePreTrainedWeightsInt or args.savePreTrainedWeightsFloat or args.saveImgAndWtData:
DumpTFMtData.updateWeightsForBN(optimized_graph_def, sess, feed_dict)
predictions = None
if args.runPrediction:
print("*************** Starting Prediction****************")
start_time = time.time()
predictions = sess.run(output_tensor, feed_dict=feed_dict)
end_time = time.time()
print("*************** Done Prediction****************")
print(predictions)
trainVarsName = []
for node in optimized_graph_def.node:
if node.op=="VariableV2":
trainVarsName.append(node.name)
trainVars = list(map(lambda x : tf.get_default_graph().get_operation_by_name(x).outputs[0] , trainVarsName))
if args.saveImgAndWtData:
DumpTFMtData.dumpImgAndWeightsData(sess, images[0], trainVars, 'ChestXRay_img_{0}_input.inp'.format(args.scalingFac), args.scalingFac)
if args.savePreTrainedWeightsInt:
DumpTFMtData.dumpTrainedWeights(sess, trainVars, 'ChestXRay_img_input_weights_{0}_int.inp'.format(args.scalingFac), args.scalingFac, 'w')
if args.savePreTrainedWeightsFloat:
DumpTFMtData.dumpTrainedWeightsFloat(sess, trainVars, 'ChestXRay_img_input_weights_float.inp', 'w')
\ No newline at end of file
*
*/
!.gitignore
!KerasModel
!TFModel
\ No newline at end of file
*
*/
!.gitignore
\ No newline at end of file
*
*/
!.gitignore
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment