Professional Documents
Culture Documents
RESNET - Dynamometer - Ipynb - Colaboratory
RESNET - Dynamometer - Ipynb - Colaboratory
RESNET - Dynamometer - Ipynb - Colaboratory
tensorflow.compat.v1 import ConfigProto
from tensorflow.compat.v1 import InteractiveSession
config = ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 0.5
config.gpu_options.allow_growth = True
session = InteractiveSession(config=config)
from tensorflow.keras.layers import Input, Lambda, Dense, Flatten
from tensorflow.keras.models import Model
from tensorflow.keras.applications.resnet50 import ResNet50
#from keras.applications.vgg16 import VGG16
from tensorflow.keras.applications.resnet50 import preprocess_input
from tensorflow.keras.preprocessing import image
from tensorflow.keras.preprocessing.image import ImageDataGenerator,load_img
from tensorflow.keras.models import Sequential
import numpy as np
from glob import glob
import PIL
import matplotlib.pyplot as plt
import tensorflow as tf
from google.colab import drive
drive.mount('/content/drive')
Mounted at /content/drive
# re-size all the images to this
IMAGE_SIZE = [224, 224]
train_path = '/content/drive/MyDrive/DYNOCARD_DATA/Train'
valid_path = '/content/drive/MyDrive/DYNOCARD_DATA/Test'
train_ds = tf.keras.preprocessing.image_dataset_from_directory(train_path)
class_names = train_ds.class_names
print(class_names)
['ANCHORED BUT NOT SET', 'ASPHALTENES IN PUMP', 'BAD TAIL BEARING', 'DEEP WELL', 'FLU
#plt.figure(figsize=(20,20))
#for images, labels in train_ds.take(1):
#for i in range(2):
# ax = plt.subplot(2,2, i + 1)
# plt.imshow(images[i].numpy().astype("uint8"))
#plt.title(class_names[labels[i]])
#plt.axis("off")
import tensorflow
resnet50 =tensorflow.keras.applications.ResNet152V2(input_shape=IMAGE_SIZE + [3], weights=
# don't train existing weights
for layer in resnet50.layers:
layer.trainable = False
# useful for getting number of output classes
folders = glob('/content/drive/MyDrive/DYNOCARD_DATA/Train/*')
# our layers - you can add more if you want
x = Flatten()(resnet50.output)
prediction = Dense(len(folders), activation='softmax')(x)
# create a model object
model = Model(inputs=resnet50.input, outputs=prediction)
model.summary()
# tell the model what cost and optimization method to use
model.compile(
loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy']
# Use the Image Data Generator to import the images from the dataset
from tensorflow.keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(rescale = 1./255,
shear_range = 0.1,
zoom_range = 0.1,
horizontal_flip = False)
test_datagen = ImageDataGenerator(rescale = 1./255)
# Make sure you provide the same target size as initialied for the image size
training_set = train_datagen.flow_from_directory('/content/drive/MyDrive/DYNOCARD_DATA/Tra
target_size = (224, 224),
batch_size = 32,
class_mode = 'categorical')
test_set = test_datagen.flow_from_directory('/content/drive/MyDrive/DYNOCARD_DATA/Test',
target_size = (224, 224),
batch_size = 32,
class_mode = 'categorical')
#fluidpound = list(train_path.glob('fluidpound/*'))
#print(fluidpound[0])
#PIL.Image.open(str(fluidpound[0]))
import matplotlib.pyplot as plt
# fit the model
# Run the cell. It will take some time to execute
r = model.fit_generator(
training_set,
validation_data=test_set,
epochs=7,
steps_per_epoch=len(training_set),
validation_steps=len(test_set)
Epoch 1/7
import matplotlib.pyplot as plt
# plot the loss
plt.plot(r.history['loss'], label='train loss')
plt.plot(r.history['val_loss'], label='val loss')
plt.ylabel("Percentage",
fontweight ='bold',fontsize=20, c = "r")
plt.xlabel("No of EPOCHS",
fontweight ='bold', c = "green",fontsize=16)
plt.title("RESNET Model Losses", fontsize=22, fontweight = "bold", loc="center", c="yellow
plt.grid(color = 'black', linestyle = '--', linewidth = 0.35)
plt.legend(["Training Loss", "Validation Loss"], loc ="upper right")
plt.show()
#plt.savfig('LossVal_loss')
print('')
# plot the accuracy
plt.plot(r.history['accuracy'], label='train acc')
plt.plot(r.history['val_accuracy'], label='val acc')
plt.ylabel("Percentage",
fontweight ='bold',fontsize=20, c = "r")
plt.xlabel("No of EPOCHS",
fontweight ='bold', c = "green",fontsize=16)
plt.title("RESNET Model Accuracy", fontsize=22, fontweight = "bold", loc="center", c="yell
plt.grid(color = 'black', linestyle = '--', linewidth = 0.35)
plt.legend(["Training Accuracy", "Validation Accuracy"], loc ="lower right")
plt.show()