This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun classifySequence ( sequence : IntArray ): FloatArray { | |
val interpreter = Interpreter( loadModelFile() ) | |
val inputs : Array<FloatArray> = arrayOf( sequence.map{ it.toFloat() }.toFloatArray() ) | |
val outputs : Array<FloatArray> = arrayOf( floatArrayOf( 0.0f , 0.0f ) ) | |
interpreter.run( inputs , outputs ) | |
return outputs[0] | |
} | |
val tokenizedMessage = tokenize( message_text.text.toString().toLowerCase().trim() ) | |
val paddedMessage = padSequence( tokenizedMessage ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Throws(IOException::class) | |
private fun loadModelFile(): MappedByteBuffer { | |
val MODEL_ASSETS_PATH = "model.tflite" | |
val assetFileDescriptor = assets.openFd(MODEL_ASSETS_PATH) | |
val fileInputStream = FileInputStream(assetFileDescriptor.getFileDescriptor()) | |
val fileChannel = fileInputStream.getChannel() | |
val startoffset = assetFileDescriptor.getStartOffset() | |
val declaredLength = assetFileDescriptor.getDeclaredLength() | |
return fileChannel.map(FileChannel.MapMode.READ_ONLY, startoffset, declaredLength) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tensorflow.keras.backend as K | |
input_shape = ( (DIMEN**2) * 3 , ) | |
convolution_shape = ( DIMEN , DIMEN , 3 ) | |
kernel_size_1 = ( 8 , 8 ) | |
kernel_size_2 = ( 6 , 6 ) | |
kernel_size_3 = ( 4 , 4 ) | |
pool_size_1 = ( 6 , 6 ) | |
pool_size_2 = ( 4 , 4 ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
model.save( 'model.h5' ) | |
converter = tf.lite.TFLiteConverter.from_keras_model_file( 'model.h5' ) | |
converter.post_training_quantize = True | |
tflite_model = converter.convert() | |
size = open( 'recog_model.tflite' , 'wb' ).write( tflite_model ) | |
print( 'Model size > {} Megabytes'.format( size / 1024**2)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public float getSimilarity ( Bitmap image1 , Bitmap image2 ) { | |
try { | |
Interpreter interpreter = new Interpreter( loadModelFile() ) ; | |
int INPUT_DIM = 32; | |
float[][] x1 = new float[][]{mapBitmapToFloatArray( resizeBitmap( image1 , INPUT_DIM) , INPUT_DIM) }; | |
float[][] x2 = new float[][]{mapBitmapToFloatArray( resizeBitmap( image2 , INPUT_DIM) , INPUT_DIM) }; | |
Object[] inputs = { x1 , x2 } ; | |
Map<Integer, Object> outputs = new HashMap<>(); | |
outputs.put(0, new float[1][1] ); | |
interpreter.runForMultipleInputsOutputs( inputs , outputs ) ; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Vocabulary { | |
companion object { | |
val positiveBagOfWords = arrayOf( ... ) // Some words here | |
val negativeBagOfWords = arrayOf( ... ) // Some words here | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
companion object { | |
val CLASS_POSITIVE = 0 // Spam | |
val CLASS_NEGATIVE = 1 // Ham | |
private val englishStopWords = arrayOf( | |
"i", "me", "my", "myself", "we", "our", "ours", "ourselves", "you", "your", "yours", "yourself", "yourselves", | |
"he", "him", "his", "himself", "she", "her", "hers", "herself", "it", "its", "itself", "they", "them", "their", | |
"theirs", "themselves", "what", "which", "who", "whom", "this", "that", "these", "those", "am", "is", "are", "was", | |
"were", "be", "been", "being", "have", "has", "had", "having", "do", "does", "did", "doing", "a", "an", "the", | |
"and", "but", "if", "or", "because", "as", "until", "while", "of", "at", "by", "for", "with", "about", "against", | |
"between", "into", "through", "during", "before", "after", "above", "below", "to", "from", "up", "down", "in", |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private fun findClassProbability(classVocab1 : Array<String>, classVocab2 : Array<String> , classVocab : Array<String>) : Float{ | |
val total_words = (classVocab1.count() + classVocab2.count()).toFloat() | |
val class_count = (classVocab.count()).toFloat() | |
return ( class_count / total_words ) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private fun findProbabilityGivenClass( x : String , classVocab : Array<String> ) : Float { | |
val x_count = classVocab.count { it.contains(x) or x.contains(it) }.toFloat() | |
val class_count = classVocab.count().toFloat() | |
return (( x_count / class_count ) + 1) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private fun findProbabilityGivenSample( document : String , classVocab : Array<String> ) : Float { | |
val tokens = getTokens( document ) | |
var probability_given_class = 1.0f | |
for ( token in tokens ) { | |
probability_given_class *= findProbabilityGivenClass( token , classVocab ) | |
} | |
return probability_given_class * findClassProbability( positiveBagOfWords , negativeBagOfWords , classVocab ) | |
} |