简体   繁体   English

在另一个模型中加载一个张量流模型,并连接两个模型

[英]Load a tensorflow model inside another one, and concatenate two models

The idea is to create a deconvolution model following my convolution model to see the importance of the learned pixels.这个想法是按照我的卷积模型创建一个反卷积模型,以查看学习像素的重要性。

Overview of the model模型概述

I am having problems that I cannot explain.我遇到了无法解释的问题。 The first is that when I have created my 3 models, if I train the convolution model and test my auto model everything works fine.首先是当我创建了我的 3 个模型时,如果我训练卷积模型并测试我的自动模型,一切正常。 Whereas if I create my 3 models then I load my convolution model (trained previously) and if I test my auto model everything looks as if I had not loaded the weights of my convolution model.而如果我创建了我的 3 个模型,然后我加载我的卷积模型(之前训练过),如果我测试我的自动模型,一切看起来都好像我没有加载我的卷积模型的权重。 My first question is how to load the weights into my convolution model and that they are taken into account for my auto model ?我的第一个问题是如何将权重加载到我的卷积模型中,并在我的汽车模型中考虑它们?

The second problem is perhaps related to the first one, it's that everything works well when I use predict on my auto model but if I decompose it, it doesn't work.第二个问题可能与第一个问题有关,当我在我的汽车模型上使用 predict 时一切正常,但如果我分解它,它就不起作用。 By decomposing I mean take a x_test predict it with convolution model then use what we get to predict the deconv model.通过分解,我的意思是使用卷积模型进行 x_test 预测,然后使用我们得到的结果来预测 deconv 模型。 I'm getting an error when I give the result of the convolution model to the deconv model.当我将卷积模型的结果提供给 deconv 模型时出现错误。 "Invalid argument: You must feed a value for placeholder tensor 'input_CNN' with dtype float" “无效参数:您必须使用 dtype float 为占位符张量‘input_CNN’提供一个值”

To create the auto model I'm doing :要创建我正在做的汽车模型:

inputs = layers.Input(shape(128,128,1),name='input_CNN')
model_auto = models.Model(inputs,model_deconv(model_conv(inputs)))

I can give you more details if needed.如果需要,我可以为您提供更多详细信息。

Edit :编辑 :

def CNN():
   inputs = layers.Input(shape=(128,128,1),name="input_CNN")
   layers_CNN = CNN_1_layers(inputs)
   model_conv = models.Model(inputs,layers_CNN,name='CNN_1')
   model_conv.compile(loss='categorical_crossentropy',optimizer=Adam(), metrics=[accuracy"])

   inputs_deconv = layers.Inputs(shape=(1,10),name="input_CNN_deconv")
   layers_CNN_deconv = CNN_1_deconv_layers(inputs_deconv)
   model_deconv = models.Model(inputs_deconv, layers_CNN_deconv,name="CNN_1_deconv")
   model_deconv.compile(loss='categorical_crossentropy',optimize=Adam(), metrics=["accuracy"])

   model_auto = models.Model(inputs,model_deconv(model_conv(inputs)))
   model_auto.compile(loss='categorical_crossentropy',optimize=Adam(), metrics=["accuracy"])

   return model_auto, model_conv, model_deconv

The last layer of my model_conv :我的 model_conv 的最后一层:

def CNN_1_layers(inputs):
   x = layers.Flattend(input_shape(1,1,10))(x)
   x = layers.Dense(10,activation='softmax')(x)

   return x

And the deconv layers :和 deconv 层:

def CNN_1_deconv_layers(inputs_deconv):
   x = layers.Reshape((1,1,10))(x)
   w1 = tf.get_variable("w1",shape=[4,4,128,10],dtype=tf.float32)
   x = layers.Lambda(lambda x: tf.nn.conv2d_transpose(x,w1,output_shape=[1,4,4,128],strides=(1,1,1,1),padding='VALID'),name='deconv_0')(x)
   ...
   return x


def apprentissage(model,nb_epoch=100):
   checkpoint = ModelCheckpoint(filepath="CNN_1.h5",monitor='vall_acc', save_best_only=True,save_weights_only=False,mode='auto')
   hist= model.fit(train_X, train_y, batch_size=128, nb_epoch=nb_epoch, validation_data=(test_X,test_y), callbacks=[checkpoint])
   return hist

I'm coding with another computer off the network so I have to rewrite everything by hand so I'm summarizing a bit.我正在用另一台网络外的计算机编码,所以我必须手工重写所有内容,所以我总结了一下。

I don't know if my comment helped to your first question or not (actually I did not expect your first problem to happen but anyway I gave you a suggestion).我不知道我的评论是否对你的第一个问题有帮助(实际上我没想到你的第一个问题会发生,但无论如何我给了你一个建议)。

For your second problem I've tested your code (man you had a lot of typo errors :D) and it had no problem.对于您的第二个问题,我已经测试了您的代码(伙计,您有很多拼写错误:D)并且没有问题。 I will write the code below:我会写下面的代码:

import numpy as np
import tensorflow as tf
from tensorflow import keras

# use below as you need
from tensorflow.keras.models import Sequential
from tensorflow.keras import layers
from tensorflow.keras.layers import *

# ----------------------

def CNN():
    inputs = layers.Input(shape=(128,128,1),name="input_CNN")
    layers_CNN = CNN_1_layers(inputs)
    model_conv = keras.Model(inputs,layers_CNN,name='CNN_1')
    model_conv.compile(loss='categorical_crossentropy',optimizer='adam', metrics=["accuracy"])

    inputs_deconv = layers.Input(shape=(1,10),name="input_CNN_deconv")
    layers_CNN_deconv = CNN_1_deconv_layers(inputs_deconv)
    model_deconv = keras.Model(inputs_deconv, layers_CNN_deconv,name="CNN_1_deconv")
    model_deconv.compile(loss='categorical_crossentropy',optimizer='adam', metrics=["accuracy"])

    model_auto = keras.Model(inputs,model_deconv(model_conv(inputs)))
    model_auto.compile(loss='categorical_crossentropy',optimizer='adam', metrics=["accuracy"])
    return model_auto, model_conv, model_deconv

# ----------------------

def CNN_1_layers(inputs):
   x = layers.Flatten(input_shape = (1,1,10))(inputs)
   x = layers.Dense(10,activation='softmax')(x)

   return x

# ----------------------

testX = np.random.rand(10, 128, 128, 1)

model_auto, model_conv, model_deconv = CNN()

print(model_auto(testX).shape)
conv_out = model_conv(testX)
deconv_out = model_deconv(conv_out)
print(deconv_out.shape)

Thanks Amin, You answered my question, the problem came from the type of my input variable.谢谢阿明,你回答了我的问题,问题出在我输入变量的类型上。 (uint8 instead of float32) The predict managed to work even if the type was wrong but by fixing this problem I can now decompose my model and everything works! (uint8 而不是 float32)即使类型错误,预测也能正常工作,但通过解决这个问题,我现在可以分解我的模型,一切正常!

It only stays the problem to load the data.它只是加载数据的问题。 If you tell me you didn't have a problem doing something similar I'll do some more testing.如果你告诉我你做类似的事情没有问题,我会做更多的测试。

好吧,我只是放弃并将我所有的库上传到最后一个它似乎可以工作的库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用tensorflow.keras连接两个模型 - Concatenate two models with tensorflow.keras Tensorflow:使用在一个模型中训练的权重在另一个模型中,不同的模型 - Tensorflow: Using weights trained in one model inside another, different model 连接来自两个预训练的 Tensorflow 模型的预测 - Concatenate predictions from two pre-trained Tensorflow models Tensorflow:在另一个 model 中使用 model 作为层 - Tensorflow: Use model inside another model as layer 如何在单个模板中呈现两个模型的内容,其中一个模型由外键通过 django 中的另一个模型链接? - How to render contents of two models in a single template where one model is linked by Foreign Key through another in django? 当两个模型同时训练不同的数据时,如何将一层从一个模型传递到另一个模型? - How to pass a layer from one model to another when two models are training on different data simultaneously? 如何在 TensorFlow 中堆叠两个模型以创建新模型? - How to stack two models to create a new model in TensorFlow? Tensorflow 模型预测两个值而不是一个 - Tensorflow model is predicting two values instead of one Tensorflow 加入两个模型 - Tensorflow joining two models 加入两个模型并在 Django 中连接? - Join two models and concatenate in Django?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM