I am attempting to run a GoogLeNet code, but when I run it, for some reason it says
[INFO] loading CIFAR-10 data...
[INFO] compiling model..
but when my friend runs the same code, his shows
[INFO] loading CIFAR-10 data...
Downloading data from https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz
[INFO] compiling model..
Is there a reason why mine does not show that? I have posted the full code below, which is the exact same code that my friend is also running. The only difference on our system, is that his laptop does not have a GPU, and my desktop runs on GEFORCE RTX 3080.
# Python: 3.6
# keras: 2.2.4 for GoogLeNet on CIFAR-10
# Tensorflow :1.13.0
# cuda toolkit: 10.0
# cuDNN: 7.4.2
# scikit-learn 0.20.2
# Imutils
# NumPy
# set the matplotlib backend so figures can be saved in the background
import matplotlib
matplotlib.use("Agg")
# import packages
from sklearn.metrics import classification_report
from sklearn.preprocessing import LabelBinarizer
from pipeline.nn.conv import MiniGoogLeNet
from pipeline.callbacks import TrainingMonitor
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import LearningRateScheduler
from keras.optimizers import SGD
from keras.datasets import cifar10
import numpy as np
import argparse
import os
# define the total number of epochs to train for along with initial learning rate
NUM_EPOCHS =70
INIT_LR = 5e-3
def poly_decay(epoch):
# initialize the maximum number of epochs, base learning rate,
# and power of the polynomial
maxEpochs = NUM_EPOCHS
baseLR = INIT_LR
power = 1.0
# compute the new learning rate based on polynomial decay
alpha = baseLR * (1 - (epoch / float(maxEpochs))) ** power
# return the new learning rate
return alpha
# construct the argument parser
ap = argparse.ArgumentParser()
ap.add_argument("-m", "--model", required = True, help = "path to output model")
ap.add_argument("-o", "--output", required = True,
help = "path to output directory (logs, plots, etc.)")
args = vars(ap.parse_args())
# load the training and testing data, converting the image from integers to floats
print("[INFO] loading CIFAR-10 data...")
((trainX, trainY), (testX, testY)) = cifar10.load_data()
trainX = trainX.astype("float")
testX = testX.astype("float")
# apply mean subtraction to the data
mean = np.mean(trainX, axis = 0)
trainX -= mean
testX -= mean
# convert the labels from integers to vectors
lb = LabelBinarizer()
trainY = lb.fit_transform(trainY)
testY = lb.transform(testY)
# initialize the label name for CIFAR-10 dataset
labelNames = ["airplane", "automobile", "bird", "cat", "deer",
"dog", "frog", "horse", "ship", "truck"]
# construct the image generator for data augmentation
aug = ImageDataGenerator(width_shift_range = 0.1, height_shift_range = 0.1,
horizontal_flip = True, fill_mode = "nearest")
# construct the set of callbacks
figPath = os.path.sep.join([args["output"], "{}.png".format(os.getpid())])
jsonPath = os.path.sep.join([args["output"], "{}.json".format(os.getpid())])
callbacks = [TrainingMonitor(figPath, jsonPath = jsonPath),
LearningRateScheduler(poly_decay)]
# initialize the optimizer and model
print("[INFO] compiling model...")
opt = SGD(lr = INIT_LR, momentum = 0.9)
model = MiniGoogLeNet.build(width = 32, height = 32, depth = 3, classes = 10)
model.compile(loss = "categorical_crossentropy", optimizer = opt, metrics = ["accuracy"])
#model.compile(optimizer = tf.keras.optimizers.Adam(learning_rate=0.001), loss = 'categorical_crossentropy', metrics = ["accuracy"])
# train the network
print("[INFO] training network...")
model.fit(aug.flow(trainX, trainY, batch_size = 64),
validation_data = (testX, testY), steps_per_epoch = len(trainX) // 64,
epochs = NUM_EPOCHS, callbacks = callbacks, verbose = 1)
# evaluate network
print("[INFO] evaluating network...")
predictions = model.predict(testX, batch_size = 64)
print(classification_report(testY.argmax(axis = 1),
predictions.argmax(axis = 1), target_names = labelNames))
# save the network to disk
print("[INFO] serializing network...")
model.save(args["model"])
#Run Command: python.exe googlenet_cifar10.py --model output/minigooglenet_cifar10.hdf5 --output output
Have you looked at the local datasets?
[ Local datasets ]:
C:\Users\Jirayu Kaewprateep\.keras\datasets
[ Sample ]:
import os
from os.path import exists
import tensorflow as tf
import tensorflow_datasets as tfds
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]
None
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
physical_devices = tf.config.experimental.list_physical_devices('GPU')
assert len(physical_devices) > 0, "Not enough GPU hardware devices available"
config = tf.config.experimental.set_memory_growth(physical_devices[0], True)
print(physical_devices)
print(config)
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Variables
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
class_10_names = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']
checkpoint_path = "F:\\models\\checkpoint\\" + os.path.basename(__file__).split('.')[0] + "\\TF_DataSets_01.h5"
checkpoint_dir = os.path.dirname(checkpoint_path)
if not exists(checkpoint_dir) :
os.mkdir(checkpoint_dir)
print("Create directory: " + checkpoint_dir)
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
DataSet
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
(train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.cifar10.load_data()
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Model Initialize
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
model = tf.keras.models.Sequential([
tf.keras.layers.InputLayer(input_shape=( 32, 32, 3 )),
tf.keras.layers.Normalization(mean=3., variance=2.),
tf.keras.layers.Normalization(mean=4., variance=6.),
tf.keras.layers.Conv2D(32, (3, 3), activation='relu'),
tf.keras.layers.MaxPooling2D((2, 2)),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Reshape((128, 225)),
tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(96, return_sequences=True, return_state=False)),
tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(96)),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(192, activation='relu'),
tf.keras.layers.Dense(10),
])
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Callback
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
class custom_callback(tf.keras.callbacks.Callback):
def on_epoch_end(self, epoch, logs={}):
if( logs['accuracy'] >= 0.97 ):
self.model.stop_training = True
custom_callback = custom_callback()
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Optimizer
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
optimizer = tf.keras.optimizers.Nadam(
learning_rate=0.00001, beta_1=0.9, beta_2=0.999, epsilon=1e-07,
name='Nadam'
)
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Loss Fn
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
lossfn = tf.keras.losses.SparseCategoricalCrossentropy(
from_logits=False,
reduction=tf.keras.losses.Reduction.AUTO,
name='sparse_categorical_crossentropy'
)
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Model Summary
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
model.compile(optimizer=optimizer, loss=lossfn, metrics=['accuracy'])
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: FileWriter
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
if exists(checkpoint_path) :
model.load_weights(checkpoint_path)
print("model load: " + checkpoint_path)
input("Press Any Key!")
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Training
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
history = model.fit( train_images, train_labels, batch_size=100, epochs=50, callbacks=[custom_callback] )
model.save_weights(checkpoint_path)
plt.figure(figsize=(6, 6))
plt.title("Actors recognitions")
for i in range( 36 ):
img = tf.keras.preprocessing.image.array_to_img(
test_images[i],
data_format=None,
scale=True
)
img_array = tf.keras.preprocessing.image.img_to_array(img)
img_array = tf.expand_dims(img_array, 0)
predictions = model.predict(img_array)
score = tf.nn.softmax(predictions[0])
plt.subplot(6, 6, i + 1)
plt.xticks([])
plt.yticks([])
plt.grid(False)
plt.imshow(test_images[i])
plt.xlabel(str(round(score[tf.math.argmax(score).numpy()].numpy(), 2)) + ":" + str(class_10_names[tf.math.argmax(score)]))
plt.show()
input('...')
Related
I am trying to predict a sequence of images. Before I was using only a CNN that took as input these concatenated images, but it didn't give me very good results with some databases.
I am using two types of databases, one takes a single image and classifies it. The other database takes a sequence of images and classifies it. So I use total_x_test_indexes=tf.expand_dims(total_x_test_indexes, axis=1) to generalize the model when only classifies one image.
As I saw I could better use a CNN and then apply a LSTM and I saw how to do it here.
But I'm only getting confusion matrices like this, classifying almost everything to a class.
My code is this:
inp = Input((None,size_image,size_image,1), ragged=True)
x = TimeDistributed(cnn)(inp)
x = LSTM(25)(x)
size_predictions=len(dicTiposNumbers)
print("tamaƱo ",size_predictions)
out = Dense(size_predictions)(x)
model = Model(inp, out)
print(model.summary())
opt = keras.optimizers.Adam(learning_rate=0.05)
opt = keras.optimizers.SGD(learning_rate=0.15)
# Compile the model
model.compile(optimizer=opt,
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
metrics=['accuracy'])
print('------------------------------------------------------------------------')
print(f'Training for fold {fold_no} ...')
total_x_train_indexes=tf.gather(total_x,indices=train)
total_y_train_indexes=tf.gather(total_y,indices=train)
total_x_train_indexes=tf.expand_dims(total_x_train_indexes, axis=1)
print("shape after gather",np.shape(total_x_train_indexes[0]))
history = model.fit(total_x_train_indexes, total_y_train_indexes,
batch_size=512,
epochs=5)
But I'm getting this and similar with other databases with more classes:
From your question, determine the network's purpose and input data responsive. I created a simple custom layer telling you that the process layer is nothing than simple calculation at each layer data process output is from convolution layers and the dense layers.
Sample: Improving method is done by compare of input / output and try to expands the effects.
Confusion matrix, he tries to see the effects of overall model training and predict with input data.
import os
from os.path import exists
import tensorflow as tf
import matplotlib.pyplot as plt
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]
None
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
physical_devices = tf.config.experimental.list_physical_devices('GPU')
assert len(physical_devices) > 0, "Not enough GPU hardware devices available"
config = tf.config.experimental.set_memory_growth(physical_devices[0], True)
print(physical_devices)
print(config)
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Variables
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
BATCH_SIZE = 1
IMAGE_SIZE = ( 21, 16 )
objects_classes = [ 'plane', 'helicopter', 'truck' ]
checkpoint_path = "F:\\models\\checkpoint\\" + os.path.basename(__file__).split('.')[0] + "\\TF_DataSets_01.h5"
checkpoint_dir = os.path.dirname(checkpoint_path)
if not exists(checkpoint_dir) :
os.mkdir(checkpoint_dir)
print("Create directory: " + checkpoint_dir)
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Class / Definition
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
class MyLSTMLayer( tf.keras.layers.LSTM ):
def __init__(self, units, return_sequences, return_state):
super(MyLSTMLayer, self).__init__( units, return_sequences=True, return_state=False )
self.num_units = units
def build(self, input_shape):
self.kernel = self.add_weight("kernel",
shape=[int(input_shape[-1]),
self.num_units])
def call(self, inputs):
return tf.matmul(inputs, self.kernel)
def gen():
train_generator = tf.keras.preprocessing.image.ImageDataGenerator(
# rescale=1./255,
# shear_range=0.2,
# zoom_range=0.2,
# horizontal_flip=True
)
train_generator = train_generator.flow_from_directory(
'F:\\temp\\image_catagorize',
classes=[ 'plane', 'helicopter', 'truck' ],
target_size=IMAGE_SIZE,
batch_size=BATCH_SIZE,
color_mode='grayscale',
class_mode='sparse', # None # categorical # binary # sparse
subset='training')
return train_generator
train_generator = gen()
val_generator = train_generator
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Callback
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
class custom_callback(tf.keras.callbacks.Callback):
def on_epoch_end(self, epoch, logs={}):
if( logs['accuracy'] >= 0.97 ):
self.model.stop_training = True
custom_callback = custom_callback()
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Model Initialize
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
mycustomlayer = MyLSTMLayer( 64, True, False )
model = tf.keras.models.Sequential([
tf.keras.layers.InputLayer(input_shape=( IMAGE_SIZE[0], IMAGE_SIZE[1], 1 ), name="Input_01Layer"),
tf.keras.layers.UpSampling2D(size=(4, 4), name="UpSampling2DLayer_01"),
tf.keras.layers.Normalization(mean=3., variance=2., name="NormalizationLayer_01"),
tf.keras.layers.Normalization(mean=4., variance=6., name="NormalizationLayer_02"),
tf.keras.layers.Conv2D(32, (3, 3), activation='relu', name="Conv2DLayer_01"),
tf.keras.layers.MaxPooling2D((3, 3), name="MaxPooling2DLayer_01"),
tf.keras.layers.Conv2D(32, (2, 2), activation='relu', name="Conv2DLayer_02"),
tf.keras.layers.MaxPooling2D((2, 2), name="MaxPooling2DLayer_02"),
tf.keras.layers.Conv2D(64, (3, 3), activation='relu', name="Conv2DLayer_03"),
tf.keras.layers.Reshape(( 7 * 11, 64 )),
###
mycustomlayer,
###
tf.keras.layers.Dense(512, activation='relu', name="DenseLayer_01"),
tf.keras.layers.Flatten(name="FlattenLayer_01"),
tf.keras.layers.Dense(192, activation='relu', name="DenseLayer_02"),
tf.keras.layers.Dense(3, name="DenseLayer_03"),
], name="MyModelClassification")
model.summary()
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Optimizer
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
optimizer = tf.keras.optimizers.SGD(
learning_rate=0.000001,
momentum=0.5,
nesterov=True,
name='SGD',
)
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Loss Fn
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
lossfn = tf.keras.losses.SparseCategoricalCrossentropy(
from_logits=True,
reduction=tf.keras.losses.Reduction.AUTO,
name='sparse_categorical_crossentropy'
)
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Model Summary
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
model.compile(optimizer=optimizer, loss=lossfn, metrics=['accuracy'])
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: FileWriter
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
if exists(checkpoint_path) :
model.load_weights(checkpoint_path)
print("model load: " + checkpoint_path)
input("Press Any Key!")
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Training
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
history = model.fit(train_generator, validation_data=val_generator, batch_size=100, epochs=3, callbacks=[custom_callback] )
model.save_weights(checkpoint_path)
PATH = os.path.join('F:\\temp\\image_catagorize\\helicopter', '*.png')
files = tf.data.Dataset.list_files(PATH)
list_file = []
for file in files.take(20):
image = tf.io.read_file( file )
image = tf.io.decode_png( image, channels=1, dtype=tf.dtypes.uint8, name='decode_png' )
image = tf.keras.preprocessing.image.img_to_array(image)
image = tf.image.resize(image, IMAGE_SIZE, method='nearest')
list_file.append(image)
PATH = os.path.join('F:\\temp\\image_catagorize\\plane', '*.png')
files = tf.data.Dataset.list_files(PATH)
for file in files.take(8):
image = tf.io.read_file( file )
image = tf.io.decode_png( image, channels=1, dtype=tf.dtypes.uint8, name='decode_png' )
image = tf.keras.preprocessing.image.img_to_array(image)
image = tf.image.resize(image, IMAGE_SIZE, method='nearest')
list_file.append(image)
PATH = os.path.join('F:\\temp\\image_catagorize\\Truck', '*.png')
files = tf.data.Dataset.list_files(PATH)
for file in files.take(8):
image = tf.io.read_file( file )
image = tf.io.decode_png( image, channels=1, dtype=tf.dtypes.uint8, name='decode_png' )
image = tf.keras.preprocessing.image.img_to_array(image)
image = tf.image.resize(image, IMAGE_SIZE, method='nearest')
list_file.append(image)
plt.figure(figsize=(6, 6))
plt.title("Actors recognitions")
for i in range(len(list_file)):
img = tf.keras.preprocessing.image.array_to_img(
list_file[i],
data_format=None,
scale=True
)
img_array = tf.keras.preprocessing.image.img_to_array(img)
img_array = tf.expand_dims(img_array, 0)
predictions = model.predict(img_array)
score = tf.nn.softmax(predictions[0])
plt.subplot(6, 6, i + 1)
plt.xticks([])
plt.yticks([])
plt.grid(False)
plt.imshow(list_file[i])
plt.xlabel(str(round(score[tf.math.argmax(score).numpy()].numpy(), 2)) + ":" + str(objects_classes[tf.math.argmax(score)]))
plt.show()
import os
os.add_dll_directory("C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.2/bin")
import tensorflow as tf
from keras import models
from keras import Sequential
from keras import layers
from keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
import os
print(len(os.listdir('COVID-19_Radiography_Dataset/COVID/images')))
print(len(os.listdir('COVID-19_Radiography_Dataset/Normal/images')))
import cv2
img = cv2.imread('COVID-19_Radiography_Dataset/Normal/images/Normal-10005.png')
import matplotlib.pyplot as plt
#plt.imshow(img)
#plt.show()
print(img.shape)
import pandas as pd
import numpy as np
df = pd.read_excel('COVID-19_Radiography_Dataset/COVID.metadata.xlsx')
print(df.head())
urls = os.listdir('COVID-19_Radiography_Dataset/COVID/images')
path = "COVID-19_Radiography_Dataset/COVID/images/" + urls[0]
print(path)
def loadImages(path, urls, target):
images = []
labels = []
for i in range(len(urls)):
img_path = path + "/" + urls[i]
img = cv2.imread(img_path)
img = img / 255.0
img = cv2.resize(img, (100, 100))
images.append(img)
labels.append(target)
images = np.asarray(images)
return images, labels
covid_path = "COVID-19_Radiography_Dataset/COVID/images"
covidUrl = os.listdir(covid_path)
covidImages, covidTargets = loadImages(covid_path, covidUrl, 1)
print(len(covidUrl))
print(len(covidImages))
normal_path = "COVID-19_Radiography_Dataset/Normal/images"
normal_urls = os.listdir(normal_path)
normalImages, normalTargets = loadImages(normal_path, normal_urls, 0)
viral_path = "COVID-19_Radiography_Dataset/Viral Pneumonia/images"
viral_urls = os.listdir(viral_path)
viralImages, viralTargets = loadImages(viral_path, viral_urls, 0)
print(covidImages.shape)
print(normalImages.shape)
print(viralImages.shape)
data = np.r_[covidImages, normalImages, viralImages]
print(data.shape)
targets = np.r_[covidTargets, normalTargets, viralTargets]
print(targets.shape)
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(data, targets, test_size=0.25)
#import tensorflow as tf
#from tensorflow import keras
#from keras import models
#from keras import Sequential
#from keras import layers
#from keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
##
model = Sequential([
Conv2D(32, 3, input_shape=(100,100,3), activation='relu'),
MaxPooling2D(),
Conv2D(16, 3, activation='relu'),
MaxPooling2D(),
Conv2D(16, 3, activation='relu'),
MaxPooling2D(),
Flatten(),
Dense(512, activation='relu'),
Dense(256, activation='relu'),
Dense(1, activation='sigmoid')
])
print(model.summary())
model.compile(optimizer='adam', loss=tf.keras.losses.BinaryCrossentropy(),metrics=['accuracy'])
model.fit(x_train, y_train,batch_size=32,epochs=15,validation_data=(x_test, y_test))
plt.plot(model.history.history['accuracy'], label = 'train accuracy')
plt.plot(model.history.history['val_accuracy'],label = 'test_accuracy')
plt.legend()
plt.show()
plt.plot(model.history.history['loss'], label = 'train loss')
plt.plot(model.history.history['val_loss'],label = 'test_loss')
plt.legend()
plt.show()
url = "https://radiologyassistant.nl/assets/_1-old-1.jpg"
testimage = tf.keras.utils.get_file('X-ray', origin=url)
img = tf.keras.utils.load_img(
testimage, target_size=(100, 100)
)
img_array = tf.keras.utils.img_to_array(img)
img_array = tf.expand_dims(img_array, 0)
predictions = model.predict(img_array)
score = tf.nn.softmax(predictions[0])
print(predictions)
print(score)
Its supposed to classify it as COVID, normal, or Viral, however it the prediction always returns 1... I want it to return one of the three classes, I am not quite sure why it keeps returning 1 when printing predictions, please help. I am using a data set of COVID related pneumonia chest X-rays, Normal Chest x-rays and viral pneumonia chest xrays.
it is for medical instruments BinaryCrossEntrophy classification because it required the most accurate from 0 to 1. I skipped the model path and sample as there are no data examples but one answer repeating may occur for these reasons.
Model training and learning rates, the model needs to learn in appropriate manners and stop or re-train in order that why image classification uses a custom callback class when testing ( for actually not use the custom callback class that is because data wide ranges and variety of data must be learning, the early stop may work well with scopes data but not always for new data )
Folding and variety, I used to train numbers recognitions from Thailand's public automatic telephony box exchange which is a large dataset. Create datasets as folds working with sample data and new data you will see the results as a long-time learning dense layer is made senses but longer-time convolution layers are critical parameters, those input voices are different by personality and domestic cultures.
Feature extractions and enchantment methods, sometimes input images are huge, and repeats of data, mathematics functions and wave properties work well with natural logarithms ( not only waves, bones, body and response are )
Mapping and cross functions, image masking, feature mapping by related data than feature extraction such as options selection, dictionary, and command and language model but for image classification image masking and info data ( geographic data, sexual, age and related )
Model transform and database learning, you will find sometimes for convenience use after training the model we transform knowledge into a simple dense layers model that we can remove or add layers and parameters for better results.
The code is already correct experiments is the tasks.
Sample: ( My template ) << There are many tasks to create a pair of sandals than you know to wear and feel comfortable to see grandmother >>
import os
from os.path import exists
import tensorflow as tf
import tensorflow_io as tfio
import matplotlib.pyplot as plt
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]
None
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
physical_devices = tf.config.experimental.list_physical_devices('GPU')
assert len(physical_devices) > 0, "Not enough GPU hardware devices available"
config = tf.config.experimental.set_memory_growth(physical_devices[0], True)
print(physical_devices)
print(config)
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Variables
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
PATH = os.path.join('F:\\datasets\\downloads\\Actors\\train\\Pikaploy', '*.tif')
PATH_2 = os.path.join('F:\\datasets\\downloads\\Actors\\train\\Candidt Kibt', '*.tif')
files = tf.data.Dataset.list_files(PATH)
files_2 = tf.data.Dataset.list_files(PATH_2)
list_file = []
list_file_actual = []
list_label = []
list_label_actual = [ 'Pikaploy', 'Pikaploy', 'Pikaploy', 'Pikaploy', 'Pikaploy', 'Candidt Kibt', 'Candidt Kibt', 'Candidt Kibt', 'Candidt Kibt', 'Candidt Kibt' ]
for file in files.take(5):
image = tf.io.read_file( file )
image = tfio.experimental.image.decode_tiff(image, index=0)
list_file_actual.append(image)
image = tf.image.resize(image, [32,32], method='nearest')
list_file.append(image)
list_label.append(1)
for file in files_2.take(5):
image = tf.io.read_file( file )
image = tfio.experimental.image.decode_tiff(image, index=0)
list_file_actual.append(image)
image = tf.image.resize(image, [32,32], method='nearest')
list_file.append(image)
list_label.append(9)
checkpoint_path = "F:\\models\\checkpoint\\" + os.path.basename(__file__).split('.')[0] + "\\TF_DataSets_01.h5"
checkpoint_dir = os.path.dirname(checkpoint_path)
loggings = "F:\\models\\checkpoint\\" + os.path.basename(__file__).split('.')[0] + "\\loggings.log"
if not exists(checkpoint_dir) :
os.mkdir(checkpoint_dir)
print("Create directory: " + checkpoint_dir)
log_dir = checkpoint_dir
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
DataSet
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
dataset = tf.data.Dataset.from_tensor_slices((tf.constant(tf.cast(list_file, dtype=tf.int64), shape=(10, 1, 32, 32, 4), dtype=tf.int64),tf.constant(list_label, shape=(10, 1, 1), dtype=tf.int64)))
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Model Initialize
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
model = tf.keras.models.Sequential([
tf.keras.layers.InputLayer(input_shape=( 32, 32, 4 )),
tf.keras.layers.Normalization(mean=3., variance=2.),
tf.keras.layers.Normalization(mean=4., variance=6.),
tf.keras.layers.Conv2D(32, (3, 3), activation='relu'),
tf.keras.layers.MaxPooling2D((2, 2)),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Reshape((128, 225)),
tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(96, return_sequences=True, return_state=False)),
tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(96)),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(192, activation='relu'),
tf.keras.layers.Dense(10),
])
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Callback
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
class custom_callback(tf.keras.callbacks.Callback):
def on_epoch_end(self, epoch, logs={}):
# if( logs['loss'] <= 0.2 ):
# self.model.stop_training = True
if( logs['accuracy'] >= 0.60 ):
self.model.stop_training = True
custom_callback = custom_callback()
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Optimizer
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
optimizer = tf.keras.optimizers.Nadam(
learning_rate=0.00001, beta_1=0.9, beta_2=0.999, epsilon=1e-07,
name='Nadam'
)
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Loss Fn
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
lossfn = tf.keras.losses.SparseCategoricalCrossentropy(
from_logits=False,
reduction=tf.keras.losses.Reduction.AUTO,
name='sparse_categorical_crossentropy'
)
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Model Summary
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
model.compile(optimizer=optimizer, loss=lossfn, metrics=['accuracy'])
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: FileWriter
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
if exists(checkpoint_path) :
model.load_weights(checkpoint_path)
print("model load: " + checkpoint_path)
input("Press Any Key!")
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Training
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
history = model.fit( dataset, batch_size=100, epochs=10000, callbacks=[custom_callback] )
model.save_weights(checkpoint_path)
plt.figure(figsize=(5,2))
plt.title("Actors recognitions")
for i in range(len(list_file)):
img = tf.keras.preprocessing.image.array_to_img(
list_file[i],
data_format=None,
scale=True
)
img_array = tf.keras.preprocessing.image.img_to_array(img)
img_array = tf.expand_dims(img_array, 0)
predictions = model.predict(img_array)
score = tf.nn.softmax(predictions[0])
plt.subplot(5, 2, i + 1)
plt.xticks([])
plt.yticks([])
plt.grid(False)
plt.imshow(list_file_actual[i])
plt.xlabel(str(round(score[tf.math.argmax(score).numpy()].numpy(), 2)) + ":" + str(list_label_actual[tf.math.argmax(score)]))
plt.show()
input('...')
Below dataframe contains image paths and the output columns(A1 to A7).
Output columns show what category the image belongs to. There are
seven different categories. Image can be of multiple category.
(A0...A7).
Total image count is 30000.
I want to train a model on this data using tensor flow. Need help for
data_generator for this data as my resources are getting exausted.
Even for batch size 2 it is giving error 'ResourceExhaustedError'
My CPU 13 GB and GPU 15 GB.
DataFrame:
Image Path
A0
A1
A2
A3
A4
A5
A6
Img Path1
1
1
0
0
0
0
0
Img Path2
1
1
0
0
0
0
0
Img Path3
0
1
1
0
0
0
0
'''
'''
Img Pathn
0
0
0
0
0
0
1
My Code for model building:
def data_generator():
for i, study_instance in enumerate(meta_seg.StudyInstanceUID.unique()):
for dcm in os.listdir(DATA_DIR + f"/train_images/{study_instance}"):
train_labels = []
path = DATA_DIR + f"/train_images/{study_instance}/{dcm}"
#print(path)
img = load_dicom(path)
img = np.resize(img, (512, 512))
# normalize image
img = img / 255.0
img = tf.expand_dims(img, axis=-1)
img = tf.image.grayscale_to_rgb(img)
train_labels.extend([
meta_seg.loc[i, "A0"],
meta_seg.loc[i, "A1"],
meta_seg.loc[i, "A2"],
meta_seg.loc[i, "A3"],
meta_seg.loc[i, "A4"],
meta_seg.loc[i, "A5"],
meta_seg.loc[i, "A6"]])
yield img, train_labels
train_data = tf.data.Dataset.from_generator(data_generator, (tf.float32, tf.int8))
def configure_for_performance(data):
data = data.cache()
data = data.batch(2)
data = data.prefetch(buffer_size=tf.data.AUTOTUNE)
return data
train_data = configure_for_performance(train_data)
val_data = configure_for_performance(val_data)
def cnn_model():
model = Sequential()
Layer 1...
Layer 2...
I have a sample code, it may help with the database working memory problem, a label number you can replace it with data [ A0, A1, A2, A3, A4, A5, A6 ]. Try to change the Loss and Optimizer function.
Sample I am using with "Street Fighters" game as discrete outputs.
dataset = tf.data.Dataset.from_tensor_slices((tf.constant(np.reshape(output_picture[np.argmax(result)], (1, 1, 1, 60, 78, 3)) , dtype=tf.float32), tf.constant(np.reshape(action, (1, 1, 2, 3, 2, 1)))))
Sample Codes: Use database buffers.
import os
from os.path import exists
import tensorflow as tf
import h5py
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]
None
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
physical_devices = tf.config.experimental.list_physical_devices('GPU')
assert len(physical_devices) > 0, "Not enough GPU hardware devices available"
config = tf.config.experimental.set_memory_growth(physical_devices[0], True)
print(physical_devices)
print(config)
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Variables
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
filters = 32
kernel_size = (3, 3)
strides = 1
database_buffer = "F:\\models\\buffer\\" + os.path.basename(__file__).split('.')[0] + "\\TF_DataSets_01.h5"
database_buffer_dir = os.path.dirname(database_buffer)
checkpoint_path = "F:\\models\\checkpoint\\" + os.path.basename(__file__).split('.')[0] + "\\TF_DataSets_01.h5"
checkpoint_dir = os.path.dirname(checkpoint_path)
if not exists(checkpoint_dir) :
os.mkdir(checkpoint_dir)
print("Create directory: " + checkpoint_dir)
if not exists(database_buffer_dir) :
os.mkdir(database_buffer_dir)
print("Create directory: " + database_buffer_dir)
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Functions
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
def conv_batchnorm_relu(filters, kernel_size, strides=1):
model = tf.keras.models.Sequential([
tf.keras.layers.InputLayer(input_shape=( 32, 32, 3 )),
tf.keras.layers.Conv2D(filters=filters, kernel_size=kernel_size, strides=strides, padding = 'same'),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.ReLU(),
])
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(64))
model.add(tf.keras.layers.Dense(10))
model.summary()
return model
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: DataSet
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
(train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.cifar10.load_data()
# Create hdf5 file
hdf5_file = h5py.File(database_buffer, mode='w')
# Train images
hdf5_file['x_train'] = train_images
hdf5_file['y_train'] = train_labels
# Test images
hdf5_file['x_test'] = test_images
hdf5_file['y_test'] = test_labels
hdf5_file.close()
# Visualize dataset train sample
hdf5_file = h5py.File(database_buffer, mode='r')
x_train = hdf5_file['x_train'][0: 10000]
x_test = hdf5_file['x_test'][0: 100]
y_train = hdf5_file['y_train'][0: 10000]
y_test = hdf5_file['y_test'][0: 100]
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Optimizer
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
optimizer = tf.keras.optimizers.Nadam( learning_rate=0.0001, beta_1=0.9, beta_2=0.999, epsilon=1e-07, name='Nadam' )
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Loss Fn
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
lossfn = tf.keras.losses.MeanSquaredLogarithmicError(reduction=tf.keras.losses.Reduction.AUTO, name='mean_squared_logarithmic_error')
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Model Summary
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
model = conv_batchnorm_relu(filters, kernel_size, strides=1)
model.compile(optimizer=optimizer, loss=lossfn, metrics=['accuracy'])
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: FileWriter
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
if exists(checkpoint_path) :
model.load_weights(checkpoint_path)
print("model load: " + checkpoint_path)
input("Press Any Key!")
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Training
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
history = model.fit(x_train, y_train, epochs=1 ,validation_data=(x_train, y_train))
model.save_weights(checkpoint_path)
input('...')
Plable with famous retro game, I like his herriken kicks actions.
I am trying to save a custom Tensorflow model after 1 epoch training. When the model contains BatchNormalization layer it can not be saved. I can see that "fused_batch_norm" is can not be serialized. How can I call another BatchNormalization layer which can be serialized and saved with both ".h5" and ".pb" format. I am using Tensorflow 2.8 with Tensorflow-metal on MacOS.
def conv_batchnorm_relu(x, filters, kernel_size, strides=1):
# s
x = tf.keras.layers.Conv2D(filters=filters, kernel_size=kernel_size, strides=strides, padding = 'same')(x)
x = tf.keras.layers.BatchNormalization()(x)
x = tf.keras.layers.ReLU()(x)
return x
TypeError: Layer tf.compat.v1.nn.fused_batch_norm was passed non-JSON-serializable arguments. Arguments had types: {'scale': <class 'tensorflow.python.ops.resource_variable_ops.ResourceVariable'>, 'offset': <class 'tensorflow.python.ops.resource_variable_ops.ResourceVariable'>, 'mean': <class 'tensorflow.python.ops.resource_variable_ops.ResourceVariable'>, 'variance': <class 'tensorflow.python.ops.resource_variable_ops.ResourceVariable'>, 'epsilon': <class 'float'>, 'is_training': <class 'bool'>, 'data_format': <class 'str'>}. They cannot be serialized out when saving the model.
Edit: I used tf.keras.layers.experimental.SyncBatchNormalization()function instead of tf.keras.layers.BatchNormalization() and problem solved for now.
What I understood from your given template is, you can not use support function this way for adding custom layers in your model. Typically you inherit from keras.Model when you need the model methods like: Model.fit ,Model.evaluate, and Model.save
Here is a example code for better understanding
class ExampleBlock(tf.keras.Model):
def __init__(self, kernel_size, filter1,filter2):
super(ExampleBlock, self).__init__(name='')
self.conv2a = tf.keras.layers.Conv2D(filters1, (1, 1))
self.bn2a = tf.keras.layers.BatchNormalization()
self.conv2b = tf.keras.layers.Conv2D(filters2, kernel_size, padding='same')
self.bn2b = tf.keras.layers.BatchNormalization()
# You can as many layer you like
def call(self, input_tensor, training=False):
x = self.conv2a(input_tensor)
x = self.bn2a(x, training=training)
x = tf.nn.relu(x)
x = self.conv2b(x)
x = self.bn2b(x, training=training)
x = tf.nn.relu(x)
return tf.nn.relu(x)
After defining your model you can create checkpoint after each epoch by defining the call back function.
import keras
model_save_path = "/content/drive/MyDrive/Project/" #path for saving checkpoint
checkpoint = keras.callbacks.ModelCheckpoint(model_save_path+'checkpoint_{epoch:02d}', save_freq='epoch')
You can use this checkpoint in the callback funtion
H = model.fit(
aug.flow(trainX, trainY, batch_size=BS), validation_data=(validX, validY), steps_per_epoch=len(trainX) // BS, epochs=EPOCHS,
class_weight=classWeight,
verbose=1,callbacks=[checkpoint])
You can apply matrix properties or you can create a model with a support function.
Sample:
import os
from os.path import exists
import tensorflow as tf
import h5py
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Variables
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
filters = 32
kernel_size = (3, 3)
strides = 1
database_buffer = "F:\\models\\buffer\\" + os.path.basename(__file__).split('.')[0] + "\\TF_DataSets_01.h5"
database_buffer_dir = os.path.dirname(database_buffer)
checkpoint_path = "F:\\models\\checkpoint\\" + os.path.basename(__file__).split('.')[0] + "\\TF_DataSets_01.h5"
checkpoint_dir = os.path.dirname(checkpoint_path)
loggings = "F:\\models\\checkpoint\\" + os.path.basename(__file__).split('.')[0] + "\\loggings.log"
if not exists(checkpoint_dir) :
os.mkdir(checkpoint_dir)
print("Create directory: " + checkpoint_dir)
if not exists(database_buffer_dir) :
os.mkdir(database_buffer_dir)
print("Create directory: " + database_buffer_dir)
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Functions
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
def conv_batchnorm_relu(filters, kernel_size, strides=1):
model = tf.keras.models.Sequential([
tf.keras.layers.InputLayer(input_shape=( 32, 32, 3 )),
tf.keras.layers.Conv2D(filters=filters, kernel_size=kernel_size, strides=strides, padding = 'same'),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.ReLU(),
])
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(64))
model.add(tf.keras.layers.Dense(10))
model.summary()
return model
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
DataSet
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
(train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.cifar10.load_data()
# Create hdf5 file
hdf5_file = h5py.File(database_buffer, mode='w')
# Train images
hdf5_file['x_train'] = train_images
hdf5_file['y_train'] = train_labels
# Test images
hdf5_file['x_test'] = test_images
hdf5_file['y_test'] = test_labels
hdf5_file.close()
# Visualize dataset train sample
hdf5_file = h5py.File(database_buffer, mode='r')
x_train = hdf5_file['x_train'][0: 10000]
x_test = hdf5_file['x_test'][0: 100]
y_train = hdf5_file['y_train'][0: 10000]
y_test = hdf5_file['y_test'][0: 100]
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Optimizer
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
optimizer = tf.keras.optimizers.Nadam( learning_rate=0.0001, beta_1=0.9, beta_2=0.999, epsilon=1e-07, name='Nadam' )
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Loss Fn
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
lossfn = tf.keras.losses.MeanSquaredLogarithmicError(reduction=tf.keras.losses.Reduction.AUTO, name='mean_squared_logarithmic_error')
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Model Summary
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
model = conv_batchnorm_relu(filters, kernel_size, strides=1)
model.compile(optimizer=optimizer, loss=lossfn, metrics=['accuracy'])
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: FileWriter
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
if exists(checkpoint_path) :
model.load_weights(checkpoint_path)
print("model load: " + checkpoint_path)
input("Press Any Key!")
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Training
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
history = model.fit(x_train, y_train, epochs=1 ,validation_data=(x_train, y_train))
model.save_weights(checkpoint_path)
input('...')
I have created a CNN model using keras from tensorflow.
The model is not accurate yet but just as a part of POC I wanted to check if the output of the same image given to the keras model and keras-to-tflite converted model are the same or not.
Just to be clear, my main intension here is to make sure the output of the keras model and the converted tflite model is exactly the same.
And by saying "exactly the same" I mean the floating point values of the output neurons/tensors of both models should be the same when the same image is fed as input image. This is what I think should happen IDEALLY.
used this link to create the basic code and will modify later as needed https://www.tensorflow.org/tutorials/images/classification#create_a_dataset
this is the code for creating a CNN using keras
'''
link: https://www.tensorflow.org/tutorials/images/classification#create_a_dataset
'''
import os
from datetime import datetime
import matplotlib.pyplot as plt
import numpy as np
from time import time
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.models import Sequential
def download_training_dataset():
import pathlib
dataset_url = 'https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz'
data_dir = tf.keras.utils.get_file('flower_photos', origin=dataset_url, untar=True)
data_dir = pathlib.Path(data_dir)
def test_image_dataset():
sunflower_url = 'https://storage.googleapis.com/download.tensorflow.org/example_images/592px-Red_sunflower.jpg'
sunflower_path = tf.keras.utils.get_file('Red_sunflower', origin=sunflower_url)
def create_and_configure_datasets(img_height, img_width, batch_size):
# It's good practice to use a validation split when developing your model.
# Let's use 80% of the images for training, and 20% for validation.
train_ds = tf.keras.utils.image_dataset_from_directory(
data_dir,
validation_split=0.2,
subset='training',
seed=123,
image_size=(img_height, img_width),
batch_size=batch_size
)
val_ds = tf.keras.utils.image_dataset_from_directory(
data_dir,
validation_split=0.2,
subset='validation',
seed=123,
image_size=(img_height, img_width),
batch_size=batch_size
)
# You can find the class names in the class_names attribute on these datasets
class_names = train_ds.class_names
print(class_names)
# # Visualize the data
# print('train_ds:', train_ds)
# plt.figure(figsize=(10, 10))
# for images, labels in train_ds.take(1):
# for i in range(9):
# ax = plt.subplot(3, 3, i + 1)
# print('img type:', type(images[i]), images[i].shape)
# print('pixel[500, 379]', images[i][500][379][0], images[i][500][379][1], images[i][500][379][2])
# print('pixel[379, 500]', images[i][379][500][0], images[i][379][500][1], images[i][379][500][2])
# print('pixel[-500, -379]', images[i][-500][-379][0], images[i][-500][-379][1], images[i][-500][-379][2])
# print('pixel[-379, -500]', images[i][-379][-500][0], images[i][-379][-500][1], images[i][-379][-500][2])
# npimg = images[i].numpy().astype("uint8")
# print('pixel[500, 379]', npimg[500][379][0], npimg[500][379][1], npimg[500][379][2])
# print('pixel[379, 500]', npimg[379][500][0], npimg[379][500][1], npimg[379][500][2])
# print('pixel[-500, -379]', npimg[-500][-379][0], npimg[-500][-379][1], npimg[-500][-379][2])
# print('pixel[-379, -500]', npimg[-379][-500][0], npimg[-379][-500][1], npimg[-379][-500][2])
# plt.imshow(npimg)
# plt.title(class_names[labels[i]])
# plt.axis("off")
# exit()
################ Configure the dataset for performance ################
AUTOTUNE = tf.data.AUTOTUNE
train_ds = train_ds.cache().shuffle(1000).prefetch(buffer_size=AUTOTUNE)
val_ds = val_ds.cache().prefetch(buffer_size=AUTOTUNE)
################ Standardize the data ################
normalization_layer = layers.Rescaling(1./255)
normalized_ds = train_ds.map(lambda x, y: (normalization_layer(x), y))
image_batch, labels_batch = next(iter(normalized_ds))
first_image = image_batch[0]
# Notice the pixel values are now in `[0,1]`.
print(np.min(first_image), np.max(first_image))
return train_ds, val_ds, class_names
def create_and_compile_model(img_height, img_width, class_names, use_data_augmentation = False):
################ Create the model ################
num_classes = len(class_names)
################ Data augmentation ################
# data augmentation steps makes sure the data is fed to the model
# 8 to 10 (by understanding) different angles(with by rotating)
# and with different levels of zooms
# the complete usability of use_data_augmentation is not done
# currently no data_augmentation will be done(as in commenting in model creation statement)
if use_data_augmentation:
data_augmentation = keras.Sequential(
[
layers.RandomFlip(
'horizontal',
input_shape=(img_height,img_width, 3)),
layers.RandomRotation(0.1),
layers.RandomZoom(0.1),
]
)
# # below is the less accurate model architecture(without dropout or data-augmentation)
# # that leads to overfitting (acc. to example)
# model = Sequential([
# layers.Rescaling(1./255, input_shape=(img_height, img_width, 3)),
# layers.Conv2D(16, 3, padding='same', activation='relu'),
# layers.MaxPooling2D(),
# layers.Conv2D(32, 3, padding='same', activation='relu'),
# layers.MaxPooling2D(),
# layers.Conv2D(64, 3, padding='same', activation='relu'),
# layers.MaxPooling2D(),
# layers.Flatten(),
# layers.Dense(128, activation='relu'),
# layers.Dense(num_classes)
# ])
# specify data augmentation by
model = Sequential([
# data_augmentation, # thinking of skipping data augmentation as the training
layers.Rescaling(1./255, input_shape=(img_height, img_width, 3)),
layers.Conv2D(16, 3, padding='same', activation='relu'),
layers.MaxPooling2D(),
layers.Conv2D(32, 3, padding='same', activation='relu'),
layers.MaxPooling2D(),
layers.Conv2D(64, 3, padding='same', activation='relu'),
layers.MaxPooling2D(),
layers.Dropout(0.1),
layers.Flatten(),
layers.Dense(320, activation='relu'),
layers.Dense(num_classes)
])
################ Compile the model ################
model.compile(
optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy']
)
################ Model summary ################
model.summary()
return model
def train_and_save_model(model, train_ds, val_ds, model_h5_path, epochs=10):
# # tensor-board requisites
# log_dir = "./tf-logs/fit/" + datetime.now().strftime("%Y%m%d-%H%M%S")
# tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir=log_dir, histogram_freq=1)
# # tensor-board requisites
# # model saving requisites
# checkpoint_path = "training_1/cp.ckpt"
# checkpoint_dir = os.path.dirname(checkpoint_path)
# # Create a callback that saves the model's weights
# cp_callback = tf.keras.callbacks.ModelCheckpoint(
# filepath=checkpoint_path,
# save_weights_only=True,
# verbose=1
# )
# # model saving requisites
history = model.fit(
train_ds,
validation_data=val_ds,
epochs=epochs,
# callbacks=[
# cp_callback,
# # tensorboard_callback
# ]
)
print('saving model to', model_h5_path)
model.save(model_h5_path)
# TODO save history here too
return model
def evaluate_model(model, validation_ds):
model = tf.keras.models.load_model('./models/dentAndBurr_detector.h5')
loss, acc = model.evaluate(validation_ds, verbose=2)
print('Model - accuracy: {:5.2f}% | loss: {:5.2f}%'.format(100 * acc, 100 * loss))
def predict_using_model(model, test_img_path, class_names):
img = tf.keras.utils.load_img(
test_img_path, target_size=(img_height, img_width)
)
s = time()
img_array = tf.keras.utils.img_to_array(img)
print(type(img_array), img_array.shape)
print('for', test_img_path)
print('pixel[500, 379]', img_array.item(500, 379, 0), img_array.item(500, 379, 1), img_array.item(500, 379, 2))
img_array = tf.expand_dims(img_array, 0) # Create a batch
predictions = model.predict(img_array)
print('predictions:', predictions)
score = tf.nn.softmax(predictions[0])
print('score:', score)
print(
'This image most likely belongs to {} with a {:.2f} percent confidence.'
.format(class_names[np.argmax(score)], 100 * np.max(score))
)
print('time taken for prediction:', time() - s, 's')
if __name__ == '__main__':
read_dataset = True
create_model = False
train_model = False
eval_model = False
reval_model = False
read_model = True
test_model = True
model = None
data_dir = 'D:/CRM/TT_Collar_Inspection_Project/scratch_dent_detection_MLAI/' + \
'datasets_scratch_detection/dataset_20211231/feat_original/ds'
################ CREATING DATA-SET ################
# Define some parameters for the loader
batch_size = 10
img_height = 758
img_width = 1072
if read_dataset:
train_ds, val_ds, class_names = create_and_configure_datasets(img_height, img_width, batch_size)
# class_names = train_ds.class_names
if create_model:
model = create_and_compile_model(img_height, img_width, class_names)
# exit() if input('continue?(y for YES anything else is a NO): ') == 'n' else print()
################ Train the model ################
model_h5_path = 'models/dentAndBurr_detector.h5'
if train_model:
# calling the model training method fit
model = train_and_save_model(model, train_ds, val_ds, model_h5_path, epochs=45)
if eval_model:
print('evaluating model to check its metrics while in memory')
evaluate_model(model, val_ds)
if read_model:
# to check if model read after saving has changed or not
print('revaluating model after reading from file to check its metrics are same as before saving')
model = tf.keras.models.load_model(model_h5_path)
if reval_model:
evaluate_model(read_model, val_ds)
if test_model:
################ Predict on new data ################
# unseen notok
# test_img_path = 'D:/CRM/TT_Collar_Inspection_Project/scratch_dent_detection_MLAI/datasets_scratch_detection/' + \
# 'dataset_20211231/feat_original/test_prediction/20211229181715.515.jpg'
# seen notok
# test_img_path = 'D:/CRM/TT_Collar_Inspection_Project/scratch_dent_detection_MLAI/datasets_scratch_detection/' + \
# 'dataset_20211231/feat_original/ds/notok/20211229183415.689.jpg'
# unseen ok
# test_img_path = 'D:/CRM/TT_Collar_Inspection_Project/scratch_dent_detection_MLAI/datasets_scratch_detection/dataset_20220108/' + \
# '20220103_141858.321589_20220103141858.321/CAM1_20220103141858.321.jpg'
test_img_path = 'D:/CRM/TT_Collar_Inspection_Project/scratch_dent_detection_MLAI/' + \
'datasets_scratch_detection/dataset_20211231/feat_original/tf-test/test_1.jpg' # file_path = filedialog.askopenfilename()
predict_using_model(model, test_img_path, class_names)
output of the above code when used to predict a specific image using the original keras model
predictions: [[0.47940558 0.8012595 ]]
score: tf.Tensor([0.42022398 0.579776 ], shape=(2,), dtype=float32)
This image most likely belongs to ok with a 57.98 percent confidence.
time taken for prediction: 1.344334602355957 s
this is the code for converting the above CNN to tflite model
from sys import argv
import tensorflow as tf
if __name__ == '__main__':
if len(argv) < 3:
print(
'err: path to the model and saving path of converted tf-lite model is required!!\n' +
'Usage: python tf_to_tflite_model_converter.py <path-to-tf-model> <saving-path-of-tflite-model>'
)
exit(0)
tf_model_read_path = argv[1]
tflite_model_save_path = argv[2]
model = tf.keras.models.load_model(tf_model_read_path)
# Convert the model.
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()
# Save the model.
with open(tflite_model_save_path, 'wb') as f:
f.write(tflite_model)
code for interpreting the tflite model for a specific image
import tensorflow as tf
# import tflite_runtime.interpreter as tflite
import tkinter as tk
from tkinter import filedialog
import cv2 as cv
import numpy as np
import time
def load_labels(filename = None):
if filename is None:
return ['notok', 'ok']
my_labels = []
input_file = open(filename, 'r')
for l in input_file:
my_labels.append(l.strip())
return my_labels
# DEF. PARAMETERS
img_row, img_column = 1072, 758
num_channel = 3
num_batch = 1
input_mean = 0.
input_std = 255.
floating_model = False
keras_model_path = "./models/dentAndBurr_detector.h5.tflite"
labels_path = "./models/labels_mobilenet.txt"
interpreter = tf.lite.Interpreter(keras_model_path)
interpreter.allocate_tensors()
# obtaining the input-output shapes and types
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
print(input_details, '\n', output_details)
# file selection window for input selection
# root = tk.Tk()
# root.withdraw()
# file_path = filedialog.askopenfilename()
file_path = 'D:/CRM/TT_Collar_Inspection_Project/scratch_dent_detection_MLAI/' + \
'datasets_scratch_detection/dataset_20211231/feat_original/tf-test/test_1.jpg'
# input_img = Image.open(file_path) # input_img = Image.open(file_path)
input_img = cv.imread(file_path)
input_img = cv.cvtColor(input_img, cv.COLOR_BGR2RGB)
print(type(input_img), input_img.shape)
print('pixel[500, 379]', input_img.item(500, 379, 0), input_img.item(500, 379, 1), input_img.item(500, 379, 2))
# input_img = input_img.resize((img_row, img_column))
input_img = np.expand_dims(input_img, axis=0)
input_img = (np.float32(input_img) - input_mean) / input_std
interpreter.set_tensor(input_details[0]['index'], input_img)
# running inference
interpreter.invoke()
output_data = interpreter.get_tensor(output_details[0]['index'])
print('for', file_path)
print(output_data)
results = np.squeeze(output_data)
print(results)
top_k = results.argsort()[-5:][::-1]
print(results.argsort())
print(results.argsort()[-5:])
print(top_k)
labels = load_labels() # load_labels(labels_path)
for i in top_k:
print('{0:08.6f}'.format(float(results[i] / 255.0)) + ":", labels[i])
output of tflite interpretation of the same image
for D:/CRM/TT_Collar_Inspection_Project/scratch_dent_detection_MLAI/datasets_scratch_detection/dataset_20211231/feat_original/tf-test/test_1.jpg
[[0.88043416 0.18769042]]
[0.88043416 0.18769042]
[1 0]
[1 0]
[0 1]
0.003453: notok
0.000736: ok
for interpreting tflite I referred the code in the below stackoverflow question and also applied the change suggested in accepted answer: .tflite model (converted from keras .h5 model) always predicts the same class with same probability
As you can see here the the floating point values of the output layer tensors are lot different, and so much so that it is even giving me the wrong answer.
One of my oberservation was that tf.keras.utils.img_to_array reads the image in RGB format so I tried converting the img from BGR to RGB using opencv but that did not work.
I also tried using freeze-graph way to create freeze-graph of keras and then convert it to tflite but could not find the binary of freeze_graph or how to run it using the python file in tensorflow/python/tools folder in venv.
This is the answer that suggests the use of freeze-graph https://stackoverflow.com/a/51881268
Please let me know what should be done here, is there something that I am missing or have not understood things properly.
Tensorflow version: 2.7.0
FYI, I have only scratched the surface of Machine Learning & Deep Learning so do not have all the required knowledge.
Thanks in Advance!!