Minor modification to remove pylint messages
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
"""
|
"""
|
||||||
Simple linear regression application.
|
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
|
Source: https://www.guru99.com/keras-tutorial.html
|
||||||
"""
|
"""
|
||||||
|
|||||||
+55
-61
@@ -1,65 +1,66 @@
|
|||||||
# Source: https://www.guru99.com/keras-tutorial.html
|
"""
|
||||||
|
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
|
#### Data preparation
|
||||||
from keras.preprocessing.image import ImageDataGenerator
|
from keras.preprocessing.image import ImageDataGenerator
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import matplotlib.pyplot as plt
|
import matplotlib.pyplot as plt
|
||||||
|
import keras
|
||||||
|
from keras.layers import Flatten, Dense
|
||||||
|
from keras.applications.vgg16 import VGG16
|
||||||
|
|
||||||
train_path = 'images/train/'
|
TRAIN_PATH = 'images/train/'
|
||||||
test_path = 'images/test/'
|
TEST_PATH = 'images/test/'
|
||||||
batch_size = 16
|
BATCH_SIZE = 16
|
||||||
image_size = 224
|
IMAGE_SIZE = 224
|
||||||
num_class = 8
|
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.
|
||||||
|
|
||||||
'''
|
|
||||||
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,
|
train_datagen = ImageDataGenerator(validation_split=0.3,
|
||||||
shear_range=0.2,
|
shear_range=0.2,
|
||||||
zoom_range=0.2,
|
zoom_range=0.2,
|
||||||
horizontal_flip=True)
|
horizontal_flip=True)
|
||||||
|
|
||||||
train_generator = train_datagen.flow_from_directory(
|
train_generator = train_datagen.flow_from_directory(
|
||||||
directory=train_path,
|
directory=TRAIN_PATH,
|
||||||
target_size=(image_size,image_size),
|
target_size=(IMAGE_SIZE,IMAGE_SIZE),
|
||||||
batch_size=batch_size,
|
batch_size=BATCH_SIZE,
|
||||||
class_mode='categorical',
|
class_mode='categorical',
|
||||||
color_mode='rgb',
|
color_mode='rgb',
|
||||||
shuffle=True)
|
shuffle=True)
|
||||||
|
|
||||||
'''
|
|
||||||
Let's see our images randomly by plotting them with matplotlib
|
# Let's see our images randomly by plotting them with matplotlib
|
||||||
'''
|
# x_batch, y_batch = train_generator.next()
|
||||||
x_batch, y_batch = train_generator.next()
|
x_batch, y_batch = next(train_generator)
|
||||||
fig=plt.figure()
|
fig=plt.figure()
|
||||||
columns = 4
|
|
||||||
rows = 4
|
COLUMNS = 4
|
||||||
for i in range(1, columns*rows):
|
ROWS = 4
|
||||||
num = np.random.randint(batch_size)
|
|
||||||
|
for i in range(1, COLUMNS*ROWS):
|
||||||
|
num = np.random.randint(BATCH_SIZE)
|
||||||
image = x_batch[num].astype(np.int)
|
image = x_batch[num].astype(np.int)
|
||||||
fig.add_subplot(rows, columns, i)
|
fig.add_subplot(ROWS, COLUMNS, i)
|
||||||
plt.imshow(image)
|
plt.imshow(image)
|
||||||
plt.show()
|
plt.show()
|
||||||
|
|
||||||
### Creating model
|
### Creating model
|
||||||
import keras
|
# Let's create our network model from VGG16 with imageNet pre-trained weight.
|
||||||
from keras.models import Model, load_model
|
# We will freeze these layers so that the layers are not trainable to help us
|
||||||
from keras.layers import Activation, Dropout, Flatten, Dense
|
# reduce the computation time.
|
||||||
from keras.preprocessing.image import ImageDataGenerator
|
|
||||||
from keras.applications.vgg16 import VGG16
|
|
||||||
|
|
||||||
'''
|
|
||||||
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
|
# Load the VGG model
|
||||||
base_model = VGG16(weights='imagenet', include_top=False, input_shape=(image_size, image_size, 3))
|
base_model = VGG16(weights='imagenet', include_top=False, input_shape=(IMAGE_SIZE, IMAGE_SIZE, 3))
|
||||||
|
|
||||||
print(base_model.summary())
|
print(base_model.summary())
|
||||||
|
|
||||||
@@ -77,45 +78,39 @@ model.add(base_model)
|
|||||||
model.add(Flatten())
|
model.add(Flatten())
|
||||||
model.add(Dense(1024, activation='relu'))
|
model.add(Dense(1024, activation='relu'))
|
||||||
model.add(Dense(1024, activation='relu'))
|
model.add(Dense(1024, activation='relu'))
|
||||||
model.add(Dense(num_class, activation='softmax'))
|
model.add(Dense(NUM_CLASSES, activation='softmax'))
|
||||||
|
|
||||||
# Show a summary of the model. Check the number of trainable parameters
|
# Show a summary of the model. Check the number of trainable parameters
|
||||||
print(model.summary())
|
print(model.summary())
|
||||||
|
|
||||||
input("Press Enter to continue...")
|
input("Press Enter to continue...")
|
||||||
|
|
||||||
'''
|
# As you can see, the summary of our network model. From an input from
|
||||||
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
|
||||||
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
|
||||||
features and an output layer that will compute the 8 classes with the softmax
|
# activation.
|
||||||
activation.
|
|
||||||
'''
|
|
||||||
|
|
||||||
#### Training
|
#### Training
|
||||||
# Compile the model
|
# Compile the model
|
||||||
from keras.optimizers import SGD
|
# from keras.optimizers import SGD
|
||||||
|
|
||||||
model.compile(loss='categorical_crossentropy',
|
# model.compile(loss='categorical_crossentropy',
|
||||||
optimizer=SGD(lr=1e-3),
|
# optimizer=SGD(lr=1e-3),
|
||||||
metrics=['accuracy'])
|
# metrics=['accuracy'])
|
||||||
|
|
||||||
'''
|
|
||||||
# Start the training process
|
# Start the training process
|
||||||
model.fit(x_train, y_train, validation_split=0.30, batch_size=32, epochs=50, verbose=2)
|
# model.fit(x_train, y_train, validation_split=0.30, batch_size=32, epochs=50, verbose=2)
|
||||||
|
|
||||||
# Save the model
|
# Save the model
|
||||||
model.save('catdog.h5')
|
# model.save('catdog.h5')
|
||||||
'''
|
|
||||||
|
|
||||||
history = model.fit_generator(train_generator,
|
history = model.fit_generator(train_generator,
|
||||||
steps_per_epoch=train_generator.n/batch_size,
|
steps_per_epoch=train_generator.n/BATCH_SIZE,
|
||||||
epochs=10)
|
epochs=10)
|
||||||
|
|
||||||
model.save('fine_tune.h5')
|
model.save('fine_tune.h5')
|
||||||
|
|
||||||
# summarize history for accuracy
|
# Summarize history for accuracy
|
||||||
import matplotlib.pyplot as plt
|
|
||||||
|
|
||||||
plt.plot(history.history['loss'])
|
plt.plot(history.history['loss'])
|
||||||
plt.title('loss')
|
plt.title('loss')
|
||||||
plt.ylabel('loss')
|
plt.ylabel('loss')
|
||||||
@@ -123,8 +118,7 @@ plt.xlabel('epoch')
|
|||||||
plt.legend(['loss'], loc='upper left')
|
plt.legend(['loss'], loc='upper left')
|
||||||
plt.show()
|
plt.show()
|
||||||
|
|
||||||
'''
|
|
||||||
As you can see, our losses are dropped significantly and the accuracy is
|
# Our losses are dropped significantly and the accuracy is almost 100%. For
|
||||||
almost 100%. For testing our model, we randomly picked images over the internet
|
# testing our model, we randomly picked images over the internet and put it on
|
||||||
and put it on the test folder with a different class to test
|
# the test folder with a different class to test
|
||||||
'''
|
|
||||||
|
|||||||
+28
-22
@@ -1,5 +1,11 @@
|
|||||||
import keras
|
"""
|
||||||
from keras.models import Model, load_model
|
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
|
||||||
|
"""
|
||||||
|
|
||||||
|
from keras.models import load_model
|
||||||
from keras.preprocessing.image import ImageDataGenerator
|
from keras.preprocessing.image import ImageDataGenerator
|
||||||
import matplotlib.pyplot as plt
|
import matplotlib.pyplot as plt
|
||||||
import numpy as np
|
import numpy as np
|
||||||
@@ -9,27 +15,27 @@ train_datagen = ImageDataGenerator(validation_split=0.3,
|
|||||||
zoom_range=0.2,
|
zoom_range=0.2,
|
||||||
horizontal_flip=True)
|
horizontal_flip=True)
|
||||||
|
|
||||||
train_path = 'images/train/'
|
TRAIN_PATH = 'images/train/'
|
||||||
test_path = 'images/test/'
|
TEST_PATH = 'images/test/'
|
||||||
batch_size = 16
|
BATCH_SIZE = 16
|
||||||
image_size = 224
|
IMAGE_SIZE = 224
|
||||||
num_class = 8
|
NUM_CLASSES = 8
|
||||||
|
|
||||||
### Testing our model
|
### Testing our model
|
||||||
model = load_model('fine_tune.h5')
|
model = load_model('fine_tune.h5')
|
||||||
|
|
||||||
test_datagen = ImageDataGenerator()
|
test_datagen = ImageDataGenerator()
|
||||||
train_generator = train_datagen.flow_from_directory(
|
train_generator = train_datagen.flow_from_directory(
|
||||||
directory=train_path,
|
directory=TRAIN_PATH,
|
||||||
target_size=(image_size,image_size),
|
target_size=(IMAGE_SIZE, IMAGE_SIZE),
|
||||||
batch_size=batch_size,
|
batch_size=BATCH_SIZE,
|
||||||
class_mode='categorical',
|
class_mode='categorical',
|
||||||
color_mode='rgb',
|
color_mode='rgb',
|
||||||
shuffle=True)
|
shuffle=True)
|
||||||
|
|
||||||
test_generator = test_datagen.flow_from_directory(
|
test_generator = test_datagen.flow_from_directory(
|
||||||
directory=test_path,
|
directory=TEST_PATH,
|
||||||
target_size=(image_size, image_size),
|
target_size=(IMAGE_SIZE, IMAGE_SIZE),
|
||||||
color_mode='rgb',
|
color_mode='rgb',
|
||||||
shuffle=False,
|
shuffle=False,
|
||||||
class_mode='categorical',
|
class_mode='categorical',
|
||||||
@@ -39,10 +45,15 @@ filenames = test_generator.filenames
|
|||||||
nb_samples = len(filenames)
|
nb_samples = len(filenames)
|
||||||
|
|
||||||
fig = plt.figure()
|
fig = plt.figure()
|
||||||
columns = 4
|
|
||||||
rows = 4
|
COLUMNS = 4
|
||||||
for i in range(1, columns*rows -1):
|
ROWS = 4
|
||||||
x_batch, y_batch = test_generator.next()
|
|
||||||
|
# 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 = model.predict(x_batch)
|
||||||
name = np.argmax(name, axis=-1)
|
name = np.argmax(name, axis=-1)
|
||||||
@@ -55,12 +66,7 @@ for i in range(1, columns*rows -1):
|
|||||||
true_value = [label_map[k] for k in true_name]
|
true_value = [label_map[k] for k in true_name]
|
||||||
|
|
||||||
image = x_batch[0].astype(np.int)
|
image = x_batch[0].astype(np.int)
|
||||||
fig.add_subplot(rows, columns, i)
|
fig.add_subplot(ROWS, COLUMNS, i)
|
||||||
plt.title(str(predictions[0]) + ':' + str(true_value[0]))
|
plt.title(str(predictions[0]) + ':' + str(true_value[0]))
|
||||||
plt.imshow(image)
|
plt.imshow(image)
|
||||||
plt.show()
|
plt.show()
|
||||||
|
|
||||||
'''
|
|
||||||
And our test is as given below! Only 1 image is predicted wrong from a test of
|
|
||||||
14 images!
|
|
||||||
'''
|
|
||||||
Reference in New Issue
Block a user