Created
November 17, 2021 02:55
-
-
Save pvwalke/d6c96c48cdaa3f81606c2e7f1b9eb7ca to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tensorflow as tf | |
from tensorflow.keras.applications.resnet50 import preprocess_input | |
from tensorflow.keras.preprocessing.image import ImageDataGenerator | |
from tensorflow.keras.preprocessing import image | |
import pathlib | |
import matplotlib.pyplot as plt | |
import tensorflow_addons as tfa | |
from tensorflow.keras.applications import ResNet50 | |
from tensorflow.keras.models import Model, Sequential | |
from tensorflow.keras.layers import Dense, Flatten, GlobalAveragePooling2D, Dropout | |
from tensorflow.keras import metrics | |
IMG_WIDTH=224 | |
IMG_HEIGHT=224 | |
IMG_DIM = (IMG_WIDTH, IMG_HEIGHT) | |
BATCH_SIZE = 3 | |
IMG_DIR = pathlib.Path('/content/thesis_data') | |
TRAIN_DIR = '/content/thesis_data/train_set' | |
VAL_DIR = '/content/thesis_data/test_set' | |
# Specify the values for all arguments to data_generator_with_aug. | |
data_generator_with_aug = ImageDataGenerator(rescale = 1./255, rotation_range=15,shear_range=0.15,zoom_range=0.05,horizontal_flip=True,vertical_flip=True) | |
data_generator_no_aug = ImageDataGenerator(rescale = 1./255,horizontal_flip=True,vertical_flip=True) | |
train_generator = data_generator_with_aug.flow_from_directory( | |
directory=TRAIN_DIR, | |
target_size=IMG_DIM, | |
batch_size=32, | |
class_mode='categorical') | |
validation_generator = data_generator_no_aug.flow_from_directory( | |
directory=VAL_DIR, | |
target_size=IMG_DIM,batch_size=32, | |
class_mode='categorical') | |
num_classes = 6 | |
resnet = ResNet50(include_top=False, weights='imagenet', input_shape=(IMG_HEIGHT,IMG_WIDTH,3),pooling='max') | |
resnet.summary() | |
output = resnet.layers[-1].output | |
output = tf.keras.layers.Flatten()(output) | |
resnet = Model(resnet.input, output) | |
model = Sequential() | |
model.add(resnet) | |
model.add(Dense(1024, activation='relu')) | |
model.add(Dropout(0.5)) | |
model.add(Dense(1024, activation='relu')) | |
model.add(Dropout(0.5)) | |
model.add(Dense(num_classes, activation='softmax')) | |
model.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics=['accuracy',metrics.mean_squared_error]) | |
model.fit(train_generator, | |
steps_per_epoch=590//32, | |
epochs = 20, | |
validation_steps=117//32, | |
validation_data = validation_generator) | |
accuracy = model.history.history['accuracy'] | |
val_accuracy = model.history.history['val_accuracy'] | |
loss = model.history.history['loss'] | |
val_loss = model.history.history['val_loss'] | |
mse = model.history.history['mean_squared_error'] | |
val_mse = model.history.history['mean_squared_error'] | |
epochs = range(len(accuracy)) | |
plt.plot(epochs,accuracy,'bo',label='Training accuracy') | |
plt.plot(epochs,val_accuracy,'b',label='Validation accuracy') | |
plt.title('Training and validation accuracy') | |
plt.legend() | |
plt.show() | |
plt.figure() | |
plt.plot(epochs, loss, 'bo', label='Training loss') | |
plt.plot(epochs, val_loss, 'b', label='Validation loss') | |
plt.title('Training and validation loss') | |
plt.legend() | |
plt.show() | |
plt.figure() | |
plt.plot(epochs, mse, 'bo', label='Training Mean Squared Error') | |
plt.plot(epochs, val_mse, 'b', label='Validation Mean Squared Errror') | |
plt.legend() | |
plt.show() | |
save_path = '/content/thesis_data/resnet_output/' | |
tf.keras.models.save_model(model,save_path) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
img_path = '/content/sample_data/Capture14.JPG' | |
from keras.preprocessing import image | |
import numpy as np | |
from keras.models import load_model | |
img = image.load_img(img_path, target_size=(150, 150)) | |
img_tensor = image.img_to_array(img) | |
img_tensor = np.expand_dims(img_tensor, axis=0) | |
img_tensor /= 255. | |
print(img_tensor.shape) | |
import matplotlib.pyplot as plt | |
plt.imshow(img_tensor[0]) | |
plt.show() | |
model = load_model("/content/thesis_data/resnet_output") | |
from keras import models | |
layer_outputs = [layer.output for layer in model.layers[:4]] | |
activation_model = models.Model(inputs=model.input, outputs=layer_outputs) | |
#activation_model = models.Model(inputs=classifier.input, outputs=layer_outputs) | |
activations = activation_model.predict(img_tensor) | |
first_layer_activation = activations[0] | |
print(first_layer_activation.shape) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment