### Install Object Detection API Source: https://www.tensorflow.org/hub/tutorials/tf2_object_detection Commands to install protobuf compiler, compile protos, and install the Object Detection package. ```bash sudo apt install -y protobuf-compiler cd models/research/ protoc object_detection/protos/*.proto --python_out=. cp object_detection/packages/tf2/setup.py . python -m pip install . ``` -------------------------------- ### Setup Evaluation Metric Source: https://www.tensorflow.org/hub/tutorials/wav2vec2_saved_model_finetuning Installs the datasets library and initializes the Word Error Rate (WER) metric. ```python !pip3 install -q datasets from datasets import load_metric metric = load_metric("wer") ``` -------------------------------- ### Install Dependencies Source: https://www.tensorflow.org/hub/tutorials/action_recognition_with_tf_hub Install the necessary libraries for video processing and documentation visualization. ```bash pip install -q imageio pip install -q opencv-python pip install -q git+https://github.com/tensorflow/docs ``` -------------------------------- ### Install Kaggle CLI Source: https://www.tensorflow.org/hub/tutorials/text_classification_with_tf_hub_on_kaggle Installs the Kaggle command-line tool in the environment. ```bash pip install -q kaggle ``` -------------------------------- ### Setup Environment Source: https://www.tensorflow.org/hub/tutorials/biggan_generation_with_tf_hub Import required libraries and configure TensorFlow for compatibility. ```python import tensorflow.compat.v1 as tf tf.disable_v2_behavior() import os import io import IPython.display import numpy as np import PIL.Image from scipy.stats import truncnorm import tensorflow_hub as hub ``` -------------------------------- ### Install dependencies Source: https://www.tensorflow.org/hub/tutorials/tf_hub_generative_image_module Install required libraries for image processing and documentation visualization. ```bash # Install imageio for creating animations. pip -q install imageio pip -q install scikit-image pip install git+https://github.com/tensorflow/docs ``` -------------------------------- ### Environment output example Source: https://www.tensorflow.org/hub/tutorials/object_detection Example output showing the TensorFlow version and GPU device status. ```text 2.16.1 The following GPU devices are available: 2024-03-09 13:48:48.238338: E external/local_xla/xla/stream_executor/cuda/cuda_driver.cc:282] failed call to cuInit: CUDA_ERROR_NO_DEVICE: no CUDA-capable device is detected ``` -------------------------------- ### Install system dependencies Source: https://www.tensorflow.org/hub/tutorials/spice Install required system packages for audio processing. ```bash sudo apt-get install -q -y timidity libsndfile1 ``` -------------------------------- ### Output example Source: https://www.tensorflow.org/hub/tutorials/yamnet Example output string from the inference process. ```text The main sound is: Animal ``` -------------------------------- ### Install dependencies Source: https://www.tensorflow.org/hub/tutorials/senteval_for_universal_sentence_encoder_cmlm Installs the required TensorFlow Text and PyTorch libraries. ```bash pip install --quiet "tensorflow-text==2.11.*" pip install --quiet torch==1.8.1 ``` -------------------------------- ### Install system unzip package Source: https://www.tensorflow.org/hub/tutorials/bangla_article_classifier Installs the unzip utility on the system using apt-get. ```bash sudo apt-get install -y unzip ``` -------------------------------- ### Install Required Libraries Source: https://www.tensorflow.org/hub/tutorials/semantic_approximate_nearest_neighbors Install Apache Beam, scikit-learn, and Annoy for data processing and nearest neighbor search. ```bash pip install -q apache_beam pip install -q sklearn pip install -q annoy ``` -------------------------------- ### Install System and Python Dependencies Source: https://www.tensorflow.org/hub/tutorials/movinet Install ffmpeg and required Python packages for video processing. ```bash sudo apt install -y ffmpeg pip install -q mediapy ``` ```bash pip uninstall -q -y opencv-python-headless pip install -q "opencv-python-headless<4.3" ``` -------------------------------- ### Load and Prepare Example Images Source: https://www.tensorflow.org/hub/tutorials/tf2_arbitrary_image_stylization Downloads and processes content and style images for the stylization model. ```python # @title Load example images { display-mode: "form" } content_image_url = 'https://upload.wikimedia.org/wikipedia/commons/thumb/f/fd/Golden_Gate_Bridge_from_Battery_Spencer.jpg/640px-Golden_Gate_Bridge_from_Battery_Spencer.jpg' # @param {type:"string"} style_image_url = 'https://upload.wikimedia.org/wikipedia/commons/0/0a/The_Great_Wave_off_Kanagawa.jpg' # @param {type:"string"} output_image_size = 384 # @param {type:"integer"} # The content image size can be arbitrary. content_img_size = (output_image_size, output_image_size) # The style prediction model was trained with image size 256 and it's the # recommended image size for the style image (though, other sizes work as # well but will lead to different results). style_img_size = (256, 256) # Recommended to keep it at 256. content_image = load_image(content_image_url, content_img_size) style_image = load_image(style_image_url, style_img_size) style_image = tf.nn.avg_pool(style_image, ksize=[3,3], strides=[1,1], padding='SAME') show_n([content_image, style_image], ['Content image', 'Style image']) ``` -------------------------------- ### Install Dependencies Source: https://www.tensorflow.org/hub/tutorials/tf2_object_detection Install specific versions of numpy and protobuf required for the environment. ```bash # This Colab requires a recent numpy version. pip install numpy==1.24.3 pip install protobuf==3.20.3 ``` -------------------------------- ### Install Required Libraries Source: https://www.tensorflow.org/hub/tutorials/tf2_semantic_approximate_nearest_neighbors Install the necessary packages for data processing, machine learning, and approximate nearest neighbor search. ```bash pip install -q apache_beam pip install -q 'scikit_learn~=0.23.0' # For gaussian_random_matrix. pip install -q annoy ``` -------------------------------- ### Example Output Source: https://www.tensorflow.org/hub/tutorials/image_classification Sample console output after selecting a model and configuring image size. ```text Selected model: efficientnetv2-s : https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet1k_s/classification/2 Images will be converted to 384x384 ``` -------------------------------- ### Environment Setup Source: https://www.tensorflow.org/hub/tutorials/image_retraining Initializes necessary libraries and verifies the TensorFlow version and GPU availability. ```python import itertools import os import matplotlib.pylab as plt import numpy as np import tensorflow as tf import tensorflow_hub as hub print("TF version:", tf.__version__) print("Hub version:", hub.__version__) print("GPU is", "available" if tf.config.list_physical_devices('GPU') else "NOT AVAILABLE") ``` -------------------------------- ### Install Python audio libraries Source: https://www.tensorflow.org/hub/tutorials/spice Install necessary Python packages for handling sound data. ```bash # All the imports to deal with sound data pip install pydub librosa music21 ``` -------------------------------- ### Install dependencies Source: https://www.tensorflow.org/hub/tutorials/tf_hub_delf_module Install the required scikit-image library before running the DELF module. ```bash pip install scikit-image ``` -------------------------------- ### Install dependencies for Wav2Vec2 fine-tuning Source: https://www.tensorflow.org/hub/tutorials/wav2vec2_saved_model_finetuning Install the required gsoc-wav2vec2 package, system audio libraries, and SoundFile for data processing. ```bash pip3 install -q git+https://github.com/vasudevgupta7/gsoc-wav2vec2@main sudo apt-get install -y libsndfile1-dev pip3 install -q SoundFile ``` -------------------------------- ### Display Labeled Training Examples Source: https://www.tensorflow.org/hub/tutorials/cord_19_embeddings Retrieves a batch of training examples and displays them in a pandas DataFrame. ```python NUM_EXAMPLES = 20 data = get_example_data(THE_DATASET, NUM_EXAMPLES, for_eval=False) display_df( pd.DataFrame({ TEXT_FEATURE_NAME: [ex.decode('utf8') for ex in data[0]], LABEL_NAME: [THE_DATASET.class_names()[x] for x in data[1]] })) ``` -------------------------------- ### Example Output Source: https://www.tensorflow.org/hub/tutorials/cropnet_on_device Displays the expected console output after merging the datasets. ```text Added 16968 negative examples.Training dataset has now 22624 examples in total. ``` -------------------------------- ### Install Dependencies Source: https://www.tensorflow.org/hub/tutorials/retrieval_with_tf_hub_universal_encoder_qa Installs the required TensorFlow, text processing, and nearest neighbor libraries. ```bash %%capture # Install the latest Tensorflow version. !pip install -q "tensorflow-text==2.11.*" !pip install -q simpleneighbors[annoy] !pip install -q nltk !pip install -q tqdm ``` -------------------------------- ### Install Dependencies Source: https://www.tensorflow.org/hub/tutorials/cropnet_on_device Install required system libraries and Python packages for Model Maker and TensorFlow Datasets. ```bash sudo apt install -q libportaudio2 ## image_classifier library requires numpy <= 1.23.5 pip install "numpy<=1.23.5" pip install --use-deprecated=legacy-resolver tflite-model-maker-nightly pip install -U tensorflow-datasets ## scann library requires tensorflow < 2.9.0 pip install "tensorflow<2.9.0" pip install "tensorflow-datasets~=4.8.0" # protobuf>=3.12.2 pip install tensorflow-metadata~=1.10.0 # protobuf>=3.13 ## tensorflowjs requires packaging < 20.10 pip install "packaging<20.10" ``` -------------------------------- ### Download Example Video Source: https://www.tensorflow.org/hub/tutorials/movinet Download a sample GIF file for classification testing. ```python jumpingjack_url = 'https://github.com/tensorflow/models/raw/f8af2291cced43fc9f1d9b41ddbf772ae7b0d7d2/official/projects/movinet/files/jumpingjack.gif' jumpingjack_path = tf.keras.utils.get_file( fname='jumpingjack.gif', origin=jumpingjack_url, cache_dir='.', cache_subdir='.', ) ``` -------------------------------- ### Directory Listing Output Source: https://www.tensorflow.org/hub/tutorials/semantic_approximate_nearest_neighbors Example output showing the generated index files. ```text corpus index.mapping raw.tsv index random_projection_matrix semantic_approximate_nearest_neighbors.ipynb ``` -------------------------------- ### Install dependencies and import libraries Source: https://www.tensorflow.org/hub/tutorials/text_to_video_retrieval_with_s3d_milnce Install the required OpenCV library and import TensorFlow, TensorFlow Hub, and other utility modules for video processing. ```python !pip install -q opencv-python import os import tensorflow.compat.v2 as tf import tensorflow_hub as hub import numpy as np import cv2 from IPython import display import math ``` -------------------------------- ### View sample data output Source: https://www.tensorflow.org/hub/tutorials/text_classification_with_tf_hub Displays the content of the first few movie review examples. ```python array([b"This was an absolutely terrible movie. Don't be lured in by Christopher Walken or Michael Ironside. Both are great actors, but this must simply be their worst role in history. Even their great acting could not redeem this movie's ridiculous storyline. This movie is an early nineties US propaganda piece. The most pathetic scenes were those when the Columbian rebels were making their cases for revolutions. Maria Conchita Alonso appeared phony, and her pseudo-love affair with Walken was nothing but a pathetic emotional plug in a movie that was devoid of any real meaning. I am disappointed that there are movies like this, ruining actor's like Christopher Walken's good name. I could barely sit through it.", b'I have been known to fall asleep during films, but this is usually due to a combination of things including, really tired, being warm and comfortable on the sette and having just eaten a lot. However on this occasion I fell asleep because the film was rubbish. The plot development was constant. Constantly slow and boring. Things seemed to happen, but with no explanation of what was causing them or why. I admit, I may have missed part of the film, but i watched the majority of it and everything just seemed to happen of its own accord without any real concern for anything else. I cant recommend this film at all.', b'Mann photographs the Alberta Rocky Mountains in a superb fashion, and Jimmy Stewart and Walter Brennan give enjoyable performances as they always seem to do.

But come on Hollywood - a Mountie telling the people of Dawson City, Yukon to elect themselves a marshal (yes a marshal!) and to enforce the law themselves, then gunfighters battling it out on the streets for control of the town?

Nothing even remotely resembling that happened on the Canadian side of the border during the Klondike gold rush. Mr. Mann and company appear to have mistaken Dawson City for Deadwood, the Canadian North for the American Wild West.

Canadian viewers be prepared for a Reefer Madness type of enjoyable howl with this ludicrous plot, or, to shake your head in disgust.', b'This is the kind of film for a snowy Sunday afternoon when the rest of the world can go ahead with its own business as you descend into a big arm-chair and mellow for a couple of hours. Wonderful performances from Cher and Nicolas Cage (as always) gently row the plot along. There are no rapids to cross, no dangerous waters, just a warm and witty paddle through New York life at its best. A family film in every sense and one that deserves the praise it received.', b'As others have mentioned, all the women that go nude in this film are mostly absolutely gorgeous. The plot very ably shows the hypocrisy of the female libido. When men are around they want to be pursued, but when no "men" are around, they become the pursuers of a 14 year old boy. And the boy becomes a man really fast (we should all be so lucky at this age!). He then gets up the courage to pursue his true love.', ``` -------------------------------- ### UCF101 dataset output example Source: https://www.tensorflow.org/hub/tutorials/action_recognition_with_tf_hub Example output showing the distribution of videos across various action categories in the UCF101 dataset. ```text Found 13320 videos in 101 categories. ApplyEyeMakeup 145 videos (v_ApplyEyeMakeup_g01_c01.avi, v_ApplyEyeMakeup_g01_c02.avi, ...) ApplyLipstick 114 videos (v_ApplyLipstick_g01_c01.avi, v_ApplyLipstick_g01_c02.avi, ...) Archery 145 videos (v_Archery_g01_c01.avi, v_Archery_g01_c02.avi, ...) BabyCrawling 132 videos (v_BabyCrawling_g01_c01.avi, v_BabyCrawling_g01_c02.avi, ...) BalanceBeam 108 videos (v_BalanceBeam_g01_c01.avi, v_BalanceBeam_g01_c02.avi, ...) BandMarching 155 videos (v_BandMarching_g01_c01.avi, v_BandMarching_g01_c02.avi, ...) BaseballPitch 150 videos (v_BaseballPitch_g01_c01.avi, v_BaseballPitch_g01_c02.avi, ...) BasketballDunk 131 videos (v_BasketballDunk_g01_c01.avi, v_BasketballDunk_g01_c02.avi, ...) Basketball 134 videos (v_Basketball_g01_c01.avi, v_Basketball_g01_c02.avi, ...) BenchPress 160 videos (v_BenchPress_g01_c01.avi, v_BenchPress_g01_c02.avi, ...) Biking 134 videos (v_Biking_g01_c01.avi, v_Biking_g01_c02.avi, ...) Billiards 150 videos (v_Billiards_g01_c01.avi, v_Billiards_g01_c02.avi, ...) BlowDryHair 131 videos (v_BlowDryHair_g01_c01.avi, v_BlowDryHair_g01_c02.avi, ...) BlowingCandles 109 videos (v_BlowingCandles_g01_c01.avi, v_BlowingCandles_g01_c02.avi, ...) BodyWeightSquats 112 videos (v_BodyWeightSquats_g01_c01.avi, v_BodyWeightSquats_g01_c02.avi, ...) Bowling 155 videos (v_Bowling_g01_c01.avi, v_Bowling_g01_c02.avi, ...) BoxingPunchingBag 163 videos (v_BoxingPunchingBag_g01_c01.avi, v_BoxingPunchingBag_g01_c02.avi, ...) BoxingSpeedBag 134 videos (v_BoxingSpeedBag_g01_c01.avi, v_BoxingSpeedBag_g01_c02.avi, ...) BreastStroke 101 videos (v_BreastStroke_g01_c01.avi, v_BreastStroke_g01_c02.avi, ...) BrushingTeeth 131 videos (v_BrushingTeeth_g01_c01.avi, v_BrushingTeeth_g01_c02.avi, ...) CleanAndJerk 112 videos (v_CleanAndJerk_g01_c01.avi, v_CleanAndJerk_g01_c02.avi, ...) CliffDiving 138 videos (v_CliffDiving_g01_c01.avi, v_CliffDiving_g01_c02.avi, ...) CricketBowling 139 videos (v_CricketBowling_g01_c01.avi, v_CricketBowling_g01_c02.avi, ...) CricketShot 167 videos (v_CricketShot_g01_c01.avi, v_CricketShot_g01_c02.avi, ...) CuttingInKitchen 110 videos (v_CuttingInKitchen_g01_c01.avi, v_CuttingInKitchen_g01_c02.avi, ...) Diving 150 videos (v_Diving_g01_c01.avi, v_Diving_g01_c02.avi, ...) Drumming 161 videos (v_Drumming_g01_c01.avi, v_Drumming_g01_c02.avi, ...) Fencing 111 videos (v_Fencing_g01_c01.avi, v_Fencing_g01_c02.avi, ...) FieldHockeyPenalty 126 videos (v_FieldHockeyPenalty_g01_c01.avi, v_FieldHockeyPenalty_g01_c02.avi, ...) FloorGymnastics 125 videos (v_FloorGymnastics_g01_c01.avi, v_FloorGymnastics_g01_c02.avi, ...) FrisbeeCatch 126 videos (v_FrisbeeCatch_g01_c01.avi, v_FrisbeeCatch_g01_c02.avi, ...) FrontCrawl 137 videos (v_FrontCrawl_g01_c01.avi, v_FrontCrawl_g01_c02.avi, ...) GolfSwing 139 videos (v_GolfSwing_g01_c01.avi, v_GolfSwing_g01_c02.avi, ...) Haircut 130 videos (v_Haircut_g01_c01.avi, v_Haircut_g01_c02.avi, ...) HammerThrow 150 videos (v_HammerThrow_g01_c01.avi, v_HammerThrow_g01_c02.avi, ...) Hammering 140 videos (v_Hammering_g01_c01.avi, v_Hammering_g01_c02.avi, ...) HandstandPushups 128 videos (v_HandstandPushups_g01_c01.avi, v_HandstandPushups_g01_c02.avi, ...) HandstandWalking 111 videos (v_HandstandWalking_g01_c01.avi, v_HandstandWalking_g01_c02.avi, ...) HeadMassage 147 videos (v_HeadMassage_g01_c01.avi, v_HeadMassage_g01_c02.avi, ...) HighJump 123 videos (v_HighJump_g01_c01.avi, v_HighJump_g01_c02.avi, ...) HorseRace 124 videos (v_HorseRace_g01_c01.avi, v_HorseRace_g01_c02.avi, ...) HorseRiding 164 videos (v_HorseRiding_g01_c01.avi, v_HorseRiding_g01_c02.avi, ...) HulaHoop 125 videos (v_HulaHoop_g01_c01.avi, v_HulaHoop_g01_c02.avi, ...) IceDancing 158 videos (v_IceDancing_g01_c01.avi, v_IceDancing_g01_c02.avi, ...) JavelinThrow 117 videos (v_JavelinThrow_g01_c01.avi, v_JavelinThrow_g01_c02.avi, ...) JugglingBalls 121 videos (v_JugglingBalls_g01_c01.avi, v_JugglingBalls_g01_c02.avi, ...) JumpRope 144 videos (v_JumpRope_g01_c01.avi, v_JumpRope_g01_c02.avi, ...) ``` -------------------------------- ### Model loading output Source: https://www.tensorflow.org/hub/tutorials/boundless Example console output when loading the model. ```text Loading model Boundless Quarter (https://tfhub.dev/google/boundless/quarter/1) 2024-03-09 13:56:07.413611: E external/local_xla/xla/stream_executor/cuda/cuda_driver.cc:282] failed call to cuInit: CUDA_ERROR_NO_DEVICE: no CUDA-capable device is detected ``` -------------------------------- ### Setup Imports and Utility Functions Source: https://www.tensorflow.org/hub/tutorials/s3gan_generation_with_tf_hub Initializes the environment with required libraries and defines helper functions for image grid creation and display. ```python # @title Imports and utility functions import os import IPython from IPython.display import display import numpy as np import PIL.Image import pandas as pd import six import tensorflow.compat.v1 as tf tf.disable_v2_behavior() import tensorflow_hub as hub def imgrid(imarray, cols=8, pad=1): pad = int(pad) assert pad >= 0 cols = int(cols) assert cols >= 1 N, H, W, C = imarray.shape rows = int(np.ceil(N / float(cols))) batch_pad = rows * cols - N assert batch_pad >= 0 post_pad = [batch_pad, pad, pad, 0] pad_arg = [[0, p] for p in post_pad] imarray = np.pad(imarray, pad_arg, 'constant') H += pad W += pad grid = (imarray .reshape(rows, cols, H, W, C) .transpose(0, 2, 1, 3, 4) .reshape(rows*H, cols*W, C)) return grid[:-pad, :-pad] def imshow(a, format='png', jpeg_fallback=True): a = np.asarray(a, dtype=np.uint8) if six.PY3: str_file = six.BytesIO() else: str_file = six.StringIO() PIL.Image.fromarray(a).save(str_file, format) png_data = str_file.getvalue() try: disp = display(IPython.display.Image(png_data)) except IOError: if jpeg_fallback and format != 'jpeg': print ('Warning: image was too large to display in format "{}"; ' 'trying jpeg instead.').format(format) return imshow(a, format='jpeg') else: raise return disp ``` -------------------------------- ### Install dependencies Source: https://www.tensorflow.org/hub/tutorials/cropnet_cassava Install the required version of matplotlib. ```bash pip install matplotlib==3.2.2 ``` -------------------------------- ### Importing libraries and configuring Wav2Vec2 Source: https://www.tensorflow.org/hub/tutorials/wav2vec2_saved_model_finetuning Initial setup including library imports and Wav2Vec2 configuration initialization. ```python import os import tensorflow as tf import tensorflow_hub as hub from wav2vec2 import Wav2Vec2Config config = Wav2Vec2Config() print("TF version:", tf.__version__) ``` -------------------------------- ### Load Example Videos and Queries Source: https://www.tensorflow.org/hub/tutorials/text_to_video_retrieval_with_s3d_milnce?hl=zh-tw Initializes video URLs and text queries, then processes the videos for use in the retrieval pipeline. ```python # @title Load example videos and define text queries { display-mode: "form" } video_1_url = 'https://upload.wikimedia.org/wikipedia/commons/b/b0/YosriAirTerjun.gif' # @param {type:"string"} video_2_url = 'https://upload.wikimedia.org/wikipedia/commons/e/e6/Guitar_solo_gif.gif' # @param {type:"string"} video_3_url = 'https://upload.wikimedia.org/wikipedia/commons/3/30/2009-08-16-autodrift-by-RalfR-gif-by-wau.gif' # @param {type:"string"} video_1 = load_video(video_1_url) video_2 = load_video(video_2_url) video_3 = load_video(video_3_url) all_videos = [video_1, video_2, video_3] query_1_video = 'waterfall' # @param {type:"string"} query_2_video = 'playing guitar' # @param {type:"string"} query_3_video = 'car drifting' # @param {type:"string"} all_queries_video = [query_1_video, query_2_video, query_3_video] all_videos_urls = [video_1_url, video_2_url, video_3_url] display_video(all_videos_urls) ``` -------------------------------- ### Install Seaborn dependency Source: https://www.tensorflow.org/hub/tutorials/semantic_similarity_with_tf_hub_universal_encoder Install the seaborn library for visualization purposes. ```python %%capture !pip3 install seaborn ``` -------------------------------- ### Install TensorFlow Text Source: https://www.tensorflow.org/hub/tutorials/bert_experts Install the required version of tensorflow-text for preprocessing. ```bash pip install --quiet "tensorflow-text==2.11.*" ``` -------------------------------- ### Setup Imports, Utilities, and Model Wrappers Source: https://www.tensorflow.org/hub/tutorials/s3gan_generation_with_tf_hub?hl=ar Includes essential libraries, image grid/display functions, and classes for managing S3GAN generator and discriminator modules. ```python # @title Imports and utility functions import os import IPython from IPython.display import display import numpy as np import PIL.Image import pandas as pd import six import tensorflow.compat.v1 as tf tf.disable_v2_behavior() import tensorflow_hub as hub def imgrid(imarray, cols=8, pad=1): pad = int(pad) assert pad >= 0 cols = int(cols) assert cols >= 1 N, H, W, C = imarray.shape rows = int(np.ceil(N / float(cols))) batch_pad = rows * cols - N assert batch_pad >= 0 post_pad = [batch_pad, pad, pad, 0] pad_arg = [[0, p] for p in post_pad] imarray = np.pad(imarray, pad_arg, 'constant') H += pad W += pad grid = (imarray           .reshape(rows, cols, H, W, C)           .transpose(0, 2, 1, 3, 4)           .reshape(rows*H, cols*W, C)) return grid[:-pad, :-pad] def imshow(a, format='png', jpeg_fallback=True): a = np.asarray(a, dtype=np.uint8) if six.PY3:   str_file = six.BytesIO() else:   str_file = six.StringIO() PIL.Image.fromarray(a).save(str_file, format) png_data = str_file.getvalue() try:   disp = display(IPython.display.Image(png_data)) except IOError:   if jpeg_fallback and format != 'jpeg':     print ('Warning: image was too large to display in format "{}"; '            'trying jpeg instead.').format(format)     return imshow(a, format='jpeg')   else:     raise return disp class Generator(object): def __init__(self, module_spec):   self._module_spec = module_spec   self._sess = None   self._graph = tf.Graph()   self._load_model() @property def z_dim(self):   return self._z.shape[-1].value @property def conditional(self):   return self._labels is not None def _load_model(self):   with self._graph.as_default():     self._generator = hub.Module(self._module_spec, name="gen_module",                                    tags={"gen", "bsNone"})     input_info = self._generator.get_input_info_dict()     inputs = {k: tf.placeholder(v.dtype, v.get_shape().as_list(), k)               for k, v in self._generator.get_input_info_dict().items()}     self._samples = self._generator(inputs=inputs, as_dict=True)["generated"]     print("Inputs:", inputs)     print("Outputs:", self._samples)     self._z = inputs["z"]     self._labels = inputs.get("labels", None) def _init_session(self):   if self._sess is None:     self._sess = tf.Session(graph=self._graph)     self._sess.run(tf.global_variables_initializer()) def get_noise(self, num_samples, seed=None):   if np.isscalar(seed):     np.random.seed(seed)     return np.random.normal(size=[num_samples, self.z_dim])   z = np.empty(shape=(len(seed), self.z_dim), dtype=np.float32)   for i, s in enumerate(seed):     np.random.seed(s)     z[i] = np.random.normal(size=[self.z_dim])   return z def get_samples(self, z, labels=None):   with self._graph.as_default():     self._init_session()     feed_dict = {self._z: z}     if self.conditional:       assert labels is not None       assert labels.shape[0] == z.shape[0]       feed_dict[self._labels] = labels     samples = self._sess.run(self._samples, feed_dict=feed_dict)     return np.uint8(np.clip(256 * samples, 0, 255)) class Discriminator(object): def __init__(self, module_spec):   self._module_spec = module_spec   self._sess = None   self._graph = tf.Graph()   self._load_model() @property def conditional(self):   return "labels" in self._inputs @property def image_shape(self):   return self._inputs["images"].shape.as_list()[1:] def _load_model(self):   with self._graph.as_default():     self._discriminator = hub.Module(self._module_spec, name="disc_module",                                        tags={"disc", "bsNone"})     input_info = self._discriminator.get_input_info_dict()     self._inputs = {k: tf.placeholder(v.dtype, v.get_shape().as_list(), k)                     for k, v in input_info.items()}     self._outputs = self._discriminator(inputs=self._inputs, as_dict=True)     print("Inputs:", self._inputs)     print("Outputs:", self._outputs) def _init_session(self):   if self._sess is None:     self._sess = tf.Session(graph=self._graph)     self._sess.run(tf.global_variables_initializer()) def predict(self, images, labels=None):   with self._graph.as_default():     self._init_session()     feed_dict = {self._inputs["images"]: images}     if "labels" in self._inputs:       assert labels is not None       assert labels.shape[0] == images.shape[0]       feed_dict[self._inputs["labels"]] = labels     return self._sess.run(self._outputs, feed_dict=feed_dict) ``` -------------------------------- ### Setup and Library Imports Source: https://www.tensorflow.org/hub/tutorials/cord_19_embeddings_keras Initializes the environment by importing essential data science and TensorFlow libraries required for embedding analysis and model training. ```python import functools import itertools import matplotlib.pyplot as plt import numpy as np import seaborn as sns import pandas as pd import tensorflow as tf import tensorflow_datasets as tfds import tensorflow_hub as hub from tqdm import trange ``` -------------------------------- ### Environment Setup and Version Verification Source: https://www.tensorflow.org/hub/tutorials/text_classification_with_tf_hub Imports necessary libraries and prints version information to verify the environment configuration. ```python import numpy as np import tensorflow as tf import tensorflow_hub as hub import tensorflow_datasets as tfds import matplotlib.pyplot as plt print("Version: ", tf.__version__) print("Eager mode: ", tf.executing_eagerly()) print("Hub version: ", hub.__version__) print("GPU is", "available" if tf.config.list_physical_devices('GPU') else "NOT AVAILABLE") ``` -------------------------------- ### Install dependencies for FILM Source: https://www.tensorflow.org/hub/tutorials/tf_hub_film_example Install the required mediapy library and ffmpeg system package. ```bash pip install mediapy sudo apt-get install -y ffmpeg ``` -------------------------------- ### Rapid prototyping configuration parameters Source: https://www.tensorflow.org/hub/tutorials/senteval_for_universal_sentence_encoder_cmlm Configuration dictionary for faster execution, typically taking 5-15 minutes. ```python params = {'task_path': PATH_TO_DATA, 'usepytorch': True, 'kfold': 5} params['classifier'] = {'nhid': 0, 'optim': 'rmsprop', 'batch_size': 128, 'tenacity': 3, 'epoch_size': 2} ``` -------------------------------- ### Install dependencies Source: https://www.tensorflow.org/hub/tutorials/semantic_similarity_with_tf_hub_universal_encoder_lite Install the required seaborn and sentencepiece packages for visualization and text processing. ```bash # Install seaborn for pretty visualizations pip3 install --quiet seaborn # Install SentencePiece package # SentencePiece package is needed for Universal Sentence Encoder Lite. We'll # use it for all the text processing and sentence feature ID lookup. pip3 install --quiet sentencepiece ``` -------------------------------- ### Inspect first 10 examples Source: https://www.tensorflow.org/hub/tutorials/text_classification_with_tf_hub Retrieves and displays the first 10 entries from the training dataset. ```python train_examples[:10] ``` -------------------------------- ### Initialize Model Weights and Summary Source: https://www.tensorflow.org/hub/tutorials/wav2vec2_saved_model_finetuning Builds model weights by performing a dummy forward pass and displays the model architecture summary. ```python model(tf.random.uniform(shape=(BATCH_SIZE, AUDIO_MAXLEN))) model.summary() ``` -------------------------------- ### Install and Import TensorFlow Transform Source: https://www.tensorflow.org/hub/tutorials/semantic_approximate_nearest_neighbors Install the specific version of TensorFlow Transform and import its modules. ```python # TFT needs to be installed afterwards !pip install -q tensorflow_transform==0.24 import tensorflow_transform as tft import tensorflow_transform.beam as tft_beam ``` -------------------------------- ### Initialize Environment and Helper Functions Source: https://www.tensorflow.org/hub/tutorials/retrieval_with_tf_hub_universal_encoder_qa Imports necessary modules and defines functions for downloading SQuAD data, tokenizing sentences, and displaying retrieval results. ```python import json import nltk import os import pprint import random import simpleneighbors import urllib from IPython.display import HTML, display from tqdm.notebook import tqdm import tensorflow.compat.v2 as tf import tensorflow_hub as hub from tensorflow_text import SentencepieceTokenizer nltk.download('punkt') def download_squad(url): return json.load(urllib.request.urlopen(url)) def extract_sentences_from_squad_json(squad): all_sentences = [] for data in squad['data']: for paragraph in data['paragraphs']: sentences = nltk.tokenize.sent_tokenize(paragraph['context']) all_sentences.extend(zip(sentences, [paragraph['context']] * len(sentences))) return list(set(all_sentences)) # remove duplicates def extract_questions_from_squad_json(squad): questions = [] for data in squad['data']: for paragraph in data['paragraphs']: for qas in paragraph['qas']: if qas['answers']: questions.append((qas['question'], qas['answers'][0]['text'])) return list(set(questions)) def output_with_highlight(text, highlight): output = "
  • " i = text.find(highlight) while True: if i == -1: output += text break output += text[0:i] output += ''+text[i:i+len(highlight)]+'' text = text[i+len(highlight):] i = text.find(highlight) return output + "
  • \n" def display_nearest_neighbors(query_text, answer_text=None): query_embedding = model.signatures['question_encoder'](tf.constant([query_text]))['outputs'][0] search_results = index.nearest(query_embedding, n=num_results) if answer_text: result_md = '''

    Random Question from SQuAD:

      %s

    Answer:

      %s

    ''' % (query_text , answer_text) else: result_md = '''

    Question:

      %s

    ''' % query_text result_md += '''

    Retrieved sentences :

      ''' if answer_text: for s in search_results: result_md += output_with_highlight(s, answer_text) else: for s in search_results: result_md += '
    1. ' + s + '
    2. \n' result_md += "
    " display(HTML(result_md)) ``` -------------------------------- ### Install Dependencies Source: https://www.tensorflow.org/hub/tutorials/cross_lingual_similarity_with_tf_hub_multilingual_universal_encoder Install required packages including TensorFlow Text, Bokeh, and SimpleNeighbors for the environment. ```bash %%capture # Install the latest Tensorflow version. !pip install "tensorflow-text==2.11.*" !pip install bokeh !pip install simpleneighbors[annoy] !pip install tqdm ``` -------------------------------- ### Inspect Labeled Examples Source: https://www.tensorflow.org/hub/tutorials/cord_19_embeddings_keras Extracts a batch of examples from the training set and displays them in a pandas DataFrame with human-readable labels. ```python NUM_EXAMPLES = 10 TEXT_FEATURE_NAME = builder.info.supervised_keys[0] LABEL_NAME = builder.info.supervised_keys[1] def label2str(numeric_label): m = builder.info.features[LABEL_NAME].names return m[numeric_label] data = next(iter(train_data.batch(NUM_EXAMPLES))) pd.DataFrame({ TEXT_FEATURE_NAME: [ex.numpy().decode('utf8') for ex in data[0]], LABEL_NAME: [label2str(x) for x in data[1]] }) ``` -------------------------------- ### Preparing the Environment Source: https://www.tensorflow.org/hub/tutorials/image_enhancing Imports necessary libraries and sets the environment variable for TensorFlow Hub download progress. ```python import os import time from PIL import Image import numpy as np import tensorflow as tf import tensorflow_hub as hub import matplotlib.pyplot as plt os.environ["TFHUB_DOWNLOAD_PROGRESS"] = "True" ``` -------------------------------- ### Install gdown dependency Source: https://www.tensorflow.org/hub/tutorials/bangla_article_classifier Installs the gdown package using pip with the --no-use-pep517 flag to avoid specific setuptools issues. ```bash pip install gdown --no-use-pep517 ``` -------------------------------- ### Error log output Source: https://www.tensorflow.org/hub/tutorials/tf_hub_film_example Example of a CUDA device error log. ```text 2024-03-09 12:18:00.216249: E external/local_xla/xla/stream_executor/cuda/cuda_driver.cc:282] failed call to cuInit: CUDA_ERROR_NO_DEVICE: no CUDA-capable device is detected ``` -------------------------------- ### Import Dependencies and Setup Directory Source: https://www.tensorflow.org/hub/tutorials/tweening_conv3d Imports necessary libraries and defines a temporary directory for storing the BAIR dataset test split. ```python import tensorflow as tf import matplotlib.pyplot as plt import numpy as np import seaborn as sns import tensorflow_hub as hub import tensorflow_datasets as tfds from tensorflow_datasets.core import SplitGenerator from tensorflow_datasets.video.bair_robot_pushing import BairRobotPushingSmall import tempfile import pathlib TEST_DIR = pathlib.Path(tempfile.mkdtemp()) / "bair_robot_pushing_small/softmotion30_44k/test/" ``` -------------------------------- ### Model evaluation output Source: https://www.tensorflow.org/hub/tutorials/cropnet_on_device?hl=zh-cn Example output from the model evaluation process. ```text 59/59 [==============================] - 7s 101ms/step - loss: 0.9668 - accuracy: 0.8637 [0.9668245911598206, 0.863660454750061] ``` -------------------------------- ### Downloading Sample Image Source: https://www.tensorflow.org/hub/tutorials/image_enhancing Downloads a sample image for super-resolution processing using wget. ```bash wget "https://user-images.githubusercontent.com/12981474/40157448-eff91f06-5953-11e8-9a37-f6b5693fa03f.png" -O original.png ``` -------------------------------- ### Index construction output log Source: https://www.tensorflow.org/hub/tutorials/tf2_semantic_approximate_nearest_neighbors Example output showing the progress of loading items and building the ANNOY index. ```text rm: cannot remove 'index': No such file or directory rm: cannot remove 'index.mapping': No such file or directory Found 1 embedding file(s). Loading embeddings in file 1 of 1... 100000 items loaded to the index 200000 items loaded to the index 300000 items loaded to the index 400000 items loaded to the index 500000 items loaded to the index 600000 items loaded to the index 700000 items loaded to the index 800000 items loaded to the index 900000 items loaded to the index 1000000 items loaded to the index 1100000 items loaded to the index A total of 1103664 items added to the index Building the index with 100 trees... Index is successfully built. Saving index to disk... Index is saved to disk. Index file size: 1.61 GB Saving mapping to disk... Mapping is saved to disk. Mapping file size: 50.61 MB CPU times: user 9min 54s, sys: 53.9 s, total: 10min 48s Wall time: 5min 5s ```