From ead710fbb8d393da429bcb5f5c9a7a9cd81f2c57 Mon Sep 17 00:00:00 2001 From: wizardforcel <562826179@qq.com> Date: Fri, 9 Feb 2024 02:04:26 +0800 Subject: [PATCH] 2024-02-09 02:04:26 --- totrans/dl-cb_07.md | 8 ++++---- totrans/dl-cb_11.md | 2 +- totrans/dl-cb_15.md | 6 +++--- totrans/dl-cb_16.md | 2 +- totrans/prac-dl-cld_03.md | 24 ++++++++++++------------ totrans/prac-dl-cld_04.md | 20 ++++++++++---------- totrans/prac-dl-cld_05.md | 8 ++++---- totrans/prac-dl-cld_06.md | 12 ++++++------ totrans/prac-dl-cld_09.md | 2 +- totrans/prac-dl-cld_10.md | 8 ++++---- totrans/prac-dl-cld_11.md | 4 ++-- totrans/prac-dl-cld_13.md | 4 ++-- 12 files changed, 50 insertions(+), 50 deletions(-) diff --git a/totrans/dl-cb_07.md b/totrans/dl-cb_07.md index ea40a40..2af6455 100644 --- a/totrans/dl-cb_07.md +++ b/totrans/dl-cb_07.md @@ -273,10 +273,10 @@ char_cnn_model.evaluate(test_char_vectors, test_labels) 完成后,您应该有两个密钥和两个允许访问 API 的密钥。让我们将它们存储在相应的变量中: ```py -CONSUMER_KEY = '<*`your value`*>' -CONSUMER_SECRET = '<*`your value`*>' -ACCESS_TOKEN = '<*`your value`*>' -ACCESS_SECRET = '<*`your value`*>' +CONSUMER_KEY = '' +CONSUMER_SECRET = '' +ACCESS_TOKEN = '' +ACCESS_SECRET = '' ``` 现在我们可以构建一个认证对象: diff --git a/totrans/dl-cb_11.md b/totrans/dl-cb_11.md index f308b23..0c855ca 100644 --- a/totrans/dl-cb_11.md +++ b/totrans/dl-cb_11.md @@ -166,7 +166,7 @@ git clone https://github.com/yhenon/keras-frcnn.git 在我们下载了 VOC 2007/2012 数据集并解压缩后,我们可以开始训练: ```py -python train_frcnn.py -p <*`downloaded``-``data``-``set`*> +python train_frcnn.py -p ``` diff --git a/totrans/dl-cb_15.md b/totrans/dl-cb_15.md index 27422b0..636ba53 100644 --- a/totrans/dl-cb_15.md +++ b/totrans/dl-cb_15.md @@ -340,9 +340,9 @@ Goldfinger _ Weezer _ NoFx _ L/00 AWESOME.mp3 6 现在在三个常量中输入您的各种细节: ```py -CLIENT_ID = '<*`your client id`*>' -CLIENT_SECRET = '<*`your secret`*>' -USER_ID = '<*`your user id`*>' +CLIENT_ID = '' +CLIENT_SECRET = '' +USER_ID = '' ``` diff --git a/totrans/dl-cb_16.md b/totrans/dl-cb_16.md index b22485c..bcec979 100644 --- a/totrans/dl-cb_16.md +++ b/totrans/dl-cb_16.md @@ -123,7 +123,7 @@ Postgres 有许多扩展,值得探索,特别是对于处理大量数据的 通过给定的用户名/密码/数据库/主机组合,我们可以轻松地使用 Python 连接到 Postgres: ```py -connection_str = "dbname='*`%s`*' user='*`%s`*' password='*`%s`*' host='*`%s`*'" +connection_str = "dbname='%s' user='%s' password='%s' host='%s'" conn = psycopg2.connect(connection_str % (DB_NAME, USER, PWD, HOST)) ``` diff --git a/totrans/prac-dl-cld_03.md b/totrans/prac-dl-cld_03.md index 9056947..6f025a6 100644 --- a/totrans/prac-dl-cld_03.md +++ b/totrans/prac-dl-cld_03.md @@ -303,7 +303,7 @@ def model_maker(): base_model = MobileNet(include_top=False, input_shape = (IMG_WIDTH,IMG_HEIGHT,3)) for layer in base_model.layers[:]: - layer.trainable = False *`# Freeze the layers`* + layer.trainable = False # Freeze the layers input = Input(shape=(IMG_WIDTH, IMG_HEIGHT, 3)) custom_model = base_model(input) custom_model = GlobalAveragePooling2D()(custom_model) @@ -419,7 +419,7 @@ img_path = '../../sample_images/dog.jpg' img = image.load_img(img_path, target_size=(224,224)) img_array = image.img_to_array(img) expanded_img_array = np.expand_dims(img_array, axis=0) -preprocessed_img = preprocess_input(expanded_img_array) *`# Preprocess the image`* +preprocessed_img = preprocess_input(expanded_img_array) # Preprocess the image prediction = model.predict(preprocessed_img) print(prediction) print(validation_generator.class_indices) @@ -446,12 +446,12 @@ print(validation_generator.class_indices) 在进行这之前,让我们对整个验证数据集进行预测。首先,我们正确设置管道配置: ```py -*`# VARIABLES`* +# VARIABLES IMG_WIDTH, IMG_HEIGHT = 224, 224 VALIDATION_DATA_DIR = 'data/val_data/' VALIDATION_BATCH_SIZE = 64 -*`# DATA GENERATORS`* +# DATA GENERATORS validation_datagen = ImageDataGenerator( preprocessing_function=preprocess_input) validation_generator = validation_datagen.flow_from_directory( @@ -472,10 +472,10 @@ predictions = model.predict_generator(validation_generator) 为了使我们的分析更容易,我们创建一个字典,存储每个图像的索引到预测和实际值(预期预测): ```py -*`# prediction_table is a dict with index, prediction, ground truth`* +# prediction_table is a dict with index, prediction, ground truth prediction_table = {} for index, val in enumerate(predictions): - *`# get argmax index`* + # get argmax index index_of_highest_probability = np.argmax(val) value_of_highest_probability = val[index_of_highest_probability] prediction_table[index] = [value_of_highest_probability, @@ -503,7 +503,7 @@ def display(sorted_indices, message): 现在开始有趣的部分!哪些图像我们最有信心包含狗?让我们找到预测概率最高的图像(即最接近 1.0;参见图 3-9 中概率最高的图像)与预测类别狗(即 1): ```py -*`# Most confident predictions of 'dog'`* +# Most confident predictions of 'dog' indices = get_images_with_sorted_probabilities(prediction_table, get_highest_probability=True, label=1, number_of_items=10, only_false_predictions=False) @@ -518,7 +518,7 @@ display(indices[:10], message) 这些图像确实非常像狗。概率如此之高的原因之一可能是图像中包含了多只狗,以及清晰、明确的视图。现在让我们尝试找出我们最不确定包含狗的图像(请参见图 3-10): ```py -*`# Least confident predictions of 'dog'`* +# Least confident predictions of 'dog' indices = get_images_with_sorted_probabilities(prediction_table, get_highest_probability=False, label=1, number_of_items=10, only_false_predictions=False) @@ -537,7 +537,7 @@ display(indices[:10], message) 说到错误预测,当分类器信心较低时(即两类问题的概率接近 0.5 时),显然会出现错误预测。但我们不希望的是在我们的分类器对其预测非常确信时出现错误预测。让我们看看分类器确信包含狗的图像,尽管它们实际上是猫(参见图 3-11): ```py -*`# Incorrect predictions of 'dog'`* +# Incorrect predictions of 'dog' indices = get_images_with_sorted_probabilities(prediction_table, get_highest_probability=True, label=1, number_of_items=10, only_false_predictions=True) @@ -554,7 +554,7 @@ display(indices[:10], message) 对猫类重复相同一组问题,哪些图像更像猫(参见图 3-12)? ```py -*`# Most confident predictions of 'cat'`* +# Most confident predictions of 'cat' indices = get_images_with_sorted_probabilities(prediction_table, get_highest_probability=True, label=0, number_of_items=10, only_false_predictions=False) @@ -569,7 +569,7 @@ display(indices[:10], message) 有趣的是,其中许多图像中有多只猫。这证实了我们之前的假设,即多个清晰、明确的猫的视图可以给出更高的概率。另一方面,哪些图像我们对包含猫最不确定(参见图 3-13)? ```py -*`# Least confident predictions of 'cat'`* +# Least confident predictions of 'cat' indices = get_images_with_sorted_probabilities(prediction_table, get_highest_probability=False, label=0, number_of_items=10, only_false_predictions=False) @@ -586,7 +586,7 @@ display(indices[:10], message) 最后,我们的分类器错误地确信包含猫的图像是哪些(参见图 3-14)? ```py -*`# Incorrect predictions of 'cat'`* +# Incorrect predictions of 'cat' indices = get_images_with_sorted_probabilities(prediction_table, get_highest_probability=True, label=0, number_of_items=10, only_false_predictions=True) diff --git a/totrans/prac-dl-cld_04.md b/totrans/prac-dl-cld_04.md index b876e4e..d84b068 100644 --- a/totrans/prac-dl-cld_04.md +++ b/totrans/prac-dl-cld_04.md @@ -137,7 +137,7 @@ def get_file_list(root_dir): 然后,我们提供数据集的路径并调用该函数: ```py -*`# path to the datasets`* +# path to the datasets root_dir = '../../datasets/caltech101' filenames = sorted(get_file_list(root_dir)) ``` @@ -250,7 +250,7 @@ plt.imshow(mpimg.imread(filenames[indices[1]])) for i in range(6): random_image_index = random.randint(0,num_images) distances, indices = neighbors.kneighbors([featureList[random_image_index]]) - *`# don't take the first closest image as it will be the same image`* + # don't take the first closest image as it will be the same image similar_image_paths = [filenames[random_image_index]] + [filenames[indices[0][i]] for i in range(1,4)] plot_images(similar_image_paths, distances[0]) @@ -267,13 +267,13 @@ for i in range(6): 为了做到这一点,我们需要降低特征向量的维度,因为不可能在两个维度(纸张)中绘制一个 2,048 维向量(特征长度)。t-分布随机邻居嵌入(t-SNE)算法将高维特征向量降至 2D,提供数据集的鸟瞰视图,有助于识别聚类和附近图像。t-SNE 难以扩展到大型数据集,因此通过主成分分析(PCA)降低维度然后调用 t-SNE 是一个好主意: ```py -*`# Perform PCA over the features`* -num_feature_dimensions=100 *`# Set the number of features`* +# Perform PCA over the features +num_feature_dimensions=100 # Set the number of features pca = PCA(n_components = num_feature_dimensions) pca.fit(featureList) feature_list_compressed = pca.transform(featureList) -*`# For speed and clarity, we'll analyze about first half of the dataset.`* +# For speed and clarity, we'll analyze about first half of the dataset. selected_features = feature_list_compressed[:4000] selected_class_ids = class_ids[:4000] selected_filenames = filenames[:4000] @@ -282,7 +282,7 @@ tsne_results = TSNE(n_components=2,verbose=1,metric='euclidean') .fit_transform(selected_features) -*`# Plot a scatter plot from the generated t-SNE results`* +# Plot a scatter plot from the generated t-SNE results colormap = plt.cm.get_cmap('coolwarm') scatter_plot = plt.scatter(tsne_results[:,0],tsne_results[:,1], c = selected_class_ids, cmap=colormap) @@ -438,11 +438,11 @@ pca_accuracy = [] pca_time = [] for dimensions in pca_dimensions: - *`# Perform PCA`* + # Perform PCA pca = PCA(n_components = dimensions) pca.fit(feature_list) feature_list_compressed = pca.transform(feature_list[:]) - *`# Calculate accuracy over the compressed features`* + # Calculate accuracy over the compressed features accuracy, time_taken = accuracy_calculator(feature_list_compressed[:]) pca_time.append(time_taken) pca_accuracy.append(accuracy) @@ -558,7 +558,7 @@ annoy_index = AnnoyIndex(num_dimensions) *`# Length of item vector that will be` `indexed`* for i in range(num_items): annoy_index.add_item(i, dataset[i]) -annoy_index.build(40) *`# 40 trees`* +annoy_index.build(40) # 40 trees ``` 现在让我们看看搜索一个图像的五个最近邻居需要多长时间: @@ -699,7 +699,7 @@ input_shape = (224,224,3)) input = Input(shape=(224, 224, 3)) x = model(input) x = GlobalAveragePooling2D()(x) -*`# No dense or dropout layers`* +# No dense or dropout layers x = Dense(NUM_CLASSES, activation='softmax')(x) model_similarity_optimized = Model(inputs=input, outputs=x) ``` diff --git a/totrans/prac-dl-cld_05.md b/totrans/prac-dl-cld_05.md index 645028a..a6adf07 100644 --- a/totrans/prac-dl-cld_05.md +++ b/totrans/prac-dl-cld_05.md @@ -204,7 +204,7 @@ $ tree . ```py $ sudo docker run -p 8500:8500 \ ---mount type=bind,source=/home/{*`your_username`*}/what-if-stuff/colo/model/, +--mount type=bind,source=/home/{your_username}/what-if-stuff/colo/model/, target=/models/colo \ -e MODEL_NAME=colo -t tensorflow/serving ``` @@ -220,7 +220,7 @@ $ sudo docker run -p 8500:8500 \ | 推断地址 | `ip_addr:8500` | | 模型名称 | `/models/colo` | | 模型类型 | 分类 | -| 示例路径 | */home/{*`your_username`*}/what_if_stuff/colo/models/colo.tfrec*(注意:这必须是绝对路径) | +| 示例路径 | */home/{your_username}/what_if_stuff/colo/models/colo.tfrec*(注意:这必须是绝对路径) | 我们现在可以在 TensorBoard 中的浏览器中打开 What-If 工具,如图 5-5 所示。 @@ -233,7 +233,7 @@ What-If 工具还可以根据不同的分组对数据集进行可视化,如图 ```py from witwidget.notebook.visualization import WitConfigBuilder -*`# features are the test examples that we want to load into the tool`* +# features are the test examples that we want to load into the tool models = [model2, model3, model4] config_builder = WitConfigBuilder(test_examples).set_estimator_and_feature_spec(model1, features) @@ -901,7 +901,7 @@ clf.final_fit(x_train, y_train, x_test, y_test, retrain=True) y = clf.evaluate(x_test, y_test) print(y) -*`# Save the model as a pickle file`* +# Save the model as a pickle file clf.export_autokeras_model("model.pkl") visualize('.') diff --git a/totrans/prac-dl-cld_06.md b/totrans/prac-dl-cld_06.md index bea0486..e5c2745 100644 --- a/totrans/prac-dl-cld_06.md +++ b/totrans/prac-dl-cld_06.md @@ -218,7 +218,7 @@ model.fit(train_data, 为了加快读取速度,一个想法是将成千上万个文件合并成少数几个较大的文件。这正是 TFRecord 所做的。它将数据存储在高效的 Protocol Buffer(protobuf)对象中,使其更快速读取。让我们看看如何创建 TFRecord 文件: ```py -*`# Create TFRecord files`* +# Create TFRecord files import tensorflow as tf from PIL import Image @@ -408,8 +408,8 @@ dataset = dataset.interleave(tf.data.TFRecordDataset, num_parallel_calls=4) 根据我们的情况,我们可以使用以下两行中的一行: ```py -dataset = dataset.cache() *`# in-memory`* -dataset = dataset.cache(filename='tmp.cache') *`# on-disk`* +dataset = dataset.cache() # in-memory +dataset = dataset.cache(filename='tmp.cache') # on-disk ``` 值得注意的是,内存中的缓存是易失性的,因此只在每次运行的第二个 epoch 中显示性能改进。另一方面,基于文件的缓存将使每次运行更快(超出第一次运行的第一个 epoch)。 @@ -786,11 +786,11 @@ $ conda install tensorflow-mkl 比较以下两个例子: ```py -*`# Example 1`* +# Example 1 X = tf.multiply(A, B) Y = tf.multiply(C, D) -*`# Example 2`* +# Example 2 X = tf.multiply(A, B) Y = tf.multiply(`X`, C) ``` @@ -1031,7 +1031,7 @@ Keras 提供了 API 来修剪我们的模型。这个过程可以在训练过程 加载和初始化 GPU 驱动程序需要时间。您可能已经注意到,每次启动训练或推理作业时都会有延迟。对于频繁且短暂的作业来说,开销可能会迅速变得相对昂贵。想象一下一个图像分类程序,分类需要 10 秒,其中有 9.9 秒用于加载驱动程序。我们需要的是 GPU 驱动程序在后台保持预初始化,并在我们的训练作业启动时随时准备好。这就是 NVIDIA GPU Persistence Daemon 发挥作用的地方: ```py -$ nvidia-persistenced --user *`{YOUR_USERNAME}`* +$ nvidia-persistenced --user {YOUR_USERNAME} ``` 我们的 GPU 在空闲时间会消耗更多的瓦特,但它们将在下次启动程序时准备好并可用。 diff --git a/totrans/prac-dl-cld_09.md b/totrans/prac-dl-cld_09.md index 7aaf79f..572147a 100644 --- a/totrans/prac-dl-cld_09.md +++ b/totrans/prac-dl-cld_09.md @@ -152,7 +152,7 @@ def infer(): predictions = model.predict(image) max_index = numpy.argmax(predictions) - *`# We know the labels from the model we trained previously`* + # We know the labels from the model we trained previously if max_index == 0: return "Cat" else: diff --git a/totrans/prac-dl-cld_10.md b/totrans/prac-dl-cld_10.md index 21cf63d..1ae0feb 100644 --- a/totrans/prac-dl-cld_10.md +++ b/totrans/prac-dl-cld_10.md @@ -131,9 +131,9 @@ tfjs@latest/dist/tf.min.js"> const image = document.getElementById("image"); const predictionOutput = document.getElementById("prediction_output"); - *`// Load the model.`* + // Load the model. mobilenet.load().then(model => { - *`// Classify the image. And output the predictions`* + // Classify the image. And output the predictions model.classify(image).then(predictions => { predictionOutput.innerText = predictions[0].className; }); @@ -154,7 +154,7 @@ const path = 'https://storage.googleapis.com/tfjs- models/tfjs/mobilenet_v1_1.0_224/model.json'; model = tf.loadLayersModel(path).then(model => { - *`// Load model and output predictions here`* + // Load model and output predictions here }); ``` @@ -233,7 +233,7 @@ const numLayers = mobilenet.layers.length; // 88 // Create a new feature extraction model featureExtractionModel = tf.model({inputs: mobilenet.inputs, outputs: layer.output}); -featureExtractionModel.layers.length; *`// 82`* +featureExtractionModel.layers.length; // 82 ``` 在训练过程中,我们将保持特征提取模型不变。相反,我们在其顶部添加一组可训练的层来构建我们的分类器: diff --git a/totrans/prac-dl-cld_11.md b/totrans/prac-dl-cld_11.md index 33ff145..f4e8541 100644 --- a/totrans/prac-dl-cld_11.md +++ b/totrans/prac-dl-cld_11.md @@ -647,12 +647,12 @@ import coremltools model_spec = coremltools.utils.load_spec("MyModel.mlmodel") -*`# 16-bit conversion`* +# 16-bit conversion model_fp16_spec = coremltools.utils.convert_neural_network_spec_weights_to_fp16(model_spec) coremltools.utils.save_spec(model_fp16_spec, "MyModel_FP16.mlmodel") -*`# 8-bit or lower quantization`* +# 8-bit or lower quantization num_bits = 8 model_quant_spec = coremltools.models.neural_network.quantization_utils.quantize_weights(model_spec, diff --git a/totrans/prac-dl-cld_13.md b/totrans/prac-dl-cld_13.md index 9e79845..4122367 100644 --- a/totrans/prac-dl-cld_13.md +++ b/totrans/prac-dl-cld_13.md @@ -302,7 +302,7 @@ ML Kit 还使我们能够使用自定义训练的 TensorFlow Lite 模型进行 val image = FirebaseVisionImage.fromBitmap(bitmap) val detector = FirebaseVision.getInstance().visionLabelDetector val result = detector.detectInImage(image).addOnSuccessListener { labels -> - *`// Print labels`* + // Print labels } ``` @@ -600,7 +600,7 @@ Fritz 提供了一个移动 AI 开发的端到端解决方案,包括以下值 ```py let poseModel = FritzVisionPoseModel() guard let poseResult = try? poseModel.predict(image) else { return } - let imageWithPose = poseResult.drawPose() *`//` `O``v``e``r``l``a``y``s` `p``o``s``e` `o``n` `i``n``p``u``t``.`* + let imageWithPose = poseResult.drawPose() //` `O``v``e``r``l``a``y``s` `p``o``s``e` `o``n` `i``n``p``u``t``. ``` ![Fritz SDK 在不同移动设备上的对象检测功能性能,相对于 iPhone X](img/00163.jpeg)