You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
These are step to run a keras model you've created from this repository on your smartphone under IOS. This example uses resnet_family model with the cats_vs_dogs dataset:
First train your model with your dataset using on of the cv attention models: python train_script.py -m resnet_family.RegNetZD -d cats_vs_dogs -b 8 --pretrained imagenet -i 256
This may create a model named:
checkpoints/resnet_family.RegNetZD32_LAMB_cats_vs_dogs_batchsize_8_randaug_6_mixup_0.1_cutmix_1.0_RRC_0.08_lr512_0.008_wd_0.02_epoch_6_val_acc_0.9985.h5
You can then quantize it into a tflite file as follows:
import tensorflow as tf
from keras_cv_attention_models import regnet, resnet_family, nfnets, cotnet, beit
from keras_cv_attention_models.imagenet import data
from PIL import Image
import glob
import numpy as np
from keras_cv_attention_models import model_surgery
cotnet.set_global_tpu_test(True)
mm = resnet_family.RegNetZD32(num_classes=2,pretrained=None)
mm.load_weights('checkpoints/resnet_family.RegNetZD32_LAMB_cats_vs_dogs_batchsize_8_randaug_6_mixup_0.1_cutmix_1.0_RRC_0.08_lr512_0.008_wd_0.02_epoch_6_val_acc_0.9985.h5')
mm = model_surgery.convert_groups_conv2d_2_split_conv2d(mm) #, drop_rate=(0, 0.2))
print("ll",mm.input.shape, mm.input.dtype)
# Default one
converter = tf.lite.TFLiteConverter.from_keras_model(mm)
converter.optimizations = [tf.lite.Optimize.DEFAULT]
#converter.experimental_new_converter=True
converter.target_spec.supported_types = [tf.int8]
converter.target_spec.supported_ops = [
#tf.lite.OpsSet.TFLITE_BUILTINS,
tf.lite.OpsSet.SELECT_TF_OPS,
tf.lite.OpsSet.TFLITE_BUILTINS_INT8
]
#converter.target_spec.supported_ops = [tf.lite.OpsSet.EXPERIMENTAL_TFLITE_BUILTINS_ACTIVATIONS_INT16_WEIGHTS_INT8,
#tf.lite.OpsSet.TFLITE_BUILTINS]
def representative_dataset_gen():
global mm
files=glob.glob('/home/ubuntu/efs/diabetic_macular_oedematrain/0.00__negative/*')
a = []
for fl in files:
img = np.array(Image.open(fl).resize((mm.input.shape[1],mm.input.shape[2])), dtype="float32") #dtype=mm.input.dtype)
img = img / 255.0
a.append(img)
a = np.array(a) #, dtype=mm.input.dtype)
print('total shape',a.shape) # a is np array of 160 3D images
img = tf.data.Dataset.from_tensor_slices(a).batch(1)
for i in img.take(100): #len(a)): #BATCH_SIZE):
print('shape',i.shape)
yield [i]
converter.representative_dataset = representative_dataset_gen
converter.inference_input_type = tf.uint8
converter.inference_output_type = tf.uint8
open("resnet_familyRegNetZD32cats_vs_dogssuint.tflite", "wb").write(converter.convert())
Then modify the pod file under examples-master/lite/examples/image_classification/ios/ImageClassification so that it includes TensorFlowLiteSelectTfOps like below
# Uncomment the next line to define a global platform for your project
platform :ios, '12.0'
target 'ImageClassification' do
# Comment the next line if you're not using Swift and don't want to use dynamic frameworks
use_frameworks!
# Pods for ImageClassification
pod 'TensorFlowLiteSwift', '2.6.0'
pod 'TensorFlowLiteSelectTfOps', '2.6.0'
end
Then install this pod: pod install
Then with the frozen model you created (eg resnet_familyRegNetZD32cats_vs_dogssuint.tflite ) replace replace the mobilenet model with resnet_family model in this dir
examples-master/lite/examples/image_classification/ios/ImageClassification/Model
Also change labels.txt to the classes in your tflite file eg cats and dogs
Also change the ModelDataHandler.swift model file to be the name of the resnet_familyRegNetZD32cats_vs_dogssuint.tflite and change the inputWidth and inputHeight parameters to be the size of images you trained on eg 256x256 instead 224x224
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
These are step to run a keras model you've created from this repository on your smartphone under IOS. This example uses resnet_family model with the cats_vs_dogs dataset:
First train your model with your dataset using on of the cv attention models:
python train_script.py -m resnet_family.RegNetZD -d cats_vs_dogs -b 8 --pretrained imagenet -i 256
This may create a model named:
checkpoints/resnet_family.RegNetZD32_LAMB_cats_vs_dogs_batchsize_8_randaug_6_mixup_0.1_cutmix_1.0_RRC_0.08_lr512_0.008_wd_0.02_epoch_6_val_acc_0.9985.h5
You can then quantize it into a tflite file as follows:
Then in OSX download
https://github.com/tensorflow/examples
Then modify the pod file under examples-master/lite/examples/image_classification/ios/ImageClassification so that it includes TensorFlowLiteSelectTfOps like below
Then install this pod:
pod install
Then with the frozen model you created (eg resnet_familyRegNetZD32cats_vs_dogssuint.tflite ) replace replace the mobilenet model with resnet_family model in this dir
examples-master/lite/examples/image_classification/ios/ImageClassification/Model
Also change labels.txt to the classes in your tflite file eg cats and dogs
Also change the ModelDataHandler.swift model file to be the name of the resnet_familyRegNetZD32cats_vs_dogssuint.tflite and change the inputWidth and inputHeight parameters to be the size of images you trained on eg 256x256 instead 224x224
The app should then run
Beta Was this translation helpful? Give feedback.
All reactions