-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessing.py
More file actions
31 lines (25 loc) · 1.17 KB
/
Copy pathprocessing.py
File metadata and controls
31 lines (25 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import tensorflow as tf
import re
# Text processing
strip_chars = "!\"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"
strip_chars = strip_chars.replace("<", "")
strip_chars = strip_chars.replace(">", "")
def custom_standardization(input_string):
lowercase = tf.strings.lower(input_string)
return tf.strings.regex_replace(lowercase, "[%s]" % re.escape(strip_chars), "")
# Image processing
def decode_and_resize(img_path, image_size):
img = tf.io.read_file(img_path)
img = tf.image.decode_jpeg(img, channels=3)
img = tf.image.resize(img, image_size)
img = tf.image.convert_image_dtype(img, tf.float32)
return img
def process_input(img_path, captions, image_size, vectorization):
return decode_and_resize(img_path, image_size), vectorization(captions)
def make_dataset(images, captions, image_size, vectorization, batch_size, concurrency):
dataset = tf.data.Dataset.from_tensor_slices((images, captions))
dataset = dataset.shuffle(len(images))
dataset = dataset.map(lambda x, y: process_input(x, y, image_size, vectorization),
num_parallel_calls=concurrency)
dataset = dataset.batch(batch_size).prefetch(concurrency)
return dataset