This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def create_example(image, path, example): | |
feature = { | |
"image": image_feature(image), | |
"path": bytes_feature(path), | |
"area": float_feature(example["area"]), | |
"bbox": float_feature_list(example["bbox"]), | |
"category_id": int64_feature(example["category_id"]), | |
"id": int64_feature(example["id"]), | |
"image_id": int64_feature(example["image_id"]), | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def image_feature(value): | |
"""Returns a bytes_list from a string / byte.""" | |
return tf.train.Feature( | |
bytes_list=tf.train.BytesList(value=[tf.io.encode_jpeg(value).numpy()]) | |
) | |
def bytes_feature(value): | |
"""Returns a bytes_list from a string / byte.""" | |
return tf.train.Feature( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
num_samples = 4096 | |
num_tfrecods = len(annotations) // num_samples | |
if len(annotations) % num_samples: | |
num_tfrecods += 1 # add one record if there are any remaining samples | |
if not os.path.exists(tfrecords_dir): | |
os.makedirs(tfrecords_dir) # creating TFRecords output folder |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
with tracer.start_span("get-price") as span: | |
coins = {} | |
res = requests.get("https://api.coincap.io/v2/assets") | |
span.set_tag("count", len(res.json()['data'])) | |
for coin in res.json()['data']: | |
with tracer.start_span(coin['id'], child_of=span) as coin_span: | |
print(f"Getting info for {coin['id']}") | |
try: | |
res = requests.get(f"https://api.coincap.io/v2/assets?search={coin['id']}") | |
print(f"The price of {coin['id']} is {coin['priceUsd']}") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
import requests | |
from jaeger_client import Config | |
def init_tracer(service): | |
logging.getLogger('').handlers = [] | |
logging.basicConfig(format='%(message)s', level=logging.DEBUG) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for epoch in range(15): | |
train_loss.reset_states() | |
train_accuracy.reset_states() | |
test_loss.reset_states() | |
test_accuracy.reset_states() | |
for images, labels in train_ds: | |
train_step(images, labels) | |
for test_images, test_labels in test_ds: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
loss_fn = keras.losses.CategoricalCrossentropy() | |
optimizer = keras.optimizers.Adam() | |
train_loss = keras.metrics.Mean(name='train_loss') | |
train_accuracy = keras.metrics.CategoricalAccuracy(name='train_accuracy') | |
test_loss = keras.metrics.Mean(name='test_loss') | |
test_accuracy = keras.metrics.CategoricalAccuracy(name='test_accuracy') | |
def train_step(images, labels): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MNIST(keras.Model): | |
def __init__(self): | |
super().__init__() | |
self.conv_1 = layers.Conv2D(32, kernel_size=(3, 3), activation="relu") | |
self.conv_2 = layers.Conv2D(64, kernel_size=(3, 3), activation="relu") | |
self.max_pool = layers.MaxPooling2D(pool_size=(2, 2)) | |
self.flatten = layers.Flatten() | |
self.dropout = layers.Dropout(0.5) | |
self.out = layers.Dense(num_classes, activation="softmax") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
train_ds = tf.data.Dataset.from_tensor_slices( | |
(x_train, y_train)).shuffle(10000).batch(32) | |
test_ds = tf.data.Dataset.from_tensor_slices((x_test, y_test)).batch(32) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
input = layers.Input(input_shape) | |
x_1 = layers.Conv2D(32, kernel_size=(3, 3), activation="relu")(input) | |
x_1 = layers.MaxPooling2D(pool_size=(2, 2))(x_1) | |
x_1 = layers.Conv2D(64, kernel_size=(3, 3), activation="relu")(x_1) | |
x_1 = layers.MaxPooling2D(pool_size=(2, 2))(x_1) | |
x_1 = layers.Flatten()(x_1) | |
x_2 = layers.Conv2D(16, kernel_size=(3, 3), activation="relu")(input) | |
x_2 = layers.MaxPooling2D(pool_size=(2, 2))(x_2) |