<jupyter_start><jupyter_text>Peer Review Final Assignment  Introduction In this lab, you will build an image classifier using the VGG16 pre-trained model, and you will evaluate it and compare its performance to the model we built in the last module using the ResNet50 pre-trained model. Good luck!  Table of Contents    1. Download Data 2. Part 13. Part 2  4. Part 3<jupyter_code>from keras.preprocessing.image import ImageDataGenerator
import keras
from keras.models import Sequential
from keras.layers import Dense
from keras.applications import VGG16
from keras.applications.vgg16 import preprocess_input<jupyter_output><empty_output><jupyter_text>Download Data Use the wget command to download the data for this assignment from here: https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/DL0321EN/data/concrete_data_week4.zip Use the following cells to download the data.<jupyter_code>## get the data
!wget https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/DL0321EN/data/concrete_data_week4.zip
!unzip concrete_data_week4.zip<jupyter_output>[1;30;43mStreaming output truncated to the last 5000 lines.[0m
  inflating: concrete_data_week4/train/negative/08835.jpg  
  inflating: __MACOSX/concrete_data_week4/train/negative/._08835.jpg  
  inflating: concrete_data_week4/train/negative/08821.jpg  
  inflating: __MACOSX/concrete_data_week4/train/negative/._08821.jpg  
  inflating: concrete_data_week4/train/negative/03974.jpg  
  inflating: __MACOSX/concrete_data_week4/train/negative/._03974.jpg  
  inflating: concrete_data_week4/train/negative/12879.jpg  
  inflating: __MACOSX/concrete_data_week4/train/negative/._12879.jpg  
  inflating: concrete_data_week4/train/negative/09281.jpg  
  inflating: __MACOSX/concrete_data_week4/train/negative/._09281.jpg  
  inflating: concrete_data_week4/train/negative/10908.jpg  
  inflating: __MACOSX/concrete_data_week4/train/negative/._10908.jpg  
  inflating: concrete_data_week4/train/negative/01805.jpg  
  inflating: __MACOSX/concrete_data_week4/train/negative/._01805.jpg  
  inflating: concr[...]<jupyter_text>After you unzip the data, you fill find the data has already been divided into a train, validation, and test sets.      Part 1 In this part, you will design a classifier using the VGG16 pre-trained model. Just like the ResNet50 model, you can import the model VGG16 from keras.applications. You will essentially build your classifier as follows:1. Import libraries, modules, and packages you will need. Make sure to import the *preprocess_input* function from keras.applications.vgg16.2. Use a batch size of 100 images for both training and validation.3. Construct an ImageDataGenerator for the training set and another one for the validation set. VGG16 was originally trained on 224 × 224 images, so make sure to address that when defining the ImageDataGenerator instances.4. Create a sequential model using Keras. Add VGG16 model to it and dense layer.5. Compile the mode using the adam optimizer and the categorical_crossentropy loss function.6. Fit the model on the augmented data using the ImageDataGenerators. Use the following cells to create your classifier.<jupyter_code>num_classes = 2

image_resize = 224

batch_size_training = 100
batch_size_validation = 100
data_generator = ImageDataGenerator(
    preprocessing_function=preprocess_input,
)
train_generator = data_generator.flow_from_directory(
    'concrete_data_week4/train',
    target_size=(image_resize, image_resize),
    batch_size=batch_size_training,
    class_mode='categorical')
validation_generator = data_generator.flow_from_directory(
    'concrete_data_week4/valid',
    target_size=(image_resize, image_resize),
    batch_size=batch_size_validation,
    class_mode='categorical')
model = Sequential()
model.add(VGG16(
    include_top=False,
    pooling='avg',
    weights='imagenet',
    ))
model.add(Dense(num_classes, activation='softmax'))
model.layers
model.layers[0].layers
model.layers[0].trainable = False
model.summary()
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
steps_per_epoch_training = len(train_generator)
steps_per_epoch_validation = len(validation_generator)
num_epochs = 2
fit_history = model.fit_generator(
    train_generator,
    steps_per_epoch=steps_per_epoch_training,
    epochs=num_epochs,
    validation_data=validation_generator,
    validation_steps=steps_per_epoch_validation,
    verbose=1,
)
model.save('classifier_vgg16_model.h5')<jupyter_output><empty_output><jupyter_text>Part 2 In this part, you will evaluate your deep learning models on a test data. For this part, you will need to do the following:1. Load your saved model that was built using the ResNet50 model. 2. Construct an ImageDataGenerator for the test set. For this ImageDataGenerator instance, you only need to pass the directory of the test images, target size, and the **shuffle** parameter and set it to False.3. Use the **evaluate_generator** method to evaluate your models on the test data, by passing the above ImageDataGenerator as an argument. You can learn more about **evaluate_generator** [here](https://keras.io/models/sequential/).4. Print the performance of the classifier using the VGG16 pre-trained model.5. Print the performance of the classifier using the ResNet pre-trained model. Use the following cells to evaluate your models.<jupyter_code># load and evaluate a saved model
from numpy import loadtxt
from keras.models import load_model

 
# load models
model_resnet = keras.models.load_model('classifier_resnet_model.h5')
model_vgg16 = keras.models.load_model('classifier_vgg16_model.h5')
# summarize models
model_resnet.summary()
model_vgg16.summary()


# load dataset
test_generator = data_generator.flow_from_directory(
    'concrete_data_week4/test',
    target_size=(image_resize, image_resize),
    shuffle = False)



evaluate_gen = model.evaluate_generator(test_generator, verbose=0)

score_resnet = model_resnet.evaluate_generator(test_generator)
score_vgg16 = model_vgg16.evaluate_generator(test_generator)

print(f'{model_resnet.metrics_names[1]} resnet = {score_resnet[1]*100}')
print(f'{model_vgg16.metrics_names[1]} vgg16 = {score_vgg16[1]*100}')<jupyter_output><empty_output><jupyter_text>Part 3 In this model, you will predict whether the images in the test data are images of cracked concrete or not. You will do the following:1. Use the **predict_generator** method to predict the class of the images in the test data, by passing the test data ImageDataGenerator instance defined in the previous part as an argument. You can learn more about the **predict_generator** method [here](https://keras.io/models/sequential/).2. Report the class predictions of the first five images in the test set. You should print something list this:            Positive          Negative         Positive        Positive        Negative     Use the following cells to make your predictions.<jupyter_code>predictions_resnet = model_resnet.predict_classes(test_generator)
for prediction in predictions_resnet[0:5]:
  if prediction == 0:
    print('Negative')
  else:
    print('Positive')
# predictions_resnet = model_resnet.predict_generator(test_generator,steps = 5)
# # predictions_resnet = model_resnet.predict(predict_generator)
# print(f'predictions: {predictions_resnet}')
predictions_vgg16 = model_vgg16.predict_classes(test_generator)
for prediction in predictions_vgg16[0:5]:
  if prediction == 0:
    print('Negative')
  else:
    print('Positive')

# print(f'predictions: {predictions_vgg16}')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07751019871035662, 'fraction_numerical': 0.030135544150546124, 'mean_word_length': 5.540447504302926, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '132602', 'license': 'CC0-1.0', 'n_tokens_mistral': 2417, 'n_tokens_neox': 2269, 'n_words': 666, 'path': 'DL0321EN_4_1_Comparing_Models_py_v1_2.ipynb', 'repo_name': 'pusinuke/Python_projects', 'size': '7599'}