<jupyter_start><jupyter_text>Data Augmentation<jupyter_code>!pip install Augmentor
import sys
import Augmentor

p= Augmentor.Pipeline("/content/drive/MyDrive/Agriculture Vision Dataset/All Images/Img labels/cloud_shadow")
p.flip_left_right(0.7)
p.rotate(0.3, 5,5)
p.flip_top_bottom(0.3)
p.zoom_random(0.4, percentage_area=0.5)
p.skew(0.2,0.5)
p.sample(2000)
p= Augmentor.Pipeline("/content/drive/MyDrive/Agriculture Vision Dataset/All Images/Img labels/standing_water")
p.flip_left_right(0.7)
p.rotate(0.3, 5,5)
p.flip_top_bottom(0.3)
p.zoom_random(0.4, percentage_area=0.5)
p.skew(0.2,0.5)
p.sample(2000)
p= Augmentor.Pipeline("/content/drive/MyDrive/Agriculture Vision Dataset/All Images/Img labels/weed_cluster")
p.flip_left_right(0.7)
p.rotate(0.3, 5,5)
p.flip_top_bottom(0.3)
p.zoom_random(0.4, percentage_area=0.5)
p.skew(0.2,0.5)
p.sample(2000)
p= Augmentor.Pipeline("/content/drive/MyDrive/Agriculture Vision Dataset/All Images/Img labels/waterway")
p.flip_left_right(0.7)
p.rotate(0.3, 5,5)
p.flip_top_bottom(0.3)
p.zoom_random(0.4, percentage_area=0.5)
p.skew(0.2,0.5)
p.sample(2000)<jupyter_output>Executing Pipeline:   0%|          | 0/2000 [00:00<?, ? Samples/s]<jupyter_text>Splitting Dataset<jupyter_code>image_size=(512,512)
train_ds = tf.keras.preprocessing.image_dataset_from_directory(
    "/content/drive/MyDrive/Agriculture Vision Dataset/New Augmented",
    validation_split=0.1,
    subset="training",
    seed=456,
    image_size=image_size,
    batch_size=32,
)
val_ds = tf.keras.preprocessing.image_dataset_from_directory(
    "/content/drive/MyDrive/Agriculture Vision Dataset/New Augmented",
    validation_split=0.1,
    subset="validation",
    seed=456,
    image_size=image_size,
    batch_size=32,
)<jupyter_output><empty_output><jupyter_text>Experiment 1 Using Adam Optimizer<jupyter_code>num_classes = 4

model = Sequential([
  layers.experimental.preprocessing.Rescaling(1./255, input_shape=(512, 512, 3)),
  layers.Conv2D(16, 3, padding='same', activation='relu'),
  layers.MaxPooling2D(),
  layers.Conv2D(32, 3, padding='same', activation='relu'),
  layers.MaxPooling2D(),
  layers.Conv2D(64, 3, padding='same', activation='relu'),
  layers.MaxPooling2D(),
  layers.Flatten(),
  layers.Dense(128, activation='relu'),
  layers.Dense(num_classes)
])


model.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])<jupyter_output><empty_output><jupyter_text>10 Epochs<jupyter_code>epochs=10
h1 = model.fit(
  train_ds,
  validation_data=val_ds,
  epochs=epochs
)
acc = h1.history['accuracy']
val_acc = h1.history['val_accuracy']

loss = h1.history['loss']
val_loss = h1.history['val_loss']

epochs_range = range(epochs)

plt.figure(figsize=(8, 8))
plt.subplot(1, 2, 1)
plt.plot(epochs_range, acc, label='Training Accuracy')
plt.plot(epochs_range, val_acc, label='Validation Accuracy')
plt.legend(loc='lower right')
plt.title('Training and Validation Accuracy')

plt.subplot(1, 2, 2)
plt.plot(epochs_range, loss, label='Training Loss')
plt.plot(epochs_range, val_loss, label='Validation Loss')
plt.legend(loc='upper right')
plt.title('Training and Validation Loss')
plt.show()<jupyter_output><empty_output><jupyter_text>20 Epochs<jupyter_code>epochs=20
h1 = model.fit(
  train_ds,
  validation_data=val_ds,
  epochs=epochs
)
acc = h1.history['accuracy']
val_acc = h1.history['val_accuracy']

loss = h1.history['loss']
val_loss = h1.history['val_loss']

epochs_range = range(epochs)

plt.figure(figsize=(8, 8))
plt.subplot(1, 2, 1)
plt.plot(epochs_range, acc, label='Training Accuracy')
plt.plot(epochs_range, val_acc, label='Validation Accuracy')
plt.legend(loc='lower right')
plt.title('Training and Validation Accuracy')

plt.subplot(1, 2, 2)
plt.plot(epochs_range, loss, label='Training Loss')
plt.plot(epochs_range, val_loss, label='Validation Loss')
plt.legend(loc='upper right')
plt.title('Training and Validation Loss')
plt.show()<jupyter_output><empty_output><jupyter_text>Using SGD Optimizer Epochs = 10<jupyter_code>opt = tf.keras.optimizers.SGD(learning_rate=0.0001)
model.compile(optimizer=opt,
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])

epochs=10
h1 = model.fit(
  train_ds,
  validation_data=val_ds,
  epochs=epochs
)
acc = h1.history['accuracy']
val_acc = h1.history['val_accuracy']

loss = h1.history['loss']
val_loss = h1.history['val_loss']

epochs_range = range(epochs)

plt.figure(figsize=(8, 8))
plt.subplot(1, 2, 1)
plt.plot(epochs_range, acc, label='Training Accuracy')
plt.plot(epochs_range, val_acc, label='Validation Accuracy')
plt.legend(loc='lower right')
plt.title('Training and Validation Accuracy')

plt.subplot(1, 2, 2)
plt.plot(epochs_range, loss, label='Training Loss')
plt.plot(epochs_range, val_loss, label='Validation Loss')
plt.legend(loc='upper right')
plt.title('Training and Validation Loss')
plt.show()<jupyter_output><empty_output><jupyter_text>Epochs = 20<jupyter_code>opt = tf.keras.optimizers.SGD(learning_rate=0.0001)
model.compile(optimizer=opt,
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])

epochs=20
h1 = model.fit(
  train_ds,
  validation_data=val_ds,
  epochs=epochs
)
acc = h1.history['accuracy']
val_acc = h1.history['val_accuracy']

loss = h1.history['loss']
val_loss = h1.history['val_loss']

epochs_range = range(epochs)

plt.figure(figsize=(8, 8))
plt.subplot(1, 2, 1)
plt.plot(epochs_range, acc, label='Training Accuracy')
plt.plot(epochs_range, val_acc, label='Validation Accuracy')
plt.legend(loc='lower right')
plt.title('Training and Validation Accuracy')

plt.subplot(1, 2, 2)
plt.plot(epochs_range, loss, label='Training Loss')
plt.plot(epochs_range, val_loss, label='Validation Loss')
plt.legend(loc='upper right')
plt.title('Training and Validation Loss')
plt.show()<jupyter_output><empty_output><jupyter_text>VGG16 Experiment 1<jupyter_code>from tensorflow.keras.applications.vgg16 import VGG16

ip=layers.Input(shape=(512, 512, 3))
base_model = VGG16(input_tensor = ip,  # Shape of our images
include_top = False, # Leave out the last fully connected layer
weights = "/content/drive/MyDrive/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5",
classes = 4, pooling = 'avg') 
output = layers.Dense(4, activation='softmax')(base_model.output)
model = tf.keras.models.Model(base_model.input, output)

model.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])
vgghist = model.fit(train_ds, validation_data = val_ds, steps_per_epoch = 10, epochs = 10)<jupyter_output><empty_output><jupyter_text>Experiment 2<jupyter_code>from tensorflow.keras.applications.vgg16 import VGG16


train_ds = tf.keras.preprocessing.image_dataset_from_directory(
    "/content/drive/MyDrive/Agriculture Vision Dataset/New Augmented",
    image_size=(224,224),
    validation_split=0.1,
    subset="training",
    seed=123,
    batch_size=16,
)
val_ds = tf.keras.preprocessing.image_dataset_from_directory(
    "/content/drive/MyDrive/Agriculture Vision Dataset/New Augmented",
    validation_split=0.1,
    subset="validation",
    seed=123,
    image_size=(224,224),
    batch_size=16,
)

num_classes = 4

base_model = VGG16(include_top=False,
                  input_shape = (224,224,3),
                  weights = None)
for layer in base_model.layers[:-8]:
    layer.trainable = False
    
for layer in base_model.layers:
    print(layer,layer.trainable)

from keras.layers import Conv2D, MaxPooling2D, Dense, Dropout, Input, Flatten, SeparableConv2D,GlobalAveragePooling2D

model = Sequential()
model.add(base_model)
# model.add(GlobalAveragePooling2D())
model.add(Flatten())
# model.add(Dropout(0.5))
model.add(Dense(256,activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(128,activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(4,activation='softmax'))
model.summary()<jupyter_output>Model: "sequential_24"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
vgg16 (Functional)           (None, 7, 7, 512)         14714688  
_________________________________________________________________
flatten_10 (Flatten)         (None, 25088)             0         
_________________________________________________________________
dense_62 (Dense)             (None, 256)               6422784   
_________________________________________________________________
dropout_19 (Dropout)         (None, 256)               0         
_________________________________________________________________
dense_63 (Dense)             (None, 128)               32896     
_________________________________________________________________
dropout_20 (Dropout)         (None, 128)               0         
_____________________________________________________[...]<jupyter_text>Using SGD optimizer<jupyter_code>opt = tf.keras.optimizers.SGD(learning_rate=0.0001)
model.compile(optimizer=opt,
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])

vgghist = model.fit(train_ds, validation_data = val_ds, steps_per_epoch = 20, epochs = 20)<jupyter_output><empty_output><jupyter_text>Using Adam Optimizer<jupyter_code># opt = tf.keras.optimizers.SGD(learning_rate=0.0001,momentum = 0.5)
model.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])

vgghist = model.fit(train_ds, validation_data = val_ds, steps_per_epoch = 20, epochs = 20)<jupyter_output><empty_output><jupyter_text>Experiment 3 Using Image size (224,224)<jupyter_code>from tensorflow.keras.applications.vgg16 import VGG16


train_ds = tf.keras.preprocessing.image_dataset_from_directory(
    "/content/drive/MyDrive/Agriculture Vision Dataset/New Augmented",
    image_size=(224,224),
    validation_split=0.1,
    subset="training",
    seed=123,
    batch_size=32,
)
val_ds = tf.keras.preprocessing.image_dataset_from_directory(
    "/content/drive/MyDrive/Agriculture Vision Dataset/New Augmented",
    validation_split=0.1,
    subset="validation",
    seed=123,
    image_size=(224,224),
    batch_size=32,
)

num_classes = 4

base_model = VGG16(include_top=False,
                  input_shape = (224,224,3),
                  weights = None)
for layer in base_model.layers[:-12]:
    layer.trainable = False
    
for layer in base_model.layers:
    print(layer,layer.trainable)

from keras.layers import Conv2D, MaxPooling2D, Dense, Dropout, Input, Flatten, SeparableConv2D,GlobalAveragePooling2D

model = Sequential()
model.add(base_model)
model.add(GlobalAveragePooling2D())
model.add(Dense(1024,activation='relu'))
# model.add(Dropout(0.5))
model.add(Dense(512,activation='relu'))
# model.add(Dropout(0.5))
model.add(Dense(6,activation='relu'))
model.summary()


model.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])

vgghist = model.fit(train_ds, validation_data = val_ds, steps_per_epoch = 10, epochs = 20)<jupyter_output><empty_output><jupyter_text>Using Image Size (512,512)<jupyter_code>model.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])

vgghist = model.fit(train_ds, validation_data = val_ds, steps_per_epoch = 10, epochs = 20)<jupyter_output>Epoch 1/20
10/10 [==============================] - 35s 4s/step - loss: 375.6192 - accuracy: 0.2718 - val_loss: 1.7777 - val_accuracy: 0.2713
Epoch 2/20
10/10 [==============================] - 32s 3s/step - loss: 1.8545 - accuracy: 0.2469 - val_loss: 1.7666 - val_accuracy: 0.2438
Epoch 3/20
10/10 [==============================] - 32s 3s/step - loss: 1.7869 - accuracy: 0.2388 - val_loss: 1.7768 - val_accuracy: 0.2713
Epoch 4/20
10/10 [==============================] - 32s 3s/step - loss: 1.7778 - accuracy: 0.2612 - val_loss: 1.7336 - val_accuracy: 0.2713
Epoch 5/20
10/10 [==============================] - 32s 3s/step - loss: 1.7430 - accuracy: 0.2873 - val_loss: 1.7393 - val_accuracy: 0.3512
Epoch 6/20
10/10 [==============================] - 32s 3s/step - loss: 1.7314 - accuracy: 0.3291 - val_loss: 1.7486 - val_accuracy: 0.3200
Epoch 7/20
10/10 [==============================] - 32s 3s/step - loss: 1.7582 - accuracy: 0.2585 - val_loss: 1.7479 - val_accuracy: 0.2713
Epoch 8/20
10/10 [[...]
{'fraction_non_alphanumeric': 0.15011600928074245, 'fraction_numerical': 0.05274555297757154, 'mean_word_length': 4.518992744344857, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 50, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '255993', 'license': 'MIT', 'n_tokens_mistral': 5137, 'n_tokens_neox': 4618, 'n_words': 720, 'path': 'DCNN_and_VGG16.ipynb', 'repo_name': 'igshrey01/Agriculture-Field-Pattern-Classification-Using-Deep-Learning', 'size': '12930'}