NJU-Jet / SR_Mobile_Quantization

Winner solution of mobile AI (CVPRW 2021).
148 stars 41 forks source link

how to evaluate by pretrained pb? #11

Closed lyy-zz closed 3 years ago

lyy-zz commented 3 years ago

I try to evaluate with pre-training pb, but got an error I rarely use tf2.0, trying to read the document or google did not solve the problem Do you have any solutions? Thank you

code example

def evaluate_by_pb(model_path, save_path):
    model = tf.saved_model.load(model_path)
    model = model.signatures[tf.saved_model.DEFAULT_SERVING_SIGNATURE_DEF_KEY]
    for i in range(601, 701):
        lr_path = 'data/DIV2K/DIV2K_train_LR_bicubic/X3_pt/0{}x3.pt'.format(i)
        with open(lr_path, 'rb') as f:
            lr = pickle.load(f)
        h, w, c = lr.shape
        lr = np.expand_dims(lr, 0).astype(np.float32)
        input_tensor = tf.convert_to_tensor(lr)
        print(input_tensor.shape)
        output = model(input_tensor)
        print(output)

error TypeError: signature_wrapper(*, input_1) missing required arguments: input_1

NJU-Jet commented 3 years ago

Try to use the following codes to load keras model: import tensorflow as tf

model = tf.keras.models.load_model(model_path, custom_object={'tf': tf})

lyy-zz commented 3 years ago

The solution you provided can load the model and infer normally, thank you

image https://www.tensorflow.org/api_docs/python/tf/saved_model/load

norris9410 commented 3 years ago

Hi I am also trying to use the pretrained pb to make prediction.

the code to make prediction is:

pb_file_path = './experiment/base7_D4C28_bs16ps64_lr1e-3/best_status'
image_path = './test_images/butterfly.png'

image = cv2.imread(image_path)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = cv2.resize(image/255, (256,256))
input_data = image[np.newaxis].astype(np.float32)

model = tf.saved_model.load(pb_file_path)
output = model(input_data)`

And I got the result as shown below. The generated image seems a bit weird. For example, there are many mosaic pixels in the bottom left part. sr_mobile

Could you help me figure out why may cause this problem? I also attached butterfly image if you need. Thank you in advance. butterfly_x2_GT

NJU-Jet commented 3 years ago
  1. You can try to use tf.keras.models.load_model(model_path, custom_object={'tf': tf}) to load the saved keras model.
  2. The model is trained without normalization, that is, training with pixel range from 0 to 255. You don't need to divide 255 when loading the image.   ------------------ Original ------------------ From: @.>; Date:  Thu, Jun 17, 2021 11:58 AM To: @.>; Cc: @.>; @.>; Subject:  Re: [NJU-Jet/SR_Mobile_Quantization] how to evaluate by pretrained pb? (#11)

 

Hi I am also trying to use the pretrained pb to make prediction.

the code to make prediction is: pb_file_path = './experiment/base7_D4C28_bs16ps64_lr1e-3/best_status' image_path = './test_images/butterfly.png' image = cv2.imread(image_path) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) image = cv2.resize(image/255, (256,256)) input_data = image[np.newaxis].astype(np.float32) model = tf.saved_model.load(pb_file_path) output = model(input_data)`
And I got the result as shown below. The generated image seems a bit weird. For example, there are many mosaic pixels in the bottom left part.

Could you help me figure out why may cause this problem? I also attached butterfly image if you need. Thank you in advance.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or unsubscribe.

norris9410 commented 3 years ago

Thank you for your quick reply.

I encountered some errors that are likely due to the python/tf version inconsistent. Could you share your python and tf version info?

NJU-Jet commented 3 years ago

Python version: 3.7.10 Tensorflow version: 2.4.1  And conda environment file (tf.yaml) is provided in the github repository, you can check more details about package versions.   ------------------ Original ------------------ From: @.>; Date:  Fri, Jun 18, 2021 12:00 PM To: @.>; Cc: @.>; @.>; Subject:  Re: [NJU-Jet/SR_Mobile_Quantization] how to evaluate by pretrained pb? (#11)

 

Thank you for your quick reply.

I encountered some errors that are likely due to the python/tf version inconsistent. Could you share your python and tf version info?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or unsubscribe.

tjasmin111 commented 10 months ago

I know it has been a while, but can someone please share the evaluation snippet to test the .pb (or .tflite, whatever available) model?

I'm using this snippet (also tried the revisions suggested above), but didn't succeed :(

import tensorflow as tf
import cv2
import numpy as np

model_dir = './experiment/base7_D4C28_bs16ps64_lr1e-3/best_status/saved_model'
image_path = 'sample.png'

image = cv2.imread(image_path)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = cv2.resize(image, (256, 256))
input_data = image[np.newaxis].astype(np.float32)

model = tf.keras.models.load_model(model_dir)
output = model.predict(input_data)

output = cv2.cvtColor(output[0], cv2.COLOR_RGB2BGR)
cv2.imshow('hr', output)
cv2.waitKey(0)
cv2.destroyAllWindows()