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
| from torch import package | |
| path = "/tmp/dcgan.pt" | |
| package_name = "dcgan" | |
| resource_name = "model.pkl" | |
| with package.PackageExporter(path) as exp: | |
| exp.save_pickle(package_name, resource_name, model) |
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 run_model(model): | |
| num_images = 64 | |
| noise, _ = model.buildNoiseData(num_images) | |
| with torch.no_grad(): | |
| generated_images = model.test(noise) | |
| # let's plot these images using torchvision and matplotlib | |
| import matplotlib.pyplot as plt | |
| import torchvision | |
| plt.imshow(torchvision.utils.make_grid(generated_images).permute(1, 2, 0).cpu().numpy()) |
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 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 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
| from _libmath import lib | |
| x = lib.sqrt(4.5) | |
| print(F"The square root of 4.5 is {x}.") |
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
| from cffi import FFI | |
| ffibuilder = FFI() | |
| ffibuilder.cdef(""" | |
| double sqrt(double x); | |
| """) | |
| ffibuilder.set_source("_libmath", |
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 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 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
| #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 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_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 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_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 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 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) |