This file contains 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 torch | |
use_gpu = True if torch.cuda.is_available() else False | |
model = torch.hub.load('facebookresearch/pytorch_GAN_zoo:hub', 'DCGAN', pretrained=True, useGPU=use_gpu) |
This file contains 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
from _libmath import lib | |
x = lib.sqrt(4.5) | |
print(F"The square root of 4.5 is {x}.") |
This file contains 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
from cffi import FFI | |
ffibuilder = FFI() | |
ffibuilder.cdef(""" | |
double sqrt(double x); | |
""") | |
ffibuilder.set_source("_libmath", |
This file contains 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 ctypes | |
import pathlib | |
if __name__ == "__main__": | |
# load the lib | |
libname = pathlib.Path().absolute() / "libcadd.so" | |
c_lib = ctypes.CDLL(libname) | |
x, y = 6, 2.3 |
This file contains 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
#include <stdio.h> | |
float cadd(int x, float y) { | |
float res = x + y; | |
printf("In cadd: int %d float %.1f returning %.1f\n", x, y, res); | |
return res; | |
} |
This file contains 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_tensor = tf.keras.layers.Input(shape=(224, 224, 3), name="image") | |
model = tf.keras.applications.EfficientNetB0( | |
input_tensor=input_tensor, weights=None, classes=91 | |
) | |
model.compile( | |
optimizer=tf.keras.optimizers.Adam(), | |
loss=tf.keras.losses.SparseCategoricalCrossentropy(), | |
metrics=[tf.keras.metrics.SparseCategoricalAccuracy()], |
This file contains 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_filenames = tf.io.gfile.glob(f"{tfrecords_dir}/*.tfrec") | |
batch_size = 32 | |
epochs = 1 | |
steps_per_epoch = 50 | |
AUTOTUNE = tf.data.AUTOTUNE | |
def prepare_sample(features): | |
image = tf.image.resize(features["image"], size=(224, 224)) | |
return image, features["category_id"] |
This file contains 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 tfrec_num in range(num_tfrecods): | |
samples = annotations[(tfrec_num * num_samples) : ((tfrec_num + 1) * num_samples)] | |
with tf.io.TFRecordWriter( | |
tfrecords_dir + "/file_%.2i-%i.tfrec" % (tfrec_num, len(samples)) | |
) as writer: | |
for sample in samples: | |
image_path = f"{images_dir}/{sample['image_id']:012d}.jpg" | |
image = tf.io.decode_jpeg(tf.io.read_file(image_path)) | |
example = create_example(image, image_path, sample) |
This file contains 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 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( |