Related
I got a dataset for motion blur. 29216 good images and 29216 blurry images with a 20% train test split. Initially, I was only using a dataset of 2000 total images so my autoencoder looked like this:
# Below is a custom data loader.
def load_image(file, target_size):
image = tf.keras.preprocessing.image.load_img(file, target_size=target_size)
image = tf.keras.preprocessing.image.img_to_array(image).astype('float32') / 255
return image
clean_frames = []
blurry_frames = []
extensions = ['.jpg', 'jpeg', '.png']
for file in tqdm(sorted(os.listdir(good_frames))):
if any(extension in file for extension in extensions):
file_path = os.path.join(good_frames, file)
clean_frames.append(load_image(file_path, (128,128)))
clean_frames = np.array(clean_frames)
for file in tqdm(sorted(os.listdir(bad_frames))):
if any(extension in file for extension in extensions):
file_path = os.path.join(bad_frames, file)
blurry_frames.append(load_image(file_path, (128,128)))
blurry_frames = np.array(blurry_frames)
print('number of clean frames: ', len(clean_frames))
print('number of blurry frames: ', len(blurry_frames))
# Train test split
x_train, x_test, y_train, y_test = train_test_split(clean_frames, blurry_frames, test_size=0.2, random_state=42)
# Network Parameters
input_shape = (128, 128, 3)
batch_size = 32
kernel_size = 3
latent_dim = 256
inputs = Input(shape = input_shape, name = 'encoder_input')
x = inputs
# Layers of the encoder
x = Conv2D(filters=64, kernel_size=kernel_size, strides=2, activation='relu', padding='same')(x)
x = Conv2D(filters=128, kernel_size=kernel_size, strides=2, activation='relu', padding='same')(x)
x = Conv2D(filters=256, kernel_size=kernel_size, strides=2, activation='relu', padding='same')(x)
shape = K.int_shape(x)
x = Flatten()(x)
latent = Dense(latent_dim, name='latent_vector')(x)
encoder = Model(inputs, latent, name='encoder')
encoder.summary()
# Layers of the decoder
latent_inputs = Input(shape=(latent_dim,), name='decoder_input')
x = Dense(shape[1]*shape[2]*shape[3])(latent_inputs)
x = Reshape((shape[1], shape[2], shape[3]))(x)
x = Conv2DTranspose(filters=256,kernel_size=kernel_size, strides=2, activation='relu', padding='same')(x)
x = Conv2DTranspose(filters=128,kernel_size=kernel_size, strides=2, activation='relu', padding='same')(x)
x = Conv2DTranspose(filters=64,kernel_size=kernel_size, strides=2, activation='relu', padding='same')(x)
outputs = Conv2DTranspose(filters=3, kernel_size=kernel_size, activation='sigmoid', padding='same', name='decoder_output')(x)
decoder = Model(latent_inputs, outputs, name='decoder')
autoencoder = Model(inputs, decoder(encoder(inputs)), name='autoencoder')
autoencoder.compile(loss='mse', optimizer='adam',metrics=["acc"])
# Automated Learning Rate reducer
lr_reducer = ReduceLROnPlateau(factor=np.sqrt(0.1),
cooldown=0,
patience=5,
verbose=1,
min_lr=0.5e-6)
callbacks = [lr_reducer]
# Begins training
history = autoencoder.fit(blurry_frames,
clean_frames,
validation_data=(blurry_frames, clean_frames),
epochs=100,
batch_size=batch_size,
callbacks=callbacks)
This would yield an accuracy of 82% after 100 epochs.
Now that I got more data, I believe I can get better performance. How should I adjust my layers and parameters to make the most of the data that I have?
mmm = Input(shape=(input_shape))
conv1 = Conv2D(filters=32, kernel_size=3, activation='relu',padding="same")(mmm)
batch_norm1 = BatchNormalization()(conv1)
act1 = ReLU()(batch_norm1)
#pool1 = MaxPooling2D(pool_size=(2, 2))(act1)
conv2 = Conv2D(filters=32, kernel_size=3, activation='relu',padding="same", strides=(2,2))(act1)
batch_norm2 = BatchNormalization()(conv2)
act2 = ReLU()(batch_norm2)
#pool1 = MaxPooling2D(pool_size=(2, 2))(act1)
conv3 = Conv2D(filters=32, kernel_size=3, activation='relu',padding="same")(act2)
batch_norm3 = BatchNormalization()(conv3)
#pool1 = MaxPooling2D(pool_size=(2, 2))(act1)
conv4 = Conv2D(filters=32, kernel_size=3, activation='relu',padding="same", strides=(2,2))(act1)
batch_norm4 = BatchNormalization()(conv4)
act4 = ReLU()(batch_norm4)
#pool1 = MaxPooling2D(pool_size=(2, 2))(act1)
conv5 = Conv2D(filters=32, kernel_size=3, activation='relu',padding="same")(act4)
batch_norm5 = BatchNormalization()(conv5)
#pool1 = MaxPooling2D(pool_size=(2, 2))(act1)
add1 = concatenate([batch_norm3, batch_norm5])
phrase1 = ReLU()(add1)
conv6 = Conv2D(filters=64, kernel_size=3, activation='relu',padding="same", strides=(2,2))(phrase1)
batch_norm6 = BatchNormalization()(conv6)
act6 = ReLU()(batch_norm6)
conv7 = Conv2D(filters=64, kernel_size=3, activation='relu',padding="same")(act6)
batch_norm7 = BatchNormalization()(conv7)
conv8 = Conv2D(filters=64, kernel_size=3, activation='relu',padding="same", strides=(2,2))(phrase1)
batch_norm8 = BatchNormalization()(conv8)
act8 = ReLU()(batch_norm8)
# ninth conv layer
conv9 = Conv2D(filters=64, kernel_size=3, activation='relu',padding="same")(act8)
batch_norm9 = BatchNormalization()(conv9)
add2 = concatenate([batch_norm7, batch_norm9])
phrase2 = ReLU()(add2)
conv10 = Conv2D(filters=128, kernel_size=3, activation='relu',padding="same", strides=(2,2))(phrase2)
batch_norm10 = BatchNormalization()(conv10)
act10 = ReLU()(batch_norm10)
conv11 = Conv2D(filters=128, kernel_size=3, activation='relu',padding="same")(act10)
batch_norm11 = BatchNormalization()(conv11)
conv12 = Conv2D(filters=128, kernel_size=3, activation='relu',padding="same", strides=(2,2))(phrase2)
batch_norm12 = BatchNormalization()(conv12)
act12 = ReLU()(batch_norm12)
conv13 = Conv2D(filters=128, kernel_size=3, activation='relu',padding="same")(act12)
batch_norm13 = BatchNormalization()(conv13)
add3 = concatenate([batch_norm11, batch_norm13])
phrase3 = ReLU()(add3)
conv14 = Conv2D(filters=256, kernel_size=3, activation='relu',padding="same", strides=(2,2))(phrase3)
batch_norm14 = BatchNormalization()(conv14)
act14 = ReLU()(batch_norm14)
conv15 = Conv2D(filters=256, kernel_size=3, activation='relu',padding="same")(act14)
batch_norm15 = BatchNormalization()(conv15)
conv16 = Conv2D(filters=256, kernel_size=3, activation='relu',padding="same", strides=(2,2))(phrase3)
batch_norm16 = BatchNormalization()(conv16)
act16 = ReLU()(batch_norm16)
conv17 = Conv2D(filters=256, kernel_size=3, activation='relu',padding="same")(act16)
batch_norm17 = BatchNormalization()(conv17)
add4 = concatenate([batch_norm15, batch_norm17])
phrase4 = ReLU()(add4)
avgpool = AveragePooling2D(pool_size=8,strides=1)(phrase4)
flat= Flatten()(avgpool)
fc1 = Dense(100, activation='relu')(flat )
Drop1 = Dropout(0.5)(fc1)
fc2 = Dense(1, activation='sigmoid')(Drop1)
model = Model(inputs= mmm, outputs=fc2)
# summarize layers
print(model.summary())
from tensorflow.keras.optimizers import Adam
opt =tf.keras.optimizers.SGD(learning_rate=0.1)
model.compile(loss = 'binary_crossentropy', optimizer= opt, metrics=['accuracy'])
early = EarlyStopping(monitor='val_accuracy', patience=6, verbose=1, mode='max')
history = model.fit(train_generator,epochs = 5, validation_data = test_generator,callbacks = [early])
the code above is my model when i trained it on my MRI images the valaccuracy and val_loss still the same even i changed the epoch
i changed the optimizers and learning rate but still the val_accuracy is the same i hope to see my model if i have errors in the architecture of the model i take the model from DFU_NET and then want to apdate it for my research
please can anyone help me to solve it
I have the following model, where keep_features=900 or so,y is one-hot encoding of classes. I am looking for the architecture below though(is that possible with keras, and what would the notation idea look like,specially the parallel part and the concatination)
model = Sequential()
model.add(Dense(keep_features, activation='relu'))
model.add(BatchNormalization())
model.add(Dense(256, activation='relu'))
model.add(BatchNormalization())
model.add(Dense(64, activation='relu'))
model.add(BatchNormalization())
model.add(Dense(3, activation='softmax'))
model.compile(loss=losses.categorical_crossentropy,optimizer='adam',metrics=['mae', 'acc'])
With the chapter "Multi-input and multi-output models" here you can make something like this for your desired model:
K = tf.keras
input1 = K.layers.Input(keep_features_shape)
denseA1 = K.layers.Dense(256, activation='relu')(input1)
denseB1 = K.layers.Dense(256, activation='relu')(input1)
denseC1 = K.layers.Dense(256, activation='relu')(input1)
batchA1 = K.layers.BatchNormalization()(denseA1)
batchB1 = K.layers.BatchNormalization()(denseB1)
batchC1 = K.layers.BatchNormalization()(denseC1)
denseA2 = K.layers.Dense(64, activation='relu')(batchA1)
denseB2 = K.layers.Dense(64, activation='relu')(batchB1)
denseC2 = K.layers.Dense(64, activation='relu')(batchC1)
batchA2 = K.layers.BatchNormalization()(denseA2)
batchB2 = K.layers.BatchNormalization()(denseB2)
batchC2 = K.layers.BatchNormalization()(denseC2)
denseA3 = K.layers.Dense(32, activation='softmax')(batchA2) # individual layer
denseB3 = K.layers.Dense(16, activation='softmax')(batchB2) # individual layer
denseC3 = K.layers.Dense(8, activation='softmax')(batchC2) # individual layer
concat1 = K.layers.Concatenate(axis=-1)([denseA3, denseB3, denseC3])
model = K.Model(inputs=[input1], outputs=[concat1])
model.compile(loss = K.losses.categorical_crossentropy, optimizer='adam', metrics=['mae', 'acc'])
This results in:
I'm trying to build a Neural Network, based on the Inception architecture used for images, but for 1D vectors.
I have based the model I created on this one from the keras getting started guide from this link https://keras.io/getting-started/functional-api-guide/:
tf.keras.backend.clear_session()
logger = tf.get_logger()
logger.setLevel(logging.ERROR)
input_vector = Input(shape=(71276,1),)
tower_1 = tf.keras.layers.Conv1D(filters=64, kernel_size=1, padding='same', activation='relu', name='conv_1')(input_vector)
tower_1 = tf.keras.layers.Conv1D(filters=64, kernel_size=3, padding='same', activation='relu', name='conv_2')(tower_1)
tower_2 = tf.keras.layers.Conv1D(filters=64, kernel_size=1, padding='same', activation='relu', name='conv_3')(input_vector)
tower_2 = tf.keras.layers.Conv1D(filters=64, kernel_size=1, padding='same', activation='relu', name='conv_4')(tower_2)
tower_3 = tf.keras.layers.MaxPooling1D(pool_size=3, strides=1, padding='same')(input_vector)
tower_3 = tf.keras.layers.Conv1D(filters=64, kernel_size=1, padding='same', activation='relu', name='conv_4')(tower_3)
output = tf.keras.layers.concatenate([tower_1, tower_2, tower_3])
model = tf.keras.models.Model(inputs=input_vector, outputs=output)
model.compile(loss='mse',
optimizer=tf.keras.optimizers.Adam(lr=0.001),
metrics=['mae'])
model.summary()
This is my code:
from keras.layers import Conv1D, MaxPooling1D, Input
from keras.models import Model
tf.keras.backend.clear_session()
logger = tf.get_logger()
logger.setLevel(logging.ERROR)
input_vector = Input(shape=(71276,1),)
tower_1 = Conv1D(filters=64, kernel_size=1, padding='same', activation='relu', name='conv_1')(input_vector)
tower_1 = Conv1D(filters=64, kernel_size=3, padding='same', activation='relu', name='conv_1')(tower_1)
tower_2 = Conv1D(filters=64, kernel_size=1, padding='same', activation='relu', name='conv_1')(input_vector)
tower_2 = Conv1D(filters=64, kernel_size=1, padding='same', activation='relu', name='conv_1')(tower_2)
tower_3 = MaxPooling1D(pool_size=3, strides=1, padding='same')(input_vector)
tower_3 = Conv1D(filters=64, kernel_size=1, padding='same', activation='relu', name='conv_1')(tower_3)
output = tf.keras.layers.concatenate([tower_1, tower_2, tower_3])
model = Model(inputs=input_vector, outputs=output)
model.compile(loss='mse',
optimizer=tf.keras.optimizers.Adam(lr=0.001),
metrics=['mae'])
model.summary()
When executing, I'm getting the following error, and don't really understand why:
AttributeError Traceback (most recent call last)
<ipython-input-9-2931ae837421> in <module>()
6 input_vector = Input(shape=(71276,1),)
7
----> 8 tower_1 = tf.keras.layers.Conv1D(filters=64, kernel_size=1, padding='same', activation='relu', name='conv_1')(input_vector)
9 tower_1 = tf.keras.layers.Conv1D(filters=64, kernel_size=3, padding='same', activation='relu', name='conv_2')(tower_1)
10
5 frames
/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/engine/base_layer.py in <lambda>(t)
2056 `call` method of the layer at the call that created the node.
2057 """
-> 2058 inbound_layers = nest.map_structure(lambda t: t._keras_history.layer,
2059 input_tensors)
2060 node_indices = nest.map_structure(lambda t: t._keras_history.node_index,
AttributeError: 'tuple' object has no attribute 'layer'
I don't have a lot of experience with convolutional layers so it is very possible I have made a very obvious mistake. Searching online I haven't been able to find someone else having the same problem.
I'm running this on Google Colaboratory, in a python 3 runtime.
Any help would be appreciated, thank you!
A few things:
All your layers have the same name? I bet that could cause lots of strange bugs
tower_3 doesn't have the same shape as the other two towers. It's impossible to concatenate. (You're using a MaxPooling1D, check the summary to confirm.)
You are mixing keras and tf.keras, that is certainly a huge problem. Choose only one.
I'm just getting started with Keras and with Deep learning, so the answer to my question could be obvious to some, but for me it isn't.
I made a model to colorize some black and white photos following the article on Floydhub (where I'm training it) and it works just fine when I train it with similar pictures (such as human faces) but as soon as I use a larger dataset as an input with different pictures, the loss just remains stable and doesn't get better.
I've tried different learning rates and optimizers but just cannot get a good result.
What could I change to get a better result?
This is the code (thanks to Emil Wallner for the article on Floydhub)
# Get images
X = []
for filename in os.listdir('/data/images/Train/'):
X.append(img_to_array(load_img('/data/images/Train/'+filename)))
X = np.array(X, dtype=float)
Xtrain = 1.0/255*X
#Load weights
inception = InceptionResNetV2(weights=None, include_top=True)
inception.load_weights('/data/inception_resnet_v2_weights_tf_dim_ordering_tf_kernels.h5')
inception.graph = tf.get_default_graph()
embed_input = Input(shape=(1000,))
#Encoder
encoder_input = Input(shape=(256, 256, 1,))
encoder_output = Conv2D(64, (3,3), activation='relu', padding='same', strides=2)(encoder_input)
encoder_output = Conv2D(128, (3,3), activation='relu', padding='same')(encoder_output)
encoder_output = Conv2D(128, (3,3), activation='relu', padding='same', strides=2)(encoder_output)
encoder_output = Conv2D(256, (3,3), activation='relu', padding='same')(encoder_output)
encoder_output = Conv2D(256, (3,3), activation='relu', padding='same', strides=2)(encoder_output)
encoder_output = Conv2D(512, (3,3), activation='relu', padding='same')(encoder_output)
encoder_output = Conv2D(512, (3,3), activation='relu', padding='same')(encoder_output)
encoder_output = Conv2D(256, (3,3), activation='relu', padding='same')(encoder_output)
#Fusion
fusion_output = RepeatVector(32 * 32)(embed_input)
fusion_output = Reshape(([32, 32, 1000]))(fusion_output)
fusion_output = concatenate([encoder_output, fusion_output], axis=3)
fusion_output = Conv2D(256, (1, 1), activation='relu', padding='same')(fusion_output)
#Decoder
decoder_output = Conv2D(128, (3,3), activation='relu', padding='same')(fusion_output)
decoder_output = UpSampling2D((2, 2))(decoder_output)
decoder_output = Conv2D(64, (3,3), activation='relu', padding='same')(decoder_output)
decoder_output = UpSampling2D((2, 2))(decoder_output)
decoder_output = Conv2D(32, (3,3), activation='relu', padding='same')(decoder_output)
decoder_output = Conv2D(16, (3,3), activation='relu', padding='same')(decoder_output)
decoder_output = Conv2D(2, (3, 3), activation='tanh', padding='same')(decoder_output)
decoder_output = UpSampling2D((2, 2))(decoder_output)
model = Model(inputs=[encoder_input, embed_input], outputs=decoder_output)
#Create embedding
def create_inception_embedding(grayscaled_rgb):
grayscaled_rgb_resized = []
for i in grayscaled_rgb:
i = resize(i, (299, 299, 3), mode='constant')
grayscaled_rgb_resized.append(i)
grayscaled_rgb_resized = np.array(grayscaled_rgb_resized)
grayscaled_rgb_resized = preprocess_input(grayscaled_rgb_resized)
with inception.graph.as_default():
embed = inception.predict(grayscaled_rgb_resized)
return embed
# Image transformer
datagen = ImageDataGenerator(
shear_range=0.4,
zoom_range=0.4,
rotation_range=40,
horizontal_flip=True)
#Generate training data
batch_size = 20
def image_a_b_gen(batch_size):
for batch in datagen.flow(Xtrain, batch_size=batch_size):
grayscaled_rgb = gray2rgb(rgb2gray(batch))
embed = create_inception_embedding(grayscaled_rgb)
lab_batch = rgb2lab(batch)
X_batch = lab_batch[:,:,:,0]
X_batch = X_batch.reshape(X_batch.shape+(1,))
Y_batch = lab_batch[:,:,:,1:] / 128
yield ([X_batch, create_inception_embedding(grayscaled_rgb)], Y_batch)
#Train model
tensorboard = TensorBoard(log_dir="/output")
model.compile(optimizer='adam', loss='mse')
model.fit_generator(image_a_b_gen(batch_size), callbacks=[tensorboard], epochs=1000, steps_per_epoch=20)
#Make a prediction on the unseen images
color_me = []
for filename in os.listdir('../Test/'):
color_me.append(img_to_array(load_img('../Test/'+filename)))
color_me = np.array(color_me, dtype=float)
color_me = 1.0/255*color_me
color_me = gray2rgb(rgb2gray(color_me))
color_me_embed = create_inception_embedding(color_me)
color_me = rgb2lab(color_me)[:,:,:,0]
color_me = color_me.reshape(color_me.shape+(1,))
# Test model
output = model.predict([color_me, color_me_embed])
output = output * 128
# Output colorizations
for i in range(len(output)):
cur = np.zeros((256, 256, 3))
cur[:,:,0] = color_me[i][:,:,0]
cur[:,:,1:] = output[i]
imsave("result/img_"+str(i)+".png", lab2rgb(cur))