CNN validation accuracy high, but bad at predictions? - python-3.x

I am trying to build a CNN to distinguish between 3 classes which are genuine faces, printed faces, and replayed faces. I prepared the data as so:
classes = ['Genuine', 'Printed', 'Replay']
base_dir = '/Dataset'
import os
import numpy as np
import glob
import shutil
for cl in classes:
img_path = os.path.join(base_dir, cl)
images = glob.glob(img_path + '/*.jpg')
print("{}: {} Images".format(cl, len(images)))
num_train = int(round(len(images)*0.8))
train, val = images[:num_train], images[num_train:]
for t in train:
if not os.path.exists(os.path.join(base_dir, 'train', cl)):
os.makedirs(os.path.join(base_dir, 'train', cl))
shutil.move(t, os.path.join(base_dir, 'train', cl))
for v in val:
if not os.path.exists(os.path.join(base_dir, 'val', cl)):
os.makedirs(os.path.join(base_dir, 'val', cl))
shutil.move(v, os.path.join(base_dir, 'val', cl))
from tensorflow.keras.preprocessing.image import ImageDataGenerator
image_gen_train = ImageDataGenerator(
rescale=1./255,
rotation_range=45,
width_shift_range=.15,
height_shift_range=.15,
horizontal_flip=True,
zoom_range=0.5
)
batch_size = 32
IMG_SHAPE = 96
train_data_gen = image_gen_train.flow_from_directory(
batch_size=batch_size,
directory=train_dir,
shuffle=True,
target_size=(IMG_SHAPE,IMG_SHAPE),
class_mode='sparse'
)
I built a simple model like the following:
## Model
import tensorflow as tf
from keras import regularizers
from keras.layers.normalization import BatchNormalization
IMG_SHAPE = (96, 96, 3)
batch_size = 32
## Trainable classification head
aConv_layer = tf.keras.layers.Conv2D(576, (3, 3), padding="same",
activation="relu", input_shape= IMG_SHAPE)
aConv_layer = tf.keras.layers.Conv2D(144, (3, 3), padding="same",
activation="relu", input_shape= IMG_SHAPE)
gmaxPool_layer = tf.keras.layers.GlobalMaxPooling2D() #reduces input from 4D to 2D
maxPool_layer = tf.keras.layers.MaxPool2D(pool_size=(1, 1), strides=None,
padding='valid', data_format=None,
)
batNor_layer = tf.keras.layers.BatchNormalization(axis=-1, momentum=0.99,
epsilon=0.001,
center=True, scale=True,
beta_initializer='zeros',
gamma_initializer='ones',
moving_mean_initializer='zeros',
moving_variance_initializer='ones',
beta_regularizer=None, gamma_regularizer=None,
beta_constraint=None, gamma_constraint=None)
flat_layer = tf.keras.layers.Flatten()
dense_layer = tf.keras.layers.Dense(9, activation='softmax',
kernel_regularizer=regularizers.l2(0.01))
prediction_layer = tf.keras.layers.Dense(3, activation='softmax')
model = tf.keras.Sequential([
#base_model,
tf.keras.layers.Conv2D(576, (3, 3), padding="same", activation="relu", input_shape= IMG_SHAPE),
tf.keras.layers.Dense(288, activation='softmax', kernel_regularizer=regularizers.l2(0.01)),
tf.keras.layers.MaxPool2D(pool_size=(2, 2), strides=None, padding='valid', data_format=None),
tf.keras.layers.Conv2D(144, (3, 3), padding="same", activation="relu"),
tf.keras.layers.Dense(72, activation='softmax', kernel_regularizer=regularizers.l2(0.01)),
tf.keras.layers.MaxPool2D(pool_size=(2, 2), strides=None, padding='valid', data_format=None),
#
batNor_layer,
gmaxPool_layer,
tf.keras.layers.Flatten(),
#tf.keras.layers.Dropout(0.5),
prediction_layer
])
learning_rate = 0.001
## Compiles the model
model.compile(optimizer=tf.keras.optimizers.Adam(lr=learning_rate),
loss='sparse_categorical_crossentropy',
metrics=['accuracy']
)
I trained the model and got the following, which I would assume to be great results:
However, whenever I tried to predict an image with the following code, it would almost always get it wrong:
import numpy as np
from google.colab import files
from keras.preprocessing import image
uploaded = files.upload()
for fn in uploaded.keys():
# predicting images
path = fn
img = image.load_img(path, target_size=(96, 96))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
images = np.vstack([x])
classes = model.predict(images, batch_size=10)
print(fn)
print('Genuine | Printout | Replay')
print(np.argmax(classes))
How can the predictions be wrong when the validation accuracy be so high?
Here is the Codelab, if it helps.

Process the images for prediction in the same way that you processed your images for training. Specifically, rescale your images like you did with ImageDataGenerator.
import numpy as np
from google.colab import files
from keras.preprocessing import image
uploaded = files.upload()
for fn in uploaded.keys():
# predicting images
path = fn
img = image.load_img(path, target_size=(96, 96))
x = image.img_to_array(img)
# Rescale image.
x = x / 255.
x = np.expand_dims(x, axis=0)
images = np.vstack([x])
classes = model.predict(images, batch_size=10)
print(fn)
print('Genuine | Printout | Replay')
print(np.argmax(classes))

Somehow, the image generator of Keras works well when combined with fit() or fit_generator() function, but fails miserably when combined
with predict_generator() or the predict() function. The Keras predict() function generally fails when working with batch prediction.
When using Plaid-ML Keras back-end for AMD processor, I would rather loop through all test images one-by-one and get the prediction for each image in each iteration.
import os
from PIL import Image
import keras
import numpy
# code for creating dan training model is not included
print("Prediction result:")
dir = "/path/to/test/images"
files = os.listdir(dir)
correct = 0
total = 0
#dictionary to label all animal category class.
classes = {
0:'This is Cat',
1:'This is Dog',
}
for file_name in files:
total += 1
image = Image.open(dir + "/" + file_name).convert('RGB')
image = image.resize((100,100))
image = numpy.expand_dims(image, axis=0)
image = numpy.array(image)
image = image/255
pred = model.predict_classes([image])[0]
animals_category = classes[pred]
if ("cat" in file_name) and ("cat" in sign):
print(correct,". ", file_name, animals_category)
correct+=1
elif ("dog" in file_name) and ("dog" in animals_category):
print(correct,". ", file_name, animals_category)
correct+=1
print("accuracy: ", (correct/total))

Related

How do I know what the output of model.predict() correspond to?

I am trying to make a CNN that classifies cats and dogs and I am using flow_from_directory() to prepare my data for the model.
from keras import Sequential
from keras_preprocessing.image import ImageDataGenerator
from keras.layers import *
from keras.callbacks import ModelCheckpoint
from keras.optimizers import *
import keras
import numpy as np
import os
img_size = 250 # number of pixels for width and height
#Random Seed
np.random.seed(123456789)
training_path = os.getcwd() + "/cats and dogs images/train"
testing_path = os.getcwd() + "/cats and dogs images/test"
#Defines the Model
model = Sequential([
Conv2D(filters=128, kernel_size=(3,3), activation="relu", padding="same", input_shape=(img_size,img_size,3)),
MaxPool2D(pool_size=(2,2), strides=2),
Conv2D(filters=64, kernel_size=(3,3), activation="relu", padding="same"),
Flatten(),
Dense(32, activation="relu"),
Dense(2, activation="softmax")
])
#Scales the pixel values to between 0 to 1
datagen = ImageDataGenerator(rescale=1.0/255.0)
Batch_size = 10
#Prepares Training Data
training_dataset = datagen.flow_from_directory(directory = training_path,
target_size=(img_size,img_size),
classes = ["cat","dog"],
class_mode = "categorical",
batch_size = Batch_size)
#Prepares Testing Data
testing_dataset = datagen.flow_from_directory(directory = testing_path,
target_size=(img_size,img_size),
classes = ["cat","dog"],
class_mode = "categorical",
batch_size = Batch_size)
#Compiles the model
#model.compile(loss="categorical_crossentropy", optimizer="sgd", metrics=['accuracy'])
model.compile(loss="binary_crossentropy", optimizer="adam", metrics=['accuracy'])
#model.compile(loss="mse", optimizer="sgd", metrics=[keras.metrics.MeanSquaredError()])
#Checkpoint
filepath = os.getcwd() + "/trained_model.h5"
checkpoint = ModelCheckpoint(filepath, monitor='loss', verbose=1, save_best_only=True, mode='min', save_freq=1)
#Fitting the model to the dataset (Training the Model)
model.fit(x = training_dataset, steps_per_epoch = 400,
validation_data=testing_dataset, validation_steps=100,
epochs = 10, callbacks=[checkpoint], verbose = 1)
# evaluate model on training dataset
_,acc = model.evaluate_generator(training_dataset, steps=len(training_dataset), verbose=0)
print("Accuracy on training dataset:")
print('> %.3f' % (acc * 100.0))
#evaluate model on testing dataset
_,acc = model.evaluate_generator(testing_dataset, steps=len(testing_dataset), verbose=0)
print("Accuracy on testing dataset:")
print('> %.3f' % (acc * 100.0))
I want to know how the output of model.predict() is going to correspond to the labels cats and dogs and which one of the two numbers in the output is a cat and which is a dog?
Here's my code for loading the model and giving a prediction:
from keras.models import Sequential
from keras_preprocessing.image import *
from keras.layers import *
import tensorflow as tf
import numpy as np
from keras.layers.experimental.preprocessing import Rescaling
import os
import cv2
from keras.models import *
img_size = 250
#Load weights into new model
filepath = os.getcwd() + "/trained_model.h5"
model = load_model(filepath)
print("Loaded model from disk")
#Scales the pixel values to between 0 to 1
#datagen = ImageDataGenerator(rescale=1.0/255.0)
#Prepares Testing Data
testing_dataset = cv2.imread(os.getcwd() + "/cats and dogs images/single test sample/507.png")
#img = datagen.flow_from_directory(testing_dataset, target_size=(img_size,img_size))
img = cv2.resize(testing_dataset, (img_size,img_size))
newimg = np.asarray(img)
pixels = newimg.astype('float32')
pixels /= 255.0
print(pixels.shape)
pixels = np.expand_dims(pixels, axis=0)
print(pixels.shape)
prediction = model.predict(pixels)
print(prediction)
And here is the output from the prediction code above:
Loaded model from disk
(250, 250, 3)
(1, 250, 250, 3)
[[5.4904184e-27 1.0000000e+00]]
As you can see, the prediction gave an array of two numbers, but which one corresponds to the dog label and which to the cat label? By the way, the model isn't fully trained so I am just testing out the code to see if it works.
The model output depends on how you loaded the data and specified how the classes are going to be ordered/labelled in this code you provided:
training_dataset = datagen.flow_from_directory(directory = training_path,
target_size=(img_size,img_size),
classes = ["cat","dog"],
class_mode = "categorical",
batch_size = Batch_size)
#Prepares Testing Data
testing_dataset = datagen.flow_from_directory(directory = testing_path,
target_size=(img_size,img_size),
classes = ["cat","dog"],
class_mode = "categorical",
batch_size = Batch_size)
You specified during the loading of the data that the classes are going to be ordered Cat then Dog in classes argument.
Therefor the output is going to be ordered as two probabilities (summing to 1)
The first probability refers to by how % that the input image is cat and the second probability refers to by how % that the input image is dog.
You use this line:
output_class = np.argmax(prediction, axis=1)
This line will compare the elements of the list and outputs which index of the elements of the list is the greatest (In our case the list containing the two probabilities) in the form of [1] (or [0, 1] depending on the shape of the output) this means that the said image is a dog, since the 2nd element in the output list is 1 if it were [0] (or [1, 0] depending on the shape of the output) then that means that the output class of the input image is cat.

Why is my GAN not producing more good images after a certain point?

Question
I was training a gan to generate human faces. Within approximately 500 epochs, it learned to generate images like this:
Well, now this image is not too bad. We can see a face in the center of the image.
Then I trained it for more 1000 epochs and it learned nothing. It was still generating the same type of images as shown above. Why was that? Why wasn't my gan not learning to produce even better images?
Code for the Models
Here is the code of the discriminator:
def define_discriminator(in_shape=(64, 64, 3)):
Model = Sequential([
Conv2D(32, (3, 3), padding='same', input_shape=in_shape),
BatchNormalization(),
LeakyReLU(alpha=0.2),
MaxPooling2D(pool_size=(2,2)),
Dropout(0.2),
Conv2D(64, (3,3), padding='same'),
BatchNormalization(),
LeakyReLU(alpha=0.2),
MaxPooling2D(pool_size=(2,2)),
Dropout(0.3),
Conv2D(128, (3,3), padding='same'),
BatchNormalization(),
LeakyReLU(alpha=0.2),
MaxPooling2D(pool_size=(2,2)),
Dropout(0.3),
Conv2D(256, (3,3), padding='same'),
BatchNormalization(),
LeakyReLU(alpha=0.2),
MaxPooling2D(pool_size=(2,2)),
Dropout(0.4),
Flatten(),
Dense(1, activation='sigmoid')
])
opt = Adam(lr=0.00002)
Model.compile(loss='binary_crossentropy', optimizer=opt, metrics=['accuracy'])
return Model
Here is the code of the generator and the GAN:
def define_generator(in_shape=100):
Model = Sequential([
Dense(256*8*8, input_dim=in_shape),
BatchNormalization(),
LeakyReLU(alpha=0.2),
Reshape((8, 8, 256)),
Conv2DTranspose(256, (3,3), strides=(2,2), padding='same'),
BatchNormalization(),
LeakyReLU(alpha=0.2),
Conv2DTranspose(64, (3,3), strides=(2,2), padding='same'),
BatchNormalization(),
LeakyReLU(alpha=0.2),
Conv2DTranspose(3, (4, 4), strides=(2,2), padding='same', activation='sigmoid')
])
return Model
def define_gan(d_model, g_model):
d_model.trainable = False
model = Sequential([
g_model,
d_model
])
opt = Adam(lr=0.0002, beta_1=0.5)
model.compile(loss='binary_crossentropy', optimizer=opt)
return model
Entire Reproducible Code
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, BatchNormalization
from tensorflow.keras.layers import Dropout, Flatten, Dense, Conv2DTranspose
from tensorflow.keras.layers import MaxPooling2D, Activation, Reshape, LeakyReLU
from tensorflow.keras.datasets import mnist
from tensorflow.keras.optimizers import Adam
from numpy import ones
from numpy import zeros
from numpy.random import rand
from numpy.random import randint
from numpy.random import randn
from numpy import vstack
from numpy import array
import os
from tensorflow.keras.preprocessing.image import load_img
from tensorflow.keras.preprocessing.image import img_to_array
from matplotlib import pyplot
def load_data(filepath):
image_array = []
n = 0
for fold in os.listdir(filepath):
if fold != 'wiki.mat':
if n > 1:
break
for img in os.listdir(os.path.join(filepath, fold)):
image = load_img(filepath + fold + '/'+ img, target_size=(64, 64))
img_array = img_to_array(image)
img_array = img_array.astype('float32')
img_array = img_array / 255.0
image_array.append(img_array)
n += 1
return array(image_array)
def generate_latent_points(n_samples, latent_dim=100):
latent_points = randn(n_samples * latent_dim)
latent_points = latent_points.reshape(n_samples, latent_dim)
return latent_points
def generate_real_samples(n_samples, dataset):
ix = randint(0, dataset.shape[0], n_samples)
x = dataset[ix]
y = ones((n_samples, 1))
return x, y
def generate_fake_samples(g_model, n_samples):
latent_points = generate_latent_points(n_samples)
x = g_model.predict(latent_points)
y = zeros((n_samples, 1))
return x, y
def save_plot(examples, epoch, n=10):
# plot images
for i in range(n * n):
# define subplot
pyplot.subplot(n, n, 1 + i)
# turn off axis
pyplot.axis('off')
# plot raw pixel data
pyplot.imshow(examples[i, :, :, 0])
# save plot to file
filename = 'generated_plot_e%03d.png' % (epoch+1)
pyplot.savefig(filename)
pyplot.close()
def summarize_performance(d_model, g_model, gan_model, dataset, epoch, n_samples=100):
real_x, real_y = generate_real_samples(n_samples, dataset)
_, d_real_acc = d_model.evaluate(real_x, real_y)
fake_x, fake_y = generate_fake_samples(g_model, n_samples)
_, d_fake_acc = d_model.evaluate(fake_x, fake_y)
latent_points, y = generate_latent_points(n_samples), ones((n_samples, 1))
gan_loss = gan_model.evaluate(latent_points, y)
print('Epoch %d, acc_real=%.3d, acc_fake=%.3f, gan_loss=%.3f' % (epoch, d_real_acc, d_fake_acc, gan_loss))
save_plot(fake_x, epoch)
filename = 'Genarator_Model % d' % (epoch + 1)
g_model.save(filename)
def train(d_model, g_model, gan_model, dataset, epochs=200):
batch_size = 64
half_batch = int(batch_size / 2)
batch_per_epoch = int(dataset.shape[0] / batch_size)
for epoch in range(epochs):
for i in range(batch_per_epoch):
real_x, real_y = generate_real_samples(half_batch, dataset)
_, d_real_acc = d_model.train_on_batch(real_x, real_y)
fake_x, fake_y = generate_fake_samples(g_model, half_batch)
_, d_fake_acc = d_model.train_on_batch(fake_x, fake_y)
latent_points, y = generate_latent_points(batch_size), ones((batch_size, 1))
gan_loss = gan_model.train_on_batch(latent_points, y)
print('Epoch %d, acc_real=%.3d, acc_fake=%.3f, gan_loss=%.3f' % (epoch, d_real_acc, d_fake_acc, gan_loss))
if (epoch % 2) == 0:
summarize_performance(d_model, g_model, gan_model, dataset, epoch)
dataset = load_data(filepath) # filepath is not defined since every person will have seperate filepath
discriminator_model = define_discriminator()
generator_model = define_generator()
gan_model = define_gan(discriminator_model, generator_model)
train(discriminator_model, generator_model, gan_model, dataset)
Dataset
If you want here is the dataset.

ValueError: It seems that you are using the Keras 2 and you are passing both `kernel_size` and `strides` as integer positional arguments

I'm a computer science undergraduate student in the 4th semester, and I'm learning about Machine Learning in this lockdown.
from __future__ import print_function
from keras import backend as K
K.common.set_image_dim_ordering('th') # ensure our dimension notation matches
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.layers import Reshape
from keras.layers.core import Activation
from keras.layers.normalization import BatchNormalization
from keras.layers.convolutional import UpSampling2D
from keras.layers.convolutional import Convolution2D, AveragePooling2D
from keras.layers.core import Flatten
from keras.optimizers import SGD, Adam
from keras.datasets import mnist
from keras import utils
import numpy as np
from PIL import Image, ImageOps
import argparse
import math
import os
import os.path
import glob
def generator_model():
model = Sequential()
model.add(Dense(input_dim=100, output_dim=1024))
model.add(Activation('tanh'))
model.add(Dense(128*8*8))
model.add(BatchNormalization())
model.add(Activation('tanh'))
model.add(Reshape((128, 8, 8), input_shape=(128*8*8,)))
model.add(UpSampling2D(size=(4, 4)))
model.add(Convolution2D(64, 5, 5, border_mode='same'))
model.add(Activation('tanh'))
model.add(UpSampling2D(size=(4, 4)))
model.add(Convolution2D(1, 5, 5, border_mode='same'))
model.add(Activation('tanh'))
return model
def discriminator_model():
model = Sequential()
model.add(Convolution2D(64, 5, 5, border_mode='same', input_shape=(1, 128, 128)))
model.add(Activation('tanh'))
model.add(AveragePooling2D(pool_size=(4, 4)))
model.add(Convolution2D(128, 5, 5))
model.add(Activation('tanh'))
model.add(AveragePooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(256))
model.add(Activation('tanh'))
model.add(Dense(1))
model.add(Activation('sigmoid'))
return model
def generator_containing_discriminator(generator, discriminator):
model = Sequential()
model.add(generator)
discriminator.trainable = False
model.add(discriminator)
return model
def combine_images(generated_images):
num = generated_images.shape[0]
width = int(math.sqrt(num))
height = int(math.ceil(float(num)/width))
shape = generated_images.shape[2:]
image = np.zeros((height*shape[0], width*shape[1]),
dtype=generated_images.dtype)
for index, img in enumerate(generated_images):
i = int(index/width)
j = index % width
image[i*shape[0]:(i+1)*shape[0], j*shape[1]:(j+1)*shape[1]] = \
img[0, :, :]
return image
model = generator_model()
print(model.summary())
def load_data(pixels=128, verbose=False):
print("Loading data")
X_train = []
paths = glob.glob(os.path.normpath(os.getcwd() + '/logos/*.jpg'))
for path in paths:
if verbose: print(path)
im = Image.open(path)
im = ImageOps.fit(im, (pixels, pixels), Image.ANTIALIAS)
im = ImageOps.grayscale(im)
#im.show()
im = np.asarray(im)
X_train.append(im)
print("Finished loading data")
return np.array(X_train)
def train(epochs, BATCH_SIZE, weights=False):
"""
:param epochs: Train for this many epochs
:param BATCH_SIZE: Size of minibatch
:param weights: If True, load weights from file, otherwise train the model from scratch.
Use this if you have already saved state of the network and want to train it further.
"""
X_train = load_data()
X_train = (X_train.astype(np.float32) - 127.5)/127.5
X_train = X_train.reshape((X_train.shape[0], 1) + X_train.shape[1:])
discriminator = discriminator_model()
generator = generator_model()
if weights:
generator.load_weights('goodgenerator.h5')
discriminator.load_weights('gooddiscriminator.h5')
discriminator_on_generator = \
generator_containing_discriminator(generator, discriminator)
d_optim = SGD(lr=0.0005, momentum=0.9, nesterov=True)
g_optim = SGD(lr=0.0005, momentum=0.9, nesterov=True)
generator.compile(loss='binary_crossentropy', optimizer="SGD")
discriminator_on_generator.compile(
loss='binary_crossentropy', optimizer=g_optim)
discriminator.trainable = True
discriminator.compile(loss='binary_crossentropy', optimizer=d_optim)
noise = np.zeros((BATCH_SIZE, 100))
for epoch in range(epochs):
print("Epoch is", epoch)
print("Number of batches", int(X_train.shape[0]/BATCH_SIZE))
for index in range(int(X_train.shape[0]/BATCH_SIZE)):
for i in range(BATCH_SIZE):
noise[i, :] = np.random.uniform(-1, 1, 100)
image_batch = X_train[index*BATCH_SIZE:(index+1)*BATCH_SIZE]
generated_images = generator.predict(noise, verbose=0)
#print(generated_images.shape)
if index % 20 == 0 and epoch % 10 == 0:
image = combine_images(generated_images)
image = image*127.5+127.5
destpath = os.path.normpath(os.getcwd()+ "/logo-generated-images/"+str(epoch)+"_"+str(index)+".png")
Image.fromarray(image.astype(np.uint8)).save(destpath)
X = np.concatenate((image_batch, generated_images))
y = [1] * BATCH_SIZE + [0] * BATCH_SIZE
d_loss = discriminator.train_on_batch(X, y)
print("batch %d d_loss : %f" % (index, d_loss))
for i in range(BATCH_SIZE):
noise[i, :] = np.random.uniform(-1, 1, 100)
discriminator.trainable = False
g_loss = discriminator_on_generator.train_on_batch(
noise, [1] * BATCH_SIZE)
discriminator.trainable = True
print("batch %d g_loss : %f" % (index, g_loss))
if epoch % 10 == 9:
generator.save_weights('goodgenerator.h5', True)
discriminator.save_weights('gooddiscriminator.h5', True)
def clean(image):
for i in range(1, image.shape[0] - 1):
for j in range(1, image.shape[1] - 1):
if image[i][j] + image[i+1][j] + image[i][j+1] + image[i-1][j] + image[i][j-1] > 127 * 5:
image[i][j] = 255
return image
def generate(BATCH_SIZE):
generator = generator_model()
generator.compile(loss='binary_crossentropy', optimizer="SGD")
generator.load_weights('goodgenerator.h5')
noise = np.zeros((BATCH_SIZE, 100))
a = np.random.uniform(-1, 1, 100)
b = np.random.uniform(-1, 1, 100)
grad = (b - a) / BATCH_SIZE
for i in range(BATCH_SIZE):
noise[i, :] = np.random.uniform(-1, 1, 100)
generated_images = generator.predict(noise, verbose=1)
#image = combine_images(generated_images)
print(generated_images.shape)
for image in generated_images:
image = image[0]
image = image*127.5+127.5
Image.fromarray(image.astype(np.uint8)).save("dirty.png")
Image.fromarray(image.astype(np.uint8)).show()
clean(image)
image = Image.fromarray(image.astype(np.uint8))
image.show()
image.save("clean.png")
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument("--mode", type=str)
parser.add_argument("--batch_size", type=int, default=128)
parser.add_argument("--nice", dest="nice", action="store_true")
parser.set_defaults(nice=False)
args = parser.parse_args()
return args
train(400, 10, False)
generate(1)
I was trying this GAN code from this GitHub repository, for learning about Generative Adversarial Networks, but the below error occurred. Can you please tell me where the definitions are provided in the code? Please help me!
The Troublesome Line:-
train(400, 10, False)
This is the error:-
ValueError: It seems that you are using the Keras 2 and you are passing both `kernel_size` and `strides` as integer positional arguments. For safety reasons, this is disallowed. Pass `strides` as a keyword argument instead.
The error arises from every addition of a Conv2D layer in your model. You need to change the line in your code
model.add(Convolution2D(64, 5, 5, border_mode='same'))
to something like (depending on what exactly you want)
model.add(Conv2D(64,kernel_size=5,strides=2,padding='same'))
Notice that I have explicity named the argument strides here because the error says I should pass it as a keyword argument.

Load model from Keras and investigate the details

I'm using Keras to fit a function, and I'm new to Keras.
With a very simple network, the Keras can fit my function very well, I just want to know what the function is and try to understand why it works very well. But the "predict" function hide the details.
Here is the code I create the network:
import numpy as np
import tensorflow as tf
from tensorflow import keras
LABEL_COLUMN = "shat"
BATCH_SIZE = 16
EPOCHS = 20
trainfilePath = "F:\\PyworkingFolder\\WWSHat\\_Data\\alpha0train.csv"
testfilePath = "F:\\PyworkingFolder\\WWSHat\\_Data\\alpha0test.csv"
with open(trainfilePath, encoding='utf-8') as txtContent:
trainArray = np.loadtxt(txtContent, delimiter=",")
with open(testfilePath, encoding='utf-8') as txtContent:
testArray = np.loadtxt(txtContent, delimiter=",")
trainSample = trainArray[:, 0:14]
trainLable = trainArray[:, 14]
testSample = testArray[:, 0:14]
testLable = testArray[:, 14]
model = keras.Sequential([
keras.layers.Dense(14, activation='relu', input_shape=[14]),
keras.layers.Dense(15, activation='relu'),
keras.layers.Dense(1)
])
optimizer = tf.keras.optimizers.RMSprop(0.001)
# optimizer = keras.optimizers.Adadelta(lr=1.0, rho=0.95, epsilon=None, decay=0.0)
model.compile(loss='mse',
optimizer=optimizer,
metrics=['mae', 'mse'])
model.summary()
history = model.fit(trainSample, trainLable, epochs=EPOCHS, batch_size=BATCH_SIZE)
model.evaluate(testSample, testLable, verbose=1)
model.save("F:\\PyworkingFolder\\WWSHat\\_Data\\alpha0.h5")
What I understand is:
the layers are weight matrices and basis matrices, it works as
out=max(0, weight * input + basis)
After some search, I find I can read the .h5 file using
import h5py
import numpy as np
FILENAME = "F:\\PyworkingFolder\\WWSHat\\_Data\\alpha0.h5"
with h5py.File(FILENAME, 'r') as f:
dense_1 = f['/model_weights/dense_1/dense_1']
dense_1_bias = dense_1['bias:0'][:]
dense_1_kernel = dense_1['kernel:0'][:]
dense_2 = f['/model_weights/dense_2/dense_2']
dense_2_bias = dense_2['bias:0'][:]
dense_2_kernel = dense_2['kernel:0'][:]
# print("Weight matrix 1:\n")
# print(dense_1_kernel)
# print("Basis matrix 1:\n")
# print(dense_1_bias)
# print("Weight matrix 2:\n")
# print(dense_2_kernel)
# print("Basis matrix 2:\n")
# print(dense_2_bias)
def layer_output(v, kernel, bias):
return np.dot(v, kernel) + bias
reluFunction = np.vectorize(lambda x: x if x >= 0.0 else 0.0)
testV = np.array([[-0.004090321213057993,
0.009615388501909157,
-0.24223693596921558,
0.015504079563927319,
-0.02659541428995062,
0.018512968977547152,
0.00836788544720289,
-0.10874776132746002,
-0.045863474556415526,
-0.010195799916571194,
0.09474219315939948,
0.03606698737846194,
-0.004560110004741025,
0.028042417959738858]])
output_1 = layer_output(testV, dense_1_kernel, dense_1_bias)
output_2 = reluFunction(output_1)
output_3 = layer_output(output_2, dense_2_kernel, dense_2_bias)
output_4 = reluFunction(output_3)
however, the result of output_4 is very different from what I get using
loaded_model = keras.models.load_model("F:\\PyworkingFolder\\WWSHat\\_Data\\alpha0.h5")
predicted = loaded_model(testV)
The "predicted" is very close to the ground truth while "output_4" is far away from the ground truth.
I get stuck here and don't know why and failed to find information about how to extract the function I want from the Keras model, I need your help!
Thanks!
model = keras.Sequential([
keras.layers.Dense(14, activation='relu', input_shape=[14]),
keras.layers.Dense(15, activation='relu'),
keras.layers.Dense(1)
])
In your model, there are 3 layers, the last dense layer has weight and biases too, you didn't consider them in your calculation.

Keras CNN's input dimension error: expected 4-dim, but found 3-dim

I have a question about coding CNN using Keras.
The shape of input data(adj) is (20000, 50, 50); 20000 is the number of samples, 50 x 50 are 2-D data (like images). Batch size is 100.
(actually, there are two inputs: adj=(20000, 50, 50), features=(20000, 50, 52).
The issued part is like below:
from keras.layers import Conv2D, MaxPool2D, Flatten
adj_visible1 = Input(shape=(50, 50, 1))
conv11 = Conv2D(16, kernel_size=5, activation='relu')(adj_visible1)
pool11 = MaxPool2D(pool_size=(2, 2))(conv11)
conv12 = Conv2D(8, kernel_size=5, activation='relu')(pool11)
pool12 = MaxPool2D(pool_size=(2, 2))(conv12)
flat1 = Flatten()(pool12)
But an error message occurred like below:
ValueError: Input 0 is incompatible with layer conv2d_1: expected ndim=4, found ndim=3
I found similar cases that print the same message, however, most of the reason is that they didn't consider the filter like (50, 50), not (50, 50, "1") for input shape.
In my case, I used the shape (50, 50, 1) not (50, 50). However, it still prints the same error message.
What should I do?
I'm attaching the full code as follows:
from sklearn.cross_validation import train_test_split
from keras.models import Sequential
from keras.layers.core import Dense, Dropout
from keras.optimizers import RMSprop, Adam, Adadelta
from keras.utils import plot_model
from keras.models import Model
from keras.layers import Input, Flatten, MaxPool2D
from keras.layers.convolutional import Conv2D
from keras.layers.merge import concatenate
from keras.callbacks import CSVLogger
#Settings
epoch = 100
batch_size = 100
test_size = 10000
# Load data
adj = np.load('adj.npy') #(20000, 50, 50)
features = np.load('features.npy') #(20000, 50, 52)
Prop = np.load('Properties.npy') #(20000, 1)
database = np.dstack((adj, features)) #(20000, 50, 102)
#Train/Test split
X_tr, X_te, Y_tr, Y_te = train_test_split(database, Prop, test_size=test_size)
X_tr_adj, X_tr_features = X_tr[:, :, 0:50], X_tr[:, :, 50:]
X_te_adj, X_te_features = X_te[:, :, 0:50], X_te[:, :, 50:]
def create_model():
# first input model
adj_visible1 = Input(shape=(50, 50, 1))
conv11 = Conv2D(16, kernel_size=5, activation='relu')(adj_visible1)
pool11 = MaxPool2D(pool_size=(2, 2))(conv11)
conv12 = Conv2D(8, kernel_size=5, activation='relu')(pool11)
pool12 = MaxPool2D(pool_size=(2, 2))(conv12)
flat1 = Flatten()(pool12)
# second input model
features_visible2 = Input(shape=(50, 52, 1))
conv21 = Conv2D(16, kernel_size=5, activation='relu')(features_visible2)
pool21 = MaxPool2D(pool_size=(2, 2))(conv21)
conv22 = Conv2D(8, kernel_size=5, activation='relu')(pool21)
pool22 = MaxPool2D(pool_size=(2, 2))(conv22)
flat2 = Flatten()(pool22)
# merge input models
merge = concatenate([flat1, flat2])
# interpretation model
hidden1 = Dense(128, activation='relu')(merge)
hidden2 = Dense(32, activation='relu')(hidden1)
output = Dense(1, activation='linear')(hidden2)
model = Model(inputs=[adj_visible1, features_visible2], outputs=output)
model.compile(loss='mean_squared_error', optimizer=Adam())
# summarize layers
print(model.summary())
return model
def train_model(batch_size = 100, nb_epoch = 20):
model = create_model()
csv_logger = CSVLogger('CNN trial.csv')
history = model.fit([X_tr_adj, X_tr_features], Y_tr,
batch_size=batch_size,
epochs=nb_epoch,
verbose=1,
validation_data=([X_te_adj, X_te_features], Y_te),
callbacks=[csv_logger])
predictions_valid = model.predict(X_te_adj, X_te_features, batch_size=batch_size, verbose=1)
return model
train_model(nb_epoch = epoch)
I wrote the code with reference to the following material: https://machinelearningmastery.com/keras-functional-api-deep-learning/
You have to use conv1D and MaxPool1D instead of conv2D and MaxPool2D cause your dataset is a single-channel image instead of 3 channel image. In conv1D layer expect the input to be in format Batch X Height X Width, while in conv2D it expects the input to dimension = 4 i.e Batch X Height X Width X Channels.
from sklearn.model_selection import train_test_split
from keras.models import Sequential
from keras.layers.core import Dense, Dropout
from keras.optimizers import RMSprop, Adam, Adadelta
from keras.utils import plot_model
from keras.models import Model
from keras.layers import Input, Flatten, MaxPool1D
from keras.layers.convolutional import Conv1D
from keras.layers.merge import concatenate
from keras.callbacks import CSVLogger
import numpy as np
epoch = 100
batch_size = 100
test_size = 10000
adj = np.random.randint(0,high=100, size=(20000, 50, 50)) #(20000, 50, 50)
features = np.random.randint(0,high=100, size=(20000, 50, 52)) #(20000, 50, 52)
Prop = np.random.randint(0,high=100, size=(20000,)) #(20000, 1)
database = np.dstack((adj, features)) #(20000, 50, 102)
print( " shape of database :", database.shape)
t
X_tr, X_te, Y_tr, Y_te = train_test_split(database, Prop, test_size=test_size)
X_tr_adj, X_tr_features = X_tr[:, :, 0:50], X_tr[:, :, 50:]
X_te_adj, X_te_features = X_te[:, :, 0:50], X_te[:, :, 50:]
def create_model():
# first input model
adj_visible1 = Input(shape=(50, 50))
conv11 = Conv1D(16, kernel_size=5, activation='relu')(adj_visible1)
pool11 = MaxPool1D(pool_size=2)(conv11)
conv12 = Conv1D(8, kernel_size=5, activation='relu')(pool11)
pool12 = MaxPool1D(pool_size=2)(conv12)
flat1 = Flatten()(pool12)
# second input model
features_visible2 = Input(shape=(50, 52))
conv21 = Conv1D(16, kernel_size=5, activation='relu')(features_visible2)
pool21 = MaxPool1D(pool_size=2)(conv21)
conv22 = Conv1D(8, kernel_size=5, activation='relu')(pool21)
pool22 = MaxPool1D(pool_size=2)(conv22)
flat2 = Flatten()(pool22)
# merge input models
merge = concatenate([flat1, flat2])
# interpretation model
hidden1 = Dense(128, activation='relu')(merge)
hidden2 = Dense(32, activation='relu')(hidden1)
output = Dense(1, activation='linear')(hidden2)
model = Model(inputs=[adj_visible1, features_visible2], outputs=output)
model.compile(loss='mean_squared_error', optimizer=Adam())
# summarize layers
print(model.summary())
return model
def train_model(batch_size = 100, nb_epoch = 20):
model = create_model()
csv_logger = CSVLogger('CNN trial.csv')
history = model.fit([X_tr_adj, X_tr_features], Y_tr,
batch_size=batch_size,
epochs=nb_epoch,
verbose=1,
validation_data=([X_te_adj, X_te_features], Y_te),
callbacks=[csv_logger])
return model
train_model(nb_epoch = 10)

Resources