johnolafenwa / Pytorch-Keras-ToAndroid

MIT License
107 stars 41 forks source link

Model always predicting 100% for one class in Android #1

Open PohSayKeong opened 6 years ago

PohSayKeong commented 6 years ago

Hi I followed the guide and converted keras model to android but the prediction is always 100% for one class.

Code used to train model

from imageai.Prediction.Custom import ModelTraining
import tensorflow as tf 
model_trainer = ModelTraining()  
model_trainer.setModelTypeAsResNet()  
model_trainer.setDataDirectory("glasses")  
model_trainer.trainModel(num_objects=2, num_experiments=100, enhance_data=True, batch_size=36, show_network_summary=True)

Code used to convert to Tensorflow pb file:

from keras.models import load_model
from keras.applications.resnet50 import ResNet50
from keras.layers import *
import os
import tensorflow as tf
def keras_to_tensorflow(keras_model, output_dir, model_name,out_prefix="output_", log_tensorboard=True):
    if os.path.exists(output_dir) == False:
        os.mkdir(output_dir)

    out_nodes = []

    for i in range(len(keras_model.outputs)):
        out_nodes.append(out_prefix + str(i + 1))
        tf.identity(keras_model.output[i], out_prefix + str(i + 1))

    sess = K.get_session()

    from tensorflow.python.framework import graph_util, graph_io

    init_graph = sess.graph.as_graph_def()

    main_graph = graph_util.convert_variables_to_constants(sess, init_graph, out_nodes)

    graph_io.write_graph(main_graph, output_dir, name=model_name, as_text=False)

    if log_tensorboard:
        from tensorflow.python.tools import import_pb_to_tensorboard

        import_pb_to_tensorboard.import_to_tensorboard(
            os.path.join(output_dir, model_name),
            output_dir)

model = ResNet50(include_top=True, weights='model_ex-028_acc-0.808081.h5', input_tensor=None, input_shape=None, pooling=None, classes=2)
output_dir = os.path.join(os.getcwd(),"checkpoint")

keras_to_tensorflow(model,output_dir=output_dir,model_name="glasses.pb")

Android side code can be found in this repository: https://github.com/Cragios/AICare

iamNCJ commented 5 years ago

I came with the same issue when trying to convert my Keras model to a TF one

iamNCJ commented 5 years ago

I fixed it Check this: https://github.com/googlecodelabs/tensorflow-for-poets-2/issues/2 and this https://github.com/googlecodelabs/tensorflow-for-poets-2/blob/01e5a68183d158c5b04944e8f44674eeaa91f679/android/src/org/tensorflow/demo/ClassifierActivity.java#L61

Junhyuk commented 5 years ago

@iamNCJ Hell NCJ , Could you please provide me with more hints how you fixed this issue ??

iamNCJ commented 5 years ago

@Junhyuk Sure I trained my Keras model from the Inception v3, and converted it into a tf one. Then, I checked the output and input layer of my new model and replaced the names in the Android code with mine. After that, I modified the parameters in the predict()

Bitmap resized_image = ImageUtils.processBitmap(bitmap,299);

                //Normalize the pixels
                floatValues = ImageUtils.normalizeBitmap(resized_image,299,128,128.0f); // changed for Inception V3

                //Pass input into the tensorflow
                tf.feed(INPUT_NAME,floatValues,1,299,299,3);

But all these changes are for my model, so you have to change these parameters to fit yours. Hope these are useful to you.