import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Conv2D, Flatten, Dropout, MaxPooling2D, Activation,GlobalMaxPooling2D,GlobalAveragePooling2D,BatchNormalization
from tensorflow.keras import optimizers
from tensorflow.keras.preprocessing import image #PIL image
from tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau
import tensorflow.keras.backend as keras_backend
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
%matplotlib inline
# list all data in training
print(training.history.keys())
# summarize training for accuracy
plt.plot(training.history['accuracy']) # training is the variable from the fit method
plt.plot(training.history['val_accuracy'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
# summarize traning for loss
plt.plot(training.history['loss'])
plt.plot(training.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
CLASSES= {0:"Angry", 1:"Disgust", 2:"Fear", 3:"Happy", 4:"Sad", 5:"Surprise", 6:"Neutral"}
plt.figure(figsize=(12, 12))
for i in range(0,9):
plt.subplot(5, 3, i+1)
plt.imshow(image_list[i], cmap="gray") #'image_list' is the list of images
plt.xlabel(CLASSES[labels[i]]) # 'labels' is the list of labels
plt.tight_layout()
plt.show()
from tensorflow.keras.applications import MobileNetV2 #name of the model to be used
from tensorflow.keras.models import Model #API for the wrapping
# different operations on the pre-trained model
pretrained_model= MobileNetV2(include_top=False,weights='imagenet',input_shape=input_shape)
# List of layers in the pretrained_model
layer_list = [layers.name for layers in pretrained_model.layers]
# make all the layers non-trainable
for layers in pretrained_model.layers:
layers.trainable = False
# make the layers(by api names) as non-trainable
for layers in pretrained_model.layers:
if layers._keras_api_names[0] == 'keras.layers.BatchNormalization':
layers.trainable = False
#to find the methods available
dir(pretrained_model)
earlystop = EarlyStopping(patience=10)
learning_rate_reduction = ReduceLROnPlateau(monitor='val_accuracy',
patience=2,
verbose=1,
factor=0.5,
min_lr=0.00001)
callbacks = [earlystop, learning_rate_reduction]
Mobilenet_v2_model.compile(loss='binary_crossentropy',
optimizer=optimizers.Adam(learning_rate=0.001),
metrics=['accuracy'])
training = Mobilenet_v2_model.fit(train_generator,
steps_per_epoch=100,epochs=50,
validation_data=validation_generator,
validation_steps=100,
callbacks=callbacks)
https://www.tensorflow.org/api_docs/python/tf/keras/preprocessing/
from tensorflow.keras.preprocessing import image
import numpy as np
img_pred = image.load_img("test_set/test_set/dogs/dog.4003.jpg",target_size=(150,150))
img_pred=image.img_to_array(img_pred)
img_pred=np.expand_dims(img_pred, axis=0)
result = Efficientnet_model.predict(img_pred)
from tensorflow.keras.models import Sequential
from tensorflow.keras import layers
img_augmentation = Sequential(
[
layers.RandomRotation(factor=0.15),
layers.RandomTranslation(height_factor=0.1, width_factor=0.1),
layers.RandomFlip(),
layers.RandomContrast(factor=0.1),
],
name="img_augmentation",
)
# applying augmentations on the image
for image, label in ds_train.take(1):
for i in range(9):
ax = plt.subplot(3, 3, i + 1)
aug_img = img_augmentation(tf.expand_dims(image, axis=0)) #augmentation sequential layer
plt.imshow(aug_img[0].numpy().astype("uint8")) #imshow need numpy array with 'unsigned-int8' precision
plt.title("{}".format(format_label(label)))
plt.axis("off")
https://blog.paperspace.com/tensorflow-callbacks/
from google.colab.patches import cv2_imshow
cv2_imshow("output.png")
tf.compat.v1.disable_eager_execution()
https://www.scivision.dev/numpy-image-bgr-to-rgb/
b,g,r = cv2.split("img.jpg")
data_rgb= cv2.merge([r,g,b])
https://github.com/novoforce/Exploring-Tensorflow/blob/main/multi_label_classification.ipynb
!wget -O <output file with extension> --no-check-certificate "<download link>"
! curl <download link without quotes> > <output_file>
# If public sharing link is available
!gdown --id 1cewMfusmPjYWbrnuJRuKhPMwRe_b9PaT
If shared link is only for you:
https://stackoverflow.com/questions/62759748/downloading-data-from-a-shared-google-drive-link-in-google-colab
Extra links:
https://stackoverflow.com/questions/48735600/file-download-from-google-drive-to-colaboratory
!zip -r /content/file.zip /content/Folder_To_Zip
from google.colab import files
files.download("/content/file.zip")
import sys
import os
sys.path.append(os.path.abspath("/home/el/foo4/stuff")) # attach the path of the directory to be included
from riaa import *
watchout()
https://stackoverflow.com/questions/2349991/how-to-import-other-python-files
Tutorial:
https://img.shields.io/static/v1?label=put-custom-label&message=Tutorial&color=yellow&style=flat-square
Snippet:
https://img.shields.io/static/v1?label=put-custom-label&message=Snippet&color=blue&style=flat-square
Issue:
https://img.shields.io/static/v1?label=put-custom-label&message=Issue&color=red&style=flat-square