Issues when loading a sequential model in Python - python

I had trained and saved the following sequential architecture
model = Sequential([layers.Resizing(IMG_SIZE, IMG_SIZE), # Resize of the image
layers.Rescaling(1./255), # Image rescaling of a factor 1./255
layers.Conv2D(filters=32, kernel_size=(3, 3), activation='relu',
padding = 'same', input_shape=(IMG_SIZE,IMG_SIZE,3)),
layers.Dropout(0.2),
layers.BatchNormalization(),
layers.MaxPool2D(pool_size=(2, 2)),
layers.Conv2D(filters=64, kernel_size=(3, 3),
activation='relu', padding = 'same'),
layers.BatchNormalization(),
layers.MaxPool2D(pool_size=(2, 2)),
layers.Conv2D(filters=128, kernel_size=(3, 3),
activation='relu', padding = 'same'),
layers.BatchNormalization(),
layers.MaxPool2D(pool_size=(2, 2)),
layers.Conv2D(filters=256, kernel_size=(3, 3),
activation='relu', padding = 'same'),
layers.BatchNormalization(),
layers.MaxPool2D(pool_size=(2, 2)),
layers.Flatten(),
layers.Dropout(0.2),
layers.Dense(300, activation='relu'),
layers.Dense(150, activation='relu'),
layers.Dense(n_classes, activation='softmax')])
EPOCHS = 50
BATCH_SIZE = 8
history = model.fit(x=train_batches, validation_data=validation_batches,
steps_per_epoch=len(train_batches),
validation_steps=len(validation_batches),
epochs=EPOCHS,
batch_size=BATCH_SIZE)
model.save('my_model.h5')
when i load the saved model
model=tf.keras.models.load_model('my_model.h5')
this error occurre
Traceback (most recent call last):
File "C:\Users\Antonio Di Stolfo\Desktop\nuova codici 3\pythonProject2\main.py", line 88, in <module>
model=tf.keras.models.load_model('my_model.h5')
File "C:\Users\Antonio Di Stolfo\Desktop\nuova codici 3\cnnalzheimer\lib\site-packages\keras\utils\traceback_utils.py", line 70, in error_handler
raise e.with_traceback(filtered_tb) from None
File "C:\Users\Antonio Di Stolfo\Desktop\nuova codici 3\cnnalzheimer\lib\site-packages\keras\layers\convolutional\base_conv.py", line 409, in _get_input_channel
raise ValueError( ValueError: The channel dimension of the inputs should be defined. The input_shape received is (None, 224, 224, None), where axis -1 (0-based) is the channel dimension, which found to be `None`.
Does anyone of you know how to solve this error or how to define the channel dimension of the inputs?
I tried to load a saved model but occurred an error

Related

why does this error "input_2_1:0 is both fed and fetched" happen?

I am trying to show the output of intermediate layers in my network and I used the below code:
from keras import models
layer_outputs = [layer.output for layer in w_extraction.layers[:102]]
activation_model = models.Model(inputs=w_extraction.input, outputs=layer_outputs)
activations = activation_model.predict([x_test[8000:8001],wt_expand])
but it produces this error. I do not know why it produces this error! coudl you please help me with this issue
Traceback (most recent call last):
File "", line 1, in
activations = activation_model.predict([x_test[8000:8001],wt_expand])
File
"D:\software\Anaconda3\envs\py36\lib\site-packages\keras\engine\training.py",
line 1169, in predict
steps=steps)
File
"D:\software\Anaconda3\envs\py36\lib\site-packages\keras\engine\training_arrays.py",
line 294, in predict_loop
batch_outs = f(ins_batch)
File
"D:\software\Anaconda3\envs\py36\lib\site-packages\keras\backend\tensorflow_backend.py",
line 2715, in call
return self._call(inputs)
File
"D:\software\Anaconda3\envs\py36\lib\site-packages\keras\backend\tensorflow_backend.py",
line 2671, in _call
session)
File
"D:\software\Anaconda3\envs\py36\lib\site-packages\keras\backend\tensorflow_backend.py",
line 2623, in _make_callable
callable_fn = session._make_callable_from_options(callable_opts)
File
"D:\software\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\client\session.py",
line 1471, in _make_callable_from_options
return BaseSession._Callable(self, callable_options)
File
"D:\software\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\client\session.py",
line 1425, in init
session._session, options_ptr, status)
File
"D:\software\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\framework\errors_impl.py",
line 528, in exit
c_api.TF_GetCode(self.status.status))
InvalidArgumentError: input_2_1:0 is both fed and fetched.
my complete code is here:
from keras.layers import Input, Concatenate, GaussianNoise,Dropout,BatchNormalization
from keras.layers import Conv2D, AtrousConv2D
from keras.models import Model
from keras.datasets import mnist
from keras.callbacks import TensorBoard
from keras import backend as K
from keras import layers
import matplotlib.pyplot as plt
import tensorflow as tf
import keras as Kr
from keras.optimizers import SGD,RMSprop,Adam
from keras.callbacks import ReduceLROnPlateau
from keras.callbacks import EarlyStopping
from keras.callbacks import ModelCheckpoint
import numpy as np
import pylab as pl
import matplotlib.cm as cm
import keract
from matplotlib import pyplot
from keras import optimizers
from keras import regularizers
from tensorflow.python.keras.layers import Lambda;
#-----------------building w train---------------------------------------------
w_expand=np.zeros((49999,28,28),dtype='float32')
wv_expand=np.zeros((9999,28,28),dtype='float32')
wt_random=np.random.randint(2, size=(49999,4,4))
wt_random=wt_random.astype(np.float32)
wv_random=np.random.randint(2, size=(9999,4,4))
wv_random=wv_random.astype(np.float32)
w_expand[:,:4,:4]=wt_random
wv_expand[:,:4,:4]=wv_random
x,y,z=w_expand.shape
w_expand=w_expand.reshape((x,y,z,1))
x,y,z=wv_expand.shape
wv_expand=wv_expand.reshape((x,y,z,1))
#-----------------building w test---------------------------------------------
w_test = np.random.randint(2,size=(1,4,4))
w_test=w_test.astype(np.float32)
wt_expand=np.zeros((1,28,28),dtype='float32')
wt_expand[:,0:4,0:4]=w_test
wt_expand=wt_expand.reshape((1,28,28,1))
#-----------------------encoder------------------------------------------------
#------------------------------------------------------------------------------
wtm=Input((28,28,1))
image = Input((28, 28, 1))
conv1 = Conv2D(64, (5, 5), activation='relu', padding='same', name='convl1e',dilation_rate=(2,2))(image)
conv2 = Conv2D(64, (5, 5), activation='relu', padding='same', name='convl2e',dilation_rate=(2,2))(conv1)
conv3 = Conv2D(64, (5, 5), activation='relu', padding='same', name='convl3e',dilation_rate=(2,2))(conv2)
BN=BatchNormalization()(conv3)
encoded = Conv2D(1, (5, 5), activation='relu', padding='same',name='encoded_I',dilation_rate=(2,2))(BN)
add_const = Kr.layers.Lambda(lambda x: x[0] + x[1])
encoded_merged = add_const([encoded,wtm])
#-----------------------decoder------------------------------------------------
#------------------------------------------------------------------------------
deconv1 = Conv2D(64, (5, 5), activation='relu', padding='same', name='convl1d',dilation_rate=(2,2))(encoded_merged)
deconv2 = Conv2D(64, (5, 5), activation='relu', padding='same', name='convl2d',dilation_rate=(2,2))(deconv1)
deconv3 = Conv2D(64, (5, 5), activation='relu',padding='same', name='convl3d',dilation_rate=(2,2))(deconv2)
deconv4 = Conv2D(64, (5, 5), activation='relu',padding='same', name='convl4d',dilation_rate=(2,2))(deconv3)
BNd=BatchNormalization()(deconv3)
#DrO2=Dropout(0.25,name='DrO2')(BNd)
decoded = Conv2D(1, (5, 5), activation='sigmoid', padding='same', name='decoder_output',dilation_rate=(2,2))(BNd)
#model=Model(inputs=image,outputs=decoded)
model=Model(inputs=[image,wtm],outputs=decoded)
decoded_noise = GaussianNoise(0.5)(decoded)
#----------------------w extraction------------------------------------
convw1 = Conv2D(64, (3,3), activation='relu', padding='same', name='conl1w',dilation_rate=(2,2))(decoded_noise)
convw2 = Conv2D(64, (3, 3), activation='relu', padding='same', name='convl2w',dilation_rate=(2,2))(convw1)
convw3 = Conv2D(64, (3, 3), activation='relu', padding='same', name='conl3w',dilation_rate=(2,2))(convw2)
convw4 = Conv2D(64, (3, 3), activation='relu', padding='same', name='conl4w',dilation_rate=(2,2))(convw3)
convw5 = Conv2D(64, (3, 3), activation='relu', padding='same', name='conl5w',dilation_rate=(2,2))(convw4)
convw6 = Conv2D(64, (3, 3), activation='relu', padding='same', name='conl6w',dilation_rate=(2,2))(convw5)
pred_w = Conv2D(1, (1, 1), activation='sigmoid', padding='same', name='reconstructed_W',dilation_rate=(2,2))(convw6)
w_extraction=Model(inputs=[image,wtm],outputs=[decoded,pred_w])
#----------------------training the model--------------------------------------
#------------------------------------------------------------------------------
#----------------------Data preparation----------------------------------------
(x_train, _), (x_test, _) = mnist.load_data()
x_validation=x_train[1:10000,:,:]
x_train=x_train[10001:60000,:,:]
#
x_train = x_train.astype('float32') / 255.
x_test = x_test.astype('float32') / 255.
x_validation = x_validation.astype('float32') / 255.
x_train = np.reshape(x_train, (len(x_train), 28, 28, 1)) # adapt this if using `channels_first` image data format
x_test = np.reshape(x_test, (len(x_test), 28, 28, 1)) # adapt this if using `channels_first` image data format
x_validation = np.reshape(x_validation, (len(x_validation), 28, 28, 1))
#---------------------compile and train the model------------------------------
w_extraction.compile(optimizer='adam', loss={'decoder_output':'mse','reconstructed_W':'binary_crossentropy'}, loss_weights={'decoder_output': 0.45, 'reconstructed_W': 1.0},metrics=['mae'])
es = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=20)
#rlrp = ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=20, min_delta=1E-4, verbose=1)
mc = ModelCheckpoint('best_model_5x5F_dil_Los751.h5', monitor='val_loss', mode='min', verbose=1, save_best_only=True)
history=w_extraction.fit([x_train,w_expand], [x_train,w_expand],
epochs=200,
batch_size=16,
validation_data=([x_validation,wv_expand], [x_validation,wv_expand]),
callbacks=[TensorBoard(log_dir='E:concatnatenetwork', histogram_freq=0, write_graph=False),es,mc])
You can't both feed and fetch a placeholder (i.e. the underlying Tensor of an Input layer). See this answer for more information. Therefore, you must exclude the input Tensors of w_extraction model from the outputs of activation_model. One way to do this is to filter them based on layer name:
layer_outputs = [layer.output for layer in w_extraction.layers[:102] if not layer.name.startswith('input')]

Keras with Tensorflow backend---Memoryerror in model.fit() with checkpoint callbacks

I'm trying to train an autoencoder. It keeps getting Memoryerror from Keras at model.fit(), it always occurs when i add validation-related parameters to model.fit like validation_split.
Error:
Traceback (most recent call last):
File "/root/abnormal-spatiotemporal-ae/start_train.py", line 53, in <module>
train(dataset=dataset, job_folder=job_folder, logger=logger)
File "/root/abnormal-spatiotemporal-ae/classifier.py", line 109, in train
callbacks=[snapshot, earlystop, history_log]
File "/root/anaconda3/envs/py35/lib/python3.5/site-packages/keras/engine/training.py",
line 990, in fit
y, val_y = (slice_arrays(y, 0, split_at),
File "/root/anaconda3/envs/py35/lib/python3.5/site-packages/keras/utils/generic_utils.py",
line 528, in slice_arrays
return [None if x is None else x[start:stop] for x in arrays]
File "/root/anaconda3/envs/py35/lib/python3.5/site-packages/keras/utils/generic_utils.py",
line 528, in
return [None if x is None else x[start:stop] for x in arrays]
File "/root/anaconda3/envs/py35/lib/python3.5/site-packages/keras/utils/io_utils.py",
line 110, in getitem
return self.data[idx]
File "h5py/_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
File "h5py/_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
File "/root/anaconda3/envs/py35/lib/python3.5/site-packages/h5py/_hl/dataset.py",
line 485, in getitem
arr = numpy.ndarray(mshape, new_dtype, order='C')
MemoryError
Code:
data = HDF5Matrix(os.path.join(video_root_path, '{0}/{0}_train_t{1}.h5'.format(dataset, time_length)),
'data')
snapshot = ModelCheckpoint(os.path.join(job_folder,
'model_snapshot_e{epoch:03d}_{val_loss:.6f}.h5'))
earlystop = EarlyStopping(patience=10)
history_log = LossHistory(job_folder=job_folder, logger=logger)
logger.info("Initializing training...")
history = model.fit(
data,
data,
batch_size=batch_size,
epochs=nb_epoch,
validation_split=0.15,
shuffle='batch',
callbacks=[snapshot, earlystop, history_log]
)
The code will run correctly when i remove validation_split=0.15 in model.fit and snapshot in callbacks.
data variable contains all processed images from training dataset,
its shape is (15200, 8, 224, 224, 1) and size is 6101401600
This code is used on computer with 64GB RAM and a Tesla P100, no worry for memory space, and my python is 64-bit
Model:
input_tensor = Input(shape=(t, 224, 224, 1))
conv1 = TimeDistributed(Conv2D(128, kernel_size=(11, 11), padding='same', strides=(4, 4), name='conv1'),
input_shape=(t, 224, 224, 1))(input_tensor)
conv1 = TimeDistributed(BatchNormalization())(conv1)
conv1 = TimeDistributed(Activation('relu'))(conv1)
conv2 = TimeDistributed(Conv2D(64, kernel_size=(5, 5), padding='same', strides=(2, 2), name='conv2'))(conv1)
conv2 = TimeDistributed(BatchNormalization())(conv2)
conv2 = TimeDistributed(Activation('relu'))(conv2)
convlstm1 = ConvLSTM2D(64, kernel_size=(3, 3), padding='same', return_sequences=True, name='convlstm1')(conv2)
convlstm2 = ConvLSTM2D(32, kernel_size=(3, 3), padding='same', return_sequences=True, name='convlstm2')(convlstm1)
convlstm3 = ConvLSTM2D(64, kernel_size=(3, 3), padding='same', return_sequences=True, name='convlstm3')(convlstm2)
deconv1 = TimeDistributed(Conv2DTranspose(128, kernel_size=(5, 5), padding='same', strides=(2, 2), name='deconv1'))(convlstm3)
deconv1 = TimeDistributed(BatchNormalization())(deconv1)
deconv1 = TimeDistributed(Activation('relu'))(deconv1)
decoded = TimeDistributed(Conv2DTranspose(1, kernel_size=(11, 11), padding='same', strides=(4, 4), name='deconv2'))(
deconv1)
This question faced the same problem. Here the explanation was, that there were too much data points before the flattening layer. This caused the RAM to overflow. This was solved by adding additional convolution layer.

Error in Image Segmentation using Unet and Keras

I am using a Unet model for satellite image segmentation with inputs 512x512x3. But on executing the model i am getting the following error:
ValueError: Cannot feed value of shape (3, 512, 512) for Tensor 'conv2d_19_target:0', which has shape '(?, ?, ?, ?)'. the code for the Unet model is :
from __future__ import print_function
import os
from skimage.transform import resize
from skimage.io import imsave
import numpy as np
from keras.models import Model
from keras.layers import Input, concatenate, Conv2D, MaxPooling2D, Conv2DTranspose
from keras.optimizers import Adam
from keras.callbacks import ModelCheckpoint
from keras import backend as K
from data import load_train_data, load_test_data
K.set_image_data_format('channels_last') # TF dimension ordering in this code
img_rows = 512
img_cols = 512
image_channels=3
smooth = 1.
OUTPUT_MASK_CHANNELS = 1
def dice_coef(y_true, y_pred):
y_true_f = K.flatten(y_true)
y_pred_f = K.flatten(y_pred)
intersection = K.sum(y_true_f * y_pred_f)
return (2. * intersection + smooth) / (K.sum(y_true_f) + K.sum(y_pred_f) + smooth)
def dice_coef_loss(y_true, y_pred):
return -dice_coef(y_true, y_pred)
def get_unet():
inputs = Input((img_rows, img_cols, 3))
conv1 = Conv2D(32, (3, 3), activation='relu', padding='same')(inputs)
conv1 = Conv2D(32, (3, 3), activation='relu', padding='same')(conv1)
pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)
conv2 = Conv2D(64, (3, 3), activation='relu', padding='same')(pool1)
conv2 = Conv2D(64, (3, 3), activation='relu', padding='same')(conv2)
pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)
conv3 = Conv2D(128, (3, 3), activation='relu', padding='same')(pool2)
conv3 = Conv2D(128, (3, 3), activation='relu', padding='same')(conv3)
pool3 = MaxPooling2D(pool_size=(2, 2))(conv3)
conv4 = Conv2D(256, (3, 3), activation='relu', padding='same')(pool3)
conv4 = Conv2D(256, (3, 3), activation='relu', padding='same')(conv4)
pool4 = MaxPooling2D(pool_size=(2, 2))(conv4)
conv5 = Conv2D(512, (3, 3), activation='relu', padding='same')(pool4)
conv5 = Conv2D(512, (3, 3), activation='relu', padding='same')(conv5)
up6 = concatenate([Conv2DTranspose(256, (2, 2), strides=(2, 2), padding='same')(conv5), conv4], axis=3)
conv6 = Conv2D(256, (3, 3), activation='relu', padding='same')(up6)
conv6 = Conv2D(256, (3, 3), activation='relu', padding='same')(conv6)
up7 = concatenate([Conv2DTranspose(128, (2, 2), strides=(2, 2), padding='same')(conv6), conv3], axis=3)
conv7 = Conv2D(128, (3, 3), activation='relu', padding='same')(up7)
conv7 = Conv2D(128, (3, 3), activation='relu', padding='same')(conv7)
up8 = concatenate([Conv2DTranspose(64, (2, 2), strides=(2, 2), padding='same')(conv7), conv2], axis=3)
conv8 = Conv2D(64, (3, 3), activation='relu', padding='same')(up8)
conv8 = Conv2D(64, (3, 3), activation='relu', padding='same')(conv8)
up9 = concatenate([Conv2DTranspose(32, (2, 2), strides=(2, 2), padding='same')(conv8), conv1], axis=3)
conv9 = Conv2D(32, (3, 3), activation='relu', padding='same')(up9)
conv9 = Conv2D(32, (3, 3), activation='relu', padding='same')(conv9)
conv_final = Conv2D(OUTPUT_MASK_CHANNELS, (1, 1),activation='sigmoid')(conv9)
#conv_final = Activation('sigmoid')(conv_final)
model = Model(inputs, conv_final, name="ZF_UNET_224")
#conv10 = Conv2D(1, (1, 1), activation='sigmoid')(conv9)
#model = Model(inputs=[inputs], outputs=[conv10])
model.compile(optimizer=Adam(lr=1e-5), loss=dice_coef_loss, metrics=[dice_coef])
return model
def preprocess(imgs):
imgs_p = np.ndarray((imgs.shape[0], img_rows, img_cols), dtype=np.uint8)
for i in range(imgs.shape[0]):
imgs_p[i] = resize(imgs[i], (img_cols, img_rows), preserve_range=True)
imgs_p = imgs_p[..., np.newaxis]
return imgs_p
def train_and_predict():
print('-'*30)
print('Loading and preprocessing train data...')
print('-'*30)
imgs_train, imgs_mask_train = load_train_data()
#imgs_train = preprocess(imgs_train)
#imgs_mask_train = preprocess(imgs_mask_train)
imgs_train = imgs_train.astype('float32')
mean = np.mean(imgs_train) # mean for data centering
std = np.std(imgs_train) # std for data normalization
imgs_train -= mean
imgs_train /= std
imgs_mask_train = imgs_mask_train.astype('float32')
imgs_mask_train /= 255. # scale masks to [0, 1]
print('-'*30)
print('Creating and compiling model...')
print('-'*30)
model = get_unet()
model_checkpoint = ModelCheckpoint('weights.h5', monitor='val_loss', save_best_only=True)
print('-'*30)
print('Fitting model...')
print('-'*30)
model.fit(imgs_train, imgs_mask_train, batch_size=3, epochs=20, verbose=2, shuffle=True,
validation_split=0.2,
callbacks=[model_checkpoint])
print('-'*30)
print('Loading and preprocessing test data...')
print('-'*30)
imgs_test, imgs_id_test = load_test_data()
imgs_test = preprocess(imgs_test)
imgs_test = imgs_test.astype('float32')
imgs_test -= mean
imgs_test /= std
print('-'*30)
print('Loading saved weights...')
print('-'*30)
model.load_weights('weights.h5')
print('-'*30)
print('Predicting masks on test data...')
print('-'*30)
imgs_mask_test = model.predict(imgs_test, verbose=1)
np.save('imgs_mask_test.npy', imgs_mask_test)
print('-' * 30)
print('Saving predicted masks to files...')
print('-' * 30)
pred_dir = 'preds'
if not os.path.exists(pred_dir):
os.mkdir(pred_dir)
for image, image_id in zip(imgs_mask_test, imgs_id_test):
image = (image[:, :, 0] * 255.).astype(np.uint8)
imsave(os.path.join(pred_dir, str(image_id) + '_pred.png'), image)
if __name__ == '__main__':
train_and_predict()
The error traceback is as follows:
File "/home/deeplearning/Downloads/Models/ultrasound-nerve-segmentation-master/train.py", line 158, in <module> train_and_predict()
File "/home/deeplearning/Downloads/Models/ultrasound-nerve-segmentation-master/train.py", line 124, in train_and_predict callbacks=[model_checkpoint])
File "/home/deeplearning/anaconda3/envs/myenv/lib/python3.6/site-packages/keras/engine/training.py", line 1037, in fit
validation_steps=validation_steps)
File "/home/deeplearning/anaconda3/envs/myenv/lib/python3.6/site-packages/keras/engine/training_arrays.py", line 199, in fit_loop
outs = f(ins_batch)
File "/home/deeplearning/anaconda3/envs/myenv/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 2672, in __call__
return self._legacy_call(inputs)
File "/home/deeplearning/anaconda3/envs/myenv/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 2654, in _legacy_call
**self.session_kwargs)
File "/home/deeplearning/anaconda3/envs/myenv/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 767, in run
run_metadata_ptr)
File "/home/deeplearning/anaconda3/envs/myenv/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 944, in _run
% (np_val.shape, subfeed_t.name, str(subfeed_t.get_shape())))
ValueError: Cannot feed value of shape (3, 512, 512) for Tensor 'conv2d_19_target:0', which has shape '(?, ?, ?, ?)'
Plz help me finding what wrong is going in it
You set K.set_image_data_format('channels_last'), but your input image (3 X 512 X 512) has channels first. Either change to K.set_image_data_format('channels_first')(which may not work for the UNET), or permute the dimensions of your input image with np.tranpose to have the input shape (512,512,3).

How does Keras(Tensorflow) calc. shape of last layer Tensor?

I'm currently working on an image generating Conv NN and an audio generating Recurrent NN. I built for both the generators but for some reason the build_audio_generator model has in its last layer a Tensor (Tensor("model_4/sequential_4/activation_4/Tanh:0") with shape (?, 1) instead of (?, 28, 28, 1) as needed. My question, how do I have to change the code of build_audio_generator so that it has the same shape(?, 28, 28, 1) as build_generator?
Code:
def build_generator(latent_dim, channels, num_classes):
model = Sequential()
model.add(Dense(128 * 7 * 7, activation="relu", input_dim=latent_dim))
model.add(Reshape((7, 7, 128)))
model.add(BatchNormalization(momentum=0.8))
model.add(UpSampling2D())
model.add(Conv2D(128, kernel_size=3, padding="same"))
model.add(Activation("relu"))
model.add(BatchNormalization(momentum=0.8))
model.add(UpSampling2D())
model.add(Conv2D(64, kernel_size=3, padding="same"))
model.add(Activation("relu"))
model.add(BatchNormalization(momentum=0.8))
model.add(Conv2D(channels, kernel_size=3, padding='same'))
model.add(Activation("tanh"))
model.summary()
noise = Input(shape=(latent_dim,))
label = Input(shape=(1,), dtype='int32')
label_embedding = Flatten()(Embedding(num_classes, 100)(label))
model_input = multiply([noise, label_embedding])
img = model(model_input)
return Model([noise, label], img)
def build_audio_generator(latent_dim, num_classes):
model = Sequential()
model.add(LSTM(512, input_dim=latent_dim, return_sequences=True))
model.add(Dropout(0.3))
model.add(LSTM(512, return_sequences=True))
model.add(Dropout(0.3))
model.add(LSTM(512))
model.add(Dense(256))
model.add(Dropout(0.3))
model.add(Dense(num_classes))
model.add(Activation('tanh'))
model.summary()
noise = Input(shape=(None, latent_dim,))
label = Input(shape=(1,), dtype='int32')
label_embedding = Flatten()(Embedding(num_classes, 100)(label))
model_input = multiply([noise, label_embedding])
sound = model(model_input)
return Model([noise, label], sound)
# Build the generator
generator = build_generator(100, 3, 1)
audio_generator = build_audio_generator(100, 1)
# The generator takes noise and the target label as input
# and generates the corresponding digit of that label
noise = Input(shape=(None, 100,))
label = Input(shape=(1,))
img = generator([noise, label])
audio = audio_generator([noise, label])
print('Audio: '+ str(audio))
print('Audio shape: ' + str(audio.shape))
print('IMG: '+str(img))
print('IMG shape: ' + str(img.shape))
Console output:
Audio: Tensor("model_4/sequential_4/activation_4/Tanh:0", shape=(?, 1), dtype=float32)
Audio shape: (?, 1)
IMG: Tensor("model_3/sequential_3/activation_3/Tanh:0", shape=(?, 28, 28, 1), dtype=float32)
IMG shape: (?, 28, 28, 1)
I think you would want 3D for audio instead, no?
Just keep return_sequences=True in all LSTMs.

KeyError when trying to save a concatenated model

You can check this question for problem description. I create 7 models by a for loop then train them (didn't mention training process in the code):
for i in range(1, 8):
j=str(i)
img_input = Input(shape=(img_width,img_height,1),name='input')
x = Conv2D(32, (3,3), activation='relu', padding='same', name='conv1-'+j)(img_input)
x = MaxPooling2D((2, 2), strides=(2, 2), name='pool1-'+j)(x)
x = Conv2D(64, (3,3), activation='relu', padding='same', name='conv2-'+j)(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='pool2-'+j)(x)
x = Conv2D(128, (3,3), activation='relu', padding='same', name='conv3-'+j)(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='pool3-'+j)(x)
x = Flatten()(x)
x = Dense(512, name='dense1-'+j)(x)
x = Dense(512, name='dense2-'+j)(x)
predictions = Dense(6, activation='softmax', name='predictions-'+j)(x)
model = Model(inputs=img_input, outputs=predictions)
model.compile(optimizer='Adam', loss='binary_crossentropy',
metrics=['accuracy'])
Popping the last softmax layer of each model and saving the rest of the models into a list named as models[]:
inputTensor = Input(shape=(img_width,img_height,1),name='inputs')
for i in range (1,8): #save models into a models[] list
j = str(i)
models[i] = load_model(path+j+"/weights.best.hdf5") #load models from disk
models[i].layers.pop() #Pop last layer
models[i].outputs= [models[i].layers[-1].output] #Fix layer pop bug
models[i].layers[-1].outbound_nodes = [] #Fix layer pop bug
for layer in models[i].layers: #Make intermediate
layer.trainable=False #layers untrainable
Implementing a concat to all of these models and then trying to save the finalModel:
outputTensors= [models[m](inputTensor) for m in models]
output = Concatenate()(outputTensors)
predictions = Dense(6, activation='softmax', name='predictionss')(output)
model=Model(inputTensor, predictions)
model.compile(optimizer='Adam', loss='binary_crossentropy', metrics=['accuracy'])
model.save('model')
But when saving occurs it throws an error:
Traceback (most recent call last):
File "<ipython-input-43-6221fb2664c1>", line 10, in <module>
model.save('model')
File "C:\Anaconda3\envs\tensorflow\lib\site-packages\keras\engine\topology.py", line 2553, in save
save_model(self, filepath, overwrite, include_optimizer)
File "C:\Anaconda3\envs\tensorflow\lib\site-packages\keras\models.py", line 107, in save_model
'config': model.get_config()
File "C:\Anaconda3\envs\tensorflow\lib\site-packages\keras\engine\topology.py", line 2326, in get_config
layer_config = layer.get_config()
File "C:\Anaconda3\envs\tensorflow\lib\site-packages\keras\engine\topology.py", line 2390, in get_config
new_node_index = node_conversion_map[node_key]
KeyError: 'predictions-1_ib-0
And I can't overcome this error.
Appreciate your helps.

Categories

Resources