From c0e021e0be102ddbd768291e6677a03d65ebcd2f Mon Sep 17 00:00:00 2001 From: jakmro Date: Sun, 15 Dec 2024 21:14:53 +0100 Subject: [PATCH] Add Classification model (android) --- .../swmansion/rnexecutorch/Classification.kt | 59 + .../rnexecutorch/RnExecutorchPackage.kt | 14 +- .../rnexecutorch/models/BaseModel.kt | 4 +- .../rnexecutorch/models/StyleTransferModel.kt | 16 +- .../classification/ClassificationModel.kt | 45 + .../models/classification/Constants.kt | 1004 +++++++++++++++++ .../models/classification/Utils.kt | 8 + 7 files changed, 1140 insertions(+), 10 deletions(-) create mode 100644 android/src/main/java/com/swmansion/rnexecutorch/Classification.kt create mode 100644 android/src/main/java/com/swmansion/rnexecutorch/models/classification/ClassificationModel.kt create mode 100644 android/src/main/java/com/swmansion/rnexecutorch/models/classification/Constants.kt create mode 100644 android/src/main/java/com/swmansion/rnexecutorch/models/classification/Utils.kt diff --git a/android/src/main/java/com/swmansion/rnexecutorch/Classification.kt b/android/src/main/java/com/swmansion/rnexecutorch/Classification.kt new file mode 100644 index 0000000..eb573c7 --- /dev/null +++ b/android/src/main/java/com/swmansion/rnexecutorch/Classification.kt @@ -0,0 +1,59 @@ +package com.swmansion.rnexecutorch + +import android.util.Log +import com.facebook.react.bridge.Promise +import com.facebook.react.bridge.ReactApplicationContext +import com.swmansion.rnexecutorch.models.classification.ClassificationModel +import com.swmansion.rnexecutorch.utils.ETError +import com.swmansion.rnexecutorch.utils.ImageProcessor +import org.opencv.android.OpenCVLoader +import com.facebook.react.bridge.Arguments +import com.facebook.react.bridge.WritableMap + +class Classification(reactContext: ReactApplicationContext) : + NativeClassificationSpec(reactContext) { + + private lateinit var classificationModel: ClassificationModel + + companion object { + const val NAME = "Classification" + init { + if(!OpenCVLoader.initLocal()){ + Log.d("rn_executorch", "OpenCV not loaded") + } else { + Log.d("rn_executorch", "OpenCV loaded") + } + } + } + + override fun loadModule(modelSource: String, promise: Promise) { + try { + classificationModel = ClassificationModel(reactApplicationContext) + classificationModel.loadModel(modelSource) + promise.resolve(0) + } catch (e: Exception) { + promise.reject(e.message!!, ETError.InvalidModelPath.toString()) + } + } + + override fun forward(input: String, promise: Promise) { + try { + val image = ImageProcessor.readImage(input) + val output = classificationModel.runModel(image) + + val writableMap: WritableMap = Arguments.createMap() + + for ((key, value) in output) { + writableMap.putDouble(key, value.toDouble()) + } + + promise.resolve(writableMap) + }catch(e: Exception){ + promise.reject(e.message!!, e.message) + } + } + + override fun getName(): String { + return NAME + } +} diff --git a/android/src/main/java/com/swmansion/rnexecutorch/RnExecutorchPackage.kt b/android/src/main/java/com/swmansion/rnexecutorch/RnExecutorchPackage.kt index 3b5934c..fc4ba2f 100644 --- a/android/src/main/java/com/swmansion/rnexecutorch/RnExecutorchPackage.kt +++ b/android/src/main/java/com/swmansion/rnexecutorch/RnExecutorchPackage.kt @@ -19,7 +19,10 @@ class RnExecutorchPackage : TurboReactPackage() { ETModule(reactContext) } else if (name == StyleTransfer.NAME) { StyleTransfer(reactContext) - } else { + } else if (name == Classification.NAME) { + Classification(reactContext) + } + else { null } @@ -51,6 +54,15 @@ class RnExecutorchPackage : TurboReactPackage() { false, // isCxxModule true ) + + moduleInfos[Classification.NAME] = ReactModuleInfo( + Classification.NAME, + Classification.NAME, + false, // canOverrideExistingModule + false, // needsEagerInit + false, // isCxxModule + true + ) moduleInfos } } diff --git a/android/src/main/java/com/swmansion/rnexecutorch/models/BaseModel.kt b/android/src/main/java/com/swmansion/rnexecutorch/models/BaseModel.kt index c3c60fe..4ce67bc 100644 --- a/android/src/main/java/com/swmansion/rnexecutorch/models/BaseModel.kt +++ b/android/src/main/java/com/swmansion/rnexecutorch/models/BaseModel.kt @@ -38,7 +38,7 @@ abstract class BaseModel(val context: Context) { abstract fun runModel(input: Input): Output - protected abstract fun preprocess(input: Input): Input + protected abstract fun preprocess(input: Input): EValue - protected abstract fun postprocess(input: Tensor): Output + protected abstract fun postprocess(output: Array): Output } diff --git a/android/src/main/java/com/swmansion/rnexecutorch/models/StyleTransferModel.kt b/android/src/main/java/com/swmansion/rnexecutorch/models/StyleTransferModel.kt index c108437..0a3dfee 100644 --- a/android/src/main/java/com/swmansion/rnexecutorch/models/StyleTransferModel.kt +++ b/android/src/main/java/com/swmansion/rnexecutorch/models/StyleTransferModel.kt @@ -6,6 +6,7 @@ import org.opencv.core.Mat import org.opencv.core.Size import org.opencv.imgproc.Imgproc import org.pytorch.executorch.Tensor +import org.pytorch.executorch.EValue class StyleTransferModel(reactApplicationContext: ReactApplicationContext) : BaseModel(reactApplicationContext) { @@ -19,22 +20,23 @@ class StyleTransferModel(reactApplicationContext: ReactApplicationContext) : Bas return Size(height.toDouble(), width.toDouble()) } - override fun preprocess(input: Mat): Mat { + override fun preprocess(input: Mat): EValue { originalSize = input.size() Imgproc.resize(input, input, getModelImageSize()) - return input + return ImageProcessor.matToEValue(input, module.getInputShape(0)) } - override fun postprocess(input: Tensor): Mat { + override fun postprocess(output: Array): Mat { + val tensor = output[0].toTensor() val modelShape = getModelImageSize() - val result = ImageProcessor.EValueToMat(input.dataAsFloatArray, modelShape.width.toInt(), modelShape.height.toInt()) + val result = ImageProcessor.EValueToMat(tensor.dataAsFloatArray, modelShape.width.toInt(), modelShape.height.toInt()) Imgproc.resize(result, result, originalSize) return result } override fun runModel(input: Mat): Mat { - val inputTensor = ImageProcessor.matToEValue(preprocess(input), module.getInputShape(0)) - val outputTensor = forward(inputTensor) - return postprocess(outputTensor[0].toTensor()) + val modelInput = preprocess(input) + val modelOutput = forward(modelInput) + return postprocess(modelOutput) } } diff --git a/android/src/main/java/com/swmansion/rnexecutorch/models/classification/ClassificationModel.kt b/android/src/main/java/com/swmansion/rnexecutorch/models/classification/ClassificationModel.kt new file mode 100644 index 0000000..fbaef28 --- /dev/null +++ b/android/src/main/java/com/swmansion/rnexecutorch/models/classification/ClassificationModel.kt @@ -0,0 +1,45 @@ +package com.swmansion.rnexecutorch.models.classification + +import com.facebook.react.bridge.ReactApplicationContext +import com.swmansion.rnexecutorch.utils.ImageProcessor +import org.opencv.core.Mat +import org.opencv.core.Size +import org.opencv.imgproc.Imgproc +import org.pytorch.executorch.Tensor +import org.pytorch.executorch.EValue +import com.swmansion.rnexecutorch.models.BaseModel + + +class ClassificationModel(reactApplicationContext: ReactApplicationContext) : BaseModel>(reactApplicationContext) { + private fun getModelImageSize(): Size { + val inputShape = module.getInputShape(0) + val width = inputShape[inputShape.lastIndex] + val height = inputShape[inputShape.lastIndex - 1] + + return Size(height.toDouble(), width.toDouble()) + } + + override fun preprocess(input: Mat): EValue { + Imgproc.resize(input, input, getModelImageSize()) + return ImageProcessor.matToEValue(input, module.getInputShape(0)) + } + + override fun postprocess(output: Array): Map { + val tensor = output[0].toTensor() + val probabilities = tensor.dataAsFloatArray + + val result = mutableMapOf() + + for (i in probabilities.indices) { + result[imagenet1k_v1_labels_map[i]!!] = probabilities[i] + } + + return result + } + + override fun runModel(input: Mat): Map { + val modelInput = preprocess(input) + val modelOutput = forward(modelInput) + return postprocess(modelOutput) + } +} diff --git a/android/src/main/java/com/swmansion/rnexecutorch/models/classification/Constants.kt b/android/src/main/java/com/swmansion/rnexecutorch/models/classification/Constants.kt new file mode 100644 index 0000000..e90d6a5 --- /dev/null +++ b/android/src/main/java/com/swmansion/rnexecutorch/models/classification/Constants.kt @@ -0,0 +1,1004 @@ +package com.swmansion.rnexecutorch.models.classification + +val imagenet1k_v1_labels_map: Map = mapOf( + 0 to "tench, Tinca tinca", + 1 to "goldfish, Carassius auratus", + 2 to "great white shark, white shark, man-eater, man-eating shark, Carcharodon carcharias", + 3 to "tiger shark, Galeocerdo cuvieri", + 4 to "hammerhead, hammerhead shark", + 5 to "electric ray, crampfish, numbfish, torpedo", + 6 to "stingray", + 7 to "cock", + 8 to "hen", + 9 to "ostrich, Struthio camelus", + 10 to "brambling, Fringilla montifringilla", + 11 to "goldfinch, Carduelis carduelis", + 12 to "house finch, linnet, Carpodacus mexicanus", + 13 to "junco, snowbird", + 14 to "indigo bunting, indigo finch, indigo bird, Passerina cyanea", + 15 to "robin, American robin, Turdus migratorius", + 16 to "bulbul", + 17 to "jay", + 18 to "magpie", + 19 to "chickadee", + 20 to "water ouzel, dipper", + 21 to "kite", + 22 to "bald eagle, American eagle, Haliaeetus leucocephalus", + 23 to "vulture", + 24 to "great grey owl, great gray owl, Strix nebulosa", + 25 to "European fire salamander, Salamandra salamandra", + 26 to "common newt, Triturus vulgaris", + 27 to "eft", + 28 to "spotted salamander, Ambystoma maculatum", + 29 to "axolotl, mud puppy, Ambystoma mexicanum", + 30 to "bullfrog, Rana catesbeiana", + 31 to "tree frog, tree-frog", + 32 to "tailed frog, bell toad, ribbed toad, tailed toad, Ascaphus trui", + 33 to "loggerhead, loggerhead turtle, Caretta caretta", + 34 to "leatherback turtle, leatherback, leathery turtle, Dermochelys coriacea", + 35 to "mud turtle", + 36 to "terrapin", + 37 to "box turtle, box tortoise", + 38 to "banded gecko", + 39 to "common iguana, iguana, Iguana iguana", + 40 to "American chameleon, anole, Anolis carolinensis", + 41 to "whiptail, whiptail lizard", + 42 to "agama", + 43 to "frilled lizard, Chlamydosaurus kingi", + 44 to "alligator lizard", + 45 to "Gila monster, Heloderma suspectum", + 46 to "green lizard, Lacerta viridis", + 47 to "African chameleon, Chamaeleo chamaeleon", + 48 to "Komodo dragon, Komodo lizard, dragon lizard, giant lizard, Varanus komodoensis", + 49 to "African crocodile, Nile crocodile, Crocodylus niloticus", + 50 to "American alligator, Alligator mississipiensis", + 51 to "triceratops", + 52 to "thunder snake, worm snake, Carphophis amoenus", + 53 to "ringneck snake, ring-necked snake, ring snake", + 54 to "hognose snake, puff adder, sand viper", + 55 to "green snake, grass snake", + 56 to "king snake, kingsnake", + 57 to "garter snake, grass snake", + 58 to "water snake", + 59 to "vine snake", + 60 to "night snake, Hypsiglena torquata", + 61 to "boa constrictor, Constrictor constrictor", + 62 to "rock python, rock snake, Python sebae", + 63 to "Indian cobra, Naja naja", + 64 to "green mamba", + 65 to "sea snake", + 66 to "horned viper, cerastes, sand viper, horned asp, Cerastes cornutus", + 67 to "diamondback, diamondback rattlesnake, Crotalus adamanteus", + 68 to "sidewinder, horned rattlesnake, Crotalus cerastes", + 69 to "trilobite", + 70 to "harvestman, daddy longlegs, Phalangium opilio", + 71 to "scorpion", + 72 to "black and gold garden spider, Argiope aurantia", + 73 to "barn spider, Araneus cavaticus", + 74 to "garden spider, Aranea diademata", + 75 to "black widow, Latrodectus mactans", + 76 to "tarantula", + 77 to "wolf spider, hunting spider", + 78 to "tick", + 79 to "centipede", + 80 to "black grouse", + 81 to "ptarmigan", + 82 to "ruffed grouse, partridge, Bonasa umbellus", + 83 to "prairie chicken, prairie grouse, prairie fowl", + 84 to "peacock", + 85 to "quail", + 86 to "partridge", + 87 to "African grey, African gray, Psittacus erithacus", + 88 to "macaw", + 89 to "sulphur-crested cockatoo, Kakatoe galerita, Cacatua galerita", + 90 to "lorikeet", + 91 to "coucal", + 92 to "bee eater", + 93 to "hornbill", + 94 to "hummingbird", + 95 to "jacamar", + 96 to "toucan", + 97 to "drake", + 98 to "red-breasted merganser, Mergus serrator", + 99 to "goose", + 100 to "black swan, Cygnus atratus", + 101 to "tusker", + 102 to "echidna, spiny anteater, anteater", + 103 to "platypus, duckbill, duckbilled platypus, duck-billed platypus, Ornithorhynchus anatinus", + 104 to "wallaby, brush kangaroo", + 105 to "koala, koala bear, kangaroo bear, native bear, Phascolarctos cinereus", + 106 to "wombat", + 107 to "jellyfish", + 108 to "sea anemone, anemone", + 109 to "brain coral", + 110 to "flatworm, platyhelminth", + 111 to "nematode, nematode worm, roundworm", + 112 to "conch", + 113 to "snail", + 114 to "slug", + 115 to "sea slug, nudibranch", + 116 to "chiton, coat-of-mail shell, sea cradle, polyplacophore", + 117 to "chambered nautilus, pearly nautilus, nautilus", + 118 to "Dungeness crab, Cancer magister", + 119 to "rock crab, Cancer irroratus", + 120 to "fiddler crab", + 121 to "king crab, Alaska crab, Alaskan king crab, Alaska king crab, Paralithodes camtschatica", + 122 to "American lobster, Northern lobster, Maine lobster, Homarus americanus", + 123 to "spiny lobster, langouste, rock lobster, crawfish, crayfish, sea crawfish", + 124 to "crayfish, crawfish, crawdad, crawdaddy", + 125 to "hermit crab", + 126 to "isopod", + 127 to "white stork, Ciconia ciconia", + 128 to "black stork, Ciconia nigra", + 129 to "spoonbill", + 130 to "flamingo", + 131 to "little blue heron, Egretta caerulea", + 132 to "American egret, great white heron, Egretta albus", + 133 to "bittern", + 134 to "crane", + 135 to "limpkin, Aramus pictus", + 136 to "European gallinule, Porphyrio porphyrio", + 137 to "American coot, marsh hen, mud hen, water hen, Fulica americana", + 138 to "bustard", + 139 to "ruddy turnstone, Arenaria interpres", + 140 to "red-backed sandpiper, dunlin, Erolia alpina", + 141 to "redshank, Tringa totanus", + 142 to "dowitcher", + 143 to "oystercatcher, oyster catcher", + 144 to "pelican", + 145 to "king penguin, Aptenodytes patagonica", + 146 to "albatross, mollymawk", + 147 to "grey whale, gray whale, devilfish, Eschrichtius gibbosus, Eschrichtius robustus", + 148 to "killer whale, killer, orca, grampus, sea wolf, Orcinus orca", + 149 to "dugong, Dugong dugon", + 150 to "sea lion", + 151 to "Chihuahua", + 152 to "Japanese spaniel", + 153 to "Maltese dog, Maltese terrier, Maltese", + 154 to "Pekinese, Pekingese, Peke", + 155 to "Shih-Tzu", + 156 to "Blenheim spaniel", + 157 to "papillon", + 158 to "toy terrier", + 159 to "Rhodesian ridgeback", + 160 to "Afghan hound, Afghan", + 161 to "basset, basset hound", + 162 to "beagle", + 163 to "bloodhound, sleuthhound", + 164 to "bluetick", + 165 to "black-and-tan coonhound", + 166 to "Walker hound, Walker foxhound", + 167 to "English foxhound", + 168 to "redbone", + 169 to "borzoi, Russian wolfhound", + 170 to "Irish wolfhound", + 171 to "Italian greyhound", + 172 to "whippet", + 173 to "Ibizan hound, Ibizan Podenco", + 174 to "Norwegian elkhound, elkhound", + 175 to "otterhound, otter hound", + 176 to "Saluki, gazelle hound", + 177 to "Scottish deerhound, deerhound", + 178 to "Weimaraner", + 179 to "Staffordshire bullterrier, Staffordshire bull terrier", + 180 to "American Staffordshire terrier, Staffordshire terrier, American pit bull terrier, pit bull terrier", + 181 to "Bedlington terrier", + 182 to "Border terrier", + 183 to "Kerry blue terrier", + 184 to "Irish terrier", + 185 to "Norfolk terrier", + 186 to "Norwich terrier", + 187 to "Yorkshire terrier", + 188 to "wire-haired fox terrier", + 189 to "Lakeland terrier", + 190 to "Sealyham terrier, Sealyham", + 191 to "Airedale, Airedale terrier", + 192 to "cairn, cairn terrier", + 193 to "Australian terrier", + 194 to "Dandie Dinmont, Dandie Dinmont terrier", + 195 to "Boston bull, Boston terrier", + 196 to "miniature schnauzer", + 197 to "giant schnauzer", + 198 to "standard schnauzer", + 199 to "Scotch terrier, Scottish terrier, Scottie", + 200 to "Tibetan terrier, chrysanthemum dog", + 201 to "silky terrier, Sydney silky", + 202 to "soft-coated wheaten terrier", + 203 to "West Highland white terrier", + 204 to "Lhasa, Lhasa apso", + 205 to "flat-coated retriever", + 206 to "curly-coated retriever", + 207 to "golden retriever", + 208 to "Labrador retriever", + 209 to "Chesapeake Bay retriever", + 210 to "German short-haired pointer", + 211 to "vizsla, Hungarian pointer", + 212 to "English setter", + 213 to "Irish setter, red setter", + 214 to "Gordon setter", + 215 to "Brittany spaniel", + 216 to "clumber, clumber spaniel", + 217 to "English springer, English springer spaniel", + 218 to "Welsh springer spaniel", + 219 to "cocker spaniel, English cocker spaniel, cocker", + 220 to "Sussex spaniel", + 221 to "Irish water spaniel", + 222 to "kuvasz", + 223 to "schipperke", + 224 to "groenendael", + 225 to "malinois", + 226 to "briard", + 227 to "kelpie", + 228 to "komondor", + 229 to "Old English sheepdog, bobtail", + 230 to "Shetland sheepdog, Shetland sheep dog, Shetland", + 231 to "collie", + 232 to "Border collie", + 233 to "Bouvier des Flandres, Bouviers des Flandres", + 234 to "Rottweiler", + 235 to "German shepherd, German shepherd dog, German police dog, alsatian", + 236 to "Doberman, Doberman pinscher", + 237 to "miniature pinscher", + 238 to "Greater Swiss Mountain dog", + 239 to "Bernese mountain dog", + 240 to "Appenzeller", + 241 to "EntleBucher", + 242 to "boxer", + 243 to "bull mastiff", + 244 to "Tibetan mastiff", + 245 to "French bulldog", + 246 to "Great Dane", + 247 to "Saint Bernard, St Bernard", + 248 to "Eskimo dog, husky", + 249 to "malamute, malemute, Alaskan malamute", + 250 to "Siberian husky", + 251 to "dalmatian, coach dog, carriage dog", + 252 to "affenpinscher, monkey pinscher, monkey dog", + 253 to "basenji", + 254 to "pug, pug-dog", + 255 to "Leonberg", + 256 to "Newfoundland, Newfoundland dog", + 257 to "Great Pyrenees", + 258 to "Samoyed, Samoyede", + 259 to "Pomeranian", + 260 to "chow, chow chow", + 261 to "keeshond", + 262 to "Brabancon griffon", + 263 to "Pembroke, Pembroke Welsh corgi", + 264 to "Cardigan, Cardigan Welsh corgi", + 265 to "toy poodle", + 266 to "miniature poodle", + 267 to "standard poodle", + 268 to "Mexican hairless", + 269 to "timber wolf, grey wolf, gray wolf, Canis lupus", + 270 to "white wolf, Arctic wolf, Canis lupus tundrarum", + 271 to "red wolf, maned wolf, Canis rufus, Canis niger", + 272 to "coyote, prairie wolf, brush wolf, Canis latrans", + 273 to "dingo, warrigal, warragal, Canis dingo", + 274 to "dhole, Cuon alpinus", + 275 to "African hunting dog, hyena dog, Cape hunting dog, Lycaon pictus", + 276 to "hyena, hyaena", + 277 to "red fox, Vulpes vulpes", + 278 to "kit fox, Vulpes macrotis", + 279 to "Arctic fox, white fox, Alopex lagopus", + 280 to "grey fox, gray fox, Urocyon cinereoargenteus", + 281 to "tabby, tabby cat", + 282 to "tiger cat", + 283 to "Persian cat", + 284 to "Siamese cat, Siamese", + 285 to "Egyptian cat", + 286 to "cougar, puma, catamount, mountain lion, painter, panther, Felis concolor", + 287 to "lynx, catamount", + 288 to "leopard, Panthera pardus", + 289 to "snow leopard, ounce, Panthera uncia", + 290 to "jaguar, panther, Panthera onca, Felis onca", + 291 to "lion, king of beasts, Panthera leo", + 292 to "tiger, Panthera tigris", + 293 to "cheetah, chetah, Acinonyx jubatus", + 294 to "brown bear, bruin, Ursus arctos", + 295 to "American black bear, black bear, Ursus americanus, Euarctos americanus", + 296 to "ice bear, polar bear, Ursus Maritimus, Thalarctos maritimus", + 297 to "sloth bear, Melursus ursinus, Ursus ursinus", + 298 to "mongoose", + 299 to "meerkat, mierkat", + 300 to "tiger beetle", + 301 to "ladybug, ladybeetle, lady beetle, ladybird, ladybird beetle", + 302 to "ground beetle, carabid beetle", + 303 to "long-horned beetle, longicorn, longicorn beetle", + 304 to "leaf beetle, chrysomelid", + 305 to "dung beetle", + 306 to "rhinoceros beetle", + 307 to "weevil", + 308 to "fly", + 309 to "bee", + 310 to "ant, emmet, pismire", + 311 to "grasshopper, hopper", + 312 to "cricket", + 313 to "walking stick, walkingstick, stick insect", + 314 to "cockroach, roach", + 315 to "mantis, mantid", + 316 to "cicada, cicala", + 317 to "leafhopper", + 318 to "lacewing, lacewing fly", + 319 to "dragonfly, darning needle, devil's darning needle, sewing needle, snake feeder, snake doctor, mosquito hawk, skeeter hawk", + 320 to "damselfly", + 321 to "admiral", + 322 to "ringlet, ringlet butterfly", + 323 to "monarch, monarch butterfly, milkweed butterfly, Danaus plexippus", + 324 to "cabbage butterfly", + 325 to "sulphur butterfly, sulfur butterfly", + 326 to "lycaenid, lycaenid butterfly", + 327 to "starfish, sea star", + 328 to "sea urchin", + 329 to "sea cucumber, holothurian", + 330 to "wood rabbit, cottontail, cottontail rabbit", + 331 to "hare", + 332 to "Angora, Angora rabbit", + 333 to "hamster", + 334 to "porcupine, hedgehog", + 335 to "fox squirrel, eastern fox squirrel, Sciurus niger", + 336 to "marmot", + 337 to "beaver", + 338 to "guinea pig, Cavia cobaya", + 339 to "sorrel", + 340 to "zebra", + 341 to "hog, pig, grunter, squealer, Sus scrofa", + 342 to "wild boar, boar, Sus scrofa", + 343 to "warthog", + 344 to "hippopotamus, hippo, river horse, Hippopotamus amphibius", + 345 to "ox", + 346 to "water buffalo, water ox, Asiatic buffalo, Bubalus bubalis", + 347 to "bison", + 348 to "ram, tup", + 349 to "bighorn, bighorn sheep, cimarron, Rocky Mountain bighorn, Rocky Mountain sheep, Ovis canadensis", + 350 to "ibex, Capra ibex", + 351 to "hartebeest", + 352 to "impala, Aepyceros melampus", + 353 to "gazelle", + 354 to "Arabian camel, dromedary, Camelus dromedarius", + 355 to "llama", + 356 to "weasel", + 357 to "mink", + 358 to "polecat, fitch, foulmart, foumart, Mustela putorius", + 359 to "black-footed ferret, ferret, Mustela nigripes", + 360 to "otter", + 361 to "skunk, polecat, wood pussy", + 362 to "badger", + 363 to "armadillo", + 364 to "three-toed sloth, ai, Bradypus tridactylus", + 365 to "orangutan, orang, orangutang, Pongo pygmaeus", + 366 to "gorilla, Gorilla gorilla", + 367 to "chimpanzee, chimp, Pan troglodytes", + 368 to "gibbon, Hylobates lar", + 369 to "siamang, Hylobates syndactylus, Symphalangus syndactylus", + 370 to "guenon, guenon monkey", + 371 to "patas, hussar monkey, Erythrocebus patas", + 372 to "baboon", + 373 to "macaque", + 374 to "langur", + 375 to "colobus, colobus monkey", + 376 to "proboscis monkey, Nasalis larvatus", + 377 to "marmoset", + 378 to "capuchin, ringtail, Cebus capucinus", + 379 to "howler monkey, howler", + 380 to "titi, titi monkey", + 381 to "spider monkey, Ateles geoffroyi", + 382 to "squirrel monkey, Saimiri sciureus", + 383 to "Madagascar cat, ring-tailed lemur, Lemur catta", + 384 to "indri, indris, Indri indri, Indri brevicaudatus", + 385 to "Indian elephant, Elephas maximus", + 386 to "African elephant, Loxodonta africana", + 387 to "lesser panda, red panda, panda, bear cat, cat bear, Ailurus fulgens", + 388 to "giant panda, panda, panda bear, coon bear, Ailuropoda melanoleuca", + 389 to "barracouta, snoek", + 390 to "eel", + 391 to "coho, cohoe, coho salmon, blue jack, silver salmon, Oncorhynchus kisutch", + 392 to "rock beauty, Holocanthus tricolor", + 393 to "anemone fish", + 394 to "sturgeon", + 395 to "gar, garfish, garpike, billfish, Lepisosteus osseus", + 396 to "lionfish", + 397 to "puffer, pufferfish, blowfish, globefish", + 398 to "abacus", + 399 to "abaya", + 400 to "academic gown, academic robe, judge's robe", + 401 to "accordion, piano accordion, squeeze box", + 402 to "acoustic guitar", + 403 to "aircraft carrier, carrier, flattop, attack aircraft carrier", + 404 to "airliner", + 405 to "airship, dirigible", + 406 to "altar", + 407 to "ambulance", + 408 to "amphibian, amphibious vehicle", + 409 to "analog clock", + 410 to "apiary, bee house", + 411 to "apron", + 412 to "ashcan, trash can, garbage can, wastebin, ash bin, ash-bin, ashbin, dustbin, trash barrel, trash bin", + 413 to "assault rifle, assault gun", + 414 to "backpack, back pack, knapsack, packsack, rucksack, haversack", + 415 to "bakery, bakeshop, bakehouse", + 416 to "balance beam, beam", + 417 to "balloon", + 418 to "ballpoint, ballpoint pen, ballpen, Biro", + 419 to "Band Aid", + 420 to "banjo", + 421 to "bannister, banister, balustrade, balusters, handrail", + 422 to "barbell", + 423 to "barber chair", + 424 to "barbershop", + 425 to "barn", + 426 to "barometer", + 427 to "barrel, cask", + 428 to "barrow, garden cart, lawn cart, wheelbarrow", + 429 to "baseball", + 430 to "basketball", + 431 to "bassinet", + 432 to "bassoon", + 433 to "bathing cap, swimming cap", + 434 to "bath towel", + 435 to "bathtub, bathing tub, bath, tub", + 436 to "beach wagon, station wagon, wagon, estate car, beach waggon, station waggon, waggon", + 437 to "beacon, lighthouse, beacon light, pharos", + 438 to "beaker", + 439 to "bearskin, busby, shako", + 440 to "beer bottle", + 441 to "beer glass", + 442 to "bell cote, bell cot", + 443 to "bib", + 444 to "bicycle-built-for-two, tandem bicycle, tandem", + 445 to "bikini, two-piece", + 446 to "binder, ring-binder", + 447 to "binoculars, field glasses, opera glasses", + 448 to "birdhouse", + 449 to "boathouse", + 450 to "bobsled, bobsleigh, bob", + 451 to "bolo tie, bolo, bola tie, bola", + 452 to "bonnet, poke bonnet", + 453 to "bookcase", + 454 to "bookshop, bookstore, bookstall", + 455 to "bottlecap", + 456 to "bow", + 457 to "bow tie, bow-tie, bowtie", + 458 to "brass, memorial tablet, plaque", + 459 to "brassiere, bra, bandeau", + 460 to "breakwater, groin, groyne, mole, bulwark, seawall, jetty", + 461 to "breastplate, aegis, egis", + 462 to "broom", + 463 to "bucket, pail", + 464 to "buckle", + 465 to "bulletproof vest", + 466 to "bullet train, bullet", + 467 to "butcher shop, meat market", + 468 to "cab, hack, taxi, taxicab", + 469 to "caldron, cauldron", + 470 to "candle, taper, wax light", + 471 to "cannon", + 472 to "canoe", + 473 to "can opener, tin opener", + 474 to "cardigan", + 475 to "car mirror", + 476 to "carousel, carrousel, merry-go-round, roundabout, whirligig", + 477 to "carpenter's kit, tool kit", + 478 to "carton", + 479 to "car wheel", + 480 to "cash machine, cash dispenser, automated teller machine, automatic teller machine, automated teller, automatic teller, ATM", + 481 to "cassette", + 482 to "cassette player", + 483 to "castle", + 484 to "catamaran", + 485 to "CD player", + 486 to "cello, violoncello", + 487 to "cellular telephone, cellular phone, cellphone, cell, mobile phone", + 488 to "chain", + 489 to "chainlink fence", + 490 to "chain mail, ring mail, mail, chain armor, chain armour, ring armor, ring armour", + 491 to "chain saw, chainsaw", + 492 to "chest", + 493 to "chiffonier, commode", + 494 to "chime, bell, gong", + 495 to "china cabinet, china closet", + 496 to "Christmas stocking", + 497 to "church, church building", + 498 to "cinema, movie theater, movie theatre, movie house, picture palace", + 499 to "cleaver, meat cleaver, chopper", + 500 to "cliff dwelling", + 501 to "cloak", + 502 to "clog, geta, patten, sabot", + 503 to "cocktail shaker", + 504 to "coffee mug", + 505 to "coffeepot", + 506 to "coil, spiral, volute, whorl, helix", + 507 to "combination lock", + 508 to "computer keyboard, keypad", + 509 to "confectionery, confectionary, candy store", + 510 to "container ship, containership, container vessel", + 511 to "convertible", + 512 to "corkscrew, bottle screw", + 513 to "cornet, horn, trumpet, trump", + 514 to "cowboy boot", + 515 to "cowboy hat, ten-gallon hat", + 516 to "cradle", + 517 to "crane", + 518 to "crash helmet", + 519 to "crate", + 520 to "crib, cot", + 521 to "Crock Pot", + 522 to "croquet ball", + 523 to "crutch", + 524 to "cuirass", + 525 to "dam, dike, dyke", + 526 to "desk", + 527 to "desktop computer", + 528 to "dial telephone, dial phone", + 529 to "diaper, nappy, napkin", + 530 to "digital clock", + 531 to "digital watch", + 532 to "dining table, board", + 533 to "dishrag, dishcloth", + 534 to "dishwasher, dish washer, dishwashing machine", + 535 to "disk brake, disc brake", + 536 to "dock, dockage, docking facility", + 537 to "dogsled, dog sled, dog sleigh", + 538 to "dome", + 539 to "doormat, welcome mat", + 540 to "drilling platform, offshore rig", + 541 to "drum, membranophone, tympan", + 542 to "drumstick", + 543 to "dumbbell", + 544 to "Dutch oven", + 545 to "electric fan, blower", + 546 to "electric guitar", + 547 to "electric locomotive", + 548 to "entertainment center", + 549 to "envelope", + 550 to "espresso maker", + 551 to "face powder", + 552 to "feather boa, boa", + 553 to "file, file cabinet, filing cabinet", + 554 to "fireboat", + 555 to "fire engine, fire truck", + 556 to "fire screen, fireguard", + 557 to "flagpole, flagstaff", + 558 to "flute, transverse flute", + 559 to "folding chair", + 560 to "football helmet", + 561 to "forklift", + 562 to "fountain", + 563 to "fountain pen", + 564 to "four-poster", + 565 to "freight car", + 566 to "French horn, horn", + 567 to "frying pan, frypan, skillet", + 568 to "fur coat", + 569 to "garbage truck, dustcart", + 570 to "gasmask, respirator, gas helmet", + 571 to "gas pump, gasoline pump, petrol pump, island dispenser", + 572 to "goblet", + 573 to "go-kart", + 574 to "golf ball", + 575 to "golfcart, golf cart", + 576 to "gondola", + 577 to "gong, tam-tam", + 578 to "gown", + 579 to "grand piano, grand", + 580 to "greenhouse, nursery, glasshouse", + 581 to "grille, radiator grille", + 582 to "grocery store, grocery, food market, market", + 583 to "guillotine", + 584 to "hair slide", + 585 to "hair spray", + 586 to "half track", + 587 to "hammer", + 588 to "hamper", + 589 to "hand blower, blow dryer, blow drier, hair dryer, hair drier", + 590 to "hand-held computer, hand-held microcomputer", + 591 to "handkerchief, hankie, hanky, hankey", + 592 to "hard disc, hard disk, fixed disk", + 593 to "harmonica, mouth organ, harp, mouth harp", + 594 to "harp", + 595 to "harvester, reaper", + 596 to "hatchet", + 597 to "holster", + 598 to "home theater, home theatre", + 599 to "honeycomb", + 600 to "hook, claw", + 601 to "hoopskirt, crinoline", + 602 to "horizontal bar, high bar", + 603 to "horse cart, horse-cart", + 604 to "hourglass", + 605 to "iPod", + 606 to "iron, smoothing iron", + 607 to "jack-o'-lantern", + 608 to "jean, blue jean, denim", + 609 to "jeep, landrover", + 610 to "jersey, T-shirt, tee shirt", + 611 to "jigsaw puzzle", + 612 to "jinrikisha, ricksha, rickshaw", + 613 to "joystick", + 614 to "kimono", + 615 to "knee pad", + 616 to "knot", + 617 to "lab coat, laboratory coat", + 618 to "ladle", + 619 to "lampshade, lamp shade", + 620 to "laptop, laptop computer", + 621 to "lawn mower, mower", + 622 to "lens cap, lens cover", + 623 to "letter opener, paper knife, paperknife", + 624 to "library", + 625 to "lifeboat", + 626 to "lighter, light, igniter, ignitor", + 627 to "limousine, limo", + 628 to "liner, ocean liner", + 629 to "lipstick, lip rouge", + 630 to "Loafer", + 631 to "lotion", + 632 to "loudspeaker, speaker, speaker unit, loudspeaker system, speaker system", + 633 to "loupe, jeweler's loupe", + 634 to "lumbermill, sawmill", + 635 to "magnetic compass", + 636 to "mailbag, postbag", + 637 to "mailbox, letter box", + 638 to "maillot", + 639 to "maillot, tank suit", + 640 to "manhole cover", + 641 to "maraca", + 642 to "marimba, xylophone", + 643 to "mask", + 644 to "matchstick", + 645 to "maypole", + 646 to "maze, labyrinth", + 647 to "measuring cup", + 648 to "medicine chest, medicine cabinet", + 649 to "megalith, megalithic structure", + 650 to "microphone, mike", + 651 to "microwave, microwave oven", + 652 to "military uniform", + 653 to "milk can", + 654 to "minibus", + 655 to "miniskirt, mini", + 656 to "minivan", + 657 to "missile", + 658 to "mitten", + 659 to "mixing bowl", + 660 to "mobile home, manufactured home", + 661 to "Model T", + 662 to "modem", + 663 to "monastery", + 664 to "monitor", + 665 to "moped", + 666 to "mortar", + 667 to "mortarboard", + 668 to "mosque", + 669 to "mosquito net", + 670 to "motor scooter, scooter", + 671 to "mountain bike, all-terrain bike, off-roader", + 672 to "mountain tent", + 673 to "mouse, computer mouse", + 674 to "mousetrap", + 675 to "moving van", + 676 to "muzzle", + 677 to "nail", + 678 to "neck brace", + 679 to "necklace", + 680 to "nipple", + 681 to "notebook, notebook computer", + 682 to "obelisk", + 683 to "oboe, hautboy, hautbois", + 684 to "ocarina, sweet potato", + 685 to "odometer, hodometer, mileometer, milometer", + 686 to "oil filter", + 687 to "organ, pipe organ", + 688 to "oscilloscope, scope, cathode-ray oscilloscope, CRO", + 689 to "overskirt", + 690 to "oxcart", + 691 to "oxygen mask", + 692 to "packet", + 693 to "paddle, boat paddle", + 694 to "paddlewheel, paddle wheel", + 695 to "padlock", + 696 to "paintbrush", + 697 to "pajama, pyjama, pj's, jammies", + 698 to "palace", + 699 to "panpipe, pandean pipe, syrinx", + 700 to "paper towel", + 701 to "parachute, chute", + 702 to "parallel bars, bars", + 703 to "park bench", + 704 to "parking meter", + 705 to "passenger car, coach, carriage", + 706 to "patio, terrace", + 707 to "pay-phone, pay-station", + 708 to "pedestal, plinth, footstall", + 709 to "pencil box, pencil case", + 710 to "pencil sharpener", + 711 to "perfume, essence", + 712 to "Petri dish", + 713 to "photocopier", + 714 to "pick, plectrum, plectron", + 715 to "pickelhaube", + 716 to "picket fence, paling", + 717 to "pickup, pickup truck", + 718 to "pier", + 719 to "piggy bank, penny bank", + 720 to "pill bottle", + 721 to "pillow", + 722 to "ping-pong ball", + 723 to "pinwheel", + 724 to "pirate, pirate ship", + 725 to "pitcher, ewer", + 726 to "plane, carpenter's plane, woodworking plane", + 727 to "planetarium", + 728 to "plastic bag", + 729 to "plate rack", + 730 to "plow, plough", + 731 to "plunger, plumber's helper", + 732 to "Polaroid camera, Polaroid Land camera", + 733 to "pole", + 734 to "police van, police wagon, paddy wagon, patrol wagon, wagon, black Maria", + 735 to "poncho", + 736 to "pool table, billiard table, snooker table", + 737 to "pop bottle, soda bottle", + 738 to "pot, flowerpot", + 739 to "potter's wheel", + 740 to "power drill", + 741 to "prayer rug, prayer mat", + 742 to "printer", + 743 to "prison, prison house", + 744 to "projectile, missile", + 745 to "projector", + 746 to "puck, hockey puck", + 747 to "punching bag, punch bag, punching ball, punchball", + 748 to "purse", + 749 to "quill, quill pen", + 750 to "quilt, comforter, comfort, puff", + 751 to "racer, race car, racing car", + 752 to "racket, racquet", + 753 to "radiator", + 754 to "radio, wireless", + 755 to "radio telescope, radio reflector", + 756 to "rain barrel", + 757 to "recreational vehicle, RV, R.V.", + 758 to "reel", + 759 to "reflex camera", + 760 to "refrigerator, icebox", + 761 to "remote control, remote", + 762 to "restaurant, eating house, eating place, eatery", + 763 to "revolver, six-gun, six-shooter", + 764 to "rifle", + 765 to "rocking chair, rocker", + 766 to "rotisserie", + 767 to "rubber eraser, rubber, pencil eraser", + 768 to "rugby ball", + 769 to "rule, ruler", + 770 to "running shoe", + 771 to "safe", + 772 to "safety pin", + 773 to "saltshaker, salt shaker", + 774 to "sandal", + 775 to "sarong", + 776 to "sax, saxophone", + 777 to "scabbard", + 778 to "scale, weighing machine", + 779 to "school bus", + 780 to "schooner", + 781 to "scoreboard", + 782 to "screen, CRT screen", + 783 to "screw", + 784 to "screwdriver", + 785 to "seat belt, seatbelt", + 786 to "sewing machine", + 787 to "shield, buckler", + 788 to "shoe shop, shoe-shop, shoe store", + 789 to "shoji", + 790 to "shopping basket", + 791 to "shopping cart", + 792 to "shovel", + 793 to "shower cap", + 794 to "shower curtain", + 795 to "ski", + 796 to "ski mask", + 797 to "sleeping bag", + 798 to "slide rule, slipstick", + 799 to "sliding door", + 800 to "slot, one-armed bandit", + 801 to "snorkel", + 802 to "snowmobile", + 803 to "snowplow, snowplough", + 804 to "soap dispenser", + 805 to "soccer ball", + 806 to "sock", + 807 to "solar dish, solar collector, solar furnace", + 808 to "sombrero", + 809 to "soup bowl", + 810 to "space bar", + 811 to "space heater", + 812 to "space shuttle", + 813 to "spatula", + 814 to "speedboat", + 815 to "spider web, spider's web", + 816 to "spindle", + 817 to "sports car, sport car", + 818 to "spotlight, spot", + 819 to "stage", + 820 to "steam locomotive", + 821 to "steel arch bridge", + 822 to "steel drum", + 823 to "stethoscope", + 824 to "stole", + 825 to "stone wall", + 826 to "stopwatch, stop watch", + 827 to "stove", + 828 to "strainer", + 829 to "streetcar, tram, tramcar, trolley, trolley car", + 830 to "stretcher", + 831 to "studio couch, day bed", + 832 to "stupa, tope", + 833 to "submarine, pigboat, sub, U-boat", + 834 to "suit, suit of clothes", + 835 to "sundial", + 836 to "sunglass", + 837 to "sunglasses, dark glasses, shades", + 838 to "sunscreen, sunblock, sun blocker", + 839 to "suspension bridge", + 840 to "swab, swob, mop", + 841 to "sweatshirt", + 842 to "swimming trunks, bathing trunks", + 843 to "swing", + 844 to "switch, electric switch, electrical switch", + 845 to "syringe", + 846 to "table lamp", + 847 to "tank, army tank, armored combat vehicle, armoured combat vehicle", + 848 to "tape player", + 849 to "teapot", + 850 to "teddy, teddy bear", + 851 to "television, television system", + 852 to "tennis ball", + 853 to "thatch, thatched roof", + 854 to "theater curtain, theatre curtain", + 855 to "thimble", + 856 to "thresher, thrasher, threshing machine", + 857 to "throne", + 858 to "tile roof", + 859 to "toaster", + 860 to "tobacco shop, tobacconist shop, tobacconist", + 861 to "toilet seat", + 862 to "torch", + 863 to "totem pole", + 864 to "tow truck, tow car, wrecker", + 865 to "toyshop", + 866 to "tractor", + 867 to "trailer truck, tractor trailer, trucking rig, rig, articulated lorry, semi", + 868 to "tray", + 869 to "trench coat", + 870 to "tricycle, trike, velocipede", + 871 to "trimaran", + 872 to "tripod", + 873 to "triumphal arch", + 874 to "trolleybus, trolley coach, trackless trolley", + 875 to "trombone", + 876 to "tub, vat", + 877 to "turnstile", + 878 to "typewriter keyboard", + 879 to "umbrella", + 880 to "unicycle, monocycle", + 881 to "upright, upright piano", + 882 to "vacuum, vacuum cleaner", + 883 to "vase", + 884 to "vault", + 885 to "velvet", + 886 to "vending machine", + 887 to "vestment", + 888 to "viaduct", + 889 to "violin, fiddle", + 890 to "volleyball", + 891 to "waffle iron", + 892 to "wall clock", + 893 to "wallet, billfold, notecase, pocketbook", + 894 to "wardrobe, closet, press", + 895 to "warplane, military plane", + 896 to "washbasin, handbasin, washbowl, lavabo, wash-hand basin", + 897 to "washer, automatic washer, washing machine", + 898 to "water bottle", + 899 to "water jug", + 900 to "water tower", + 901 to "whiskey jug", + 902 to "whistle", + 903 to "wig", + 904 to "window screen", + 905 to "window shade", + 906 to "Windsor tie", + 907 to "wine bottle", + 908 to "wing", + 909 to "wok", + 910 to "wooden spoon", + 911 to "wool, woolen, woollen", + 912 to "worm fence, snake fence, snake-rail fence, Virginia fence", + 913 to "wreck", + 914 to "yawl", + 915 to "yurt", + 916 to "web site, website, internet site, site", + 917 to "comic book", + 918 to "crossword puzzle, crossword", + 919 to "street sign", + 920 to "traffic light, traffic signal, stoplight", + 921 to "book jacket, dust cover, dust jacket, dust wrapper", + 922 to "menu", + 923 to "plate", + 924 to "guacamole", + 925 to "consomme", + 926 to "hot pot, hotpot", + 927 to "trifle", + 928 to "ice cream, icecream", + 929 to "ice lolly, lolly, lollipop, popsicle", + 930 to "French loaf", + 931 to "bagel, beigel", + 932 to "pretzel", + 933 to "cheeseburger", + 934 to "hotdog, hot dog, red hot", + 935 to "mashed potato", + 936 to "head cabbage", + 937 to "broccoli", + 938 to "cauliflower", + 939 to "zucchini, courgette", + 940 to "spaghetti squash", + 941 to "acorn squash", + 942 to "butternut squash", + 943 to "cucumber, cuke", + 944 to "artichoke, globe artichoke", + 945 to "bell pepper", + 946 to "cardoon", + 947 to "mushroom", + 948 to "Granny Smith", + 949 to "strawberry", + 950 to "orange", + 951 to "lemon", + 952 to "fig", + 953 to "pineapple, ananas", + 954 to "banana", + 955 to "jackfruit, jak, jack", + 956 to "custard apple", + 957 to "pomegranate", + 958 to "hay", + 959 to "carbonara", + 960 to "chocolate sauce, chocolate syrup", + 961 to "dough", + 962 to "meat loaf, meatloaf", + 963 to "pizza, pizza pie", + 964 to "potpie", + 965 to "burrito", + 966 to "red wine", + 967 to "espresso", + 968 to "cup", + 969 to "eggnog", + 970 to "alp", + 971 to "bubble", + 972 to "cliff, drop, drop-off", + 973 to "coral reef", + 974 to "geyser", + 975 to "lakeside, lakeshore", + 976 to "promontory, headland, head, foreland", + 977 to "sandbar, sand bar", + 978 to "seashore, coast, seacoast, sea-coast", + 979 to "valley, vale", + 980 to "volcano", + 981 to "ballplayer, baseball player", + 982 to "groom, bridegroom", + 983 to "scuba diver", + 984 to "rapeseed", + 985 to "daisy", + 986 to "yellow lady's slipper, yellow lady-slipper, Cypripedium calceolus, Cypripedium parviflorum", + 987 to "corn", + 988 to "acorn", + 989 to "hip, rose hip, rosehip", + 990 to "buckeye, horse chestnut, conker", + 991 to "coral fungus", + 992 to "agaric", + 993 to "gyromitra", + 994 to "stinkhorn, carrion fungus", + 995 to "earthstar", + 996 to "hen-of-the-woods, hen of the woods, Polyporus frondosus, Grifola frondosa", + 997 to "bolete", + 998 to "ear, spike, capitulum", + 999 to "toilet tissue, toilet paper, bathroom tissue" +) diff --git a/android/src/main/java/com/swmansion/rnexecutorch/models/classification/Utils.kt b/android/src/main/java/com/swmansion/rnexecutorch/models/classification/Utils.kt new file mode 100644 index 0000000..d40ba26 --- /dev/null +++ b/android/src/main/java/com/swmansion/rnexecutorch/models/classification/Utils.kt @@ -0,0 +1,8 @@ +package com.swmansion.rnexecutorch.models.classification + +fun softmax(x: Array): Array { + val max = x.maxOrNull()!! + val exps = x.map { kotlin.math.exp(it - max) } + val sum = exps.sum() + return exps.map { it / sum }.toTypedArray() +} \ No newline at end of file