I'm trying to create an binary image classifier to detect if someone is using a surgical mask following the 'Cat vs Dogs' example available at the TensorFlow website (https://www.tensorflow.org/tutorials/images/classification)
I've created a small dataset with some images of people wearing surgical masks and some people without it, trainned my CNN and got an accuray around 70%, which is fine by now. But the thing is, how do I make predictions? The 'CatVsDogs' example stops at augmentation.
Right now I'm not worried about accuracy, just wondering how I can get predictions from my model.
This is my code:
import sys
import time
import numpy as np
import matplotlib.pyplot as plt
import os
import cv2
import random
import keras
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten, Conv2D, MaxPooling2D
from tensorflow.keras.callbacks import TensorBoard
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from keras.optimizers import adam
IMG_SIZE = 100 # Image dimensions
batch_size = 100 # Amount of data that will be fed to the NN at a time
epochs = 1 # Amount of times the data will be pass to the NN
training_data = []
#img_array = []
new_array = []
####### DATASET LOCATION #######
TRAIN_DIR = 'C:/Users/Alex/Google Drive/Colab Notebooks/MaskDetector/Train/' #Create variable to store the path of the training images directory
VALIDATION_DIR = 'C:/Users/Alex/Google Drive/Colab Notebooks/MaskDetector/Validate/' #Create variable to store the path of the validation images directory
TEST_DIR = 'C:/Users/Alex/Google Drive/Colab Notebooks/MaskDetector/Test/' #Create variable to store the path of the testing images directory
CATEGORIES = ['MaskOn','MaskOff'] #Categories 'MaskOn' and 'MaskOff' same name as the folders
TRAIN_DIR_MASKON = os.path.join(TRAIN_DIR, 'MaskOn') # Directory with pics of ppl with masks for training
TRAIN_DIR_MASKOFF = os.path.join(TRAIN_DIR, 'MaskOff') # Directory with pics of ppl without masks for training
VALIDATION_DIR_MASKON = os.path.join(VALIDATION_DIR, 'MaskOn') # Directory with pics of ppl with masks for validation
VALIDATION_DIR_MASKOFF = os.path.join(VALIDATION_DIR, 'MaskOff') # Directory with pics of ppl without masks for validation
###########################################
####### Shows the size of the dataset #######
num_maskon_tr = len(os.listdir(TRAIN_DIR_MASKON))
num_maskoff_tr = len(os.listdir(TRAIN_DIR_MASKOFF))
num_maskon_val = len(os.listdir(VALIDATION_DIR_MASKON))
num_maskoff_val = len(os.listdir(VALIDATION_DIR_MASKOFF))
total_train = num_maskon_tr + num_maskoff_tr
total_val = num_maskon_val + num_maskoff_val
###################### DATA AUGMENTATION ######################
######### FLIP #########
image_gen = ImageDataGenerator(rescale=1./255, horizontal_flip=True)
train_data_gen = image_gen.flow_from_directory(batch_size=batch_size,
directory=TRAIN_DIR,
shuffle=True,
target_size=(IMG_SIZE, IMG_SIZE))
augmented_images = [train_data_gen[0][0][0] for i in range(5)]
# Re-use the same custom plotting function defined and used above to visualize the training images
######### ROTATE 45° #########
image_gen = ImageDataGenerator(rescale=1./255, rotation_range=45)
train_data_gen = image_gen.flow_from_directory(batch_size=batch_size,
directory=TRAIN_DIR,
shuffle=True,
target_size=(IMG_SIZE, IMG_SIZE))
augmented_images = [train_data_gen[0][0][0] for i in range(5)]
######### ZOOM FROM 0 TO 10% #########
# zoom_range from 0 - 1 where 1 = 100%.
image_gen = ImageDataGenerator(rescale=1./255, zoom_range=0.5) #
train_data_gen = image_gen.flow_from_directory(batch_size=batch_size,
directory=TRAIN_DIR,
shuffle=True,
target_size=(IMG_SIZE, IMG_SIZE))
augmented_images = [train_data_gen[0][0][0] for i in range(5)]
####################################################################################################
### PREPARES THE DATA TO BE FED INTO THE NN ###
train_image_generator = ImageDataGenerator(rescale=1./255)
image_gen_val = ImageDataGenerator(rescale=1./255)
test_data_generator = ImageDataGenerator(rescale=1./255) #Use this to make predictons?Don't know yet
val_data_gen = image_gen_val.flow_from_directory(batch_size=batch_size,
directory=VALIDATION_DIR,
target_size=(IMG_SIZE, IMG_SIZE),
class_mode='binary')
####################################################################################
train_data_gen = train_image_generator.flow_from_directory(batch_size=batch_size,
directory=TRAIN_DIR,
shuffle=True,
target_size=(IMG_SIZE, IMG_SIZE),
class_mode='binary')
#####################################################################################
test_generator = test_data_generator.flow_from_directory(TEST_DIR,
target_size=(IMG_SIZE, IMG_SIZE),
batch_size=batch_size,
class_mode="binary",
shuffle=True)
############ NN Model ############
model = Sequential([
Conv2D(16, 3, padding='same', activation='relu',
input_shape=(IMG_SIZE, IMG_SIZE ,3)),
MaxPooling2D(),
Dropout(0.2),
Conv2D(32, 3, padding='same', activation='relu'),
MaxPooling2D(),
Conv2D(64, 3, padding='same', activation='relu'),
MaxPooling2D(),
Dropout(0.2),
Flatten(),
Dense(512, activation='relu'),
Dense(1)
])
#########################################################
############ COMPILES THE NN ############
model.compile(optimizer='adam',
loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),
metrics=['accuracy'])
############ PERFORMS THE TRAINNING ############
history = model.fit_generator(
train_data_gen,
steps_per_epoch=total_train // batch_size,
epochs=epochs,
validation_data=val_data_gen,
validation_steps=total_val // batch_size
)
You've clearly found model.compile() and model.fit_generator() - all you need to do is head over to the documentation and find the other methods. Here's a link that'll tell you how to use model.predict(). Use that for your prediction.
Related
I am using Tensorflow's flow_from_directory to collect a large image dataset and then train on it. I want to use Keras Tuner but when I run
tuner.search(test_data_gen, epochs=50,
validation_split=0.2, callbacks=[stop_early])
It throws the following error,
ValueError: `validation_split` is only supported for Tensors or NumPy arrays, found following types in the input: [<class 'tensorflow.python.keras.preprocessing.image.DirectoryIterator'>]
I don't know much about converting between data types in AI so any help is truly appreciated.
Here is the rest of my code:
import tensorflow as tf
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt
import IPython.display as display
from PIL import Image, ImageSequence
import os
import pathlib
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Conv2D, Flatten, Dropout, MaxPooling2D
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import cv2
import datetime
import kerastuner as kt
tf.compat.v1.enable_eager_execution()
epochs = 50
steps_per_epoch = 10
batch_size = 20
IMG_HEIGHT = 200
IMG_WIDTH = 200
train_dir = "Data/Train"
test_dir = "Data/Val"
train_image_generator = ImageDataGenerator(rescale=1. / 255)
test_image_generator = ImageDataGenerator(rescale=1. / 255)
train_data_gen = train_image_generator.flow_from_directory(batch_size=batch_size,
directory=train_dir,
shuffle=True,
target_size=(IMG_HEIGHT, IMG_WIDTH),
class_mode='sparse')
test_data_gen = test_image_generator.flow_from_directory(batch_size=batch_size,
directory=test_dir,
shuffle=True,
target_size=(IMG_HEIGHT, IMG_WIDTH),
class_mode='sparse')
def model_builder(hp):
model = keras.Sequential()
model.add(Conv2D(265, 3, padding='same', activation='relu', input_shape=(IMG_HEIGHT, IMG_WIDTH ,3)))
model.add(MaxPooling2D())
model.add(Conv2D(64, 3, padding='same', activation='relu'))
model.add(MaxPooling2D())
model.add(Conv2D(32, 3, padding='same', activation='relu'))
model.add(MaxPooling2D())
model.add(Flatten())
model.add(keras.layers.Dense(256, activation="relu"))
hp_units = hp.Int('units', min_value=32, max_value=512, step=32)
model.add(keras.layers.Dense(hp_units, activation="relu"))
model.add(keras.layers.Dense(80, activation="softmax"))
hp_learning_rate = hp.Choice('learning_rate', values=[1e-2, 1e-3, 1e-4])
model.compile(optimizer=keras.optimizers.Adam(learning_rate=hp_learning_rate),
loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['top_k_categorical_accuracy'])
return model
tuner = kt.Hyperband(model_builder,
objective='val_accuracy',
max_epochs=30,
factor=3,
directory='Hypertuner_Dir',
project_name='AIOS')
stop_early = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=5)
and start searching with tuner
tuner.search(train_data_gen, epochs=50, validation_split=0.2, callbacks=[stop_early])
# Get the optimal hyperparameters
best_hps=tuner.get_best_hyperparameters(num_trials=1)[0]
print(f"""
The hyperparameter search is complete. The optimal number of units in the first densely-connected
layer is {best_hps.get('units')} and the optimal learning rate for the optimizer
is {best_hps.get('learning_rate')}.
""")
model = tuner.hypermodel.build(best_hps)
model.summary()
tf.keras.utils.plot_model(model, to_file="model.png", show_shapes=True, show_layer_names=True, rankdir='TB')
checkpoint_path = "training/cp.ckpt"
checkpoint_dir = os.path.dirname(checkpoint_path)
cp_callback = tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_path,
save_weights_only=True,
verbose=1)
os.system("rm -r logs")
log_dir = "logs/fit/" + datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir=log_dir, histogram_freq=1)
#history = model.fit(train_data_gen,steps_per_epoch=steps_per_epoch,epochs=epochs,validation_data=test_data_gen,validation_steps=10,callbacks=[cp_callback, tensorboard_callback])
history = model.fit(train_data_gen,steps_per_epoch=steps_per_epoch,epochs=epochs,validation_split=0.2,validation_steps=10,callbacks=[cp_callback, tensorboard_callback])
model.load_weights(tf.train.latest_checkpoint(checkpoint_dir))
model.save('model.h5', include_optimizer=True)
test_loss, test_acc = model.evaluate(test_data_gen)
print("Tested Acc: ", test_acc)
print("Tested Acc: ", test_acc*100, "%")
val_acc_per_epoch = history.history['val_accuracy']
best_epoch = val_acc_per_epoch.index(max(val_acc_per_epoch)) + 1
print('Best epoch: %d' % (best_epoch,))
===================================EDIT====================================
According to the doc about validation_split:
validation_split: Float between 0 and 1. Fraction of the training data to be used as validation data. The model will set apart this fraction of the training data, will not train on it, and will evaluate the loss and any model metrics on this data at the end of each epoch. The validation data is selected from the last samples in the x and y data provided, before shuffling. This argument is not supported when x is a dataset, generator or keras.utils.Sequence instance.
Now, as you've generator, try as follows, reference
tuner.search(train_data_gen,
epochs=50,
validation_data=test_data_gen,
callbacks=[stop_early])
Also, ensure that each of your generators properly generates the valid batches.
Unfortunately doing a validation_split=0.2 does not work in this case, because this argument assumes that the data is a Tensor or a NumPy array. Since you have the data stored as a generator (which is a good idea), you can't simply split it.
You'll need to create a validation generator, just like you did with test_data_gen, and change validation_split=0.2 to validation_data=val_data_gen.
I want to test the accuracy of 11000+ images.
I split the data into two classes ,"Yes" and "No". Then ,split them into 80/20 in training set and test set respectively.
Then I split,the training data again into 80/20 for validation set.
I create "Yes" and "No" folder for each one,validation set, test set, training set.And keep the data in respective folders.
Now, I want to train the model in Google Colab.
After train the model, I want to test the "Accuracy" of the "Test Data Set" and plot them with, specificity, sensitivity, accuracy, loss_Function and recall.
Help need for this. Detailed help will be more appreciated.
Thanks in advance.
#Yefet
Code is here:
import tensorflow as tf
import matplotlib.pyplot as plt
import zipfile
import cv2
import os
import numpy as np
import matplotlib.image as mpimg
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.preprocessing import image
from sklearn.model_selection import KFold, StratifiedKFold
img = image.load_img("drive/MyDrive/data03/train/no/1 no.jpeg" , target_size=(200, 200))
plt.imshow(img)
class myCallback(tf.keras.callbacks.Callback):
def on_epoch_end(self,epoch,logs={}):
if (logs.get('accuracy') > .98) & (logs.get('val_accuracy') > .9):
print("Reached 98% accuracy so cancelling training!")
self.model.stop_training = True
callbacks = myCallback()
train_datagen = ImageDataGenerator(rescale=1/255)
validation_datagen = ImageDataGenerator(rescale=1/255)
train_generator = train_datagen.flow_from_directory(
'drive/MyDrive/data03/train', # This is the source directory for training images
target_size=(200, 200), # All images will be resized to 150x150
batch_size=128,
# Since we use binary_crossentropy loss, we need binary labels
class_mode='binary')
validation_generator = train_datagen.flow_from_directory(
'drive/MyDrive/data03/validation', # This is the source directory for training images
target_size=(200, 200), # All images will be resized to 150x150
batch_size=128,
# Since we use binary_crossentropy loss, we need binary labels
class_mode='binary')
from tensorflow.keras.optimizers import RMSprop
model = tf.keras.models.Sequential([
# Note the input shape is the desired size of the image 150x150 with 3 bytes color
# This is the first convolution
tf.keras.layers.Conv2D(16, (3,3), activation='relu', input_shape=(200, 200, 3)),
tf.keras.layers.MaxPooling2D(2, 2),
# The second convolution
tf.keras.layers.Conv2D(32, (3,3), activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),
# The third convolution
tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),
# The fourth convolution
#tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
#tf.keras.layers.MaxPooling2D(2,2),
# The fifth convolution
#tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
#tf.keras.layers.MaxPooling2D(2,2),
# Flatten the results to feed into a DNN
tf.keras.layers.Flatten(),
# 512 neuron hidden layer
tf.keras.layers.Dense(512, activation='relu'),
# Only 1 output neuron. It will contain a value from 0-1 where 0 for 1 class ('horses') and 1 for the other ('humans')
tf.keras.layers.Dense(1, activation='sigmoid')
])
model.compile(loss='binary_crossentropy',
optimizer=RMSprop(lr=0.001),
metrics=['accuracy'])
history = model.fit(
train_generator,
steps_per_epoch=1,
epochs=29,
validation_data = validation_generator,callbacks=[callbacks])
dir_path = "drive/MyDrive/data03/test"
for i in os.listdir(dir_path):
img = image.load_img(dir_path+ '//' + i, target_size=(200, 200) )
plt.imshow(img)
plt.show()
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
images = np.vstack([x])
classes =model.predict(images)
if classes[0]==0:
print("NO TUMOR")
else:
print("YES ")
I'm only beginning with Keras and machine learning in general.
I trained a model to classify images from 9 classes and saved it using model.save(). Here is the code I used:
from keras.layers import Input, Lambda, Dense, Flatten
from keras.models import Model
from keras.applications.resnet50 import ResNet50
from keras.preprocessing import image
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
import numpy as np
from glob import glob
import matplotlib.pyplot as plt
# re-size all the images to this
IMAGE_SIZE = [224, 224]
train_path = 'Datasets/Train'
valid_path = 'Datasets/Test'
# add preprocessing layer to the front of resnet
resnet = ResNet50(input_shape=IMAGE_SIZE + [3], weights='imagenet', include_top=False)
# don't train existing weights
for layer in resnet.layers:
layer.trainable = False
# useful for getting number of classes
folders = glob('Datasets/Train/*')
# our layers - you can add more if you want
x = Flatten()(resnet.output)
# x = Dense(1000, activation='relu')(x)
prediction = Dense(len(folders), activation='softmax')(x)
# create a model object
model = Model(inputs=resnet.input, outputs=prediction)
# view the structure of the model
model.summary()
# tell the model what cost and optimization method to use
model.compile(
loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy']
)
from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(rescale=1. / 255,
shear_range=0.1,
zoom_range=0.1,
horizontal_flip=True)
test_datagen = ImageDataGenerator(rescale=1. / 255)
training_set = train_datagen.flow_from_directory('Datasets/Train',
target_size=(224, 224),
batch_size=32,
class_mode='categorical')
test_set = test_datagen.flow_from_directory('Datasets/Test',
target_size=(224, 224),
batch_size=32,
class_mode='categorical')
# fit the model
r = model.fit_generator(
training_set,
validation_data=test_set,
epochs=3,
steps_per_epoch=len(training_set),
validation_steps=len(test_set)
)
def plot_loss_accuracy(r):
fig = plt.figure(figsize=(12, 6))
ax = fig.add_subplot(1, 2, 1)
ax.plot(r.history["loss"], 'r-x', label="Train Loss")
ax.plot(r.history["val_loss"], 'b-x', label="Validation Loss")
ax.legend()
ax.set_title('cross_entropy loss')
ax.grid(True)
ax = fig.add_subplot(1, 2, 2)
ax.plot(r.history["accuracy"], 'r-x', label="Train Accuracy")
ax.plot(r.history["val_accuracy"], 'b-x', label="Validation Accuracy")
ax.legend()
ax.set_title('acuracy')
ax.grid(True)
It successfully trained. To load and test this model on new images, I used the below code:
from keras.models import load_model
import cv2
import numpy as np
model = load_model('model.h5')
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
img = cv2.imread('test.jpg')
img = cv2.resize(img,(320,240))
img = np.reshape(img,[1,320,240,3])
classes = model.predict_classes(img)
print(classes)
It outputs:
AttributeError: 'Model' object has no attribute 'predict_classes'
Why wouldn't it even predict?
Thank you,
predict_classes is only available for sequential api http://faroit.com/keras-docs/1.0.0/models/sequential/
So, you first need to get the probabilities and take the max probability as the class.
from keras.models import load_model
import cv2
import numpy as np
class_names = ['a', 'b', 'c', ...] # fill the rest
model = load_model('model.h5')
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
img = cv2.imread('test.jpg')
img = cv2.resize(img,(320,240))
img = np.reshape(img,[1,320,240,3])
classes = np.argmax(model.predict(img), axis = -1)
print(classes)
names = [class_names[i] for i in classes]
print(names)
I'm writing a CNN classifier for 20 classes. As mentioned in the title, this is the error i get when i try to fit my model.
I've already checked this answer: Error, but i have already (i think) set the categorical conversion.
Here's my code:
1) Imagegenerator:
apply_data_augmentation = True
if apply_data_augmentation:
train_datagen = ImageDataGenerator(rotation_range=10, #Training data generator + validation split (20%)
width_shift_range=10,
height_shift_range=10,
zoom_range=0.3,
horizontal_flip=True,
vertical_flip=True,
fill_mode='constant',
cval=0,
rescale=1./255,
validation_split=0.2)
else:
train_datagen = ImageDataGenerator(rescale=1./255)
2) Getting data:
dataset_dir = os.path.join(cwd, 'Classification_Dataset')
bs = 8
# img shape
img_h = 28
img_w = 28
num_classes=20
decide_class_indices = False
if decide_class_indices:
classes = ['airplane', # 0
'bear', # 1
'calculator', # 2
'computer-monitor', # 3
'fireworks', # 4
'galaxy', # 5
'grand-piano', # 6
'kangaroo', # 7
'laptop', # 8
'lightbulb', # 9
'lightning', # 10
'mountain-bike', # 11
'owl', # 12
'school-bus', # 13
'sheet-music', # 14
'skyscraper', # 15
'sword', # 16
't-shirt', # 17
'waterfall', # 18
'wine-bottle'] # 19
else:
classes=None
print(classes)
# Training
training_dir = os.path.join(dataset_dir, 'training')
train_generator = train_datagen.flow_from_directory(
training_dir,
target_size=(img_h, img_w),
batch_size=bs,
class_mode='categorical',
classes=classes,
subset='training',
shuffle=True,
seed=SEED) # set as training data
validation_generator = train_datagen.flow_from_directory(
training_dir, # same directory as training data
target_size=(img_h, img_w),
batch_size=bs,
class_mode='categorical',
classes=classes,
subset='validation',
shuffle=False,
seed=SEED) # set as validation data
print(train_generator)
3) Model + fit:
from keras.datasets import mnist
import matplotlib.pyplot as plt
import numpy as np
from keras import Sequential
from keras.layers import Conv2D, AveragePooling2D, Flatten, Dense, Dropout
from keras.callbacks import EarlyStopping
model = Sequential()
model.add(Conv2D(filters=6, kernel_size=(3, 3), activation='relu', input_shape=(28,28,3)))
model.add(AveragePooling2D())
model.add(Conv2D(filters=16, kernel_size=(3, 3), activation='relu'))
model.add(AveragePooling2D())
model.add(Flatten())
model.add(Dense(units=120, activation='relu'))
model.add(Dropout(0.3))
model.add(Dense(units=100, activation='relu'))
model.add(Dropout(0.3))
model.add(Dense(units=20, activation='softmax'))
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
nb_epochs = 5
model.fit_generator(
train_generator,
steps_per_epoch = train_generator.samples // bs,
validation_data = validation_generator,
validation_steps = validation_generator.samples // bs,
epochs = nb_epochs)
I checked all the possible problems mentioned in many questions (such as missing flatten layers, categorical conversion, non matching inputs, ) but (at least to me) everything seems fine.
I can't understand where's the problem.
Your flow_from_directory function with class_mode='categorical' generates one-hot encoded 2D labels (Ref) whereas sparse_categorical_crossentropy expects integer value. You have two ways to fix the issue:
1. Change the loss function to categorical_crossentropy when you compile your model
2. Change the label to sparse mode by using class_mode='sparse' in the flow_from_directory function
In a nutshell, when your label is an integer value (representing class) use sparse_categorical_crossentropy and when label is one-hot encoded use categorical_crossentropy as loss function.
Hi I feel that there is something wrong with the way my code is running. I'm trying to load vgg and resnet for deep learning. This is the code I used.
from keras import applications
from keras.preprocessing.image import ImageDataGenerator
from keras import optimizers
from keras.models import Sequential
from keras.layers import Dropout, Flatten, Dense
# path to the model weights files.
weights_path = '../keras/examples/vgg16_weights.h5'
top_model_weights_path = 'fc_model.h5'
# dimensions of our images.
img_width, img_height = 150, 150
train_data_dir = 'cats_and_dogs_small/train'
validation_data_dir = 'cats_and_dogs_small/validation'
nb_train_samples = 2000
nb_validation_samples = 800
epochs = 50
batch_size = 16
# build the VGG16 network
model = applications.VGG16(weights='imagenet', include_top=False)
print('Model loaded.')
# build a classifier model to put on top of the convolutional model
top_model = Sequential()
top_model.add(Flatten(input_shape=model.output_shape[1:]))
top_model.add(Dense(256, activation='relu'))
top_model.add(Dropout(0.5))
top_model.add(Dense(1, activation='sigmoid'))
# note that it is necessary to start with a fully-trained
# classifier, including the top classifier,
# in order to successfully do fine-tuning
top_model.load_weights(top_model_weights_path)
# add the model on top of the convolutional base
model.add(top_model)
# set the first 25 layers (up to the last conv block)
# to non-trainable (weights will not be updated)
for layer in model.layers[:25]:
layer.trainable = False
# compile the model with a SGD/momentum optimizer
# and a very slow learning rate.
model.compile(loss='binary_crossentropy',
optimizer=optimizers.SGD(lr=1e-4, momentum=0.9),
metrics=['accuracy'])
# prepare data augmentation configuration
train_datagen = ImageDataGenerator(
rescale=1. / 255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)
test_datagen = ImageDataGenerator(rescale=1. / 255)
train_generator = train_datagen.flow_from_directory(
train_data_dir,
target_size=(img_height, img_width),
batch_size=batch_size,
class_mode='binary')
validation_generator = test_datagen.flow_from_directory(
validation_data_dir,
target_size=(img_height, img_width),
batch_size=batch_size,
class_mode='binary')
# fine-tune the model
model.fit_generator(
train_generator,
samples_per_epoch=nb_train_samples,
epochs=epochs,
validation_data=validation_generator,
nb_val_samples=nb_validation_samples)
At the line 'model = applications.VGG16(weights='imagenet', include_top=False)'
programs starts to download weights and it displays as below.
This process would take around 5/6 days to complete fully. But it gets stuck at the middle. Is there a simple way that I can avoid this complete process. Manually downloading. Is there something I'm missing.
Please help