小编Alo*_*her的帖子

使用数据增强层在 Tensorflow 2.7.0 上保存模型

尝试使用 Tensorflow 版本 2.7.0 保存具有数据增强层的模型时出现错误。

这是数据增强的代码:

input_shape_rgb = (img_height, img_width, 3)
data_augmentation_rgb = tf.keras.Sequential(
  [ 
    layers.RandomFlip("horizontal"),
    layers.RandomFlip("vertical"),
    layers.RandomRotation(0.5),
    layers.RandomZoom(0.5),
    layers.RandomContrast(0.5),
    RandomColorDistortion(name='random_contrast_brightness/none'),
  ]
)
Run Code Online (Sandbox Code Playgroud)

现在我像这样构建我的模型:

# Build the model
input_shape = (img_height, img_width, 3)

model = Sequential([
  layers.Input(input_shape),
  data_augmentation_rgb,
  layers.Rescaling((1./255)),

  layers.Conv2D(16, kernel_size, padding=padding, activation='relu', strides=1, 
     data_format='channels_last'),
  layers.MaxPooling2D(),
  layers.BatchNormalization(),

  layers.Conv2D(32, kernel_size, padding=padding, activation='relu'), # best 4
  layers.MaxPooling2D(),
  layers.BatchNormalization(),

  layers.Conv2D(64, kernel_size, padding=padding, activation='relu'), # best 3
  layers.MaxPooling2D(),
  layers.BatchNormalization(),

  layers.Conv2D(128, kernel_size, padding=padding, activation='relu'), # best 3
  layers.MaxPooling2D(),
  layers.BatchNormalization(),

  layers.Flatten(),
  layers.Dense(128, activation='relu'), # best 1 …
Run Code Online (Sandbox Code Playgroud)

python deep-learning keras tensorflow data-augmentation

12
推荐指数
1
解决办法
3665
查看次数

使用 Tensorflow 2 中保存的模型进行推理:如何控制输入/输出?

将我的代码从 TF1 调整为 TF2.6 我遇到了麻烦。我正在尝试向 inception resnet 添加一些自定义层,保存模型,然后加载并运行它。

from tensorflow.keras.layers import Dense                                                                                                                       
from tensorflow.keras.models import Model                                                                                                                       
from tensorflow.keras.applications.inception_resnet_v2 import InceptionResNetV2                                                                                 
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D                                                                                               
import tensorflow as tf                                                                                                                                         
import numpy as np                                                                                                                                              
from PIL import Image                                                                                                                                           
                                                                                                                                                                
export_path = "./save_test"                                                                                                                                     
                                                                                                                                                                
# Get model without top and add two layers                                                                                                                      
base_model = InceptionResNetV2(weights='imagenet', input_tensor=None, include_top=False)                                                                        
out = base_model.output                                                                                                                                         
out = GlobalAveragePooling2D()(out)                                                                                                                             
predictions = Dense(7, activation='softmax', name="output")(out)                                                                                                
                                                                                                                                                                
# Make new model using inputs from base model and custom outputs                                                                                                
model = Model(inputs=base_model.input, …
Run Code Online (Sandbox Code Playgroud)

python machine-learning keras tensorflow tensorflow2.0

12
推荐指数
1
解决办法
2301
查看次数

Tensorflow:应定义输入的通道维度

我是 Tensorflow 新手,正在尝试训练特定的深度学习神经网络。我正在使用 Tensorflow (2.11.0) 来获取深度神经网络模型,如下所述。我使用的数据也给出如下:

数据:

这是一些示例数据。为了方便起见,我们可以考虑数据中的 10 个样本。在这里,每个样本都有形状:(128,128)

可以将以下代码视为示例训练数据。

x_train = np.random.rand(10, 128, 128, 1)
Run Code Online (Sandbox Code Playgroud)

归一化层:

normalizer = tf.keras.layers.Normalization(axis=-1)
normalizer.adapt(x_train)
Run Code Online (Sandbox Code Playgroud)

构建模型:

def build_and_compile_model(norm):
    model = tf.keras.Sequential([
      norm,
      layers.Conv2D(128, 128, activation='relu'),
      layers.Conv2D(3, 3, activation='relu'),
      layers.Flatten(),
      layers.Dense(units=32, activation='relu'),
      layers.Dense(units=1)
    ])

    model.compile(loss='mean_absolute_error', optimizer=tf.keras.optimizers.Adam(0.001))
    
    return model
Run Code Online (Sandbox Code Playgroud)

当我做

dnn_model = build_and_compile_model(normalizer)
dnn_model.summary()
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

ValueError: The channel dimension of the inputs should be defined. The input_shape received is (None, None, None, None), where axis -1 (0-based) is the channel dimension, which found …
Run Code Online (Sandbox Code Playgroud)

python machine-learning deep-learning keras tensorflow

8
推荐指数
1
解决办法
404
查看次数

如何在NestJS中使用SOAP服务?

我必须为我的一个项目使用 NestJs 中的 SOAP 服务。我正在尝试从网络使用虚拟 SOAP 服务。以下是详细信息——

网址 -

http://webservices.oorsprong.org/websamples.countryinfo/CountryInfoService.wso?WSDL
Run Code Online (Sandbox Code Playgroud)

方法-POST

标头 -

Content-Type: text/xml
Run Code Online (Sandbox Code Playgroud)

请求正文 -

<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
    <Body>
        <CapitalCity xmlns="http://www.oorsprong.org/websamples.countryinfo">
            <sCountryISOCode>IN</sCountryISOCode>
        </CapitalCity>
    </Body>
</Envelope>
Run Code Online (Sandbox Code Playgroud)

响应正文 -

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        <m:CapitalCityResponse xmlns:m="http://www.oorsprong.org/websamples.countryinfo">
            <m:CapitalCityResult>New Delhi</m:CapitalCityResult>
        </m:CapitalCityResponse>
    </soap:Body>
</soap:Envelope>
Run Code Online (Sandbox Code Playgroud)

现在我无法弄清楚如何在我的 NestJs 虚拟应用程序中使用此服务。经过一段时间的探索,我发现了一个 Nestjs-soap 库,我安装并配置了它,如下所述 -

npm install nestjs-soap --save
Run Code Online (Sandbox Code Playgroud)

我的 app.module.ts -

import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { …
Run Code Online (Sandbox Code Playgroud)

xml soap web-services nestjs

7
推荐指数
1
解决办法
8303
查看次数

为多个时间序列创建 Tensorflow 数据集

我有多个时间序列数据,如下所示:

df = pd.DataFrame({'Time': np.tile(np.arange(5), 2),
                   'Object': np.concatenate([[i] * 5 for i in [1, 2]]),
                   'Feature1': np.random.randint(10, size=10),
                   'Feature2': np.random.randint(10, size=10)})

   Time  Object  Feature1  Feature2
0     0       1         3         3
1     1       1         9         2
2     2       1         6         6
3     3       1         4         0
4     4       1         7         7
5     0       2         4         8
6     1       2         3         7
7     2       2         1         1
8     3       2         7         5
9     4       2         1         7
Run Code Online (Sandbox Code Playgroud)

其中每个对象(1 和 2)都有自己的数据(实际数据中大约有 2000 个对象)。我想将这些数据分块输入 …

python time-series deep-learning tensorflow tensorflow-datasets

7
推荐指数
1
解决办法
689
查看次数

Keras Conv1D 输入是如何指定的?我似乎缺乏维度

我的输入是一个包含 64 个整数的数组。

model = Sequential()
model.add( Input(shape=(68,), name="input"))
model.add(Conv1D(64, 2, activation="relu", padding="same", name="convLayer"))
Run Code Online (Sandbox Code Playgroud)

我的训练集中有 10,000 个这样的数组。我应该指定这个以便 conv1D 工作吗?

我遇到了可怕的事情

ValueError: Input 0 of layer convLayer is incompatible with the layer: : expected min_ndim=3, found ndim=2. Full shape received: [None, 68]
Run Code Online (Sandbox Code Playgroud)

错误,我真的不明白我需要做什么。

python machine-learning keras tensorflow conv1d

6
推荐指数
1
解决办法
3889
查看次数

BatchDataSet:获取img数组和标签

这是我之前创建的用于适应模型的批量数据集:

train_ds = tf.keras.preprocessing.image_dataset_from_directory(
    train_path,
    label_mode = 'categorical', #it is used for multiclass classification. It is one hot encoded labels for each class
    validation_split = 0.2,     #percentage of dataset to be considered for validation
    subset = "training",        #this subset is used for training
    seed = 1337,                # seed is set so that same results are reproduced
    image_size = img_size,      # shape of input images
    batch_size = batch_size,    # This should match with model batch size
)




valid_ds = tf.keras.preprocessing.image_dataset_from_directory(
    train_path, …
Run Code Online (Sandbox Code Playgroud)

python deep-learning keras tensorflow tensorflow-datasets

6
推荐指数
1
解决办法
8801
查看次数

ValueError:保存张量流模型时无法创建数据集(名称已存在)

我正在尝试保存下面经过训练的模型。

resnet = ResNet50V2(input_shape=(im_size,im_size,3), weights='imagenet', include_top=False)
headModel = AvgPool2D(pool_size=(3,3))(resnet.output)
headModel = Flatten(name="flatten")(headModel)
headModel = Dense(256, activation="relu")(headModel)
headModel = Dropout(0.5)(headModel)
headModel = Dense(1, activation="sigmoid")(headModel)
resnet50v2 = Model(inputs=resnet.input, outputs=headModel)


resnet50v2.compile(loss='binary_crossentropy', optimizer=opt, metrics=METRICS)

history = resnet50v2.fit(
        datagen.flow(X_train, y_train, batch_size=32, subset='training'),
        batch_size=batch_size,
        epochs=150,
        steps_per_epoch=steps_per_epoch,    
        validation_data=datagen.flow(X_train, y_train, batch_size=8, subset='validation'))
Run Code Online (Sandbox Code Playgroud)

但是,每当我尝试使用以下命令保存它时:

resnet50v2.save('Saved_Models/resnet50.h5', save_format='h5')

我收到错误


ValueError                                Traceback (most recent call last)
/tmp/ipykernel_3252071/2034094124.py in <module>
----> 1 resnet50v2.save('Saved_Models/resnet50.h5', save_format='h5')

~/.local/lib/python3.8/site-packages/keras/utils/traceback_utils.py in error_handler(*args, **kwargs)
     65     except Exception as e:  # pylint: disable=broad-except
     66       filtered_tb = _process_traceback_frames(e.__traceback__)
---> 67       raise …
Run Code Online (Sandbox Code Playgroud)

python save keras tensorflow resnet

6
推荐指数
1
解决办法
7891
查看次数

张量流的 flat_map + window.batch() 对数据集/数组做了什么?

我正在学习一门有关使用 Tensorflow 进行时间序列预测的在线课程。用于将 Numpy 数组(TS)转换为 Tensorflow 数据集的函数是基于 LSTM 的模型,已经给出(带有我的注释行):

def windowed_dataset(series, window_size, batch_size, shuffle_buffer):
     # creating a tensor from an array
     dataset = tf.data.Dataset.from_tensor_slices(series)
     # cutting the tensor into fixed-size windows
     dataset = dataset.window(window_size + 1, shift=1, drop_remainder=True)  
     # joining windows into a batch?
     dataset = dataset.flat_map(lambda window: window.batch(window_size + 1))
     # separating row into features/label
     dataset = dataset.shuffle(shuffle_buffer).map(lambda window: (window[:-1], window[-1]))
     dataset = dataset.batch(batch_size).prefetch(1)
     return dataset
Run Code Online (Sandbox Code Playgroud)

这段代码工作正常,但我想更好地理解它,以便根据我的需要修改/调整它。

如果我删除dataset.flat_map(lambda window: window.batch(window_size + 1))操作,我会收到TypeError: '_VariantDataset' object is not …

python tensorflow tensorflow-datasets data-preprocessing

6
推荐指数
1
解决办法
1539
查看次数

Blenderbot 微调

我一直在尝试微调 HuggingFace 的对话模型:Blendebot。我已经尝试过官方拥抱脸网站上给出的传统方法,该方法要求我们使用 trainer.train() 方法来完成此操作。我使用 .compile() 方法尝试了它。我尝试过使用 PyTorch 以及 TensorFlow 对我的数据集进行微调。这两种方法似乎都失败了,并给我们一个错误,指出没有适用于 Blenderbot 模型的编译或训练方法。我什至在网上到处查看 Blenderbot 如何对我的自定义数据进行微调,但没有任何地方正确提及运行时不会抛出错误。我浏览了 Youtube 教程、博客和 StackOverflow 帖子,但没有一个回答这个问题。希望有人能在这里回复并帮助我。我也愿意使用其他 HuggingFace 对话模型进行微调。

这是我用来微调 Blenderbot 模型的链接。

微调方法:https://huggingface.co/docs/transformers/training

Blenderbot:https://huggingface.co/docs/transformers/model_doc/blenderbot

from transformers import BlenderbotTokenizer, BlenderbotForConditionalGeneration
mname = "facebook/blenderbot-400M-distill"
model = BlenderbotForConditionalGeneration.from_pretrained(mname)
tokenizer = BlenderbotTokenizer.from_pretrained(mname)


#FOR TRAINING: 

trainer = Trainer(
    model=model,
    args=training_args,
    train_dataset=small_train_dataset,
    eval_dataset=small_eval_dataset,
    compute_metrics=compute_metrics,
)
trainer.train()

#OR

model.compile(
    optimizer=tf.keras.optimizers.Adam(learning_rate=5e-5),
    loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
    metrics=tf.metrics.SparseCategoricalAccuracy(),
)

model.fit(tf_train_dataset, validation_data=tf_validation_dataset, epochs=3)
Run Code Online (Sandbox Code Playgroud)

这些都不起作用。

python nlp tensorflow pytorch huggingface-transformers

6
推荐指数
1
解决办法
920
查看次数