Renamed files using VGG16 network
This commit is contained in:
@@ -11,7 +11,7 @@ OBJECT_DETECTION=prepare_train_model.py test_model.py
|
|||||||
help:
|
help:
|
||||||
@echo "make linear_regression"
|
@echo "make linear_regression"
|
||||||
@echo " runs the simple linear regression example"
|
@echo " runs the simple linear regression example"
|
||||||
@echo "make objct_detection"
|
@echo "make vgg16"
|
||||||
@ecgo " runs the object detection example"
|
@ecgo " runs the object detection example"
|
||||||
@echo "make env"
|
@echo "make env"
|
||||||
@echo " creates and prepares the environment"
|
@echo " creates and prepares the environment"
|
||||||
@@ -44,11 +44,11 @@ $(VENV_NAME)/bin/activate:
|
|||||||
linear_regression: env
|
linear_regression: env
|
||||||
${PYTHON} $(LINEAR_REGRESSION)
|
${PYTHON} $(LINEAR_REGRESSION)
|
||||||
|
|
||||||
object_detection_training: fine_tune.h5
|
vgg16_training: fine_tune.h5
|
||||||
fine_tune.h5:
|
fine_tune.h5:
|
||||||
${PYTHON} prepare_train_model.py
|
${PYTHON} vgg16-prepare_train_model.py
|
||||||
object_detection: env object_detection_training
|
vgg16: env vgg16_training
|
||||||
${PYTHON} test_model.py
|
${PYTHON} vgg16-test_model.py
|
||||||
|
|
||||||
lint: env
|
lint: env
|
||||||
${PYTHON} -m pylint --rcfile=pylintrc $(LINT_FILES)
|
${PYTHON} -m pylint --rcfile=pylintrc $(LINT_FILES)
|
||||||
|
|||||||
@@ -0,0 +1,122 @@
|
|||||||
|
"""
|
||||||
|
Simple example on how to fine tune models in Keras and how to use them.
|
||||||
|
Part 1
|
||||||
|
|
||||||
|
Source: https://www.guru99.com/keras-tutorial.html
|
||||||
|
"""
|
||||||
|
|
||||||
|
#### Data preparation
|
||||||
|
from keras.preprocessing.image import ImageDataGenerator
|
||||||
|
import numpy as np
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
import keras
|
||||||
|
from keras.layers import Flatten, Dense
|
||||||
|
from keras.applications.vgg16 import VGG16
|
||||||
|
from keras.optimizers import SGD
|
||||||
|
|
||||||
|
TRAIN_PATH = 'images/train/'
|
||||||
|
TEST_PATH = 'images/test/'
|
||||||
|
BATCH_SIZE = 16
|
||||||
|
IMAGE_SIZE = 224
|
||||||
|
NUM_CLASSES = 8
|
||||||
|
|
||||||
|
# The ImageDataGenerator will make an X_training data from a directory.
|
||||||
|
# The sub-directory in that directory will be used as a class for each object.
|
||||||
|
# The image will be loaded with the RGB color mode, with the categorical class
|
||||||
|
# mode for the Y_training data, with a batch size of 16. Finally, shuffle the
|
||||||
|
# data.
|
||||||
|
|
||||||
|
train_datagen = ImageDataGenerator(validation_split=0.3,
|
||||||
|
shear_range=0.2,
|
||||||
|
zoom_range=0.2,
|
||||||
|
horizontal_flip=True)
|
||||||
|
|
||||||
|
train_generator = train_datagen.flow_from_directory(
|
||||||
|
directory=TRAIN_PATH,
|
||||||
|
target_size=(IMAGE_SIZE,IMAGE_SIZE),
|
||||||
|
batch_size=BATCH_SIZE,
|
||||||
|
class_mode='categorical',
|
||||||
|
color_mode='rgb',
|
||||||
|
shuffle=True)
|
||||||
|
|
||||||
|
|
||||||
|
# Let's see our images randomly by plotting them with matplotlib
|
||||||
|
# x_batch, y_batch = train_generator.next()
|
||||||
|
x_batch, y_batch = next(train_generator)
|
||||||
|
fig=plt.figure()
|
||||||
|
|
||||||
|
COLUMNS = 4
|
||||||
|
ROWS = 4
|
||||||
|
|
||||||
|
for i in range(1, COLUMNS*ROWS):
|
||||||
|
num = np.random.randint(BATCH_SIZE)
|
||||||
|
image = x_batch[num].astype(np.int)
|
||||||
|
fig.add_subplot(ROWS, COLUMNS, i)
|
||||||
|
plt.imshow(image)
|
||||||
|
plt.show()
|
||||||
|
|
||||||
|
### Creating model
|
||||||
|
# Let's create our network model from VGG16 with imageNet pre-trained weight.
|
||||||
|
# We will freeze these layers so that the layers are not trainable to help us
|
||||||
|
# reduce the computation time.
|
||||||
|
|
||||||
|
# Load the VGG model
|
||||||
|
base_model = VGG16(weights='imagenet', include_top=False, input_shape=(IMAGE_SIZE, IMAGE_SIZE, 3))
|
||||||
|
|
||||||
|
print(base_model.summary())
|
||||||
|
|
||||||
|
# Freeze the layers
|
||||||
|
for layer in base_model.layers:
|
||||||
|
layer.trainable = False
|
||||||
|
|
||||||
|
# Create the model
|
||||||
|
model = keras.models.Sequential()
|
||||||
|
|
||||||
|
# Add the vgg convolutional base model
|
||||||
|
model.add(base_model)
|
||||||
|
|
||||||
|
# Add new layers
|
||||||
|
model.add(Flatten())
|
||||||
|
model.add(Dense(1024, activation='relu'))
|
||||||
|
model.add(Dense(1024, activation='relu'))
|
||||||
|
model.add(Dense(NUM_CLASSES, activation='softmax'))
|
||||||
|
|
||||||
|
# Show a summary of the model. Check the number of trainable parameters
|
||||||
|
print(model.summary())
|
||||||
|
|
||||||
|
input("Press Enter to continue...")
|
||||||
|
|
||||||
|
# As you can see, the summary of our network model. From an input from
|
||||||
|
# VGG16 Layers, then we add 2 Fully Connected Layer which will extract 1024
|
||||||
|
# features and an output layer that will compute the 8 classes with the softmax
|
||||||
|
# activation.
|
||||||
|
|
||||||
|
#### Training
|
||||||
|
# Compile the model
|
||||||
|
model.compile(loss='categorical_crossentropy',
|
||||||
|
optimizer=SGD(lr=1e-3),
|
||||||
|
metrics=['accuracy'])
|
||||||
|
|
||||||
|
# Start the training process
|
||||||
|
# model.fit(x_train, y_train, validation_split=0.30, batch_size=32, epochs=50, verbose=2)
|
||||||
|
|
||||||
|
# Save the model
|
||||||
|
# model.save('catdog.h5')
|
||||||
|
|
||||||
|
history = model.fit_generator(train_generator,
|
||||||
|
steps_per_epoch=train_generator.n/BATCH_SIZE,
|
||||||
|
epochs=10)
|
||||||
|
|
||||||
|
model.save('fine_tune.h5')
|
||||||
|
|
||||||
|
# Summarize history for accuracy
|
||||||
|
plt.plot(history.history['loss'])
|
||||||
|
plt.title('loss')
|
||||||
|
plt.ylabel('loss')
|
||||||
|
plt.xlabel('epoch')
|
||||||
|
plt.legend(['loss'], loc='upper left')
|
||||||
|
plt.show()
|
||||||
|
|
||||||
|
# Our losses are dropped significantly and the accuracy is almost 100%. For
|
||||||
|
# testing our model, we randomly picked images over the internet and put it on
|
||||||
|
# the test folder with a different class to test
|
||||||
@@ -0,0 +1,72 @@
|
|||||||
|
"""
|
||||||
|
Simple example on how to fine tune models in Keras and how to use them.
|
||||||
|
Part 2
|
||||||
|
|
||||||
|
Source: https://www.guru99.com/keras-tutorial.html
|
||||||
|
"""
|
||||||
|
|
||||||
|
from keras.models import load_model
|
||||||
|
from keras.preprocessing.image import ImageDataGenerator
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
train_datagen = ImageDataGenerator(validation_split=0.3,
|
||||||
|
shear_range=0.2,
|
||||||
|
zoom_range=0.2,
|
||||||
|
horizontal_flip=True)
|
||||||
|
|
||||||
|
TRAIN_PATH = 'images/train/'
|
||||||
|
TEST_PATH = 'images/test/'
|
||||||
|
BATCH_SIZE = 16
|
||||||
|
IMAGE_SIZE = 224
|
||||||
|
NUM_CLASSES = 8
|
||||||
|
|
||||||
|
### Testing our model
|
||||||
|
model = load_model('fine_tune.h5')
|
||||||
|
|
||||||
|
test_datagen = ImageDataGenerator()
|
||||||
|
train_generator = train_datagen.flow_from_directory(
|
||||||
|
directory=TRAIN_PATH,
|
||||||
|
target_size=(IMAGE_SIZE, IMAGE_SIZE),
|
||||||
|
batch_size=BATCH_SIZE,
|
||||||
|
class_mode='categorical',
|
||||||
|
color_mode='rgb',
|
||||||
|
shuffle=True)
|
||||||
|
|
||||||
|
test_generator = test_datagen.flow_from_directory(
|
||||||
|
directory=TEST_PATH,
|
||||||
|
target_size=(IMAGE_SIZE, IMAGE_SIZE),
|
||||||
|
color_mode='rgb',
|
||||||
|
shuffle=False,
|
||||||
|
class_mode='categorical',
|
||||||
|
batch_size=1)
|
||||||
|
|
||||||
|
filenames = test_generator.filenames
|
||||||
|
nb_samples = len(filenames)
|
||||||
|
|
||||||
|
fig = plt.figure()
|
||||||
|
|
||||||
|
COLUMNS = 4
|
||||||
|
ROWS = 4
|
||||||
|
|
||||||
|
# And our test is as given below! Only 1 image is predicted wrong from a test of
|
||||||
|
# 14 images!
|
||||||
|
|
||||||
|
for i in range(1, COLUMNS*ROWS -1):
|
||||||
|
x_batch, y_batch = next(test_generator)
|
||||||
|
|
||||||
|
name = model.predict(x_batch)
|
||||||
|
name = np.argmax(name, axis=-1)
|
||||||
|
true_name = y_batch
|
||||||
|
true_name = np.argmax(true_name, axis=-1)
|
||||||
|
|
||||||
|
label_map = (test_generator.class_indices)
|
||||||
|
label_map = dict((v,k) for k,v in label_map.items()) #flip k,v
|
||||||
|
predictions = [label_map[k] for k in name]
|
||||||
|
true_value = [label_map[k] for k in true_name]
|
||||||
|
|
||||||
|
image = x_batch[0].astype(np.int)
|
||||||
|
fig.add_subplot(ROWS, COLUMNS, i)
|
||||||
|
plt.title(str(predictions[0]) + ':' + str(true_value[0]))
|
||||||
|
plt.imshow(image)
|
||||||
|
plt.show()
|
||||||
Reference in New Issue
Block a user