gan生成器模型不保存

cclgggtu  于 2021-09-08  发布在  Java
关注(0)|答案(0)|浏览(254)

我训练这个模型没有问题,但是当要保存它的时候,我做不到。出于某种原因,我的模型在某一点上认为Tensor是非类型的。

import tensorflow as tf 
from tensorflow import keras
from keras.layers import *
from keras.models import *
import keras.backend as K
import matplotlib.pyplot as plt 
import numpy as np
from PIL import Image
import os
import random
import pickle
from google.colab import drive 
drive.mount('/content/drive')

BATCH_SIZE = 16
LR = 1e-4
EPOCHS = 100
DATASET_DIR = "drive/MyDrive/Imagine/images/"
SUBDATASET_SIZE = 64
filenames = os.listdir(DATASET_DIR)
MAX_DATASET = len( filenames) // SUBDATASET_SIZE

def AdaIN(x):
  mean = K.mean(x[0], axis = [0,1],keepdims=True)
  std = K.std(x[0], axis = [0,1], keepdims = True )
  y = (x[0] - mean) / std

  pool_size = [-1,1,1,y.shape[-1]]
  scale = K.reshape(x[1],pool_size)
  bias = K.reshape(x[2],pool_size)

  #print(x[0].shape)
  #print(scale.shape)
  #print(bias.shape)

  return y * scale + bias 

def fit(x):
  height = x[1].shape[1]
  width = x[1].shape[2]
  #print("input_noise:",x[0].shape)
  #print("input:",x[1].shape)

  return x[0][:,height*2,width*2,:]

def g_block(x,latent,input_noise,filters,kernel_size,stride):
    #print(i)
    #print(x.shape)
    gamma = Dense(filters)(latent)
    beta = Dense(filters)(latent)

    noise = Lambda(fit)([input_noise,x,i])
    noise = Dense(filters)(noise)
    #print(x.shape)

    out = UpSampling2D()(x)
    out = Conv2DTranspose(filters,kernel_size,stride)(out)
    out = add([out,noise])
    out = Lambda(AdaIN)([out,gamma,beta])
    out = LeakyReLU()(out)

    return out 

latent_input = Input([256])
noise_vec = Input([541,961,1])

latent = Dense(256,activation="relu")(latent_input)
latent = Dense(256,activation="relu")(latent)
latent = Dense(256,activation="relu")(latent)

tensor = Dense(1)(latent_input)
tensor = Lambda(lambda x: x * 0 + 1)(tensor)

tensor = Dense(2*6*256,activation="relu")(tensor)
tensor = Reshape((2,6,256))(tensor)
print(tensor.shape)

tensor = g_block(tensor,latent,noise_vec,256,(2,3),(2,1))
tensor = g_block(tensor,latent,noise_vec,128,3,2)
tensor = g_block(tensor,latent,noise_vec,64,(2,3),1)
tensor = g_block(tensor,latent,noise_vec,32,(2,5),1)
tensor = g_block(tensor,latent,noise_vec,16,(1,5),1)
tensor = g_block(tensor,latent,noise_vec,8,(1,9),1)

# tensor = UpSampling2D()(tensor)

tensor = Conv2D(3,1)(tensor)
output = Activation('sigmoid')(tensor)

generator = Model(inputs=[noise_vec,latent_input], outputs=output)
generator.summary()
generator.save('drive/MyDrive/generator_1')

错误:

TypeError                                 Traceback (most recent call last)
<ipython-input-219-42b9e6c20aef> in <module>()

        29 generator = Model(inputs=[noise_vec,latent_input], outputs=output)
         30 generator.summary()
    ---> 31 generator.save('drive/MyDrive/generator_1')
         32 
75 frames
<ipython-input-215-e7978059de2a> in fit(x)

    return x[0][:,height*2,width*2,:]

TypeError: unsupported operand type(s) for *: 'NoneType' and 'int'

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题