LogoLogo
  • Tensorleap
  • Examples
    • Semantic Segmentation
    • Image Analysis
    • Sentiment Analysis
    • MNIST Project Walkthrough
    • IMDB Project Walkthrough
  • Quickstart using CLI
  • Guides
    • Full Guides
      • MNIST Guide
        • Dataset Integration
        • Model Integration
        • Model Perception Analysis
        • Advanced Metrics
      • IMDB Guide
        • Dataset Integration
        • Model Integration
        • Model Perception Analysis
        • Advanced Metrics
    • Integration Script
      • Preprocess Function
      • Input Encoder
      • Ground Truth Encoder
      • Metadata Function
      • Visualizer Function
      • Prediction
      • Custom Metrics
      • Custom Loss Function
      • Custom Layers
      • Unlabeled Data
      • Examples
        • CelebA Object Detection (YoloV7)
        • Wikipedia Toxicity (using Tensorflow Datasets)
        • Confusion Matrix
        • CelebA Classification (using GCS)
  • Platform
    • Resources Management
    • Project
    • Dataset
    • Secret Manager
    • Network
      • Dataset Node
      • Layers
      • Loss and Optimizer
      • Visualizers
      • Import Model
      • Metrics
    • Evaluate / Train Model
    • Metrics Dashboard
    • Versions
    • Issues
    • Tests
    • Analysis
      • helpers
        • detection
          • YOLO
    • Team management
    • Insights
  • API
    • code_loader
      • leap_binder
        • add_custom_metric
        • set_preprocess
        • set_unlabeled_data_preprocess
        • set_input
        • set_ground_truth
        • set_metadata
        • add_prediction
        • add_custom_loss
        • set_visualizer
      • enums
        • DatasetMetadataType
        • LeapDataType
      • datasetclasses
        • PreprocessResponse
      • visualizer_classes
        • LeapImage
        • LeapImageWithBBox
        • LeapGraph
        • LeapText
        • LeapHorizontalBar
        • LeapImageMask
        • LeapTextMask
  • Tips & Tricks
    • Import External Code
  • Legal
    • Terms of Use
    • Privacy Policy
Powered by GitBook
On this page
  • Custom Layer Import from File
  • Append system path
  • Import using importlib

Was this helpful?

  1. Tips & Tricks

Import External Code

PreviousTips & TricksNextLegal

Last updated 2 years ago

Was this helpful?

To include any existing code in your Tensorleap you can create a library and import all your existing classes and functions to your script.

Here is an example of how to import a from an external file.

Custom Layer Import from File

  1. First, create a file that includes the custom layers that are included in your model. Here we show an example of a custom dense layer.

custom_dense.py
import tensorflow as tf

class CustomDense(tf.keras.layers.Layer):
    def __init__(self, n, **args):
        super(CustomDense, self).__init__()
        self.n = n
        self.dense = tf.keras.layers.Dense(self.n)
    
    def call(self, inputs):
        return self.dense(inputs) 
    
    def get_config(self):
        config = super().get_config()
        config.update({
            "n": self.n,
        })
        return config

2. In your Tensorleap Data Folder, create a new folder that will include the file you've created and an empty __init__.py file. For example:

|
├── ....
|   ├── custom_layers/
|           ├── __init__.py
|           └── custom_dense.py

3. Now, use one of two possible methods to import the classes Into the dataset script.

Append system path

Tensorleap dataset script
# custom function imports
import sys
sys.path.append('/home/username/tensorleap/data/custom_layers')
from custom_dense import CustomDense
...
leap_binder.set_custom_layer(CustomDense, 'CustomDense')

OR

Import using importlib

Tensorleap dataset script
# custom function imports
import importlib.util
import sys
from inspect import getmembers, isclass
...
# adding custom layers from a file using imports
spec = importlib.util.spec_from_file_location('custom_layers.custom_dense',
 '/home/username/tensorleap/data/custom_layers/custom_dense.py')
custom_layers_module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(custom_layers_module)

custom_layers_titles = getmembers(custom_layers_module, isclass)
for i in range(len(custom_layers_titles)):
    class_name = custom_layers_titles[i][0]
    function_class = getattr(custom_layers_module, class_name)
    leap_binder.set_custom_layer(function_class, class_name)
...

Parsing the dataset will now import the custom layers from the external files and add it to your Tensorleap environment.

Custom Layer
Dataset Script