Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import skvideo.io
- import skvideo.datasets
- import tensorflow as tf
- from tensorflow.keras.applications.resnet50 import ResNet50
- from tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions
- # enabling eager execution for easier explanation
- tf.enable_eager_execution()
- model = ResNet50(weights='imagenet')
- reader = skvideo.io.FFmpegReader(skvideo.datasets.bigbuckbunny(),
- inputdict={},
- outputdict={})
- def gen_frames():
- for frame in reader.nextFrame():
- yield frame
- dataset = tf.data.Dataset.from_generator(gen_frames, tf.int64)
- def preprocess(frame):
- x = tf.image.resize_bilinear(frame, [224, 224])
- x = preprocess_input(x)
- return x
- dataset = dataset.batch(64).map(preprocess, 10).prefetch(1)
- def predict():
- with tf.device("/gpu:0"):
- for frames in dataset:
- yield model.predict(frames.numpy())
- dataset2 = tf.data.Dataset.from_generator(predict, tf.float64)
- def postprocess(output):
- # do some post processing
- return tf.argsort(output)[:3]
- dataset2 = dataset2.map(postprocess, 10)
- # unbatch the output if needed
- # dataset2 = dataset2.apply(tf.data.experimental.unbatch())
- for value in dataset2:
- print(decode_predictions(value.numpy()))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement