import os
'TF_CPP_MIN_LOG_LEVEL'] = '1'
os.environ[import tensorflow as tf
print(tf.__version__)
Convolutional Neural Network (CNN)
this post will describe what is CNN, how it works and common usecases of CNN
try:
import google.colab
= True
IN_COLAB except:
= False
IN_COLAB
IN_COLAB
import zipfile, os
= (
local_zip 'datasets/images/archive.zip' if IN_COLAB == False
else '/content/drive/MyDrive/datasets/images/archive.zip'
)= zipfile.ZipFile(local_zip, 'r')
zip_ref 'datasets/images/messy_clean_room/')
zip_ref.extractall(
zip_ref.close()
= 'datasets/images/messy_clean_room/images'
base_dir = os.path.join(base_dir, 'train')
train_dir = os.path.join(base_dir, 'val') validation_dir
+'/train') os.listdir(base_dir
+'/val') os.listdir(base_dir
from tensorflow.keras.preprocessing.image import ImageDataGenerator
= ImageDataGenerator(
train_datagen = 1./255,
rescale = 20,
rotation_range = True,
horizontal_flip = 0.2,
shear_range = 'nearest')
fill_mode
= ImageDataGenerator(
test_datagen = 1./255) rescale
= train_datagen.flow_from_directory(
train_generator
train_dir,= (150,150),
target_size = 4,
batch_size = 'binary')
class_mode
= test_datagen.flow_from_directory(
validation_generator
validation_dir,= (150,150),
target_size = 4,
batch_size = 'binary') class_mode
dir(train_generator)
= tf.keras.models.Sequential([
model = (150, 150, 3)),
tf.keras.Input(shape 32, (3,3), activation = 'relu'),
tf.keras.layers.Conv2D(2,2),
tf.keras.layers.MaxPooling2D(64, (3,3), activation = 'relu'),
tf.keras.layers.Conv2D(2,2),
tf.keras.layers.MaxPooling2D(128, (3,3), activation = 'relu'),
tf.keras.layers.Conv2D(2,2),
tf.keras.layers.MaxPooling2D(512, (3,3), activation = 'relu'),
tf.keras.layers.Conv2D(2,2),
tf.keras.layers.MaxPooling2D(
tf.keras.layers.Flatten(),512, activation = 'relu'),
tf.keras.layers.Dense(1, activation = 'sigmoid')
tf.keras.layers.Dense( ])
= tf.keras.models.Sequential()
model = (150, 150, 3))),
model.add(tf.keras.Input(shape 32, (3,3), activation = 'relu'))
model.add(tf.keras.layers.Conv2D(2,2)))
model.add(tf.keras.layers.MaxPooling2D((64, (3,3), activation = 'relu'))
model.add(tf.keras.layers.Conv2D(2,2)))
model.add(tf.keras.layers.MaxPooling2D((128, (3,3), activation = 'relu'))
model.add(tf.keras.layers.Conv2D(2,2)))
model.add(tf.keras.layers.MaxPooling2D((512, (3,3), activation = 'relu'))
model.add(tf.keras.layers.Conv2D(2,2)))
model.add(tf.keras.layers.MaxPooling2D((
model.add(tf.keras.layers.Flatten())512, activation = 'relu'))
model.add(tf.keras.layers.Dense(1, activation = 'sigmoid')) model.add(tf.keras.layers.Dense(
model.summary()
compile(loss = 'binary_crossentropy',
model.= tf.optimizers.Adam(),
optimizer = ['accuracy']) metrics
model.fit( train_generator, epochs = 40, validation_data = validation_generator, validation_steps = 5, verbose = 2)
from tensorflow.keras.applications import ResNet50
from tensorflow.keras.applications import ResNet152V2
= tf.keras.models.Sequential([
transfer
ResNet152V2(= "imagenet",
weights = False,
include_top = tf.keras.layers.Input(shape = (150,150,3))),
input_tensor
tf.keras.layers.Flatten(),128, activation = 'relu'),
tf.keras.layers.Dense(1, activation = 'softmax')
tf.keras.layers.Dense( ])
compile(loss = 'binary_crossentropy',
transfer.= tf.optimizers.Adam(),
optimizer = ['accuracy']) metrics
transfer.fit(
train_generator,= 40,
epochs = validation_generator,
validation_data = 5,
validation_steps = 2) verbose