<jupyter_start><jupyter_text>Install Dependencies<jupyter_code>%tensorflow_version 2.x
!pip install pyyaml h5py
!pip install -q tensorflow-datasets tensorflow
import tensorflow as tf
from tensorflow import keras
import tensorflow_datasets as tfds
from keras import layers
import numpy as np
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>Load Data<jupyter_code>(tf_train, tf_val, tf_test), info = tfds.load('eurosat', split=['train[:80%]', 'train[80%:90%]', 'train[90%:]'], shuffle_files=True, as_supervised=True, with_info=True)
classes = info.features['label'].num_classes
labels = info.features['label']
features = ['Forest', 'HerbaceousVegetation', 'Highway', 'Industrial', 'Pasture', 'PermanentCrop', 'Residential', 'River', 'SeaLake', 'AnnualCrop']
labelList = []
imgList = []

test_labels = []
test_imgs = []
for i, j in tf_train:
  labelList.append(j)
  imgList.append(i)

for k,z in tf_test:
  test_imgs.append(k)
  test_labels.append(z)
print(labelList[3].numpy())
print(imgList[3])
x = plt.imshow(imgList[19])
y = plt.title(features[labelList[19].numpy()-1])<jupyter_output><empty_output><jupyter_text>Normalize Images (Give them pixel values)<jupyter_code>def normalize_img(image, label):
  #Turns the imgs from uint8 values to pixel float32 values
  return tf.cast(image, tf.float32) / 255., label<jupyter_output><empty_output><jupyter_text>Data Augementation<jupyter_code>dA = keras.Sequential([
    keras.layers.experimental.preprocessing.RandomFlip("horizontal_and_vertical"), #Flips images
    keras.layers.experimental.preprocessing.RandomRotation(0.2)]) #Rotates images
daImg = imgList[0]<jupyter_output><empty_output><jupyter_text><jupyter_code>plt.figure(figsize=(10, 10)) #Display data augmented images
for i in range(9):
  augmented_image = dA(daImg)
  ax = plt.subplot(3, 3, i + 1)
  plt.imshow(augmented_image[0])
  plt.axis("off")<jupyter_output><empty_output><jupyter_text>Turn into prefetched datasets and implementation of image normalization<jupyter_code>tf_train = tf_train.map(normalize_img, num_parallel_calls=tf.data.experimental.AUTOTUNE)
tf_train = tf_train.cache()
tf_train = tf_train.shuffle(info.splits['train'].num_examples)
tf_train = tf_train.batch(64)
tf_train = tf_train.prefetch(tf.data.experimental.AUTOTUNE)
tf_test = tf_test.map(normalize_img, num_parallel_calls=tf.data.experimental.AUTOTUNE)
tf_test = tf_test.batch(64)
tf_test = tf_test.cache()
tf_test = tf_test.prefetch(tf.data.experimental.AUTOTUNE)
callback = keras.callbacks.EarlyStopping(monitor='loss', patience=3) #Prevents overfitting<jupyter_output><empty_output><jupyter_text>Convolutional Neural Network Model<jupyter_code>model = tf.keras.Sequential()
model.add(layers.Conv2D(32,(4,4), input_shape=(64,64,3)))
model.add(layers.LeakyReLU(alpha=0.3))
model.add(layers.MaxPooling2D(2,2))
model.add(layers.Conv2D(64, (4,4)))
model.add(layers.LeakyReLU(alpha=0.3))
model.add(layers.MaxPooling2D(2,2))
model.add(layers.Conv2D(64, (4,4)))
model.add(layers.LeakyReLU(alpha=0.3))
model.add(layers.Flatten())
model.add(layers.Dense(32,input_dim=64))
model.add(layers.LeakyReLU(alpha=0.3))
model.add(layers.Dense(10, activation='softmax')) #10 classes
model.summary()<jupyter_output>Model: "sequential_2"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d (Conv2D)              (None, 61, 61, 32)        1568      
_________________________________________________________________
leaky_re_lu (LeakyReLU)      (None, 61, 61, 32)        0         
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 30, 30, 32)        0         
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 27, 27, 64)        32832     
_________________________________________________________________
leaky_re_lu_1 (LeakyReLU)    (None, 27, 27, 64)        0         
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 13, 13, 64)        0         
______________________________________________________[...]<jupyter_text>Compiles and Trains the model<jupyter_code>model.compile(optimizer='adam', loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True), metrics=['accuracy'])
model.fit(tf_train, epochs=15, batch_size=64, callbacks=[callback], verbose=2)<jupyter_output>Epoch 1/15<jupyter_text>Model Evaluation on testing data<jupyter_code>model.evaluate(tf_test, batch_size=64, verbose=1)
model.save('euro.h5') #Saves model, still configuring it
euroModel = keras.models.load_model('euro.h5') #loads model<jupyter_output><empty_output><jupyter_text>Classification function<jupyter_code>def classify():
  if feature_predict == "Pasture" or feature_predict == "AnnualCrop" or feature_predict == "PermanentCrop" or feature_predict == "HerbaceousVegetation":
    print("Safe")
  else:
    print("Unsafe")<jupyter_output><empty_output><jupyter_text>Prediction<jupyter_code>target = 91
prediction = model.predict(tf_test)
print(prediction[target])
#y = np.amax(prediction[target])*10
#y = int(y)
#print(y)
feature_predict = features[np.argmax(prediction[target])-1]
#print(prediction[target][y])
print("Predicted:", feature_predict)
print("Expected:", features[test_labels[target].numpy()-1])
plt.imshow(test_imgs[target])
classify()<jupyter_output>[1.3968963e-03 1.3144372e-10 7.8641679e-06 2.0983236e-07 8.3688712e-14
 1.7999422e-07 2.2762929e-09 7.7888031e-16 6.2114414e-06 9.9858862e-01]
9
Predicted: SeaLake
Expected: SeaLake
Unsafe
{'fraction_non_alphanumeric': 0.13904121705289227, 'fraction_numerical': 0.0468777640191049, 'mean_word_length': 7.1469740634005765, 'pattern_counts': {'":': 1, '<': 42, '<?xml version=': 0, '>': 42, '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': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '181126', 'license': 'MIT', 'n_tokens_mistral': 2131, 'n_tokens_neox': 1952, 'n_words': 317, 'path': 'CNNSat.ipynb', 'repo_name': 'gitSean23/LandSatClassification', 'size': '5653'}