Restoring a model trained with tf.estimator and feeding input through feed_dict

前端 未结 2 466
既然无缘
既然无缘 2021-01-20 10:17

I trained a resnet with tf.estimator, the model was saved during the training process. The saved files consist of .data, .index and .meta

2条回答
  •  礼貌的吻别
    2021-01-20 11:03

    If you have model pb or pb.txt then inference is easy. Using predictor module, we can do an inference. Check out here for more information. For image data it will be something to similar to below example. Hope this helps !!

    Example code:

    import numpy as np
    import matplotlib.pyplot as plt
    
    def extract_data(index=0, filepath='data/cifar-10-batches-bin/data_batch_5.bin'):
        bytestream = open(filepath, mode='rb')
        label_bytes_length = 1
        image_bytes_length = (32 ** 2) * 3
        record_bytes_length = label_bytes_length + image_bytes_length
        bytestream.seek(record_bytes_length * index, 0)
        label_bytes = bytestream.read(label_bytes_length)
        image_bytes = bytestream.read(image_bytes_length)
        label = np.frombuffer(label_bytes, dtype=np.uint8)  
        image = np.frombuffer(image_bytes, dtype=np.uint8)
        image = np.reshape(image, [3, 32, 32])
        image = np.transpose(image, [1, 2, 0])
        image = image.astype(np.float32)
       result = {
         'image': image,
         'label': label,
       }
       bytestream.close()
       return result
    
    
        predictor_fn = tf.contrib.predictor.from_saved_model(
      export_dir = saved_model_dir, signature_def_key='predictions')
        N = 1000
        labels = []
        images = []
        for i in range(N):
           result = extract_data(i)
           images.append(result['image'])
           labels.append(result['label'][0])
        output = predictor_fn(
          {
            'images': images,
          }
        )
    

提交回复
热议问题