renaming all files - moving training to be a single file for transfer vs not transfer learning. Made the testing file test all models. Needs to be updated to only update with new models.
@@ -7,9 +7,7 @@ import multiprocessing
|
||||
import json
|
||||
import shutil
|
||||
|
||||
from pathlib import Path
|
||||
from PIL import Image
|
||||
from pprint import pprint
|
||||
from random import randint
|
||||
from threading import Lock
|
||||
|
||||
@@ -1,193 +0,0 @@
|
||||
import keras
|
||||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
import seaborn as sn
|
||||
|
||||
from keras import optimizers
|
||||
from keras.applications import inception_v3, mobilenet_v2, vgg16
|
||||
from keras.applications.inception_v3 import preprocess_input
|
||||
from keras.callbacks import ModelCheckpoint, EarlyStopping, TensorBoard
|
||||
from keras.layers import Dense, Dropout, GlobalAveragePooling2D
|
||||
from keras.models import Sequential
|
||||
from keras.preprocessing.image import ImageDataGenerator
|
||||
from keras.utils import multi_gpu_model
|
||||
|
||||
from sklearn.metrics import accuracy_score, confusion_matrix, classification_report
|
||||
|
||||
from time import time
|
||||
from PIL import ImageFile
|
||||
|
||||
# First we some globals that we want to use for this entire process
|
||||
|
||||
ImageFile.LOAD_TRUNCATED_IMAGES = True
|
||||
|
||||
input_shape = (224, 224, 3)
|
||||
batch_size = 32
|
||||
|
||||
model_name = "mobilenet-fixed-data"
|
||||
|
||||
# Next we set up the Image Data Generators to feed into the training cycles.
|
||||
# We need one for training, validation, and testing
|
||||
train_idg = ImageDataGenerator(
|
||||
horizontal_flip=True,
|
||||
rotation_range=30,
|
||||
width_shift_range=[-.1, .1],
|
||||
height_shift_range=[-.1, .1],
|
||||
preprocessing_function=preprocess_input
|
||||
)
|
||||
|
||||
train_gen = train_idg.flow_from_directory(
|
||||
'./data/train',
|
||||
target_size=(input_shape[0], input_shape[1]),
|
||||
batch_size=batch_size
|
||||
)
|
||||
|
||||
print(len(train_gen.classes))
|
||||
|
||||
val_idg = ImageDataGenerator(
|
||||
horizontal_flip=True,
|
||||
rotation_range=30,
|
||||
width_shift_range=[-.1, .1],
|
||||
height_shift_range=[-.1, .1],
|
||||
preprocessing_function=preprocess_input
|
||||
)
|
||||
|
||||
val_gen = val_idg.flow_from_directory(
|
||||
'./data/test',
|
||||
target_size=(input_shape[0], input_shape[1]),
|
||||
batch_size=batch_size
|
||||
)
|
||||
|
||||
test_idg = ImageDataGenerator(
|
||||
preprocessing_function=preprocess_input,
|
||||
)
|
||||
test_gen = test_idg.flow_from_directory(
|
||||
'./data/test',
|
||||
target_size=(input_shape[0], input_shape[1]),
|
||||
batch_size=batch_size,
|
||||
shuffle=False
|
||||
|
||||
)
|
||||
|
||||
# Now we define the model we are going to use....to use something differnet just comment it out or add it here
|
||||
|
||||
# base_model = vgg16.VGG16(
|
||||
# weights='imagenet',
|
||||
# include_top=False,
|
||||
# input_shape=input_shape
|
||||
# )
|
||||
# base_model = inception_v3.InceptionV3(
|
||||
# weights='imagenet',
|
||||
# include_top=False,
|
||||
# input_shape=input_shape
|
||||
# )
|
||||
|
||||
base_model = mobilenet_v2.MobileNetV2(
|
||||
# weights='imagenet',
|
||||
include_top=False,
|
||||
input_shape=input_shape
|
||||
)
|
||||
|
||||
|
||||
# Create a new top for that model
|
||||
add_model = Sequential()
|
||||
add_model.add(base_model)
|
||||
add_model.add(GlobalAveragePooling2D())
|
||||
# add_model.add(Dense(4048, activation='relu'))
|
||||
# add_model.add(Dropout(0.5))
|
||||
|
||||
add_model.add(Dense(2024, activation='relu'))
|
||||
# Adding some dense layers in order to learn complex functions from the base model
|
||||
add_model.add(Dropout(0.5))
|
||||
add_model.add(Dense(512, activation='relu'))
|
||||
add_model.add(Dense(len(train_gen.class_indices), activation='softmax')) # Decision layer
|
||||
|
||||
#TODO: Add in gpu support
|
||||
model = multi_gpu_model(add_model, 2)
|
||||
# model = add_model
|
||||
|
||||
model.compile(loss='categorical_crossentropy',
|
||||
# optimizer=optimizers.SGD(lr=1e-4, momentum=0.9),
|
||||
optimizer=optimizers.Adam(lr=1e-4),
|
||||
metrics=['accuracy'])
|
||||
model.summary()
|
||||
print(
|
||||
model.output_shape
|
||||
)
|
||||
|
||||
# Now that the model is created we can go ahead and train on it using the image generators we created earlier
|
||||
file_path = model_name + ".hdf5"
|
||||
|
||||
checkpoint = ModelCheckpoint(file_path, monitor='val_acc', verbose=1, save_best_only=True, mode='max')
|
||||
|
||||
early = EarlyStopping(monitor="val_acc", mode="max", patience=15)
|
||||
|
||||
tensorboard = TensorBoard(
|
||||
log_dir="logs/" + model_name + "{}".format(time()), histogram_freq=0, batch_size=batch_size,
|
||||
write_graph=True,
|
||||
write_grads=True,
|
||||
write_images=True,
|
||||
update_freq=batch_size
|
||||
)
|
||||
|
||||
callbacks_list = [checkpoint, early, tensorboard] # early
|
||||
|
||||
history = model.fit_generator(
|
||||
train_gen,
|
||||
validation_data=val_gen,
|
||||
steps_per_epoch=len(train_gen),
|
||||
validation_steps=len(val_gen),
|
||||
epochs=25,
|
||||
shuffle=True,
|
||||
verbose=True,
|
||||
callbacks=callbacks_list
|
||||
)
|
||||
|
||||
|
||||
# Finally we are going to grab predictions from our model, save it, and then run some analysis on the results
|
||||
|
||||
predicts = model.predict_generator(test_gen, verbose=True, workers=1, steps=len(test_gen))
|
||||
|
||||
keras_file = model_name + 'finished.h5'
|
||||
keras.models.save_model(model, keras_file)
|
||||
|
||||
print(predicts)
|
||||
print(type(predicts))
|
||||
print(predicts.shape)
|
||||
# Process the predictions
|
||||
predicts = np.argmax(predicts,
|
||||
axis=1)
|
||||
# test_gen.reset()
|
||||
label_index = {v: k for k, v in train_gen.class_indices.items()}
|
||||
predicts = [label_index[p] for p in predicts]
|
||||
reals = [label_index[p] for p in test_gen.classes]
|
||||
|
||||
# Save the results
|
||||
print(label_index)
|
||||
print(test_gen.classes)
|
||||
print(test_gen.classes.shape)
|
||||
print(type(test_gen.classes))
|
||||
df = pd.DataFrame(columns=['fname', 'prediction', 'true_val'])
|
||||
df['fname'] = [x for x in test_gen.filenames]
|
||||
df['prediction'] = predicts
|
||||
df["true_val"] = reals
|
||||
df.to_csv("sub1_non_transfer.csv", index=False)
|
||||
|
||||
# Processed the saved results
|
||||
|
||||
acc = accuracy_score(reals, predicts)
|
||||
conf_mat = confusion_matrix(reals, predicts)
|
||||
print(classification_report(reals, predicts, [l for l in label_index.values()]))
|
||||
print("Testing accuracy score is ", acc)
|
||||
print("Confusion Matrix", conf_mat)
|
||||
|
||||
df_cm = pd.DataFrame(conf_mat, index=[i for i in list(set(reals))],
|
||||
columns=[i for i in list(set(reals))])
|
||||
plt.figure(figsize=(10, 7))
|
||||
sn.heatmap(df_cm, annot=True)
|
||||
plt.show()
|
||||
|
||||
with open("labels.txt", "w") as f:
|
||||
for label in label_index.values():
|
||||
f.write(label + "\n")
|
||||
@@ -1,123 +0,0 @@
|
||||
from time import time
|
||||
|
||||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
import seaborn as sn
|
||||
from PIL import ImageFile
|
||||
from tensorflow import keras
|
||||
|
||||
from model_builders import ImageClassModelBuilder, ImageClassModels
|
||||
|
||||
ImageFile.LOAD_TRUNCATED_IMAGES = True
|
||||
|
||||
input_shape = (224, 224, 3)
|
||||
|
||||
batch_size = 32
|
||||
model_name = f"mobilenetv2-dense1024-l1l2-25drop-{time()}"
|
||||
|
||||
training_idg = keras.preprocessing.image.ImageDataGenerator(
|
||||
horizontal_flip=True,
|
||||
rotation_range=30,
|
||||
width_shift_range=[-.1, .1],
|
||||
height_shift_range=[-.1, .1],
|
||||
)
|
||||
testing_idg = keras.preprocessing.image.ImageDataGenerator(
|
||||
horizontal_flip=True,
|
||||
)
|
||||
|
||||
|
||||
def get_gen(path, test_set=False):
|
||||
idg = testing_idg if test_set else training_idg
|
||||
return idg.flow_from_directory(
|
||||
path,
|
||||
target_size=(input_shape[0], input_shape[1]),
|
||||
batch_size=batch_size,
|
||||
class_mode='categorical',
|
||||
shuffle=True,
|
||||
color_mode='rgb'
|
||||
)
|
||||
|
||||
|
||||
def train_model(train_gen, val_gen):
|
||||
model = ImageClassModelBuilder(
|
||||
input_shape=input_shape,
|
||||
n_classes=807,
|
||||
optimizer=keras.optimizers.Adam(learning_rate=.0001),
|
||||
pre_trained=True,
|
||||
fine_tune=0,
|
||||
base_model=ImageClassModels.MOBILENET_V2
|
||||
).create_model()
|
||||
# Train the model
|
||||
checkpoint = keras.callbacks.ModelCheckpoint(f"./Models/keras/{model_name}.hdf5", monitor='val_loss', verbose=1,
|
||||
save_best_only=True,
|
||||
mode='min')
|
||||
early = keras.callbacks.EarlyStopping(monitor="loss", mode="min", patience=15)
|
||||
tensorboard = keras.callbacks.TensorBoard(
|
||||
log_dir="logs/" + model_name,
|
||||
histogram_freq=1,
|
||||
write_graph=True,
|
||||
write_images=True,
|
||||
update_freq=1,
|
||||
profile_batch=2,
|
||||
embeddings_freq=1,
|
||||
)
|
||||
callbacks_list = [checkpoint, early, tensorboard]
|
||||
|
||||
history = model.fit(
|
||||
train_gen,
|
||||
validation_data=val_gen,
|
||||
epochs=100,
|
||||
batch_size=batch_size,
|
||||
shuffle=True,
|
||||
verbose=True,
|
||||
workers=12,
|
||||
callbacks=callbacks_list,
|
||||
max_queue_size=1000
|
||||
)
|
||||
print(history)
|
||||
return model
|
||||
|
||||
|
||||
def test_model(model, test_gen):
|
||||
print(len(test_gen.filenames))
|
||||
score = model.evaluate(test_gen, workers=8, steps=len(test_gen))
|
||||
predicts = model.predict(test_gen, verbose=True, workers=8, steps=len(test_gen))
|
||||
print("Loss: ", score[0], "Accuracy: ", score[1])
|
||||
print(score)
|
||||
print(predicts)
|
||||
print(type(predicts))
|
||||
print(predicts.shape)
|
||||
|
||||
# Process the predictions
|
||||
predicts = np.argmax(predicts,
|
||||
axis=1)
|
||||
label_index = {v: k for k, v in test_gen.class_indices.items()}
|
||||
predicts = [label_index[p] for p in predicts]
|
||||
reals = [label_index[p] for p in test_gen.classes]
|
||||
|
||||
# Save the results
|
||||
df = pd.DataFrame(columns=['fname', 'prediction', 'true_val'])
|
||||
df['fname'] = [x for x in test_gen.filenames]
|
||||
df['prediction'] = predicts
|
||||
df["true_val"] = reals
|
||||
df.to_csv("sub1.csv", index=False)
|
||||
# Processed the saved results
|
||||
from sklearn.metrics import accuracy_score, confusion_matrix
|
||||
acc = accuracy_score(reals, predicts)
|
||||
conf_mat = confusion_matrix(reals, predicts)
|
||||
print("Testing accuracy score is ", acc)
|
||||
print("Confusion Matrix", conf_mat)
|
||||
df_cm = pd.DataFrame(conf_mat, index=[i for i in list(set(reals))],
|
||||
columns=[i for i in list(set(reals))])
|
||||
plt.figure(figsize=(10, 7))
|
||||
sn.heatmap(df_cm, annot=True)
|
||||
plt.show()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
train_gen = get_gen('./data/train')
|
||||
val_gen = get_gen('./data/val')
|
||||
test_gen = get_gen('./data/test', test_set=True)
|
||||
model = train_model(train_gen, val_gen)
|
||||
test_model(model, test_gen)
|
||||
@@ -0,0 +1,145 @@
|
||||
from enum import Enum
|
||||
|
||||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
from PIL import ImageFile
|
||||
from tensorflow import keras
|
||||
from sklearn.metrics import accuracy_score, confusion_matrix, classification_report
|
||||
from model_builder import ImageClassModelBuilder, ImageClassModels
|
||||
|
||||
ImageFile.LOAD_TRUNCATED_IMAGES = True
|
||||
|
||||
input_shape = (224, 224, 3)
|
||||
|
||||
batch_size = 32
|
||||
|
||||
training_idg = keras.preprocessing.image.ImageDataGenerator(
|
||||
horizontal_flip=True,
|
||||
rotation_range=30,
|
||||
width_shift_range=[-.1, .1],
|
||||
height_shift_range=[-.1, .1],
|
||||
)
|
||||
val_idg = keras.preprocessing.image.ImageDataGenerator(
|
||||
horizontal_flip=True,
|
||||
)
|
||||
testing_idg = keras.preprocessing.image.ImageDataGenerator(
|
||||
horizontal_flip=True,
|
||||
)
|
||||
|
||||
|
||||
class DatasetType(Enum):
|
||||
TRAIN = 0
|
||||
TEST = 1
|
||||
VAL = 2
|
||||
|
||||
|
||||
def get_gen(path, dataset_type: DatasetType = DatasetType.TRAIN):
|
||||
idg = None
|
||||
if dataset_type is DatasetType.TRAIN:
|
||||
idg = training_idg
|
||||
if dataset_type is DatasetType.TEST:
|
||||
idg = testing_idg
|
||||
if dataset_type is DatasetType.VAL:
|
||||
idg = val_idg
|
||||
|
||||
return idg.flow_from_directory(
|
||||
path,
|
||||
target_size=(input_shape[0], input_shape[1]),
|
||||
batch_size=batch_size,
|
||||
class_mode='categorical',
|
||||
shuffle=True,
|
||||
color_mode='rgb'
|
||||
)
|
||||
|
||||
|
||||
def train_model(model_builder, train_gen, val_gen):
|
||||
model = model_builder.create_model()
|
||||
model_name = "rot-shift-" + model_builder.get_name()
|
||||
print(model)
|
||||
print(f"NOW TRAINING: {model_name}")
|
||||
checkpoint = keras.callbacks.ModelCheckpoint(
|
||||
f"./models/keras/{model_name}.hdf5",
|
||||
monitor='val_loss',
|
||||
verbose=1,
|
||||
save_best_only=True,
|
||||
mode='min'
|
||||
)
|
||||
early = keras.callbacks.EarlyStopping(
|
||||
monitor="val_loss",
|
||||
mode="auto",
|
||||
patience=4,
|
||||
restore_best_weights=True,
|
||||
verbose=1,
|
||||
)
|
||||
tensorboard = keras.callbacks.TensorBoard(
|
||||
log_dir="logs/" + model_name,
|
||||
histogram_freq=1,
|
||||
write_graph=True,
|
||||
write_images=True,
|
||||
update_freq=1,
|
||||
profile_batch=2,
|
||||
embeddings_freq=1,
|
||||
)
|
||||
history = model.fit(
|
||||
train_gen,
|
||||
validation_data=val_gen,
|
||||
epochs=500,
|
||||
batch_size=batch_size,
|
||||
shuffle=True,
|
||||
verbose=True,
|
||||
workers=12,
|
||||
callbacks=[checkpoint, early, tensorboard],
|
||||
max_queue_size=1000
|
||||
)
|
||||
print(history)
|
||||
return model
|
||||
|
||||
|
||||
def test_model(model, test_gen):
|
||||
predictions = model.predict(test_gen, verbose=True, workers=1, steps=len(test_gen))
|
||||
|
||||
print(predictions)
|
||||
print(type(predictions))
|
||||
print(predictions.shape)
|
||||
# Process the predictions
|
||||
predictions = np.argmax(predictions,
|
||||
axis=1)
|
||||
# test_gen.reset()
|
||||
label_index = {v: k for k, v in test_gen.class_indices.items()}
|
||||
predictions = [label_index[p] for p in predictions]
|
||||
reals = [label_index[p] for p in test_gen.classes]
|
||||
|
||||
# Processed the saved results
|
||||
acc = accuracy_score(reals, predictions)
|
||||
conf_mat = confusion_matrix(reals, predictions)
|
||||
print(classification_report(reals, predictions, labels=[l for l in label_index.values()]))
|
||||
print("Testing accuracy score is ", acc)
|
||||
print("Confusion Matrix", conf_mat)
|
||||
|
||||
print("made dataframe")
|
||||
plt.figure(figsize=(10, 7))
|
||||
print("made plot")
|
||||
# sn.heatmap(df_cm, annot=True)
|
||||
print("showing plot")
|
||||
plt.show()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
model_builders = [
|
||||
ImageClassModelBuilder(
|
||||
input_shape=input_shape,
|
||||
n_classes=807,
|
||||
optimizer=keras.optimizers.Adam(learning_rate=.0001),
|
||||
pre_trained=True,
|
||||
fine_tune=True,
|
||||
base_model_type=ImageClassModels.MOBILENET_V2,
|
||||
dense_layer_neurons=1024,
|
||||
dropout_rate=.33,
|
||||
)
|
||||
]
|
||||
for mb in model_builders:
|
||||
train_gen = get_gen('./data/train', dataset_type=DatasetType.TRAIN)
|
||||
val_gen = get_gen('./data/val', dataset_type=DatasetType.VAL)
|
||||
test_gen = get_gen('./data/test', dataset_type=DatasetType.TEST)
|
||||
model = train_model(mb, train_gen, val_gen)
|
||||
test_model(model, test_gen)
|
||||
@@ -1,78 +0,0 @@
|
||||
import pandas as pd
|
||||
import matplotlib.pyplot as plt
|
||||
import seaborn as sn
|
||||
import numpy as np
|
||||
|
||||
from keras.applications.inception_v3 import preprocess_input
|
||||
from keras.preprocessing.image import ImageDataGenerator
|
||||
from keras.models import load_model
|
||||
from sklearn.metrics import accuracy_score, confusion_matrix, classification_report
|
||||
|
||||
|
||||
from PIL import ImageFile
|
||||
|
||||
ImageFile.LOAD_TRUNCATED_IMAGES = True
|
||||
|
||||
model = load_model("./Models/mobilenetv2-stock-all-fixed-v2/mobilenetv2.hdf5")
|
||||
|
||||
input_shape = (224, 224, 3)
|
||||
batch_size = 96
|
||||
|
||||
test_idg = ImageDataGenerator(
|
||||
preprocessing_function=preprocess_input,
|
||||
)
|
||||
|
||||
test_gen = test_idg.flow_from_directory(
|
||||
# './data/test',
|
||||
'./SingleImageTestSet',
|
||||
target_size=(input_shape[0], input_shape[1]),
|
||||
batch_size=batch_size,
|
||||
shuffle=False
|
||||
|
||||
)
|
||||
|
||||
predictions = model.predict_generator(test_gen, verbose=True, workers=1, steps=len(test_gen))
|
||||
|
||||
print(predictions)
|
||||
print(type(predictions))
|
||||
print(predictions.shape)
|
||||
# Process the predictions
|
||||
predictions = np.argmax(predictions,
|
||||
axis=1)
|
||||
# test_gen.reset()
|
||||
label_index = {v: k for k, v in test_gen.class_indices.items()}
|
||||
predictions = [label_index[p] for p in predictions]
|
||||
reals = [label_index[p] for p in test_gen.classes]
|
||||
|
||||
# Save the results
|
||||
print(label_index)
|
||||
print(test_gen.classes)
|
||||
print(test_gen.classes.shape)
|
||||
print(type(test_gen.classes))
|
||||
df = pd.DataFrame(columns=['fname', 'prediction', 'true_val'])
|
||||
df['fname'] = [x for x in test_gen.filenames]
|
||||
df['prediction'] = predictions
|
||||
df["true_val"] = reals
|
||||
df.to_csv("sub1_non_transfer.csv", index=False)
|
||||
|
||||
# Processed the saved results
|
||||
|
||||
acc = accuracy_score(reals, predictions)
|
||||
conf_mat = confusion_matrix(reals, predictions)
|
||||
print(classification_report(reals, predictions, labels=[l for l in label_index.values()]))
|
||||
print("Testing accuracy score is ", acc)
|
||||
print("Confusion Matrix", conf_mat)
|
||||
|
||||
df_cm = pd.DataFrame(conf_mat, index=[i for i in list(set(reals))],
|
||||
columns=[i for i in list(set(reals))])
|
||||
print("made dataframe")
|
||||
plt.figure(figsize=(10, 7))
|
||||
print("made plot")
|
||||
# sn.heatmap(df_cm, annot=True)
|
||||
print("showing plot")
|
||||
plt.show()
|
||||
|
||||
with open("labels.txt", "w") as f:
|
||||
for label in label_index.values():
|
||||
f.write(label + "\n")
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
import pandas as pd
|
||||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
|
||||
from keras.preprocessing.image import ImageDataGenerator
|
||||
from keras.models import load_model
|
||||
from sklearn.metrics import accuracy_score, confusion_matrix, classification_report
|
||||
from glob import glob
|
||||
|
||||
from PIL import ImageFile
|
||||
|
||||
ImageFile.LOAD_TRUNCATED_IMAGES = True
|
||||
|
||||
|
||||
accuracies = []
|
||||
losses = []
|
||||
filenames = []
|
||||
test_idg = ImageDataGenerator(
|
||||
)
|
||||
|
||||
input_shape = (224, 224, 3)
|
||||
batch_size = 32
|
||||
|
||||
test_gen = test_idg.flow_from_directory(
|
||||
# './data/test',
|
||||
'./single_image_test_set',
|
||||
target_size=(input_shape[0], input_shape[1]),
|
||||
batch_size=batch_size,
|
||||
shuffle=False
|
||||
)
|
||||
|
||||
for file in glob("./models/keras/*"):
|
||||
filenames.append(file)
|
||||
print(file)
|
||||
model = load_model(file)
|
||||
|
||||
|
||||
|
||||
predictions = model.predict(test_gen, verbose=True, workers=12, steps=len(test_gen))
|
||||
|
||||
print(predictions)
|
||||
print(type(predictions))
|
||||
print(predictions.shape)
|
||||
# Process the predictions
|
||||
predictions = np.argmax(predictions,
|
||||
axis=1)
|
||||
# test_gen.reset()
|
||||
label_index = {v: k for k, v in test_gen.class_indices.items()}
|
||||
predictions = [label_index[p] for p in predictions]
|
||||
reals = [label_index[p] for p in test_gen.classes]
|
||||
|
||||
# Save the results
|
||||
print(label_index)
|
||||
print(test_gen.classes)
|
||||
print(test_gen.classes.shape)
|
||||
print(type(test_gen.classes))
|
||||
df = pd.DataFrame(columns=['fname', 'prediction', 'true_val'])
|
||||
df['fname'] = [x for x in test_gen.filenames]
|
||||
df['prediction'] = predictions
|
||||
df["true_val"] = reals
|
||||
df.to_csv("sub1_non_transfer.csv", index=False)
|
||||
|
||||
# Processed the saved results
|
||||
|
||||
acc = accuracy_score(reals, predictions)
|
||||
conf_mat = confusion_matrix(reals, predictions)
|
||||
print(classification_report(reals, predictions, labels=[l for l in label_index.values()]))
|
||||
print("Testing accuracy score is ", acc)
|
||||
print("Confusion Matrix", conf_mat)
|
||||
|
||||
accuracies.append(acc)
|
||||
# df_cm = pd.DataFrame(conf_mat, index=[i for i in list(set(reals))],
|
||||
# columns=[i for i in list(set(reals))])
|
||||
# print("made dataframe")
|
||||
# plt.figure(figsize=(10, 7))
|
||||
# print("made plot")
|
||||
# # sn.heatmap(df_cm, annot=True)
|
||||
# print("showing plot")
|
||||
# plt.show()
|
||||
|
||||
|
||||
# with open("labels.txt", "w") as f:
|
||||
# for label in label_index.values():
|
||||
# f.write(label + "\n")
|
||||
|
||||
overall_df = pd.DataFrame(list(zip(filenames, accuracies)),
|
||||
columns =['model', 'acc']).sort_values('acc')
|
||||
|
||||
print(overall_df)
|
||||
overall_df.to_csv("all_model_output.csv")
|
||||
overall_df.plot.bar(x="model", y="acc", rot=0)
|
||||
plt.show()
|
||||
@@ -0,0 +1,98 @@
|
||||
from enum import Enum
|
||||
from time import time
|
||||
from typing import Tuple
|
||||
|
||||
import tensorflow as tf
|
||||
from tensorflow import keras
|
||||
|
||||
from .model_wrapper import ModelWrapper
|
||||
|
||||
|
||||
class ImageClassModels(Enum):
|
||||
INCEPTION_V3 = ModelWrapper(
|
||||
keras.applications.inception_v3.InceptionV3,
|
||||
keras.applications.inception_v3.preprocess_input,
|
||||
"inception_v3"
|
||||
)
|
||||
XCEPTION = ModelWrapper(
|
||||
keras.applications.xception.Xception,
|
||||
keras.applications.xception.preprocess_input,
|
||||
"xception"
|
||||
)
|
||||
MOBILENET_V2 = ModelWrapper(
|
||||
keras.applications.mobilenet_v2.MobileNetV2,
|
||||
keras.applications.mobilenet_v2.preprocess_input,
|
||||
"mobilenet_v2"
|
||||
)
|
||||
|
||||
|
||||
class ImageClassModelBuilder(object):
|
||||
|
||||
def __init__(self,
|
||||
input_shape: Tuple[int, int, int],
|
||||
n_classes: int,
|
||||
optimizer: tf.keras.optimizers.Optimizer = keras.optimizers.Adam(
|
||||
learning_rate=.0001),
|
||||
pre_trained: bool = True,
|
||||
fine_tune: bool = False,
|
||||
base_model_type: ImageClassModels = ImageClassModels.MOBILENET_V2,
|
||||
dense_layer_neurons: int = 1024,
|
||||
dropout_rate: float = .5,
|
||||
l1: float = 1e-4,
|
||||
l2: float = 1e-4):
|
||||
self.input_shape = input_shape
|
||||
self.n_classes = n_classes
|
||||
self.optimizer = optimizer
|
||||
self.pre_trained = pre_trained
|
||||
self.fine_tune = fine_tune
|
||||
self.dense_layer_neurons = dense_layer_neurons
|
||||
self.dropout_rate = dropout_rate
|
||||
self.l1 = l1
|
||||
self.l2 = l2
|
||||
self.set_base_model(base_model_type)
|
||||
|
||||
def set_base_model(self, base_model_type: ImageClassModels):
|
||||
self.base_model_type = base_model_type
|
||||
self.base_model = self.base_model_type.value.model_func(
|
||||
weights='imagenet' if self.pre_trained else None,
|
||||
input_shape=self.input_shape,
|
||||
include_top=False
|
||||
)
|
||||
|
||||
def create_model(self):
|
||||
if not self.fine_tune:
|
||||
self.base_model.trainable = False
|
||||
i = tf.keras.layers.Input([self.input_shape[0], self.input_shape[1], self.input_shape[2]], dtype=tf.float32)
|
||||
x = tf.cast(i, tf.float32)
|
||||
x = self.base_model_type.value.model_preprocessor(x)
|
||||
x = self.base_model(x)
|
||||
x = keras.layers.GlobalAveragePooling2D()(x)
|
||||
x = keras.layers.Dense(self.dense_layer_neurons, activation='relu',
|
||||
kernel_regularizer=keras.regularizers.L1L2(l1=self.l1, l2=self.l2))(x)
|
||||
x = keras.layers.Dropout(self.dropout_rate)(x)
|
||||
output = keras.layers.Dense(self.n_classes, activation='softmax')(x)
|
||||
self.model = keras.Model(inputs=i, outputs=output)
|
||||
self.model.compile(
|
||||
optimizer=self.optimizer,
|
||||
loss=keras.losses.CategoricalCrossentropy(),
|
||||
metrics=['accuracy', 'categorical_crossentropy']
|
||||
)
|
||||
self.model.summary()
|
||||
return self.model
|
||||
|
||||
def get_fine_tuning(self):
|
||||
print("self.model is found")
|
||||
self.base_model.trainable = True
|
||||
self.model.compile(
|
||||
optimizer=self.optimizer,
|
||||
loss=keras.losses.CategoricalCrossentropy(),
|
||||
metrics=['accuracy', 'categorical_crossentropy']
|
||||
)
|
||||
self.model.summary()
|
||||
return self.model
|
||||
|
||||
def get_name(self):
|
||||
return f"{'pt-' if self.pre_trained else ''}{'ft-' if self.fine_tune else ''}" \
|
||||
f"{self.base_model_type.value.name}-d{self.dense_layer_neurons}-do{self.dropout_rate}" \
|
||||
f"{'-l1' + str(self.l1) if self.l1 > 0 else ''}{'-l2' + str(self.l2) if self.l2 > 0 else ''}" \
|
||||
f"-{int(time())}"
|
||||
@@ -2,6 +2,7 @@ from collections import Callable
|
||||
|
||||
|
||||
class ModelWrapper(object):
|
||||
def __init__(self, model_func:Callable, model_preprocessor:Callable):
|
||||
def __init__(self, model_func:Callable, model_preprocessor:Callable, name:str):
|
||||
self.model_func = model_func
|
||||
self.model_preprocessor = model_preprocessor
|
||||
self.name = name
|
||||
@@ -1,76 +0,0 @@
|
||||
from enum import Enum
|
||||
from typing import Tuple
|
||||
|
||||
import tensorflow as tf
|
||||
from tensorflow import keras
|
||||
|
||||
from .modelwrapper import ModelWrapper
|
||||
|
||||
|
||||
class ImageClassModels(Enum):
|
||||
INCEPTION_V3 = ModelWrapper(
|
||||
keras.applications.InceptionV3,
|
||||
keras.applications.inception_v3.preprocess_input
|
||||
)
|
||||
XCEPTION = ModelWrapper(
|
||||
keras.applications.xception.Xception,
|
||||
keras.applications.inception_v3.preprocess_input
|
||||
)
|
||||
MOBILENET_V2 = ModelWrapper(
|
||||
keras.applications.mobilenet_v2.MobileNetV2,
|
||||
keras.applications.mobilenet_v2.preprocess_input
|
||||
)
|
||||
|
||||
|
||||
class ImageClassModelBuilder(object):
|
||||
|
||||
def __init__(self,
|
||||
input_shape: Tuple[int, int, int],
|
||||
n_classes: int,
|
||||
optimizer: tf.keras.optimizers.Optimizer = keras.optimizers.Adam(
|
||||
learning_rate=.0001),
|
||||
pre_trained: bool = True,
|
||||
fine_tune: int = 0,
|
||||
base_model: ImageClassModels = ImageClassModels.MOBILENET_V2):
|
||||
self.input_shape = input_shape
|
||||
self.n_classes = n_classes
|
||||
self.optimizer = optimizer
|
||||
self.pre_trained = pre_trained
|
||||
self.fine_tune = fine_tune
|
||||
self.base_model = base_model
|
||||
|
||||
def set_base_model(self, base_model: ImageClassModels):
|
||||
self.base_model = base_model
|
||||
|
||||
def create_model(self):
|
||||
|
||||
base_model = self.base_model.value.model_func(
|
||||
weights='imagenet' if self.pre_trained else None,
|
||||
include_top=False
|
||||
)
|
||||
if self.pre_trained:
|
||||
if self.fine_tune > 0:
|
||||
for layer in base_model.layers[:-self.fine_tune]:
|
||||
layer.trainable = False
|
||||
else:
|
||||
for layer in base_model.layers:
|
||||
layer.trainable = False
|
||||
|
||||
i = tf.keras.layers.Input([self.input_shape[0], self.input_shape[1], self.input_shape[2]], dtype=tf.float32)
|
||||
x = tf.cast(i, tf.float32)
|
||||
x = self.base_model.value.model_preprocessor(x)
|
||||
x = base_model(x)
|
||||
x = keras.layers.GlobalAveragePooling2D()(x)
|
||||
x = keras.layers.Dense(1024, activation='relu', kernel_regularizer=keras.regularizers.L1L2(l1=1e-5, l2=1e-5))(x)
|
||||
x = keras.layers.Dropout(0.25)(x)
|
||||
output = keras.layers.Dense(self.n_classes, activation='softmax')(x)
|
||||
|
||||
model = keras.Model(inputs=i, outputs=output)
|
||||
model.compile(optimizer=self.optimizer,
|
||||
loss=keras.losses.CategoricalCrossentropy(),
|
||||
metrics=[
|
||||
'accuracy',
|
||||
# 'mse'
|
||||
])
|
||||
model.summary()
|
||||
return model
|
||||
@@ -0,0 +1 @@
|
||||
tensorboard --logdir_spec=local:./logs,remote:Z:/MachineLearning/Tensorboard/Tensordex/Logs --bind_all
|
||||
|
Before Width: | Height: | Size: 60 KiB After Width: | Height: | Size: 60 KiB |
|
Before Width: | Height: | Size: 100 KiB After Width: | Height: | Size: 100 KiB |
|
Before Width: | Height: | Size: 45 KiB After Width: | Height: | Size: 45 KiB |
|
Before Width: | Height: | Size: 107 KiB After Width: | Height: | Size: 107 KiB |
|
Before Width: | Height: | Size: 159 KiB After Width: | Height: | Size: 159 KiB |
|
Before Width: | Height: | Size: 45 KiB After Width: | Height: | Size: 45 KiB |
|
Before Width: | Height: | Size: 62 KiB After Width: | Height: | Size: 62 KiB |
|
Before Width: | Height: | Size: 45 KiB After Width: | Height: | Size: 45 KiB |
|
Before Width: | Height: | Size: 62 KiB After Width: | Height: | Size: 62 KiB |
|
Before Width: | Height: | Size: 35 KiB After Width: | Height: | Size: 35 KiB |
|
Before Width: | Height: | Size: 57 KiB After Width: | Height: | Size: 57 KiB |
|
Before Width: | Height: | Size: 77 KiB After Width: | Height: | Size: 77 KiB |
|
Before Width: | Height: | Size: 75 KiB After Width: | Height: | Size: 75 KiB |
|
Before Width: | Height: | Size: 92 KiB After Width: | Height: | Size: 92 KiB |
|
Before Width: | Height: | Size: 58 KiB After Width: | Height: | Size: 58 KiB |
|
Before Width: | Height: | Size: 62 KiB After Width: | Height: | Size: 62 KiB |
|
Before Width: | Height: | Size: 33 KiB After Width: | Height: | Size: 33 KiB |
|
Before Width: | Height: | Size: 48 KiB After Width: | Height: | Size: 48 KiB |
|
Before Width: | Height: | Size: 185 KiB After Width: | Height: | Size: 185 KiB |
|
Before Width: | Height: | Size: 501 KiB After Width: | Height: | Size: 501 KiB |
|
Before Width: | Height: | Size: 48 KiB After Width: | Height: | Size: 48 KiB |
|
Before Width: | Height: | Size: 830 KiB After Width: | Height: | Size: 830 KiB |
|
Before Width: | Height: | Size: 44 KiB After Width: | Height: | Size: 44 KiB |
|
Before Width: | Height: | Size: 517 KiB After Width: | Height: | Size: 517 KiB |
|
Before Width: | Height: | Size: 58 KiB After Width: | Height: | Size: 58 KiB |
|
Before Width: | Height: | Size: 158 KiB After Width: | Height: | Size: 158 KiB |
|
Before Width: | Height: | Size: 65 KiB After Width: | Height: | Size: 65 KiB |
|
Before Width: | Height: | Size: 68 KiB After Width: | Height: | Size: 68 KiB |
|
Before Width: | Height: | Size: 45 KiB After Width: | Height: | Size: 45 KiB |
|
Before Width: | Height: | Size: 666 KiB After Width: | Height: | Size: 666 KiB |
|
Before Width: | Height: | Size: 133 KiB After Width: | Height: | Size: 133 KiB |
|
Before Width: | Height: | Size: 54 KiB After Width: | Height: | Size: 54 KiB |
|
Before Width: | Height: | Size: 171 KiB After Width: | Height: | Size: 171 KiB |
|
Before Width: | Height: | Size: 54 KiB After Width: | Height: | Size: 54 KiB |
|
Before Width: | Height: | Size: 55 KiB After Width: | Height: | Size: 55 KiB |
|
Before Width: | Height: | Size: 61 KiB After Width: | Height: | Size: 61 KiB |
|
Before Width: | Height: | Size: 54 KiB After Width: | Height: | Size: 54 KiB |
|
Before Width: | Height: | Size: 54 KiB After Width: | Height: | Size: 54 KiB |
|
Before Width: | Height: | Size: 695 KiB After Width: | Height: | Size: 695 KiB |
|
Before Width: | Height: | Size: 55 KiB After Width: | Height: | Size: 55 KiB |
|
Before Width: | Height: | Size: 381 KiB After Width: | Height: | Size: 381 KiB |
|
Before Width: | Height: | Size: 600 KiB After Width: | Height: | Size: 600 KiB |
|
Before Width: | Height: | Size: 51 KiB After Width: | Height: | Size: 51 KiB |
|
Before Width: | Height: | Size: 38 KiB After Width: | Height: | Size: 38 KiB |
|
Before Width: | Height: | Size: 38 KiB After Width: | Height: | Size: 38 KiB |
|
Before Width: | Height: | Size: 48 KiB After Width: | Height: | Size: 48 KiB |
|
Before Width: | Height: | Size: 43 KiB After Width: | Height: | Size: 43 KiB |
|
Before Width: | Height: | Size: 69 KiB After Width: | Height: | Size: 69 KiB |
|
Before Width: | Height: | Size: 58 KiB After Width: | Height: | Size: 58 KiB |
|
Before Width: | Height: | Size: 274 KiB After Width: | Height: | Size: 274 KiB |
|
Before Width: | Height: | Size: 549 KiB After Width: | Height: | Size: 549 KiB |
|
Before Width: | Height: | Size: 92 KiB After Width: | Height: | Size: 92 KiB |
|
Before Width: | Height: | Size: 106 KiB After Width: | Height: | Size: 106 KiB |
|
Before Width: | Height: | Size: 60 KiB After Width: | Height: | Size: 60 KiB |
|
Before Width: | Height: | Size: 35 KiB After Width: | Height: | Size: 35 KiB |
|
Before Width: | Height: | Size: 39 KiB After Width: | Height: | Size: 39 KiB |
|
Before Width: | Height: | Size: 58 KiB After Width: | Height: | Size: 58 KiB |
|
Before Width: | Height: | Size: 143 KiB After Width: | Height: | Size: 143 KiB |
|
Before Width: | Height: | Size: 2.0 MiB After Width: | Height: | Size: 2.0 MiB |
|
Before Width: | Height: | Size: 332 KiB After Width: | Height: | Size: 332 KiB |
|
Before Width: | Height: | Size: 36 KiB After Width: | Height: | Size: 36 KiB |
|
Before Width: | Height: | Size: 58 KiB After Width: | Height: | Size: 58 KiB |
|
Before Width: | Height: | Size: 38 KiB After Width: | Height: | Size: 38 KiB |
|
Before Width: | Height: | Size: 51 KiB After Width: | Height: | Size: 51 KiB |
|
Before Width: | Height: | Size: 89 KiB After Width: | Height: | Size: 89 KiB |
|
Before Width: | Height: | Size: 485 KiB After Width: | Height: | Size: 485 KiB |
|
Before Width: | Height: | Size: 705 KiB After Width: | Height: | Size: 705 KiB |
|
Before Width: | Height: | Size: 57 KiB After Width: | Height: | Size: 57 KiB |
|
Before Width: | Height: | Size: 110 KiB After Width: | Height: | Size: 110 KiB |
|
Before Width: | Height: | Size: 38 KiB After Width: | Height: | Size: 38 KiB |
|
Before Width: | Height: | Size: 1.0 MiB After Width: | Height: | Size: 1.0 MiB |
|
Before Width: | Height: | Size: 3.6 MiB After Width: | Height: | Size: 3.6 MiB |
|
Before Width: | Height: | Size: 46 KiB After Width: | Height: | Size: 46 KiB |
|
Before Width: | Height: | Size: 17 KiB After Width: | Height: | Size: 17 KiB |
|
Before Width: | Height: | Size: 58 KiB After Width: | Height: | Size: 58 KiB |
|
Before Width: | Height: | Size: 426 KiB After Width: | Height: | Size: 426 KiB |
|
Before Width: | Height: | Size: 676 KiB After Width: | Height: | Size: 676 KiB |
|
Before Width: | Height: | Size: 41 KiB After Width: | Height: | Size: 41 KiB |