keras image classifier model configuration - python

I am trying to learn image classification using Keras and TensorFlow.
I am referring to https://www.kaggle.com/c/dogs-vs-cats problem.
My Code is as below:
#!/usr/bin/env python
# coding: utf-8
# # Cats and Dogs Identification Kaggle
#
# https://blog.keras.io/building-powerful-image-classification-models-using-very-little-data.html
# ## Tensorflow and GPU Memory Config
# In[14]:
import tensorflow as tf
from tensorflow.keras.models import Sequential
from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
from tensorflow.keras.layers import Dense, Flatten, Activation, Conv2D, MaxPooling2D, Reshape, BatchNormalization
from keras import backend as K
# tf.reset_default_graph()
# tf.set_random_seed(42)
# tf.config.set_per_process_memory_growth(True)
# tf.config.gpu.set_per_process_memory_fraction(0.4)
tf.debugging.set_log_device_placement(True)
print("Num GPUs Available: ", len(tf.config.experimental.list_physical_devices('GPU')))
tf.config.experimental.list_physical_devices('GPU')
gpus = tf.config.experimental.list_physical_devices('GPU')
if gpus:
# Restrict TensorFlow to only allocate 1GB of memory on the first GPU
try:
tf.config.experimental.set_virtual_device_configuration(
gpus[0],
[tf.config.experimental.VirtualDeviceConfiguration(memory_limit=2048)])
logical_gpus = tf.config.experimental.list_logical_devices('GPU')
print(len(gpus), "Physical GPUs,", len(logical_gpus), "Logical GPUs")
except RuntimeError as e:
# Virtual devices must be set before GPUs have been initialized
print(e)
print('Keras image_data_format {}'.format(K.image_data_format()))
print('if keras image_data_format is channel_last means data should be loaded in 32X32X3 dimensions')
# In[2]:
import os
import shutil
from shutil import copyfile
from random import seed
from random import random
def createPathIfNoExists(path):
if not os.path.exists(path):
os.mkdir(path)
cats_dir = '/cats'
dogs_dir = '/dogs'
source_images = '/home/user/Desktop/dogs_cat_keras'
data_dir = source_images + '/data'
train_dir = data_dir + '/train'
train_cats_dir = train_dir + cats_dir
train_dogs_dir = train_dir + dogs_dir
validation_dir = data_dir + '/validation'
validation_cats_dir = validation_dir + cats_dir
validation_dogs_dir = validation_dir + dogs_dir
createPathIfNoExists(data_dir)
createPathIfNoExists(train_dir)
createPathIfNoExists(train_cats_dir)
createPathIfNoExists(train_dogs_dir)
createPathIfNoExists(validation_dir)
createPathIfNoExists(validation_cats_dir)
createPathIfNoExists(validation_dogs_dir)
print('Source directory {}'.format(source_images))
print('Data directory {}'.format(data_dir))
print('train directory {}'.format(train_dir))
print('train cats directory {}'.format(train_cats_dir))
print('train dogs directory {}'.format(train_dogs_dir))
print('validation directory {}'.format(validation_dir))
print('validation directory {}'.format(validation_cats_dir))
print('validation directory {}'.format(validation_dogs_dir))
# inputFiles = source_images + '/train'
# cats = [inputFiles+'/' + d for d in os.listdir(inputFiles) if d.startswith('cat')]
# print('All cats count {}'.format(len(cats)))
# dogs = [inputFiles+'/' + d for d in os.listdir(inputFiles) if d.startswith('dog')]
# print('All dogs count {}'.format(len(dogs)))
# data_split_70 = 8400
# train_cats = cats[:data_split_70]
# validation_cats = cats[data_split_70:]
# train_dogs = dogs[:data_split_70]
# validation_dogs = dogs[data_split_70:]
# print('Total train cats {} and validation cats {}'.format(len(train_cats), len(validation_cats)))
# print('Total train dogs {} and validation dogs {}'.format(len(train_dogs), len(validation_dogs)))
# # Put train cats data in train directory
# for item in train_cats:
# if os.path.isfile(item):
# shutil.copyfile(item, train_cats_dir + '/' + os.path.basename(item))
# # Put validation cats data in validation directory
# for item in validation_cats:
# if os.path.isfile(item):
# shutil.copyfile(item, validation_cats_dir + '/' + os.path.basename(item))
# # Put train cats data in train directory
# for item in train_dogs:
# if os.path.isfile(item):
# shutil.copyfile(item, train_dogs_dir + '/' + os.path.basename(item))
# # Put validation cats data in validation directory
# for item in validation_dogs:
# if os.path.isfile(item):
# shutil.copyfile(item, validation_dogs_dir + '/' + os.path.basename(item))
# ## General imports
# In[15]:
import datetime
import numpy as np
from sklearn.pipeline import Pipeline
from sklearn.model_selection import KFold, GridSearchCV, RandomizedSearchCV
from sklearn.preprocessing import MinMaxScaler
get_ipython().run_line_magic('matplotlib', 'inline')
import matplotlib.pyplot as plt
from sklearn.neighbors import KNeighborsClassifier
import h5py
# ## Data Import
# In[16]:
dataGenerator = ImageDataGenerator(rotation_range=40,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
fill_mode='nearest')
# ### Image Display
# In[17]:
img = load_img('/home/user/Desktop/dogs_cat_keras/data/train/cats/cat.12467.jpg') # this is a PIL image
x = img_to_array(img) # this is a Numpy array with shape (width X height X 3)
print(x.shape)
# print(type(img))
# print(img.size)
# print(img.mode)
plt.imshow(x/255)
plt.show()
print('Shape before reshape {}'.format(x.shape))
## Generate data = 0
x = x.reshape((1,) + x.shape) # this is a Numpy array with shape (1, width, height, 3)
print('Shape after reshape {}'.format(x.shape))
# ### Generate augumented data using image generator
# In[6]:
# import os
# augumented_data_path = data_path + '/preview'
# if not os.path.exists(augumented_data_path):
# os.mkdir(augumented_data_path)
# i = 0
# for batch in dataGenerator.flow(x, batch_size=1,
# save_to_dir=augumented_data_path, save_prefix='cat', save_format='jpeg'):
# i += 1
# if i > 20:
# break # otherwise the generator would loop indefinitely
# ### Building Graph
# In[23]:
img_width, img_height = 150, 150
input_shape = (img_width, img_height, 3)
nnModel = Sequential()
nnModel.add(Flatten(input_shape=(input_shape)))
nnModel.add(Dense(32, activation='relu'))
nnModel.add(Dense(16, activation='relu'))
nnModel.add(Dense(2, activation='softmax'))
# ### Compile function
# In[26]:
nnModel.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy'])
# ### Train Generator
# In[20]:
batch_size = 32
train_data_gen = ImageDataGenerator(rescale = 1/255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)
train_generator = train_data_gen.flow_from_directory(
train_dir, # this is the target directory
target_size=(img_width, img_height), # all images will be resized to 150x150
batch_size=batch_size,
class_mode='binary')
print('Train path {}'.format(train_dir))
test_data_gen = ImageDataGenerator(rescale = 1/255)
print('Test path {}'.format(validation_dir))
validation_generator = test_data_gen.flow_from_directory(
validation_dir, # this is the target directory
target_size=(img_width, img_height), # all images will be resized to 150x150
batch_size=batch_size,
class_mode='binary')
# In[24]:
nnModel.summary()
# ### Train
# In[27]:
nnModel.fit_generator(train_generator,
steps_per_epoch=2048,
epochs=64,
validation_data=validation_generator,
validation_steps=1024)
I am getting following exception
<ipython-input-27-59bf3c75cdc4> in <module>
3 epochs=64,
4 validation_data=validation_generator,
----> 5 validation_steps=1024)...
A target array with shape (32, 1) was passed for an output of shape (None, 2) while using as loss `binary_crossentropy`. This loss expects targets to have the same shape as the output.
Problem statement has lot of notebook solution, but here i don't want to use cnn for learning purpose. Please help me solve this.

Your problem in your code comes from this line:
nnModel.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy'])
You have to change your loss function to categorical_crossentropy, not binary_crossentropy. Practically, in your code, you have to write nnModel.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])
If you have a binary classification problem, you can opt for one of these two variants:
Dense(1,activation='sigmoid') + loss = 'binary_crossentropy'
Dense(2,activation='softmax') + loss = 'categorical_crossentropy'
The only small change you may need to also make is for ImageDataGenerator, with the class_mode(set it to 'binary' in the first case and to 'categorical' in the second case)

Related

Image Classification Fine-Tuning With EfficientNet

I'm trying to image classify with Keras Efficient Net. like here https://keras.io/examples/vision/image_classification_efficientnet_fine_tuning/ I have two folder named 1 and 0. The number of images in both folders is the same. F1 score is generated only for 0 class in sklearn report results. f1-score for class 1 is always 0. classifies everything as 0. How can I fix ? I would love any help.
both folders contain 62 images.
UPDATE:
I also tried with 500 images for each class and for 1 folder the results are still 0.
UPDATE
#----TF
import tensorflow as tf
from keras.utils import to_categorical
from tensorflow.python.data import AUTOTUNE
from keras.models import load_model
from keras import layers
from keras.applications import EfficientNetB0
from keras.callbacks import EarlyStopping, ModelCheckpoint
from keras.optimizers import SGD
from keras.applications import imagenet_utils
#---SK
from sklearn.metrics import classification_report
#
import imutils
from imutils import paths
import random
import cv2
import os
import numpy as np
import pandas as pd
import shutil
import matplotlib.pyplot as plt
main_path = 'C:\\Users\\A\\Desktop\\data\\'
all_images = list(paths.list_images(main_path))
#Bazi resimleri kontrol etme
random_images = random.choices(all_images, k=3)
for i in random_images:
random_image = cv2.imread(i)
random_images = cv2.cvtColor(random_image, cv2.COLOR_BGR2RGB)
random_image = imutils.resize(random_image, height=400)
cv2.imshow("example", random_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
random.shuffle(all_images)
i = int(len(all_images)*0.8) #%80
trainData = all_images[:i]
testData = all_images[i:]
#validation data
i = int(len(trainData)*0.10) #%10
validData = trainData[:i]
trainData = trainData[i:]
train_path = main_path+'training'
test_path = main_path+'test'
valid_path = main_path+'valid'
datasets = [("training", trainData, train_path ), ("validation", validData, valid_path), ("testing", testData, test_path)]
for (dtype, imagepaths, out_path) in datasets:
if not os.path.exists(out_path):
os.makedirs(out_path)
for inputpath in imagepaths:
filename = inputpath.split(os.path.sep)[-1]
label = inputpath.split(os.path.sep)[-2]
labelPath = os.path.sep.join([out_path, label])
if not os.path.exists(labelPath):
os.makedirs(labelPath) # onceden tanimlanan klasor
#goruntu alinir ve label klasorune kopyalanir
p = os.path.sep.join([labelPath, filename])
shutil.copy2(inputpath, p)
#3- tf.data() pipeline dagitma
def load_images(imagePath):
# --images
image = tf.io.read_file(imagePath)
image = tf.io.decode_jpeg(image, channels=3)
image = tf.image.resize(image, (150, 150)) / 255.0
# --labels
label = tf.strings.split(imagePath, os.path.sep)[-2]
label = tf.strings.to_number(label, tf.int32)
print(label)
return (image, label)
def augment(image, label):
image = tf.image.random_flip_up_down(image)
image = tf.image.random_flip_left_right(image)
image = tf.image.random_brightness(image,0.2) # data augmentation
return (image, label)
trainPaths = list(paths.list_images(train_path))
valPaths = list(paths.list_images(valid_path))
testPaths = list(paths.list_images(test_path))
trainDS = tf.data.Dataset.from_tensor_slices(trainPaths)
trainDS = (trainDS.shuffle(len(trainPaths)).map(load_images, num_parallel_calls=AUTOTUNE).map(augment,num_parallel_calls=AUTOTUNE).cache().batch(16).prefetch(AUTOTUNE))
# --validation
valDS = tf.data.Dataset.from_tensor_slices(valPaths)
valDS = (valDS.map(load_images, num_parallel_calls=AUTOTUNE).cache().batch(16).prefetch(AUTOTUNE))
# --Test
testDS = tf.data.Dataset.from_tensor_slices(testPaths)
testDS = (testDS.map(load_images, num_parallel_calls=AUTOTUNE).cache().batch(16).prefetch(AUTOTUNE))
NUM_CLASSES = 2
IMG_SIZE = 150
size = (IMG_SIZE, IMG_SIZE,3)
inputs = layers.Input(shape=(IMG_SIZE, IMG_SIZE, 3))
model = EfficientNetB0(include_top=False, input_shape=size)
flat1 = layers.Flatten()(model.layers[-1].output)
class1 = layers.Dense(1024, activation='relu')(flat1)
output = layers.Dense(1, activation='sigmoid')(class1)
model = tf.keras.Model(inputs=model.inputs, outputs=output)
model.summary()
# model = EfficientNetB0(include_top=False, input_shape=size)
# for layer in model.layers:
# layer.trainable = False
opt = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"])
early_s = EarlyStopping(monitor="val_loss", patience = 10, restore_best_weights=True)
save_b = ModelCheckpoint(filepath ="C:\\Users\\A\\Desktop\\Gun\\", monitor="val_loss", verbose = 1 )
callbacks = [early_s, save_b]
hist = model.fit(x = trainDS, validation_data=valDS, epochs= 50, callbacks=callbacks, verbose=1)
plt.figure()
plt.plot(hist.history["loss"], label="train_loss")
plt.plot(hist.history["val_loss"], label="val_loss")
plt.plot(hist.history["accuracy"], label="train_acc")
plt.plot(hist.history["val_accuracy"], label="val_acc")
plt.title("training loss and accuracy")
plt.xlabel("Epoch #")
plt.ylabel("loss/accuracy")
plt.legend(loc="lower left")
plt.show()
#test setinin etiketleri
test_paths = list(paths.list_images(test_path))
testlabels = [int(p.split(os.path.sep)[-2]) for p in test_paths]
testlabels = to_categorical(testlabels)
predictions = model.predict(testDS)
print(classification_report(testlabels.argmax(axis=1), predictions.argmax(axis=1), target_names=["0", "1"]))
UPDATE REPORT

How to test TensorFlow model for single input file?

I am new in TensorFlow. Code above is a python code, which trains NN for spoken language identification task.
How can I add ability to predict a single audio file and give predictes class(label) name, and also how can I save classes(labels) in model before output?
from glob import glob
import os
import yaml
import math
import matplotlib.pyplot as plt
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.layers import Input, Concatenate
from tensorflow.keras.optimizers import RMSprop
from tensorflow.keras.applications.inception_v3 import InceptionV3
from tensorflow.keras.callbacks import EarlyStopping, LearningRateScheduler
def step_decay(epoch, lr):
drop = 0.94
epochs_drop = 2.0
lrate = lr * math.pow(drop, math.floor((1+epoch)/epochs_drop))
return lrate
main_dir = os.getcwd()
#Load configurations for startup
config_file_path = os.path.join(main_dir, "config.yaml")
config_file = open(config_file_path, 'r+')
main_config = yaml.safe_load(config_file)
languages = main_config["Languages"]
dataset_root_path = main_config["Target Root Path"]
batch_size = main_config["Batch Size"]
image_width = main_config["Image Width"]
image_height = main_config["Image Height"]
validation_split = main_config["Validation Split"]
initial_learning_rate = main_config["Initial Learning Rate"]
config_file.close()
categories = ["train", "test"]
train_path = dataset_root_path + categories[0]
num_classes = len(languages)
model_file = dataset_root_path + 'model.h5'
all_files = glob(train_path + '/*/*.png')
num_validation = len(all_files) * validation_split
num_train = len(all_files) - num_validation
validation_steps = int(num_validation / batch_size)
steps_per_epoch = int(num_train / batch_size)
print('Steps per Epoch: ' + str(steps_per_epoch))
print('Validation steps: ' + str(validation_steps))
image_data_generator = ImageDataGenerator(rescale=1./255, validation_split=validation_split)
train_generator = image_data_generator.flow_from_directory(train_path, batch_size=batch_size, class_mode='categorical', target_size=(image_height, image_width), color_mode='grayscale', subset='training')
validation_generator = image_data_generator.flow_from_directory(train_path, batch_size=batch_size, class_mode='categorical', target_size=(image_height, image_width), color_mode='grayscale', subset='validation')
#Model definition
img_input = Input(shape=(image_height, image_width, 1))
img_conc = Concatenate(axis=3, name='input_concat')([img_input, img_input, img_input])
model = InceptionV3(input_tensor=img_conc, weights=None, include_top=True, classes=2)
model.summary()
model.compile(optimizer=RMSprop(learning_rate=initial_learning_rate, clipvalue=2.0), loss='categorical_crossentropy', metrics=['accuracy'])
early_stopping = EarlyStopping(monitor='val_accuracy', mode='max', patience=10, restore_best_weights=True)
learning_rate_decay = LearningRateScheduler(step_decay, verbose=1)
history = model.fit(train_generator, validation_data=validation_generator, epochs=60, steps_per_epoch=steps_per_epoch, validation_steps=validation_steps, callbacks=[early_stopping, learning_rate_decay])
model.save(model_file)
Config file (yaml) for project looks like this:
Project: Language Identification
Languages:
hy: Armenian
as: Assamese
Target Root Path: /home/nn/Desktop/Language_Identification/Data/
Batch Size: 1
Image Width: 500
Image Height: 128
Validation Split: 0.1
Initial Learning Rate: 0.045

The output neuron/tensor values after interpretation of a keras-to-tflite converted model are different and mis-leading compared to the original one

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!!

How to perform super pixel image segmentation and feature extraction

I am interested in multi class segmentation of skin tissues, I have 3000 skin tissue labels classified into 4 classes, I have created a CNN classification algorithm to train my classification model. I would like to use the classification model for segmentation task of new skin tissue image and perform feature extraction of the skin tissue belonging to each of the class
Following is the code that is written to train my classification model
from tensorflow.keras.layers import Input, Concatenate, Dropout, Flatten, Dense, GlobalAveragePooling2D, Conv2D
from tensorflow.keras import backend as K
#from tensorflow.keras.utils import np_utils
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras import optimizers
from tensorflow.keras.metrics import top_k_categorical_accuracy
from tensorflow.keras.models import Sequential, Model, load_model
import tensorflow as tf
from tensorflow.keras.initializers import he_uniform
from tensorflow.keras.callbacks import ModelCheckpoint, LearningRateScheduler, TensorBoard, EarlyStopping, CSVLogger, ReduceLROnPlateau
#from tensorflow.compat.keras.backend import KTF
#import keras.backend.tensorflow_backend as KTF
from tensorflow.keras.applications.resnet50 import ResNet50
from tensorflow.keras.applications.inception_v3 import InceptionV3
import os
import matplotlib.pylab as plt
import numpy as np
import pandas as pd
#import numpy as np, Pillow, skimage, imageio, matplotlib
#from scipy.misc import imresize
from skimage.transform import resize
from tqdm import tqdm
from tensorflow.keras import metrics
#### PREPROCESS STAGE ####
# Path to superpixels class files
classes_file = "/home/DEV/SKIN_3000_CLASSES.csv"
concatenated_data= pd.read_csv(classes_file, header=None)
# Instances with targets
targets = concatenated_data[1].tolist()
# Split data according to their classes
class_0 = concatenated_data[concatenated_data[1] == 0]
class_1 = concatenated_data[concatenated_data[1] == 1]
class_2 = concatenated_data[concatenated_data[1] == 2]
class_3 = concatenated_data[concatenated_data[1] == 3]
# Holdout split train/test set (Other options are k-folds or leave-one-out)
split_proportion = 0.8
split_size_0 = int(len(class_0)*split_proportion)
split_size_1 = int(len(class_1)*split_proportion)
split_size_2 = int(len(class_2)*split_proportion)
split_size_3 = int(len(class_3)*split_proportion)
new_class_0_train = np.random.choice(len(class_0), split_size_0, replace=False)
new_class_0_train = class_0.iloc[new_class_0_train]
new_class_0_test = ~class_0.iloc[:][0].isin(new_class_0_train.iloc[:][0])
new_class_0_test = class_0[new_class_0_test]
new_class_1_train = np.random.choice(len(class_1), split_size_1, replace=False)
new_class_1_train = class_1.iloc[new_class_1_train]
new_class_1_test = ~class_1.iloc[:][0].isin(new_class_1_train.iloc[:][0])
new_class_1_test = class_1[new_class_1_test]
new_class_2_train = np.random.choice(len(class_2), split_size_2, replace=False)
new_class_2_train = class_2.iloc[new_class_2_train]
new_class_2_test = ~class_2.iloc[:][0].isin(new_class_2_train.iloc[:][0])
new_class_2_test = class_2[new_class_2_test]
new_class_3_train = np.random.choice(len(class_3), split_size_3, replace=False)
new_class_3_train = class_3.iloc[new_class_3_train]
new_class_3_test = ~class_3.iloc[:][0].isin(new_class_3_train.iloc[:][0])
new_class_3_test = class_3[new_class_3_test]
x_train_list = pd.concat(
[new_class_0_train, new_class_1_train, new_class_2_train, new_class_3_train])
x_test_list = pd.concat(
[new_class_0_test, new_class_1_test, new_class_2_test, new_class_3_test])
# Load superpixels files
imagePath = "/home/DEV/SKIN_SET_3000/"
x_train = []
y_train = []
for index, row in tqdm(x_train_list.iterrows(), total=x_train_list.shape[0]):
try:
loadedImage = plt.imread(imagePath + str(row[0]) + ".jpg")
x_train.append(loadedImage)
y_train.append(row[1])
except:
# Try with .png file format if images are not properly loaded
try:
loadedImage = plt.imread(imagePath + str(row[0]) + ".png")
x_train.append(loadedImage)
y_train.append(row[1])
except:
# Print file names whenever it is impossible to load image files
print(imagePath + str(row[0]))
x_test = []
y_test = []
for index, row in tqdm(x_test_list.iterrows(), total=x_test_list.shape[0]):
try:
loadedImage = plt.imread(imagePath + str(row[0]) + ".jpg")
x_test.append(loadedImage)
y_test.append(row[1])
except:
# Try with .png file format if images are not properly loaded
try:
loadedImage = plt.imread(imagePath + str(row[0]) + ".png")
x_test.append(loadedImage)
y_test.append(row[1])
except:
# Print file names whenever it is impossible to load image files
print(imagePath + str(row[0]))
# Reescaling of images
img_width, img_height = 139, 139
index = 0
for image in tqdm(x_train):
#aux = resize(image, (img_width, img_height, 3), "bilinear")
aux = resize(image, (img_width, img_height))
x_train[index] = aux / 255.0 # Normalization
index += 1
index = 0
for image in tqdm(x_test):
#aux = resize(image, (img_width, img_height, 3), "bilinear")
aux = resize(image, (img_width, img_height))
x_test[index] = aux / 255.0 # Normalization
index += 1
#### TRAINING STAGE ####
os.environ["KERAS_BACKEND"] = "tensorflow"
RANDOM_STATE = 42
def get_session(gpu_fraction=0.8):
num_threads = os.environ.get('OMP_NUM_THREADS')
gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=gpu_fraction)
if num_threads:
return tf.Session(config=tf.ConfigProto(
gpu_options=gpu_options, intra_op_parallelism_threads=num_threads))
else:
return tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))
#KTF.set_session(get_session())
def precision(y_true, y_pred):
true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
precision = true_positives / (predicted_positives + K.epsilon())
return precision
def recall(y_true, y_pred):
true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))
recall = true_positives / (possible_positives + K.epsilon())
return recall
def fbeta_score(y_true, y_pred, beta=1):
if beta < 0:
raise ValueError('The lowest choosable beta is zero (only precision).')
# Set F-score as 0 if there are no true positives (sklearn-like).
if K.sum(K.round(K.clip(y_true, 0, 1))) == 0:
return 0.0
p = precision(y_true, y_pred)
r = recall(y_true, y_pred)
bb = beta ** 2
fbeta_score = (1 + bb) * (p * r) / (bb * p + r + K.epsilon())
return fbeta_score
nb_classes = 4
final_model = []
# Option = InceptionV3
model = InceptionV3(weights="imagenet", include_top=False,
input_shape=(img_width, img_height, 3))
# Option = ResNet
# model = ResNet50(weights="imagenet", include_top=False, input_shape=(3,img_width, img_height))
# Creating new outputs for the model
x = model.output
x = Flatten()(x)
x = Dense(512, activation="relu")(x)
x = Dropout(0.5)(x)
x = Dense(512, activation="relu")(x)
x = Dropout(0.5)(x)
predictions = Dense(nb_classes, activation='softmax')(x)
#predictions = Dense(nb_classes, activation='sigmoid')(x)
final_model = Model(inputs=model.input, outputs=predictions)
# Metrics
learningRate = 0.001
optimizer = optimizers.SGD(learning_rate=learningRate, momentum=0.88, nesterov=True)
# Compiling the model...
final_model.compile(loss="categorical_crossentropy", optimizer=optimizer,
metrics=["accuracy", fbeta_score])
final_model.summary()
#final_model.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
#model.compile(loss = 'sparse_categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
#x_train = np.array(x_train)
#x_test = np.array(x_test)
x_train = np.asarray(x_train).astype(np.float32)
#x_test = np.array(x_test)
x_test = np.asarray(x_test).astype(np.float32)
# Defining targets...
y_train = np.concatenate([np.full((new_class_0_train.shape[0]), 0), np.full((new_class_1_train.shape[0]), 1),
np.full((new_class_2_train.shape[0]), 2), np.full((new_class_3_train.shape[0]), 3)])
y_test = np.concatenate([np.full((new_class_0_test.shape[0]), 0), np.full((new_class_1_test.shape[0]), 1),
np.full((new_class_2_test.shape[0]), 2), np.full((new_class_3_test.shape[0]), 3)])
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)
modelFilename = "/home/DEV/SKIN_SET_3000/model_inception.h5"
trainingFilename = "/home/DEV/SKIN_SET_3000/training.csv"
nb_train_samples = y_train.shape[0]
nb_test_samples = y_test.shape[0]
#epochs = 10000
epochs = 100
batch_size = 24
trainingPatience = 200
decayPatience = trainingPatience / 4
# Setting the data generator...
train_datagen = ImageDataGenerator(
horizontal_flip=True,
fill_mode="reflect",
zoom_range=0.2
)
train_generator = train_datagen.flow(x_train, y_train, batch_size=batch_size)
# Saving the model
checkpoint = ModelCheckpoint(modelFilename,
monitor='val_accuracy',
verbose=1,
save_best_only=True,
save_weights_only=False,
mode='auto',
save_freq=1)
adaptativeLearningRate = ReduceLROnPlateau(monitor='val_accuracy',
factor=0.5,
patience=decayPatience,
verbose=1,
mode='auto',
min_delta=0.0001,
cooldown=0,
min_lr=1e-8)
early = EarlyStopping(monitor='val_accuracy',
min_delta=0,
patience=trainingPatience,
verbose=1,
mode='auto')
csv_logger = CSVLogger(trainingFilename, separator=",", append=False)
# Callbacks
callbacks = [checkpoint, early, csv_logger, adaptativeLearningRate]
# Training of the model
final_model.fit(train_generator,
steps_per_epoch=nb_train_samples / batch_size,
epochs=epochs,
shuffle=True,
validation_data=(x_test, y_test),
validation_steps=nb_test_samples / batch_size,
callbacks=callbacks)
final_model.save('/home/DEV/SKIN_SET_3000/model_inception.h5')
#compile metrics
In order to segment my image, first i have transformed my input image to super pixel using SLIC
from skimage.segmentation import slic
from skimage.segmentation import mark_boundaries
from skimage.util import img_as_float
from skimage import io; io.use_plugin('matplotlib')
import cv2 as cv
from skimage.color import label2rgb
img_width, img_height = 139, 139
# load the model we saved
model = load_model('/home/DEV/SKIN_SET_3000/model_inception.h5', compile=False)
# Get test image ready
img = skimage.img_as_float(skimage.io.imread('/home/DEV/SKIN_ULCER.jpg')).astype(np.float32)
plt.imshow(img)
test_image_slic = slic(img, n_segments=500, compactness=10.0)
test_image_slic_out = mark_boundaries(img,test_image_slic)
plt.imshow(test_image_slic_out)
#test_image=test_image/255
test_image_array = np.array(test_image_slic_out)
test_image_resize = cv2.resize(test_image_array,(img_width,img_height))
test_image_reshape = test_image_resize.reshape(1,img_width, img_height,3)
I would like to check if each superpixel of my input is labeled as one of my target class among 4 tissue classes, and extract the features belonging to each class as a mask and quantify the total surface area of mask .
any suggestions of how to implement this approach would be appreciated.

computation cannot be performed with standard 32-bit LAPACK

I am training my dataset using VGG as below. It worked well without zca whitening, but after adding zca, it cause an error called
"computation cannot be performed with standard 32-bit LAPACK"
. As you can see, I tried to train the number of batchsize..etc.. to 1, and even just train with 6 images, but it still din not work. What should I do?
Here is my code.
import os
from keras.applications.vgg16 import VGG16
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential, Model
from keras.layers import Input, Activation, Dropout, Flatten, Dense
from keras.preprocessing.image import ImageDataGenerator
from keras import optimizers
import numpy as np
import time
from PIL import Image
import csv
import shutil
# 分類するクラス
classes = ['sugi', 'hinoki']
nb_classes = len(classes)
img_width, img_height = 256, 256
# トレーニング用とバリデーション用の画像格納先
train_data_dir = 'dataset/train1'
#validation_data_dir = 'dataset/validation'
# 今回はトレーニング用に200枚、バリデーション用に50枚の画像を用意した。
nb_train_samples = 1998
#nb_validation_samples = 50
batch_size = 32
nb_epoch = 10
gen_tr_batches = 4
folder = './output'
result_dir = 'results'
if not os.path.exists(result_dir):
os.mkdir(result_dir)
train_imagelist = os.listdir(train_data_dir)
def vgg_model_maker():
""" VGG16のモデルをFC層以外使用。FC層のみ作成して結合して用意する """
# VGG16のロード。FC層は不要なので include_top=False
input_tensor = Input(shape=(img_width, img_height, 3))
vgg16 = VGG16(include_top=False, weights='imagenet', input_tensor=input_tensor)
# FC層の作成
top_model = Sequential()
top_model.add(Flatten(input_shape=vgg16.output_shape[1:]))
top_model.add(Dense(256, activation='relu'))
top_model.add(Dropout(0.5))
top_model.add(Dense(nb_classes, activation='softmax'))
# VGG16とFC層を結合してモデルを作成
model = Model(input=vgg16.input, output=top_model(vgg16.output))
return model
def image_generator():
""" ディレクトリ内の画像を読み込んでトレーニングデータとバリデーションデータの作成 """
gen_train = (ImageDataGenerator(rescale=1.0 / 255.).flow_from_directory(train_data_dir,
target_size=(img_width, img_height),
#color_mode='rgb',
batch_size=batch_size,
shuffle=True))
gen_tr_x = np.vstack(next(gen_train)[0] for _ in range(gen_tr_batches))
#train_datagen = ImageDataGenerator(
# rescale=1.0 / 255,
# zoom_range=0.2,
# horizontal_flip=True,
# zca_whitening = True)
g = ImageDataGenerator(rescale=1.0 / 255.,
zca_whitening=True)
g.fit(gen_tr_x)
#validation_datagen = ImageDataGenerator(rescale=1.0 / 255)
train_generator = g.flow_from_directory(
train_data_dir,
classes=classes,
class_mode='categorical')
#validation_generator = validation_datagen.flow_from_directory(
# validation_data_dir,
#target_size=(img_width, img_height),
#color_mode='rgb',
#classes=classes,
#class_mode='categorical',
#batch_size=batch_size,
#shuffle=True)
return (train_generator)
# Generator for the network's training generator.
# Actual generator for the network's training.
if __name__ == '__main__':
start = time.time()
for the_file in os.listdir(folder):
file_path = os.path.join(folder, the_file)
try:
if os.path.isfile(file_path):
os.unlink(file_path)
#elif os.path.isdir(file_path): shutil.rmtree(file_path)
except Exception as e:
print(e)
# モデル作成
vgg_model = vgg_model_maker()
# 最後のconv層の直前までの層をfreeze
for layer in vgg_model.layers[:15]:
layer.trainable = False
# 多クラス分類を指定
vgg_model.compile(loss='categorical_crossentropy',
optimizer=optimizers.SGD(lr=1e-3, momentum=0.9),
metrics=['accuracy'])
# 画像のジェネレータ生成
train_generator = image_generator()
# Fine-tuning
history_callback = vgg_model.fit_generator(
train_generator,
samples_per_epoch=nb_train_samples,
nb_epoch=nb_epoch)
#validation_data=validation_generator,
#nb_val_samples=nb_validation_samples)
loss_history = history_callback.history["loss"]
accuracy_history = history_callback.history["acc"]
numpy_loss_history = np.array(loss_history)
numpy_accuracy_history = np.array(accuracy_history)
f = open("result.csv","w")
writer = csv.writer(f)
writer.writerow(["loss","accuracy"])
for j in range(len(numpy_loss_history)):
writer.writerow([numpy_loss_history[j],numpy_accuracy_history[j]])
vgg_model.save_weights(os.path.join(result_dir, 'finetuning.h5'))
process_time = (time.time() - start) / 60
print(u'学習終了。かかった時間は', process_time, u'分です。')
My guess is, it's not a tensorflow issue, but rather a numpy issue, and I guess that because here's what's happening when you try to set the zca parameter to True:
if self.zca_whitening:
if self.principal_components is not None:
flatx = np.reshape(x, (-1, np.prod(x.shape[-3:])))
whitex = np.dot(flatx, self.principal_components)
x = np.reshape(whitex, x.shape)
By default, when you install numpy, it tries to find a low level linear algebra library installed on your system, and use that. LAPACK is one of them.
numpy will use its own code if no library is available. So try installing your numpy without any of those libraries as suggested in the docs:
BLAS=None LAPACK=None ATLAS=None python setup.py build
If that still uses the libraries, try the solution given here.
Then, if the above workaround solves your problem, try compiling a 64bit LAPACK and compile your numpy against it.
This answers the more general problem observed when using lapack function such as svd on very large matrices.
Instead of:
import numpy as np
from scipy.linalg import svd
a = np.ones((30000,30000))
u,s,v = svd(a) # this fails
Use:
u,s,v = svd(a, lapack_driver='gesvd') # this DOES NOT fail
My official ticket with an example is here:
https://github.com/scipy/scipy/issues/10337

Categories

Resources